Skip to content

Commit

Permalink
fix: remove encoding from response
Browse files Browse the repository at this point in the history
Co-authored-by: mefyl <mefyl@gruntech.org>

<!-- ps-id: 5a6bf1dd-0b7f-4321-a861-20982306c8e4 -->
  • Loading branch information
rgrinberg committed Jul 13, 2024
1 parent 12ca7f0 commit 64c5711
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
32 changes: 21 additions & 11 deletions cohttp-eio/src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,52 @@ let write output (response : Cohttp.Response.t) body =
(Cohttp.Header.get_transfer_encoding response.headers, content_length)
with
| Unknown, None ->
{ response with encoding = Chunked } [@ocaml.warning "-3"]
let headers =
Cohttp.Header.add_transfer_encoding response.headers Chunked
in
{ response with headers }
| Unknown, Some size ->
{ response with encoding = Fixed (Int64.of_int size) }
[@ocaml.warning "-3"]
| from_headers, _ ->
{ response with encoding = from_headers } [@ocaml.warning "-3"]
let headers =
Cohttp.Header.add_transfer_encoding response.headers
(Fixed (Int64.of_int size))
in
{ response with headers }
| _, _ -> response
in
let () = Logs.debug (fun m -> m "send headers") in
let () =
Io.Response.write ~flush:false
(fun writer ->
let () =
Logs.debug (fun m ->
(m "send body (%a)" Cohttp.Transfer.pp_encoding response.encoding
[@ocaml.warning "-3"]))
m "send body (%a)" Cohttp.Transfer.pp_encoding
(Cohttp.Header.get_transfer_encoding response.headers))
in
flow_to_writer body writer Io.Response.write_body)
response output
in
Eio.Buf_write.flush output
let respond ?(headers = Cohttp.Header.init ()) ?flush ~status ~body ()
let respond ?encoding ?(headers = Cohttp.Header.init ()) ?flush ~status ~body ()
(request, oc) =
let keep_alive = Http.Request.is_keep_alive request in
let headers =
match Cohttp.Header.connection headers with
| Some _ -> headers
| None ->
Http.Header.add headers "connection"
(if keep_alive then "keep-alive" else "close")
| Some _ -> headers
in
let response = Cohttp.Response.make ~headers ?flush ~status () in
let response = Cohttp.Response.make ?encoding ~headers ?flush ~status () in
write oc response body
let respond_string ?headers ?flush ~status ~body () =
respond ?headers ?flush ~status ~body:(Body.of_string body) ()
respond
~encoding:(Fixed (String.length body |> Int64.of_int))
?headers ?flush ~status ~body:(Body.of_string body) ()
let respond ?headers ?flush ~status ~body () response =
respond ?encoding:None ?headers ?flush ~status ~body () response
let callback { conn_closed; handler } ((_, peer_address) as conn) input output =
let id = (Cohttp.Connection.create () [@ocaml.warning "-3"]) in
Expand Down
3 changes: 2 additions & 1 deletion cohttp-lwt-unix/test/test_sanity_noisy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ let ts_noisy =
Client.get ~ctx uri >>= fun (resp, body) ->
assert_equal (Response.status resp) `Not_modified;
let headers = Response.headers resp in
assert_equal ~printer:Transfer.string_of_encoding Transfer.Unknown
assert_equal ~printer:Transfer.string_of_encoding
Transfer.(Fixed 0L)
(Header.get_transfer_encoding headers);
body |> Body.is_empty >|= fun is_empty ->
assert_bool "No body returned when not modified" is_empty
Expand Down
40 changes: 19 additions & 21 deletions cohttp/src/response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,41 @@
open Sexplib0.Sexp_conv

type t = Http.Response.t = {
encoding : Transfer.encoding;
headers : Header.t;
version : Code.version;
status : Code.status_code;
flush : bool;
}
[@@deriving sexp]

let compare { headers; flush; version; encoding; status } y =
let compare { headers; flush; version; status } y =
match Header.compare headers y.headers with
| 0 -> (
match Bool.compare flush y.flush with
| 0 -> (
match Stdlib.compare status y.status with
| 0 -> (
match Code.compare_version version y.version with
| 0 -> Stdlib.compare encoding y.encoding
| i -> i)
| 0 -> Code.compare_version version y.version
| i -> i)
| i -> i)
| i -> i

let headers t = t.headers
let encoding t = t.encoding
let encoding t = Header.get_transfer_encoding t.headers
let version t = t.version
let status t = t.status
let flush t = t.flush

let make ?(version = `HTTP_1_1) ?(status = `OK) ?(flush = false)
?(encoding = Transfer.Chunked) ?(headers = Header.init ()) () =
let encoding =
match Header.get_transfer_encoding headers with
| Transfer.Unknown -> encoding
| encoding -> encoding
?(encoding = Transfer.Unknown) ?(headers = Header.init ()) () =
let headers =
match encoding with
| Unknown -> (
match Header.get_transfer_encoding headers with
| Unknown -> Header.add_transfer_encoding headers Chunked
| _ -> headers)
| _ -> Header.add_transfer_encoding headers encoding
in
{ encoding; headers; version; flush; status }
{ headers; version; flush; status }

let pp_hum ppf r =
Format.fprintf ppf "%s" (r |> sexp_of_t |> Sexplib0.Sexp.to_string_hum)
Expand Down Expand Up @@ -103,11 +102,10 @@ module Make (IO : S.IO) = struct
| `Invalid _reason as r -> return r
| `Ok (version, status) ->
Header_IO.parse ic >>= fun headers ->
let encoding = Header.get_transfer_encoding headers in
let flush = false in
return (`Ok { encoding; headers; version; status; flush })
return (`Ok { headers; version; status; flush })

let make_body_reader { encoding; _ } ic = Transfer_IO.make_reader encoding ic
let make_body_reader t ic = Transfer_IO.make_reader (encoding t) ic
let read_body_chunk = Transfer_IO.read

let write_header res oc =
Expand All @@ -118,18 +116,18 @@ module Make (IO : S.IO) = struct
>>= fun () ->
let headers =
if allowed_body res then
Header.add_transfer_encoding res.headers res.encoding
Header.add_transfer_encoding res.headers (encoding res)
else res.headers
in
Header_IO.write headers oc

let make_body_writer ~flush { encoding; _ } oc =
Transfer_IO.make_writer ~flush encoding oc
let make_body_writer ~flush t oc =
Transfer_IO.make_writer ~flush (encoding t) oc

let write_body = Transfer_IO.write

let write_footer { encoding; _ } oc =
match encoding with
let write_footer t oc =
match encoding t with
| Transfer.Chunked ->
(* TODO Trailer header support *)
IO.write oc "0\r\n\r\n"
Expand Down
2 changes: 0 additions & 2 deletions cohttp/src/s.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ end

module type Response = sig
type t = {
encoding : Transfer.encoding;
[@deprecated "this field will be removed in the future"]
headers : Header.t; (** response HTTP headers *)
version : Code.version; (** (** HTTP version, usually 1.1 *) *)
status : Code.status_code; (** HTTP status code of the response *)
Expand Down
12 changes: 3 additions & 9 deletions http/src/http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -804,34 +804,28 @@ end

module Response = struct
type t = {
encoding : Transfer.encoding;
headers : Header.t; (** response HTTP headers *)
version : Version.t; (** (** HTTP version, usually 1.1 *) *)
status : Status.t; (** HTTP status code of the response *)
flush : bool;
}

let compare { headers; flush; version; encoding; status } y =
let compare { headers; flush; version; status } y =
match Header.compare headers y.headers with
| 0 -> (
match Bool.compare flush y.flush with
| 0 -> (
match Stdlib.compare status y.status with
| 0 -> (
match Version.compare version y.version with
| 0 -> Transfer.compare_encoding encoding y.encoding
| i -> i)
| 0 -> Version.compare version y.version
| i -> i)
| i -> i)
| i -> i

let make ?(version = `HTTP_1_1) ?(status = `OK) ?(flush = false)
?(headers = Header.empty) () =
let encoding = Header.get_transfer_encoding headers in
{ encoding; headers; version; flush; status }
{ headers; version; flush; status }

let headers t = t.headers
let encoding t = t.encoding
let version t = t.version
let status t = t.status
let flush t = t.flush
Expand Down
3 changes: 0 additions & 3 deletions http/src/http.mli
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ end

module Response : sig
type t = {
encoding : Transfer.encoding;
[@deprecated "this field will be removed in the future"]
headers : Header.t; (** response HTTP headers *)
version : Version.t; (** (** HTTP version, usually 1.1 *) *)
status : Status.t; (** HTTP status code of the response *)
Expand All @@ -456,7 +454,6 @@ module Response : sig
[respond_*] function instead."]
}

val encoding : t -> Transfer.encoding
val headers : t -> Header.t
val version : t -> Version.t
val status : t -> Status.t
Expand Down

0 comments on commit 64c5711

Please sign in to comment.