Skip to content

Commit 5ecc86e

Browse files
committed
update eio http
1 parent d7e41a8 commit 5ecc86e

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

zarr-eio/src/storage.ml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ module HttpStore = struct
136136

137137
open Cohttp_eio
138138

139-
let raise_status_error s =
140-
let c = Cohttp.Code.code_of_status s in
139+
let raise_status_error e =
140+
let c = Cohttp.Code.code_of_status e in
141141
raise (Request_failed (c, Cohttp.Code.reason_phrase_of_code c))
142142

143+
let fold_response ~success resp key = match Http.Response.status resp with
144+
| #Http.Status.success -> success ()
145+
| #Http.Status.client_error as e when e = `Not_found ->
146+
raise (Zarr.Storage.Key_not_found key)
147+
| e -> raise_status_error e
148+
143149
module IO = struct
144150
module Deferred = Deferred
145151

@@ -149,31 +155,22 @@ module HttpStore = struct
149155
Eio.Switch.run @@ fun sw ->
150156
let url = Uri.with_path t.base_url key in
151157
let resp, body = Client.get ~sw t.client url in
152-
match Http.Response.status resp with
153-
| #Http.Status.success -> Eio.Flow.read_all body
154-
| #Http.Status.client_error as e when e = `Not_found ->
155-
raise (Zarr.Storage.Key_not_found key)
156-
| e -> raise_status_error e
158+
fold_response ~success:(fun () -> Eio.Flow.read_all body) resp key
157159

158160
let size t key = try String.length (get t key) with
159161
| Zarr.Storage.Key_not_found _ -> 0
160162

161163
(*let size t key =
164+
let content_length resp () = match Http.Response.content_length resp with
165+
| Some l -> l
166+
| None -> String.length (get t key)
167+
in
162168
Eio.Switch.run @@ fun sw ->
163169
let url = Uri.with_path t.base_url key in
164170
let resp = Client.head ~sw t.client url in
165-
match Http.Response.status resp with
166-
| #Http.Status.success ->
167-
begin match Http.Response.content_length resp with
168-
| Some l -> l
169-
| None ->
170-
try String.length (get t key) with
171-
| Zarr.Storage.Key_not_found _ -> 0
172-
end
173-
| #Http.Status.client_error as e when e = `Not_found -> Deferred.return 0
174-
| e -> raise_status_error e *)
171+
fold_response ~success:(content_length resp) resp key *)
175172

176-
let is_member t key = if (size t key) = 0 then false else true
173+
let is_member t key = if (size t key) > 0 then true else false
177174

178175
let get_partial_values t key ranges =
179176
let read_range ~data ~size (ofs, len) = match len with
@@ -189,16 +186,12 @@ module HttpStore = struct
189186
let url = Uri.with_path t.base_url key in
190187
let headers = Http.Header.of_list [("Content-Length", string_of_int (String.length data))] in
191188
let body = Body.of_string data in
192-
let resp, _ = Client.post ~sw ~headers ~body t.client url in
193-
match Http.Response.status resp with
194-
| #Http.Status.success -> Deferred.return_unit
195-
| e -> raise_status_error e
189+
let resp, _ = Client.put ~sw ~headers ~body t.client url in
190+
fold_response ~success:(fun () -> ()) resp key
196191

197192
let set_partial_values t key ?(append=false) rsv =
198-
let size = size t key in
199-
let ov = match size with
200-
| 0 -> Deferred.return String.empty
201-
| _ -> get t key
193+
let ov = try get t key with
194+
| Zarr.Storage.Key_not_found _ -> String.empty
202195
in
203196
let f = if append || ov = String.empty then
204197
fun acc (_, v) -> acc ^ v else
@@ -215,6 +208,7 @@ module HttpStore = struct
215208
let resp, _ = Client.delete ~sw t.client url in
216209
match Http.Response.status resp with
217210
| #Http.Status.success -> Deferred.return_unit
211+
| #Http.Status.client_error as e when e = `Not_found -> Deferred.return_unit
218212
| e -> raise_status_error e *)
219213

220214
let erase _ = raise Not_implemented

zarr-eio/test/test_eio.ml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ let test_readable_writable_only
127127
let attrs = `Assoc [("questions", `String "answer")] in
128128
Group.create ~attrs store gnode;
129129
let exists = Group.exists store gnode in
130-
let meta = Group.metadata store gnode in
131130
assert_equal ~printer:string_of_bool true exists;
131+
let meta = Group.metadata store gnode in
132132
assert_equal ~printer:Yojson.Safe.show attrs (Metadata.Group.attributes meta);
133133
let exists = Array.exists store Node.Array.(gnode / "non-member") in
134134
assert_equal ~printer:string_of_bool false exists;
@@ -181,19 +181,14 @@ module Dir_http_server = struct
181181
let server = S.create ~max_connections ~addr:"127.0.0.1" ~port:8080 () in
182182
(* HEAD request handler *)
183183
S.add_route_handler server ~meth:`HEAD S.Route.rest_of_path_urlencoded (fun path _ ->
184+
let headers = [("Content-Type", if String.ends_with ~suffix:".json" path then "application/json" else "application/octet-stream")] in
184185
let fspath = Filename.concat dir path in
185-
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with
186-
| exception Sys_error e -> S.Response.make_raw ~code:404 e
187-
| l ->
188-
let headers =
189-
[("Content-Length", Int64.to_string l)
190-
;("Content-Type",
191-
if String.ends_with ~suffix:".json" path
192-
then "application/json"
193-
else "application/octet-stream")]
194-
in
195-
let r = S.Response.make_raw ~code:200 "" in
196-
S.Response.update_headers (List.append headers) r
186+
let headers = match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with
187+
| exception Sys_error _ -> ("Content-Length", "0") :: headers
188+
| l -> ("Content-Length", Int64.to_string l) :: headers
189+
in
190+
let r = S.Response.make_raw ~code:200 "" in
191+
S.Response.update_headers (List.append headers) r
197192
);
198193
(* GET request handler *)
199194
S.add_route_handler server ~meth:`GET S.Route.rest_of_path_urlencoded (fun path _ ->
@@ -211,7 +206,7 @@ module Dir_http_server = struct
211206
S.Response.make_raw ~headers ~code:200 s
212207
);
213208
(* POST request handler *)
214-
S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
209+
S.add_route_handler_stream server ~meth:`PUT S.Route.rest_of_path_urlencoded (fun path req ->
215210
let write oc =
216211
let max_size = 1024 * 10 * 1024 in
217212
let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in

0 commit comments

Comments
 (0)