From 7f97e75147d06e61d2aef18f82e1a6dace756704 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 2 Dec 2019 20:52:56 -0600 Subject: [PATCH] feat(http_of_dir): use `file` to guess mime type of file --- src/bin/http_of_dir.ml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index f2558ccf..b7c2a8fa 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -89,6 +89,15 @@ let html_list_dir ~top ~parent d : string = Printf.bprintf body "\n"; Buffer.contents body +let finally_ ~h x f = + try + let y = f x in + h x; + y + with e -> + h x; + raise e + (* TODO let wdays = [|"Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat"|] let date_of_time (f:float) : string = @@ -177,8 +186,17 @@ let serve ~config (dir:string) : _ result = ) else ( try let ic = open_in full_path in + let mime_type = + try + let p = Unix.open_process_in (Printf.sprintf "file -i -b %S" full_path) in + finally_ ~h:(fun p->ignore @@ Unix.close_process_in p) p + (fun p -> + try ["Content-Type", String.trim (input_line p)] + with _ -> []) + with _ -> [] + in S.Response.make_raw_stream - ~headers:["Etag", Lazy.force mtime] + ~headers:(mime_type@["Etag", Lazy.force mtime]) ~code:200 (S.Byte_stream.of_chan ic) with e -> S.Response.fail ~code:500 "error while reading file: %s" (Printexc.to_string e)