Skip to content

Commit ed1160c

Browse files
authored
Merge pull request #267 from Julow/compat_with_cohttp_610
Upgrade to Cohttp 6.0.0
2 parents c39e80f + 148315a commit ed1160c

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

dune-project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
(depends
1919
(ocaml (>= 4.08.1))
2020
(camlzip (>= 1.04))
21-
(cohttp-lwt-unix (and (>= 5.0) (< 6.0)))
22-
(conduit-lwt-unix (and (>= 2.0) (< 7.0)))
21+
(cohttp-lwt-unix (>= 6.0))
22+
conduit-lwt-unix
2323
http
2424
cryptokit
2525
(ipaddr (>= 2.1))

ocsigenserver.opam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ depends: [
1313
"dune" {>= "3.19"}
1414
"ocaml" {>= "4.08.1"}
1515
"camlzip" {>= "1.04"}
16-
"cohttp-lwt-unix" {>= "5.0" & < "6.0"}
17-
"conduit-lwt-unix" {>= "2.0" & < "7.0"}
16+
"cohttp-lwt-unix" {>= "6.0"}
17+
"conduit-lwt-unix"
1818
"http"
1919
"cryptokit"
2020
"ipaddr" {>= "2.1"}

src/extensions/deflatemod.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ let stream_filter contentencoding url deflate choice res =
205205
| None, _ | _, None -> Lwt.return res
206206
| Some a, Some b when should_compress (a, b) url choice ->
207207
let response =
208-
let response = Ocsigen_response.response res in
209-
let headers = Cohttp.Response.headers response in
208+
let {Http.Response.headers; status; version} =
209+
Ocsigen_response.response res
210+
in
210211
let headers =
211212
let name = Ocsigen_header.Name.(to_string etag) in
212213
match Cohttp.Header.get headers name with
@@ -217,13 +218,11 @@ let stream_filter contentencoding url deflate choice res =
217218
| None -> headers
218219
in
219220
let headers =
220-
Cohttp.Header.replace headers
221+
Http.Header.replace headers
221222
Ocsigen_header.Name.(to_string content_encoding)
222223
contentencoding
223224
in
224-
{ response with
225-
Cohttp.Response.headers
226-
; Cohttp.Response.encoding = Cohttp.Transfer.Chunked }
225+
Http.Response.make ~headers ~status ~version ()
227226
and body =
228227
Ocsigen_response.Body.make Cohttp.Transfer.Chunked
229228
(compress_body deflate

src/server/ocsigen_cohttp.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
155155

156156
let conn_closed (_flow, conn) =
157157
try
158-
Logs.debug ~src:section (fun fmt ->
159-
fmt "Connection closed:\n%s" (Cohttp.Connection.to_string conn));
160158
Lwt.wakeup (snd (Hashtbl.find connections conn)) ();
161159
Hashtbl.remove connections conn;
162160
decr_connected ()

src/server/ocsigen_response.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ let respond_error ?headers ?(status = `Internal_server_error) ~body () =
5353
let update ?response ?body ?cookies {a_response; a_body; a_cookies} =
5454
let a_response =
5555
match response with Some response -> response | None -> a_response
56-
and a_body = match body with Some body -> body | None -> a_body
56+
in
57+
let a_body = match body with Some body -> body | None -> a_body
5758
and a_cookies =
5859
match cookies with Some cookies -> cookies | None -> a_cookies
5960
in
@@ -90,19 +91,25 @@ let make_cookies_headers path t hds =
9091
(make_cookies_header path exp name v secure))
9192
t hds
9293

93-
let to_cohttp_response {a_response; a_cookies; a_body = _} =
94+
let to_cohttp_response {a_response; a_cookies; a_body = _, encoding} =
9495
let headers =
9596
let add name value headers = Header.add_unless_exists headers name value in
97+
let add_transfer_encoding h =
98+
match encoding with
99+
| Transfer.Chunked -> add "transfer-encoding" "chunked" h
100+
| _ -> h
101+
in
96102
Ocsigen_cookie_map.Map_path.fold make_cookies_headers a_cookies
97103
(Response.headers a_response)
98104
|> add "server" Ocsigen_config.server_name
99105
|> add "date" (Ocsigen_lib.Date.to_string (Unix.time ()))
106+
|> add_transfer_encoding
100107
in
101108
{a_response with Response.headers}
102109

103110
let to_response_expert t =
104111
let module R = Cohttp_lwt_unix.Response in
105-
let write_footer {R.encoding; _} oc =
112+
let write_footer encoding oc =
106113
(* Copied from [cohttp/response.ml]. *)
107114
match encoding with
108115
| Transfer.Chunked -> Lwt_io.write oc "0\r\n\r\n"
@@ -112,8 +119,9 @@ let to_response_expert t =
112119
( res
113120
, fun _ic oc ->
114121
let writer = R.make_body_writer ~flush:false res oc in
115-
let* () = fst t.a_body (R.write_body writer) in
116-
write_footer res oc )
122+
let body, encoding = t.a_body in
123+
let* () = body (R.write_body writer) in
124+
write_footer encoding oc )
117125

118126
let response t = t.a_response
119127
let body t = t.a_body

test/extensions/deflatemod.t/run.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ First response is not compressed:
2121
$ curl_ "index.html"
2222
HTTP/1.1 200 OK
2323
content-type: text/html
24-
server: Ocsigen
2524
content-length: 12
25+
server: Ocsigen
2626

2727
Hello world
2828

@@ -31,6 +31,7 @@ Second response is compressed:
3131
$ curl_ "index.html" --compressed
3232
HTTP/1.1 200 OK
3333
content-type: text/html
34+
content-length: 12
3435
content-encoding: gzip
3536
server: Ocsigen
3637
transfer-encoding: chunked

0 commit comments

Comments
 (0)