expose Response.make_void

it only has no body if the http code is compatible with it.
close #62
This commit is contained in:
Simon Cruanes 2023-05-23 17:18:05 -04:00
parent 9c2cf0900d
commit 997f7aa868
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 8 additions and 2 deletions

View file

@ -249,7 +249,7 @@ let add_vfs_ ~on_fs ~top ~config ~vfs:((module VFS:VFS) as vfs) ~prefix server :
(* redirect using path, not full path *) (* redirect using path, not full path *)
let new_path = "/" // prefix // path // "index.html" in let new_path = "/" // prefix // path // "index.html" in
S._debug (fun k->k "redirect to `%s`" new_path); S._debug (fun k->k "redirect to `%s`" new_path);
S.Response.make_raw ~code:301 "" S.Response.make_void ~code:301 ()
~headers:S.Headers.(empty |> set "location" new_path) ~headers:S.Headers.(empty |> set "location" new_path)
| Lists | Index_or_lists -> | Lists | Index_or_lists ->
let body = html_list_dir ~prefix vfs path ~parent |> Html.to_string_top in let body = html_list_dir ~prefix vfs path ~parent |> Html.to_string_top in

View file

@ -354,7 +354,9 @@ module Response = struct
{ code; headers; body=`Stream body; } { code; headers; body=`Stream body; }
let make_void ?(headers=[]) ~code () : t = let make_void ?(headers=[]) ~code () : t =
{ code; headers; body=`Void; } let is_ok = code < 200 || code = 204 || code = 304 in
if is_ok then { code; headers; body=`Void; }
else make_raw ~headers ~code "" (* invalid to not have a body *)
let make_string ?headers r = match r with let make_string ?headers r = match r with
| Ok body -> make_raw ?headers ~code:200 body | Ok body -> make_raw ?headers ~code:200 body

View file

@ -232,6 +232,10 @@ module Response : sig
(** Same as {!make_raw} but with a stream body. The body will be sent with (** Same as {!make_raw} but with a stream body. The body will be sent with
the chunked transfer-encoding. *) the chunked transfer-encoding. *)
val make_void : ?headers:Headers.t -> code:int -> unit -> t
(** Return a response without a body at all.
@since NEXT_RELEASE *)
val make : val make :
?headers:Headers.t -> ?headers:Headers.t ->
(body, Response_code.t * string) result -> t (body, Response_code.t * string) result -> t