diff --git a/src/Tiny_httpd_dir.ml b/src/Tiny_httpd_dir.ml index 748e7abe..c471d25c 100644 --- a/src/Tiny_httpd_dir.ml +++ b/src/Tiny_httpd_dir.ml @@ -204,10 +204,9 @@ let add_dir_path ~config ~dir ~prefix server = S.Response.make_raw ~code:405 "listing dir not allowed" ) else ( try - let fd = Unix.(openfile full_path [O_RDONLY] 0) in - let len = stat.Unix.st_size in - let bs = Bigstring_unix.map_file_descr fd len in - Gc.finalise (fun _ -> Unix.close fd) bs; + let bs = Bigstring_unix.with_map_file ~flags:[Open_rdonly] full_path + (fun s -> s) + in let mime_type = if Filename.extension full_path = ".css" then ( ["Content-Type", "text/css"] @@ -225,8 +224,10 @@ let add_dir_path ~config ~dir ~prefix server = ~headers:(mime_type@["Etag", mtime_str]) ~code:200 bs with e -> - S.Response.fail ~code:500 "error while reading file: %s" (Printexc.to_string e)))) else ( + S.Response.fail ~code:500 "error while reading file: %s" (Printexc.to_string e)) + ) + ) else ( S.add_route_handler server ~meth:`GET S.Route.(exact_path prefix (string @/ return)) (fun _ _ -> S.Response.make_raw ~code:405 "download not allowed"); - ); + ); diff --git a/tests/dl-out.expect b/tests/dl-out.expect index fb1769c9..d0e79081 100644 --- a/tests/dl-out.expect +++ b/tests/dl-out.expect @@ -1,5 +1,2 @@ serve directory . on http://127.0.0.1:8084 - 0 0 52428800 data21 - 0 0 52428800 data22 - 0 0 52428800 data23 - 0 0 157286400 total + 0 0 52428800 data2 diff --git a/tests/download_chunked.sh b/tests/download_chunked.sh index caa9a8b2..ca70f77b 100755 --- a/tests/download_chunked.sh +++ b/tests/download_chunked.sh @@ -8,16 +8,16 @@ PID=$! sleep 0.1 echo download1 1>&2 -curl -N "http://localhost:${PORT}/foo_50" -o data21 \ +curl -N "http://localhost:${PORT}/foo_50" -o data2 \ -H 'Tranfer-encoding: chunked' echo download2 1>&2 -curl -N "http://localhost:${PORT}/foo_50" -o data22 \ +curl -N "http://localhost:${PORT}/foo_50" -o data2 \ -H 'Tranfer-encoding: chunked' echo download3 1>&2 -curl -N "http://localhost:${PORT}/foo_50" -o data23 \ +curl -N "http://localhost:${PORT}/foo_50" -o data2 \ -H 'Tranfer-encoding: chunked' kill $PID -wc data21 data22 data23 +wc data2