feat: optional dep on magic-mime for http_of_dir

This commit is contained in:
Simon Cruanes 2024-02-21 22:03:31 -05:00
parent 0d1bccfd1b
commit d9b3731207
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
8 changed files with 13 additions and 13 deletions

0
.gitmodules vendored
View file

View file

@ -15,6 +15,7 @@
(tags (http thread server tiny_httpd http_of_dir simplehttpserver)) (tags (http thread server tiny_httpd http_of_dir simplehttpserver))
(depopts (depopts
logs logs
magic-mime
(mtime (>= 2.0))) (mtime (>= 2.0)))
(depends (depends
seq seq

View file

@ -310,18 +310,8 @@ let add_vfs_ ~on_fs ~top ~config ~vfs:((module VFS : VFS) as vfs) ~prefix server
[ "Content-Type", "text/javascript" ] [ "Content-Type", "text/javascript" ]
else if on_fs then ( else if on_fs then (
(* call "file" util *) (* call "file" util *)
try let ty = Mime_.mime_of_path (top // path) in
let p = [ "content-type", ty ]
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 _ -> []
) else ) else
[] []
in in

View file

@ -1,9 +1,13 @@
(library (library
(name tiny_httpd) (name tiny_httpd)
(public_name tiny_httpd) (public_name tiny_httpd)
(private_modules mime_)
(libraries threads seq unix (libraries threads seq unix
(select mime_.ml from
(magic-mime -> mime_.magic.ml)
( -> mime_.dummy.ml))
(select Tiny_httpd_log.ml from (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))) (-> Tiny_httpd_log.default.ml)))
(wrapped false)) (wrapped false))

1
src/mime_.dummy.ml Normal file
View file

@ -0,0 +1 @@
let mime_of_path _ = "application/octet-stream"

1
src/mime_.magic.ml Normal file
View file

@ -0,0 +1 @@
let mime_of_path s = Magic_mime.lookup s

2
src/mime_.mli Normal file
View file

@ -0,0 +1,2 @@
val mime_of_path : string -> string

View file

@ -24,6 +24,7 @@ depends: [
] ]
depopts: [ depopts: [
"logs" "logs"
"magic-mime"
"mtime" {>= "2.0"} "mtime" {>= "2.0"}
] ]
build: [ build: [