diff --git a/examples/echo.ml b/examples/echo.ml index d00d468d..fb06586a 100644 --- a/examples/echo.ml +++ b/examples/echo.ml @@ -88,8 +88,8 @@ let () = "echo [option]*"; let server = S.create ~port:!port_ ~max_connections:!j () in - Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16 * 1024) server; + Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16 * 1024) server; let m_stats, get_stats = middleware_stat () in S.add_middleware server ~stage:(`Stage 1) m_stats; diff --git a/src/Tiny_httpd_io.ml b/src/Tiny_httpd_io.ml index 3aef4c84..c86575c5 100644 --- a/src/Tiny_httpd_io.ml +++ b/src/Tiny_httpd_io.ml @@ -84,6 +84,8 @@ module Out_channel = struct let chunk_encoding ~close_rec (self : t) : t = let flush = self.flush in let close () = + (* write an empty chunk to close the stream *) + output_string self "0\r\n"; (* write another crlf after the stream (see #56) *) output_string self "\r\n"; self.flush (); @@ -92,7 +94,8 @@ module Out_channel = struct let output buf i n = if n > 0 then ( output_string self (Printf.sprintf "%x\r\n" n); - self.output buf i n + self.output buf i n; + output_string self "\r\n" ) in { flush; close; output } diff --git a/src/Tiny_httpd_server.ml b/src/Tiny_httpd_server.ml index 0d0685f3..835a3f7f 100644 --- a/src/Tiny_httpd_server.ml +++ b/src/Tiny_httpd_server.ml @@ -478,7 +478,7 @@ module Response = struct let self = { self with headers; body } in _debug (fun k -> k "output response: %s" - (Format.asprintf "%a" pp { self with body = `String "<…>" })); + (Format.asprintf "%a" pp { self with body = `String "<...>" })); (* write headers *) List.iter diff --git a/src/Tiny_httpd_stream.ml b/src/Tiny_httpd_stream.ml index d9a367a3..e5cfb5e4 100644 --- a/src/Tiny_httpd_stream.ml +++ b/src/Tiny_httpd_stream.ml @@ -83,11 +83,10 @@ let rec iter f (self : t) : unit = (iter [@tailcall]) f self ) -let to_chan (oc : out_channel) (self : t) = - iter (fun s i len -> output oc s i len) self +let to_chan (oc : out_channel) (self : t) = iter (output oc) self let to_chan' (oc : IO.Out_channel.t) (self : t) = - iter (fun s i len -> IO.Out_channel.output oc s i len) self + iter (IO.Out_channel.output oc) self let to_writer (self : t) : Tiny_httpd_io.Writer.t = { write = (fun oc -> to_chan' oc self) } diff --git a/src/camlzip/Tiny_httpd_camlzip.ml b/src/camlzip/Tiny_httpd_camlzip.ml index 77993147..241577ce 100644 --- a/src/camlzip/Tiny_httpd_camlzip.ml +++ b/src/camlzip/Tiny_httpd_camlzip.ml @@ -59,7 +59,6 @@ let encode_deflate_writer_ ~buf_size (w : W.t) : W.t = ) in - (* Zlib.Z_NO_FLUSH *) let flush_zlib ~flush (oc : Out.t) = let continue = ref true in while !continue do @@ -91,18 +90,19 @@ let encode_deflate_writer_ ~buf_size (w : W.t) : W.t = let write (oc : Out.t) : unit = let output buf i len = write_zlib ~flush:Zlib.Z_NO_FLUSH oc buf i len in let flush () = - flush_zlib oc ~flush:Zlib.Z_SYNC_FLUSH; + flush_zlib oc ~flush:Zlib.Z_FINISH; + assert (!o_len = 0); oc.flush () in let close () = - flush_zlib oc ~flush:Zlib.Z_FULL_FLUSH; - assert (!o_len = 0); + flush (); Zlib.deflate_end zlib_str; oc.close () in (* new output channel that compresses on the fly *) let oc' = { Out.flush; close; output } in - w.write oc' + w.write oc'; + oc'.close () in W.make ~write ()