From 997f7aa86801bd33e44329faaccbcad7393d05ae Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 23 May 2023 17:18:05 -0400 Subject: [PATCH] expose `Response.make_void` it only has no body if the http code is compatible with it. close #62 --- src/Tiny_httpd_dir.ml | 2 +- src/Tiny_httpd_server.ml | 4 +++- src/Tiny_httpd_server.mli | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Tiny_httpd_dir.ml b/src/Tiny_httpd_dir.ml index 94b63399..34a5ca03 100644 --- a/src/Tiny_httpd_dir.ml +++ b/src/Tiny_httpd_dir.ml @@ -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 *) let new_path = "/" // prefix // path // "index.html" in 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) | Lists | Index_or_lists -> let body = html_list_dir ~prefix vfs path ~parent |> Html.to_string_top in diff --git a/src/Tiny_httpd_server.ml b/src/Tiny_httpd_server.ml index ba797118..486c54ff 100644 --- a/src/Tiny_httpd_server.ml +++ b/src/Tiny_httpd_server.ml @@ -354,7 +354,9 @@ module Response = struct { code; headers; body=`Stream body; } 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 | Ok body -> make_raw ?headers ~code:200 body diff --git a/src/Tiny_httpd_server.mli b/src/Tiny_httpd_server.mli index 9f41c376..e4962b31 100644 --- a/src/Tiny_httpd_server.mli +++ b/src/Tiny_httpd_server.mli @@ -232,6 +232,10 @@ module Response : sig (** Same as {!make_raw} but with a stream body. The body will be sent with 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 : ?headers:Headers.t -> (body, Response_code.t * string) result -> t