From f5c29ceffb16622cf2c8928e15649ee655bfc039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arma=C3=ABl=20Gu=C3=A9neau?= Date: Thu, 2 Jan 2025 12:45:00 +0100 Subject: [PATCH] static handler: guess content type --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 7 +++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4916ab..2772091 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1184,6 +1184,7 @@ dependencies = [ "include_dir", "lazy_static", "lettre", + "new_mime_guess", "rand", "reqwest 0.12.9", "serde", @@ -2008,6 +2009,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "new_mime_guess" +version = "4.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02a2dfb3559d53e90b709376af1c379462f7fb3085a0177deb73e6ea0d99eff4" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "nom" version = "7.1.3" diff --git a/Cargo.toml b/Cargo.toml index 8293515..1e01ffd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ lettre = { version = "0.11", features = ["builder", "smtp-transport", "rustls-tl include_dir = "0.7" aws-config = { version = "1.1.7", features = ["behavior-version-latest"] } aws-sdk-s3 = "1.66.0" +new_mime_guess = "4" [profile.profiling] inherits = "dev" diff --git a/src/main.rs b/src/main.rs index 7b6c12a..415039a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -443,9 +443,12 @@ async fn get_static_( ) -> impl IntoResponse { eprintln!("GET {}", uri); - match STATIC_DIR.get_file(filename) { + match STATIC_DIR.get_file(&filename) { None => (StatusCode::NOT_FOUND, "404 Not found").into_response(), - Some(page) => page.contents().into_response(), + Some(page) => { + let mime = new_mime_guess::from_path(&filename).first_or_octet_stream(); + ([(header::CONTENT_TYPE, mime.to_string())], page.contents()).into_response() + }, } }