Hide real build time of application
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -578,6 +578,7 @@ dependencies = [
|
||||
"actix-remote-ip",
|
||||
"actix-session",
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"askama",
|
||||
"base32",
|
||||
"base64 0.22.1",
|
||||
|
||||
@@ -42,3 +42,4 @@ mailchecker = "6.0.19"
|
||||
httpdate = "1.0.3"
|
||||
build-time = "0.1.3"
|
||||
hex = "0.4.3"
|
||||
anyhow = "1.0.100"
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::utils::time_utils;
|
||||
use actix_web::http::header;
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use include_dir::{Dir, include_dir};
|
||||
use std::cmp::max;
|
||||
use std::ops::Add;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
@@ -23,13 +24,14 @@ pub async fn assets_route(req: HttpRequest, path: web::Path<String>) -> HttpResp
|
||||
Some(file) => {
|
||||
let res = mime_guess::from_path(path).first_or_octet_stream();
|
||||
let digest = hex::encode(sha256(file.contents()));
|
||||
let file_time = max(time_utils::time_start_of_day(), time_utils::build_time());
|
||||
|
||||
// Check if the browser already knows the file by date
|
||||
if let Some(c) = req.headers().get(header::IF_MODIFIED_SINCE) {
|
||||
let date_str = c.to_str().unwrap_or("");
|
||||
if let Ok(date) = httpdate::parse_http_date(date_str)
|
||||
&& date.add(Duration::from_secs(1))
|
||||
>= time_utils::unix_to_system_time(time_utils::build_time())
|
||||
>= time_utils::unix_to_system_time(file_time)
|
||||
{
|
||||
return HttpResponse::NotModified().finish();
|
||||
}
|
||||
@@ -45,10 +47,7 @@ pub async fn assets_route(req: HttpRequest, path: web::Path<String>) -> HttpResp
|
||||
HttpResponse::Ok()
|
||||
.content_type(res.to_string())
|
||||
.insert_header(("etag", digest))
|
||||
.insert_header((
|
||||
"last-modified",
|
||||
time_utils::unix_to_http_date(time_utils::build_time()),
|
||||
))
|
||||
.insert_header(("last-modified", time_utils::unix_to_http_date(file_time)))
|
||||
.body(file.contents())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use chrono::DateTime;
|
||||
use chrono::{DateTime, Local, NaiveTime};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Get the current time since epoch
|
||||
@@ -37,6 +37,14 @@ pub fn build_time() -> u64 {
|
||||
date.timestamp() as u64
|
||||
}
|
||||
|
||||
/// Get the first second of the day (local time)
|
||||
pub fn time_start_of_day() -> u64 {
|
||||
let local: DateTime<Local> = Local::now()
|
||||
.with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap())
|
||||
.unwrap();
|
||||
local.timestamp() as u64
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::time_utils::{fmt_time, time};
|
||||
|
||||
Reference in New Issue
Block a user