From fd5f316fb86093ac910bb494d7945465c00dae34 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 14 Apr 2025 15:09:27 +0200 Subject: [PATCH] feat(snix/castore-http): pass extension to respond_file() respond_file only uses requested_path to (only) calculate an extension for mime type guessing, and getting to there is a bit messy. Use PathComponent::extension to get the extension, and replace `requested_path: Option` with `extension: Option<&str>`. Change-Id: I0582a1127f8b505fdf77406fafb3d3b206d694f2 Reviewed-on: https://cl.snix.dev/c/snix/+/30320 Tested-by: besadii Reviewed-by: Stefan Junker --- snix/castore-http/src/lib.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/snix/castore-http/src/lib.rs b/snix/castore-http/src/lib.rs index 8873bde39..b5dc1b993 100644 --- a/snix/castore-http/src/lib.rs +++ b/snix/castore-http/src/lib.rs @@ -81,13 +81,13 @@ pub async fn get_root_node_contents { - let found_index_file_path = found_index_file_path.to_string(); - let found_index_file_path = path::Path::new(OsStr::from_bytes( - found_index_file_path.as_bytes(), - )); + let extension = found_index_file_path + .extension() + .and_then(|b| std::str::from_utf8(b).ok()); + return respond_file( blob_service, - Some(found_index_file_path), + extension, range_header, digest, *size, @@ -111,11 +111,11 @@ pub async fn get_root_node_contents { - let requested_path = - path::Path::new(OsStr::from_bytes(requested_path.as_bytes())); respond_file( blob_service, - Some(requested_path), + requested_path + .extension() + .and_then(|b| std::str::from_utf8(b).ok()), range_header, &digest, size, @@ -221,7 +221,7 @@ pub async fn respond_directory_list( #[instrument(level = "trace", skip_all, fields(digest, size))] pub async fn respond_file( blob_service: BS, - requested_path: Option<&path::Path>, + extension: Option<&str>, range_header: Option>, digest: &B3Digest, size: u64, @@ -238,9 +238,7 @@ pub async fn respond_file( StatusCode::NOT_FOUND })?; - let mime_type = requested_path - .and_then(path::Path::extension) - .and_then(std::ffi::OsStr::to_str) + let mime_type = extension .and_then(|extension| mime_guess::from_ext(extension).first()) .unwrap_or(mime::APPLICATION_OCTET_STREAM); match range_header {