Skip to content

Upgrade to Cohttp 6.0.0 #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
(depends
(ocaml (>= 4.08.1))
(camlzip (>= 1.04))
(cohttp-lwt-unix (and (>= 5.0) (< 6.0)))
(conduit-lwt-unix (and (>= 2.0) (< 7.0)))
(cohttp-lwt-unix (>= 6.0))
conduit-lwt-unix
http
cryptokit
(ipaddr (>= 2.1))
Expand Down
4 changes: 2 additions & 2 deletions ocsigenserver.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ depends: [
"dune" {>= "3.19"}
"ocaml" {>= "4.08.1"}
"camlzip" {>= "1.04"}
"cohttp-lwt-unix" {>= "5.0" & < "6.0"}
"conduit-lwt-unix" {>= "2.0" & < "7.0"}
"cohttp-lwt-unix" {>= "6.0"}
"conduit-lwt-unix"
"http"
"cryptokit"
"ipaddr" {>= "2.1"}
Expand Down
11 changes: 5 additions & 6 deletions src/extensions/deflatemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ let stream_filter contentencoding url deflate choice res =
| None, _ | _, None -> Lwt.return res
| Some a, Some b when should_compress (a, b) url choice ->
let response =
let response = Ocsigen_response.response res in
let headers = Cohttp.Response.headers response in
let {Http.Response.headers; status; version} =
Ocsigen_response.response res
in
let headers =
let name = Ocsigen_header.Name.(to_string etag) in
match Cohttp.Header.get headers name with
Expand All @@ -217,13 +218,11 @@ let stream_filter contentencoding url deflate choice res =
| None -> headers
in
let headers =
Cohttp.Header.replace headers
Http.Header.replace headers
Ocsigen_header.Name.(to_string content_encoding)
contentencoding
in
{ response with
Cohttp.Response.headers
; Cohttp.Response.encoding = Cohttp.Transfer.Chunked }
Http.Response.make ~headers ~status ~version ()
and body =
Ocsigen_response.Body.make Cohttp.Transfer.Chunked
(compress_body deflate
Expand Down
2 changes: 0 additions & 2 deletions src/server/ocsigen_cohttp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =

let conn_closed (_flow, conn) =
try
Logs.debug ~src:section (fun fmt ->
fmt "Connection closed:\n%s" (Cohttp.Connection.to_string conn));
Lwt.wakeup (snd (Hashtbl.find connections conn)) ();
Hashtbl.remove connections conn;
decr_connected ()
Expand Down
18 changes: 13 additions & 5 deletions src/server/ocsigen_response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ let respond_error ?headers ?(status = `Internal_server_error) ~body () =
let update ?response ?body ?cookies {a_response; a_body; a_cookies} =
let a_response =
match response with Some response -> response | None -> a_response
and a_body = match body with Some body -> body | None -> a_body
in
let a_body = match body with Some body -> body | None -> a_body
and a_cookies =
match cookies with Some cookies -> cookies | None -> a_cookies
in
Expand Down Expand Up @@ -90,19 +91,25 @@ let make_cookies_headers path t hds =
(make_cookies_header path exp name v secure))
t hds

let to_cohttp_response {a_response; a_cookies; a_body = _} =
let to_cohttp_response {a_response; a_cookies; a_body = _, encoding} =
let headers =
let add name value headers = Header.add_unless_exists headers name value in
let add_transfer_encoding h =
match encoding with
| Transfer.Chunked -> add "transfer-encoding" "chunked" h
| _ -> h
in
Ocsigen_cookie_map.Map_path.fold make_cookies_headers a_cookies
(Response.headers a_response)
|> add "server" Ocsigen_config.server_name
|> add "date" (Ocsigen_lib.Date.to_string (Unix.time ()))
|> add_transfer_encoding
in
{a_response with Response.headers}

let to_response_expert t =
let module R = Cohttp_lwt_unix.Response in
let write_footer {R.encoding; _} oc =
let write_footer encoding oc =
(* Copied from [cohttp/response.ml]. *)
match encoding with
| Transfer.Chunked -> Lwt_io.write oc "0\r\n\r\n"
Expand All @@ -112,8 +119,9 @@ let to_response_expert t =
( res
, fun _ic oc ->
let writer = R.make_body_writer ~flush:false res oc in
let* () = fst t.a_body (R.write_body writer) in
write_footer res oc )
let body, encoding = t.a_body in
let* () = body (R.write_body writer) in
write_footer encoding oc )

let response t = t.a_response
let body t = t.a_body
Expand Down
3 changes: 2 additions & 1 deletion test/extensions/deflatemod.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ First response is not compressed:
$ curl_ "index.html"
HTTP/1.1 200 OK
content-type: text/html
server: Ocsigen
content-length: 12
server: Ocsigen

Hello world

Expand All @@ -31,6 +31,7 @@ Second response is compressed:
$ curl_ "index.html" --compressed
HTTP/1.1 200 OK
content-type: text/html
content-length: 12
content-encoding: gzip
server: Ocsigen
transfer-encoding: chunked
Expand Down