From c6c601999a9daa66202ea4347060b3924329f48f Mon Sep 17 00:00:00 2001 From: n-dusan Date: Fri, 17 Nov 2023 14:24:24 +0100 Subject: [PATCH] fix: set `text/plain` `content-type` for .rdf extensions While testing `_rdf` route, found out that Chromium browsers do not render `application/rdf+xml` `content-type`, but instead download the contents directly. Firefox browsers, however, seem to render rdf and work as expected. After testing rdf resolution in nginx, the `content-type` that nginx returns for `.rdf` is `text/plain`. To resolve, we return `text/plain` in stelae as well. --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- src/utils/http.rs | 13 +++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b175d3c..e6e215f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -962,9 +962,9 @@ dependencies = [ [[package]] name = "mime" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "mime_guess" diff --git a/Cargo.toml b/Cargo.toml index 5598a2e..171371f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ rust-version = "1.69" actix-web = "4" actix-service = "2.0" actix-http = "3.2" -mime = "0.3.9" -mime_guess = "2.0.1" +mime = "0.3.17" +mime_guess = "2.0.4" anyhow = "1.0" clap = { version = "4.0.27", features = ["derive"] } git2 = "0.15" diff --git a/src/utils/http.rs b/src/utils/http.rs index e87b404..69bd986 100644 --- a/src/utils/http.rs +++ b/src/utils/http.rs @@ -8,12 +8,17 @@ use std::path::Path; /// for the content at `path`. If there is no extension, we assume it is /// html. If the extension cannot be converted to a str, then we return /// HTML. +/// Some browsers will not render `application/rdf+xml`, but instead will +/// download it. So we instead return `text/plain`` for `.rdf` files. #[must_use] pub fn get_contenttype(path: &str) -> ContentType { let extension = Path::new(&path) .extension() .map_or("html", |ext| ext.to_str().map_or("", |ext_str| ext_str)); let mime = file_extension_to_mime(extension).first_or(mime::TEXT_HTML); + if let ((mime::APPLICATION, "rdf")) = (mime.type_(), mime.subtype().as_str()) { + return ContentType(mime::TEXT_PLAIN); + } ContentType(mime) } @@ -53,6 +58,14 @@ mod test { assert_eq!(expected, actual); } + #[test] + fn test_get_contenttype_when_rdf_ext_expect_rdf() { + let cut = get_contenttype; + let actual = cut("a/b.rdf").to_string(); + let expected = String::from("text/plain"); + assert_eq!(expected, actual); + } + #[test] fn test_get_contenttype_when_incorrect_ext_expect_html() { let cut = get_contenttype;