Skip to content

Commit

Permalink
add HEAD support
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Apr 15, 2024
1 parent ffb8cc2 commit 6a08aea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/client.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let default_authenticator = Ca_certs.authenticator ()

let connect_via_tls ?authenticator url socket =
let connect_via_tls ?authenticator url socket =
let authenticator =
match authenticator, default_authenticator with
| Some auth, _ | None, Ok auth -> auth
match (authenticator, default_authenticator) with
| Some auth, _ | None, Ok auth -> auth
| _ -> failwith "tls certs authenticator not found"
in
let tls_config = Tls.Config.client ~authenticator () in
Expand Down Expand Up @@ -65,6 +65,7 @@ let request ?headers ?body ?authenticator ~meth env ~sw (url : string) =
in
Response.{ resp; body }

let head = request ~meth:`HEAD
let get = request ~meth:`GET
let post = request ~meth:`POST
let put = request ~meth:`PUT
Expand Down
14 changes: 13 additions & 1 deletion src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,19 @@ module Router = struct
let param, handler =
routes
|> List.find_map (fun (meth', pat, handler) ->
if req.meth <> meth' then None
let matched =
match (req.meth, meth') with
| m1, m2 when m1 = m2 -> true
| `HEAD, `GET -> true
| _ -> false
in
if not matched then None
else
Path_pattern.perform ~pat req.path
|> Option.map (fun param -> (param, handler)))
|> Option.value ~default:([], inner_handler)
in
let truncate_body = req.meth = `HEAD in
let req = Request { req with param } in
let resp =
try handler env req with
Expand All @@ -333,6 +340,11 @@ module Router = struct
(Printexc.get_backtrace ()));
respond ~status:`Internal_server_error ""
in
let resp =
match (truncate_body, resp) with
| false, _ | _, BareResponse _ -> resp
| true, Response r -> Response { r with body = "" }
in
match resp with
| Response ({ status; tags; _ } as r) when Status.is_error status ->
Response { r with tags = "log" :: tags }
Expand Down
6 changes: 6 additions & 0 deletions test/test_http_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ let test_basics () =
| _ -> assert false
in

let resp =
Yume.Client.head env ~sw
(Printf.sprintf "http://localhost:%d/" listening_port)
in
assert (Yume.Client.Response.status resp = `OK);

let resp =
Yume.Client.get env ~sw
(Printf.sprintf "http://localhost:%d/" listening_port)
Expand Down

0 comments on commit 6a08aea

Please # to comment.