From 4f730ec7b61fcf0f02839417b2e9f89fc4ee15b5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 27 May 2020 21:39:43 -0400 Subject: [PATCH] fix: set transfer-encoding header when returning a chunked stream --- src/Tiny_httpd.ml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 84a0d6fb..5843bf2a 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -619,7 +619,16 @@ module Response = struct let output_ (oc:out_channel) (self:t) : unit = Printf.fprintf oc "HTTP/1.1 %d %s\r\n" self.code (Response_code.descr self.code); - List.iter (fun (k,v) -> Printf.fprintf oc "%s: %s\r\n" k v) self.headers; + let is_chunked = match self.body with + | `String _ -> false + | `Stream _ -> true + in + let headers = + if is_chunked + then Headers.set "transfer-encoding" "chunked" self.headers + else self.headers + in + List.iter (fun (k,v) -> Printf.fprintf oc "%s: %s\r\n" k v) headers; output_string oc "\r\n"; begin match self.body with | `String "" -> ()