Hide real build time of application
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-12-05 10:10:00 +01:00
parent b72fa90b67
commit 9535ab754a
4 changed files with 15 additions and 6 deletions

1
Cargo.lock generated
View File

@@ -578,6 +578,7 @@ dependencies = [
"actix-remote-ip", "actix-remote-ip",
"actix-session", "actix-session",
"actix-web", "actix-web",
"anyhow",
"askama", "askama",
"base32", "base32",
"base64 0.22.1", "base64 0.22.1",

View File

@@ -42,3 +42,4 @@ mailchecker = "6.0.19"
httpdate = "1.0.3" httpdate = "1.0.3"
build-time = "0.1.3" build-time = "0.1.3"
hex = "0.4.3" hex = "0.4.3"
anyhow = "1.0.100"

View File

@@ -3,6 +3,7 @@ use crate::utils::time_utils;
use actix_web::http::header; use actix_web::http::header;
use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::{HttpRequest, HttpResponse, web};
use include_dir::{Dir, include_dir}; use include_dir::{Dir, include_dir};
use std::cmp::max;
use std::ops::Add; use std::ops::Add;
use std::path::Path; use std::path::Path;
use std::time::Duration; use std::time::Duration;
@@ -23,13 +24,14 @@ pub async fn assets_route(req: HttpRequest, path: web::Path<String>) -> HttpResp
Some(file) => { Some(file) => {
let res = mime_guess::from_path(path).first_or_octet_stream(); let res = mime_guess::from_path(path).first_or_octet_stream();
let digest = hex::encode(sha256(file.contents())); 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 // Check if the browser already knows the file by date
if let Some(c) = req.headers().get(header::IF_MODIFIED_SINCE) { if let Some(c) = req.headers().get(header::IF_MODIFIED_SINCE) {
let date_str = c.to_str().unwrap_or(""); let date_str = c.to_str().unwrap_or("");
if let Ok(date) = httpdate::parse_http_date(date_str) if let Ok(date) = httpdate::parse_http_date(date_str)
&& date.add(Duration::from_secs(1)) && 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(); return HttpResponse::NotModified().finish();
} }
@@ -45,10 +47,7 @@ pub async fn assets_route(req: HttpRequest, path: web::Path<String>) -> HttpResp
HttpResponse::Ok() HttpResponse::Ok()
.content_type(res.to_string()) .content_type(res.to_string())
.insert_header(("etag", digest)) .insert_header(("etag", digest))
.insert_header(( .insert_header(("last-modified", time_utils::unix_to_http_date(file_time)))
"last-modified",
time_utils::unix_to_http_date(time_utils::build_time()),
))
.body(file.contents()) .body(file.contents())
} }
} }

View File

@@ -1,4 +1,4 @@
use chrono::DateTime; use chrono::{DateTime, Local, NaiveTime};
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// Get the current time since epoch /// Get the current time since epoch
@@ -37,6 +37,14 @@ pub fn build_time() -> u64 {
date.timestamp() as 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)] #[cfg(test)]
mod test { mod test {
use crate::utils::time_utils::{fmt_time, time}; use crate::utils::time_utils::{fmt_time, time};