diff --git a/Cargo.toml b/Cargo.toml index 4790cc3..9c4e36f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,9 @@ htmlescape = "0.3.1" percent-encoding = "2.1.0" path-dedot = "1" +[target.'cfg(target_os="linux")'.dependencies] +magic = "0.12.2" + [features] default = ["tls"] tls = ["hyper-native-tls"] diff --git a/src/main.rs b/src/main.rs index 67f67ca..375fb71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -893,7 +893,24 @@ impl MainHandler { } Method::Get => { // Set mime type - let mime = mime_types::from_path(path).first_or_octet_stream(); + //let mime = mime_types::from_path(path).first_or_octet_stream(); + let mime = if cfg!(target_os = "linux") { + mime_types::from_path(path).first_or_else(|| { + if let Ok(cookie) = magic::Cookie::open(magic::flags::MIME_TYPE) { + if cookie.load::<&str>(&[]).is_ok() { + if let Ok(val) = cookie.file(&path) { + if val == "text/plain" { + return mime_types::mime::TEXT_PLAIN; + } + } + } + } + mime_types::mime::APPLICATION_OCTET_STREAM + }) + } else { + mime_types::from_path(path).first_or_octet_stream() + }; + resp.headers .set_raw("content-type", vec![mime.to_string().into_bytes()]);