From 98b43d1ba2371e720f7dd4b3b43d6fd6d445c032 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 2 Oct 2021 20:03:03 +0000 Subject: [PATCH] guess MIME for JavaScript files so that `ptth_file_server` can serve Jet Racing 4 locally. Firfox has a bunch of odd security features that I only sometimes understand. Among them is that it won't load JS files without the `content-type` header, and it also disables a lot of features for the `file://` scheme. Which I think is a shame. There's probably a good reason they do this, but I'm not aware of one. So now you can use PTTH's file server to host Jet Racing 4. --- crates/ptth_server/src/file_server.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ptth_server/src/file_server.rs b/crates/ptth_server/src/file_server.rs index a347a4c..86fa357 100644 --- a/crates/ptth_server/src/file_server.rs +++ b/crates/ptth_server/src/file_server.rs @@ -141,6 +141,7 @@ async fn serve_dir_json ( #[instrument (level = "debug", skip (f))] async fn serve_file ( + uri: &str, mut f: File, client_wants_body: bool, range: range::ValidParsed, @@ -211,6 +212,12 @@ async fn serve_file ( response.header (String::from ("content-range"), format! ("bytes {}-{}/{}", range.start, range.end - 1, range.end).into_bytes ()); } + // Guess MIME type based on the URI so that we can serve web games + + if uri.ends_with (".js") { + response.header ("content-type".into (), b"application/javascript".to_vec ()); + } + response.content_length = Some (content_length); if let Some (body) = body { @@ -402,7 +409,7 @@ impl FileServer { file, send_body, range, - }) => serve_file (file.into_inner (), send_body, range, headers.get ("if-none-match").map (|v| &v[..])).await?, + }) => serve_file (uri, file.into_inner (), send_body, range, headers.get ("if-none-match").map (|v| &v[..])).await?, MarkdownErr (e) => { #[cfg (feature = "markdown")] {