|
26 | 26 | type t = |
27 | 27 | {a_response : Response.t; a_body : Body.t; a_cookies : Ocsigen_cookie_map.t} |
28 | 28 |
|
| 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 | + |
29 | 35 | 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 |
30 | 41 | {a_response; a_body = body; a_cookies = cookies} |
31 | 42 |
|
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 |
42 | 45 | make ~body response |
43 | 46 |
|
44 | 47 | 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 |
47 | 49 | let body = Body.of_string body in |
48 | 50 | make ~body response |
49 | 51 |
|
@@ -101,7 +103,7 @@ let respond_file ?headers ?(status = `OK) fname = |
101 | 103 | let headers = |
102 | 104 | Http.Header.add_opt_unless_exists headers "content-type" mime_type |
103 | 105 | in |
104 | | - Lwt.return (respond ~headers ~status ~encoding ~body ())) |
| 106 | + Lwt.return (respond ~headers ~status ~body ())) |
105 | 107 | (function |
106 | 108 | | Unix.Unix_error (Unix.ENOENT, _, _) | Isnt_a_file -> |
107 | 109 | Lwt.return (respond_not_found ()) |
@@ -148,16 +150,14 @@ let make_cookies_headers path t hds = |
148 | 150 | (make_cookies_header path exp name v secure)) |
149 | 151 | t hds |
150 | 152 |
|
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} = |
152 | 154 | let headers = |
153 | 155 | let add name value headers = Header.add_unless_exists headers name value in |
154 | 156 | 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 |
158 | 158 | 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 |
161 | 161 | |> add "server" Ocsigen_config.server_name |
162 | 162 | |> add "date" (Ocsigen_lib.Date.to_string (Unix.time ())) |
163 | 163 | |> add_transfer_encoding |
|
0 commit comments