Skip to content

Commit

Permalink
fix: set text/plain content-type for .rdf extensions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
n-dusan committed Nov 17, 2023
1 parent 83908ad commit 3771289
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions src/utils/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (mime.type_(), mime.subtype().as_str()) == ((mime::APPLICATION, "rdf")) {
return ContentType(mime::TEXT_PLAIN);
}
ContentType(mime)
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3771289

Please sign in to comment.