@@ -152,16 +152,19 @@ let _ =
152152 test_storage (module FilesystemStore ) s;
153153 HttpStore. with_open ~net: env#net (Uri. of_string " http://127.0.0.1:8080" ) (fun store ->
154154 let module S = Tiny_httpd in
155- let server = S. create ~max_connections: 1000 ~addr: " 127.0.0.1" ~port: 8080 () in
155+ let server = S. create ~max_connections: 100 ~addr: " 127.0.0.1" ~port: 8080 () in
156156 let dir = tmp_dir in
157157 S. add_route_handler server ~meth: `HEAD S.Route. rest_of_path_urlencoded (fun path _ ->
158158 let fspath = Filename. concat dir path in
159159 match In_channel. (with_open_gen [Open_rdonly ] 0o700 fspath length) with
160160 | exception Sys_error e -> S.Response. make_raw ~code: 404 e
161- | s ->
161+ | l ->
162162 let headers =
163- [(" Content-Length" , Int64. to_string s)
164- ;(" Content-Type" , " application/octet-stream" )]
163+ [(" Content-Length" , Int64. to_string l)
164+ ;(" Content-Type" ,
165+ if String. ends_with ~suffix: " .json" path
166+ then " application/json"
167+ else " application/octet-stream" )]
165168 in
166169 let r = S.Response. make_raw ~code: 200 " " in
167170 S.Response. update_headers (List. append headers) r
@@ -170,22 +173,41 @@ let _ =
170173 let fspath = Filename. concat dir path in
171174 match In_channel. (with_open_gen [Open_rdonly ] 0o700 fspath input_all) with
172175 | exception Sys_error _ -> S.Response. make_raw ~code: 404 (Printf. sprintf " %s not found" path)
173- | s -> S.Response. make_raw ~code: 200 s
176+ | s ->
177+ let headers =
178+ [(" Content-Length" , Int. to_string (String. length s))
179+ ;(" Content-Type" ,
180+ if String. ends_with ~suffix: " .json" path
181+ then " application/json"
182+ else " application/octet-stream" )]
183+ in
184+ S.Response. make_raw ~headers ~code: 200 s
174185 );
175- S. add_route_handler server ~meth: `POST S.Route. rest_of_path_urlencoded (fun path req ->
176- let write oc = Out_channel. (output_string oc req.body; flush oc) in
186+ S. add_route_handler_stream server ~meth: `POST S.Route. rest_of_path_urlencoded (fun path req ->
187+ let write oc =
188+ let max_size = 1024 * 10 * 1024 in
189+ let req' = S.Request. limit_body_size ~bytes: (Bytes. create 4096 ) ~max_size req in
190+ S.IO.Input. iter (Out_channel. output oc) req'.body;
191+ Out_channel. flush oc
192+ in
177193 let fspath = Filename. concat dir path in
178194 Zarr.Util. create_parent_dir fspath 0o700 ;
179195 let f = [Open_wronly ; Open_trunc ; Open_creat ] in
180196 match Out_channel. (with_open_gen f 0o700 fspath write) with
181197 | exception Sys_error e -> S.Response. make_raw ~code: 500 e
182- | () -> S.Response. make_raw ~code: 201 req.body
198+ | () ->
199+ let opt = List. assoc_opt " content-type" req.headers in
200+ let content_type = Option. fold ~none: " application/octet-stream" ~some: Fun. id opt in
201+ let headers = [(" content-type" , content_type); (" Connection" , " close" )] in
202+ S.Response. make_raw ~headers ~code: 201 (Printf. sprintf " %s created" path)
183203 );
184204 S. add_route_handler server ~meth: `DELETE S.Route. rest_of_path_urlencoded (fun path _ ->
185205 let fspath = Filename. concat dir path in
186206 match Sys. remove fspath with
187207 | exception Sys_error e -> S.Response. make_raw ~code: 404 e
188- | () -> S.Response. make_raw ~code: 200 (Printf. sprintf " %s deleted successfully" path)
208+ | () ->
209+ let headers = [(" Connection" , " close" )] in
210+ S.Response. make_raw ~headers ~code: 200 (Printf. sprintf " %s deleted successfully" path)
189211 );
190212 let _ = Thread. create S. run server in
191213 let gnode = Node.Group. root in
0 commit comments