Skip to content

Commit

Permalink
Merge pull request #1012 from mefyl/fix/eio-body-encoding
Browse files Browse the repository at this point in the history
cohttp-eio: Match body encoding with headers.
  • Loading branch information
mseri authored Jan 5, 2024
2 parents 5711091 + ad50b32 commit 537c9cd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v6.0.0~beta2

- cohttp-eio: Don't blow up Server on client disconnections. (mefyl #1011)
- cohttp-eio: Match body encoding with headers. (mefyl #1012)

## v6.0.0~beta1 (2023-10-27)
- cohttp-eio: move new Cohttp.{Client,Server} modules under Cohttp.Generic (mseri #1003)
Expand Down
21 changes: 14 additions & 7 deletions cohttp-eio/src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let read input =
in
`Ok (request, body))
let write output response body =
let write output (response : Cohttp.Response.t) body =
let response =
let content_length =
let (Eio.Resource.T (body, ops)) = body in
Expand All @@ -67,19 +67,26 @@ let write output response body =
in
(* encoding field might be deprecated but it is still used
to compute headers and encode the body*)
match content_length with
| None ->
{ response with Cohttp.Response.encoding = Chunked }
[@ocaml.warning "-3"]
| Some size ->
match
(Cohttp.Header.get_transfer_encoding response.headers, content_length)
with
| Unknown, None ->
{ response with encoding = Chunked } [@ocaml.warning "-3"]
| Unknown, Some size ->
{ response with encoding = Fixed (Int64.of_int size) }
[@ocaml.warning "-3"]
| from_headers, _ ->
{ response with encoding = from_headers } [@ocaml.warning "-3"]
in
let () = Logs.debug (fun m -> m "send headers") in
let () =
Io.Response.write
(fun writer ->
let () = Logs.debug (fun m -> m "send body") in
let () =
Logs.debug (fun m ->
(m "send body (%a)" Cohttp.Transfer.pp_encoding response.encoding
[@ocaml.warning "-3"]))
in
flow_to_writer body writer Io.Response.write_body)
response output
in
Expand Down
5 changes: 5 additions & 0 deletions cohttp/src/transfer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ open Sexplib0.Sexp_conv
type encoding = Http.Transfer.encoding = Chunked | Fixed of int64 | Unknown
[@@deriving sexp]

let pp_encoding fmt = function
| Chunked -> Format.pp_print_string fmt "chunked"
| Fixed size -> Format.fprintf fmt "fixed %Ld" size
| Unknown -> Format.pp_print_string fmt "unknown"

type chunk = Chunk of string | Final_chunk of string | Done [@@deriving sexp]

let string_of_encoding = function
Expand Down
3 changes: 3 additions & 0 deletions cohttp/src/transfer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type encoding = Http.Transfer.encoding =
| Unknown (** unknown body size, which leads to best-effort *)
[@@deriving sexp]

val pp_encoding : Format.formatter -> encoding -> unit
(** Human-readable output. *)

(** A chunk of body that also signals if there to more to arrive *)
type chunk =
| Chunk of string (** chunk of data and not the end of stream *)
Expand Down

0 comments on commit 537c9cd

Please sign in to comment.