mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
fix: do not set content-length for server-sent events
This commit is contained in:
parent
62d583553e
commit
95d9bd10ef
2 changed files with 11 additions and 5 deletions
|
|
@ -576,7 +576,7 @@ end
|
||||||
*)
|
*)
|
||||||
|
|
||||||
module Response = struct
|
module Response = struct
|
||||||
type body = [`String of string | `Stream of byte_stream]
|
type body = [`String of string | `Stream of byte_stream | `Void]
|
||||||
type t = {
|
type t = {
|
||||||
code: Response_code.t;
|
code: Response_code.t;
|
||||||
headers: Headers.t;
|
headers: Headers.t;
|
||||||
|
|
@ -595,6 +595,9 @@ module Response = struct
|
||||||
let headers = Headers.set "Transfer-Encoding" "chunked" headers in
|
let headers = Headers.set "Transfer-Encoding" "chunked" headers in
|
||||||
{ code; headers; body=`Stream body; }
|
{ code; headers; body=`Stream body; }
|
||||||
|
|
||||||
|
let make_void ?(headers=[]) ~code () : t =
|
||||||
|
{ code; headers; body=`Void; }
|
||||||
|
|
||||||
let make_string ?headers r = match r with
|
let make_string ?headers r = match r with
|
||||||
| Ok body -> make_raw ?headers ~code:200 body
|
| Ok body -> make_raw ?headers ~code:200 body
|
||||||
| Error (code,msg) -> make_raw ?headers ~code msg
|
| Error (code,msg) -> make_raw ?headers ~code msg
|
||||||
|
|
@ -606,6 +609,7 @@ module Response = struct
|
||||||
let make ?headers r : t = match r with
|
let make ?headers r : t = match r with
|
||||||
| Ok (`String body) -> make_raw ?headers ~code:200 body
|
| Ok (`String body) -> make_raw ?headers ~code:200 body
|
||||||
| Ok (`Stream body) -> make_raw_stream ?headers ~code:200 body
|
| Ok (`Stream body) -> make_raw_stream ?headers ~code:200 body
|
||||||
|
| Ok `Void -> make_void ?headers ~code:200 ()
|
||||||
| Error (code,msg) -> make_raw ?headers ~code msg
|
| Error (code,msg) -> make_raw ?headers ~code msg
|
||||||
|
|
||||||
let fail ?headers ~code fmt =
|
let fail ?headers ~code fmt =
|
||||||
|
|
@ -617,6 +621,7 @@ module Response = struct
|
||||||
let pp_body out = function
|
let pp_body out = function
|
||||||
| `String s -> Format.fprintf out "%S" s
|
| `String s -> Format.fprintf out "%S" s
|
||||||
| `Stream _ -> Format.pp_print_string out "<stream>"
|
| `Stream _ -> Format.pp_print_string out "<stream>"
|
||||||
|
| `Void -> ()
|
||||||
in
|
in
|
||||||
Format.fprintf out "{@[code=%d;@ headers=[@[%a@]];@ body=%a@]}"
|
Format.fprintf out "{@[code=%d;@ headers=[@[%a@]];@ body=%a@]}"
|
||||||
self.code Headers.pp self.headers pp_body self.body
|
self.code Headers.pp self.headers pp_body self.body
|
||||||
|
|
@ -645,6 +650,7 @@ module Response = struct
|
||||||
`Stream (Byte_stream.of_string s), true
|
`Stream (Byte_stream.of_string s), true
|
||||||
| `String _ as b -> b, false
|
| `String _ as b -> b, false
|
||||||
| `Stream _ as b -> b, true
|
| `Stream _ as b -> b, true
|
||||||
|
| `Void as b -> b, false
|
||||||
in
|
in
|
||||||
let headers =
|
let headers =
|
||||||
if is_chunked then (
|
if is_chunked then (
|
||||||
|
|
@ -659,7 +665,7 @@ module Response = struct
|
||||||
List.iter (fun (k,v) -> Printf.fprintf oc "%s: %s\r\n" k v) headers;
|
List.iter (fun (k,v) -> Printf.fprintf oc "%s: %s\r\n" k v) headers;
|
||||||
output_string oc "\r\n";
|
output_string oc "\r\n";
|
||||||
begin match body with
|
begin match body with
|
||||||
| `String "" -> ()
|
| `String "" | `Void -> ()
|
||||||
| `String s -> output_string oc s;
|
| `String s -> output_string oc s;
|
||||||
| `Stream str -> output_stream_chunked_ oc str;
|
| `Stream str -> output_stream_chunked_ oc str;
|
||||||
end;
|
end;
|
||||||
|
|
@ -910,7 +916,7 @@ let add_route_server_sent_handler ?accept self route f =
|
||||||
if not !resp_sent then (
|
if not !resp_sent then (
|
||||||
resp_sent := true;
|
resp_sent := true;
|
||||||
(* send 200 response now *)
|
(* send 200 response now *)
|
||||||
let initial_resp = Response.make_raw ~headers:!headers ~code:200 "" in
|
let initial_resp = Response.make_void ~headers:!headers ~code:200 () in
|
||||||
resp initial_resp;
|
resp initial_resp;
|
||||||
)
|
)
|
||||||
in
|
in
|
||||||
|
|
|
||||||
|
|
@ -314,9 +314,9 @@ end
|
||||||
the client to answer a {!Request.t}*)
|
the client to answer a {!Request.t}*)
|
||||||
|
|
||||||
module Response : sig
|
module Response : sig
|
||||||
type body = [`String of string | `Stream of byte_stream]
|
type body = [`String of string | `Stream of byte_stream | `Void]
|
||||||
(** Body of a response, either as a simple string,
|
(** Body of a response, either as a simple string,
|
||||||
or a stream of bytes. *)
|
or a stream of bytes, or nothing (for server-sent events). *)
|
||||||
|
|
||||||
type t = {
|
type t = {
|
||||||
code: Response_code.t; (** HTTP response code. See {!Response_code}. *)
|
code: Response_code.t; (** HTTP response code. See {!Response_code}. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue