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.
main
_ 2021-10-02 20:03:03 +00:00
parent 2972e85671
commit 98b43d1ba2
1 changed files with 8 additions and 1 deletions

View File

@ -141,6 +141,7 @@ async fn serve_dir_json (
#[instrument (level = "debug", skip (f))] #[instrument (level = "debug", skip (f))]
async fn serve_file ( async fn serve_file (
uri: &str,
mut f: File, mut f: File,
client_wants_body: bool, client_wants_body: bool,
range: range::ValidParsed, 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 ()); 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); response.content_length = Some (content_length);
if let Some (body) = body { if let Some (body) = body {
@ -402,7 +409,7 @@ impl FileServer {
file, file,
send_body, send_body,
range, 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) => { MarkdownErr (e) => {
#[cfg (feature = "markdown")] #[cfg (feature = "markdown")]
{ {