Skip to content

Commit

Permalink
feat(http_of_dir): use file to guess mime type of file
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Dec 3, 2019
1 parent 8d33f84 commit 7f97e75
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/bin/http_of_dir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ let html_list_dir ~top ~parent d : string =
Printf.bprintf body "</ul></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 =
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7f97e75

Please sign in to comment.