diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index e69de29b..00000000 diff --git a/dune-project b/dune-project index 9ccb6023..301047b7 100644 --- a/dune-project +++ b/dune-project @@ -15,6 +15,7 @@ (tags (http thread server tiny_httpd http_of_dir simplehttpserver)) (depopts logs + magic-mime (mtime (>= 2.0))) (depends seq diff --git a/src/Tiny_httpd_dir.ml b/src/Tiny_httpd_dir.ml index ad048449..75c1062b 100644 --- a/src/Tiny_httpd_dir.ml +++ b/src/Tiny_httpd_dir.ml @@ -310,18 +310,8 @@ let add_vfs_ ~on_fs ~top ~config ~vfs:((module VFS : VFS) as vfs) ~prefix server [ "Content-Type", "text/javascript" ] else if on_fs then ( (* call "file" util *) - try - let p = - Unix.open_process_in - (Printf.sprintf "file -i -b %S" (top // 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 _ -> [] + let ty = Mime_.mime_of_path (top // path) in + [ "content-type", ty ] ) else [] in diff --git a/src/dune b/src/dune index b20bb974..fca51e45 100644 --- a/src/dune +++ b/src/dune @@ -1,9 +1,13 @@ (library (name tiny_httpd) (public_name tiny_httpd) + (private_modules mime_) (libraries threads seq unix + (select mime_.ml from + (magic-mime -> mime_.magic.ml) + ( -> mime_.dummy.ml)) (select Tiny_httpd_log.ml from - (logs -> Tiny_httpd_log.logs.ml) + (logs logs.fmt fmt.tty -> Tiny_httpd_log.logs.ml) (-> Tiny_httpd_log.default.ml))) (wrapped false)) diff --git a/src/mime_.dummy.ml b/src/mime_.dummy.ml new file mode 100644 index 00000000..dc944b1c --- /dev/null +++ b/src/mime_.dummy.ml @@ -0,0 +1 @@ +let mime_of_path _ = "application/octet-stream" diff --git a/src/mime_.magic.ml b/src/mime_.magic.ml new file mode 100644 index 00000000..72fcd345 --- /dev/null +++ b/src/mime_.magic.ml @@ -0,0 +1 @@ +let mime_of_path s = Magic_mime.lookup s diff --git a/src/mime_.mli b/src/mime_.mli new file mode 100644 index 00000000..1831c02d --- /dev/null +++ b/src/mime_.mli @@ -0,0 +1,2 @@ + +val mime_of_path : string -> string diff --git a/tiny_httpd.opam b/tiny_httpd.opam index a2ce739c..a5224f40 100644 --- a/tiny_httpd.opam +++ b/tiny_httpd.opam @@ -24,6 +24,7 @@ depends: [ ] depopts: [ "logs" + "magic-mime" "mtime" {>= "2.0"} ] build: [