Skip to content

Commit a3be37c

Browse files
authored
Merge pull request #274 from Julow/fix-transfer-encoding-default
Fix wrong `transfer-encoding` headers being sent
2 parents 4c79e09 + c38f745 commit a3be37c

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/server/ocsigen_response.ml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ end
2626
type t =
2727
{a_response : Response.t; a_body : Body.t; a_cookies : Ocsigen_cookie_map.t}
2828

29+
let remove_header_if_equal_to (resp : Response.t) header equals_to =
30+
match Header.get resp.headers header with
31+
| Some v when String.equal v equals_to ->
32+
{resp with headers = Header.remove resp.headers header}
33+
| _ -> resp
34+
2935
let make ?(body = Body.empty) ?(cookies = Ocsigen_cookie_map.empty) a_response =
36+
(* Remove the erroneous [transfer-encoding] set by default. *)
37+
(* TODO: Deprecate usages of [Cohttp.Response.t] exposed by this API. *)
38+
let a_response =
39+
remove_header_if_equal_to a_response "transfer-encoding" "chunked"
40+
in
3041
{a_response; a_body = body; a_cookies = cookies}
3142

32-
let respond ?headers ~status ~encoding ?(body = Body.empty) () =
33-
let encoding =
34-
match headers with
35-
| None -> encoding
36-
| Some headers -> (
37-
match Cohttp.Header.get_transfer_encoding headers with
38-
| Cohttp.Transfer.Unknown -> encoding
39-
| t -> t)
40-
in
41-
let response = Response.make ~status ~encoding ?headers () in
43+
let respond ?headers ~status ?(body = Body.empty) () =
44+
let response = Response.make ~status ?headers () in
4245
make ~body response
4346

4447
let respond_string ?headers ~status ~body () =
45-
let encoding = Transfer.Fixed (Int64.of_int (String.length body)) in
46-
let response = Response.make ~status ~encoding ?headers () in
48+
let response = Response.make ~status ?headers () in
4749
let body = Body.of_string body in
4850
make ~body response
4951

@@ -101,7 +103,7 @@ let respond_file ?headers ?(status = `OK) fname =
101103
let headers =
102104
Http.Header.add_opt_unless_exists headers "content-type" mime_type
103105
in
104-
Lwt.return (respond ~headers ~status ~encoding ~body ()))
106+
Lwt.return (respond ~headers ~status ~body ()))
105107
(function
106108
| Unix.Unix_error (Unix.ENOENT, _, _) | Isnt_a_file ->
107109
Lwt.return (respond_not_found ())
@@ -148,16 +150,14 @@ let make_cookies_headers path t hds =
148150
(make_cookies_header path exp name v secure))
149151
t hds
150152

151-
let to_cohttp_response {a_response; a_cookies; a_body = _, encoding} =
153+
let to_cohttp_response {a_response; a_cookies; a_body = _, body_encoding} =
152154
let headers =
153155
let add name value headers = Header.add_unless_exists headers name value in
154156
let add_transfer_encoding h =
155-
match encoding with
156-
| Transfer.Chunked -> add "transfer-encoding" "chunked" h
157-
| _ -> h
157+
Header.add_transfer_encoding h body_encoding
158158
in
159-
Ocsigen_cookie_map.Map_path.fold make_cookies_headers a_cookies
160-
(Response.headers a_response)
159+
Response.headers a_response
160+
|> Ocsigen_cookie_map.Map_path.fold make_cookies_headers a_cookies
161161
|> add "server" Ocsigen_config.server_name
162162
|> add "date" (Ocsigen_lib.Date.to_string (Unix.time ()))
163163
|> add_transfer_encoding

src/server/ocsigen_response.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ val make :
2323
-> ?cookies:Ocsigen_cookie_map.t
2424
-> Cohttp.Response.t
2525
-> t
26+
(** Make a response from a [Cohttp.Response.t]. Note that the
27+
[transfer-encoding] header is not taken into account if it is set to
28+
[chunked], use {!add_header}. This is because [Cohttp.Response.make] sets
29+
this header by default, which interferes with the transfer-encoding carried
30+
by the [body]. *)
2631

2732
val respond :
2833
?headers:Cohttp.Header.t
2934
-> status:Http.Status.t
30-
-> encoding:Cohttp.Transfer.encoding
3135
-> ?body:Body.t
3236
-> unit
3337
-> t

test/extensions/deflatemod.t/run.t

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
ocsigen:local-file: [INFO] Testing "./index.html".
1515
ocsigen:local-file: [INFO] checking if file index.html can be sent
1616
ocsigen:local-file: [INFO] Returning "./index.html".
17-
ocsigen:access: connection for local-test from unix:// (): /empty_dir/
17+
ocsigen:access: connection for local-test from unix: (): /empty_dir/
1818
ocsigen:ext: [INFO] host found! local-test:0 matches .*
1919
ocsigen:ext:staticmod: [INFO] Is it a static file?
2020
ocsigen:local-file: [INFO] Testing "./empty_dir/".
2121
ocsigen:local-file: [INFO] Testing "./empty_dir/index.html" as possible index.
2222
ocsigen:local-file: [INFO] No index and no listing
23-
ocsigen:access: connection for local-test from unix:// (): /doesnt_exists.html
23+
ocsigen:access: connection for local-test from unix: (): /doesnt_exists.html
2424
ocsigen:ext: [INFO] host found! local-test:0 matches .*
2525
ocsigen:ext:staticmod: [INFO] Is it a static file?
2626
ocsigen:local-file: [INFO] Testing "./doesnt_exists.html".
@@ -31,8 +31,8 @@ First response is not compressed:
3131
$ curl_ "index.html"
3232
HTTP/1.1 200 OK
3333
content-type: text/html
34-
content-length: 12
3534
server: Ocsigen
35+
content-length: 12
3636

3737
Hello world
3838

@@ -41,7 +41,6 @@ Second response is compressed:
4141
$ curl_ "index.html" --compressed
4242
HTTP/1.1 200 OK
4343
content-type: text/html
44-
content-length: 12
4544
content-encoding: gzip
4645
server: Ocsigen
4746
transfer-encoding: chunked
@@ -53,13 +52,13 @@ compression:
5352

5453
$ mkdir empty_dir && curl_ empty_dir/ --compressed
5554
HTTP/1.1 404 Not Found
56-
content-length: 16
5755
server: Ocsigen
56+
content-length: 16
5857

5958
Error: Not Found
6059
$ curl_ doesnt_exists.html --compressed
6160
HTTP/1.1 404 Not Found
62-
content-length: 16
6361
server: Ocsigen
62+
content-length: 16
6463

6564
Error: Not Found

0 commit comments

Comments
 (0)