Skip to content

Commit 402e3f7

Browse files
committed
breaking: set_top_handler takes a stream request, for more generality
1 parent 20b85c9 commit 402e3f7

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/Tiny_httpd_server.ml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ type t = {
659659
backend: (module IO_BACKEND);
660660
mutable tcp_server: IO.TCP_server.t option;
661661
buf_size: int;
662-
mutable handler: string Request.t -> Response.t;
662+
mutable handler: byte_stream Request.t -> Response.t;
663663
(** toplevel handler, if any *)
664664
mutable middlewares: (int * Middleware.t) list; (** Global middlewares *)
665665
mutable middlewares_sorted: (int * Middleware.t) list lazy_t;
@@ -920,7 +920,8 @@ module Unix_tcp_server_ = struct
920920
after_init tcp_server;
921921
922922
(* how to handle a single client *)
923-
let handle_client_unix_ (client_sock : Unix.file_descr) (client_addr : Unix.sockaddr) : unit =
923+
let handle_client_unix_ (client_sock : Unix.file_descr)
924+
(client_addr : Unix.sockaddr) : unit =
924925
Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout);
925926
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
926927
let oc =
@@ -1025,7 +1026,9 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
10251026
while !continue && running self do
10261027
_debug (fun k -> k "read next request");
10271028
let (module B) = self.backend in
1028-
match Request.parse_req_start ~client_addr ~get_time_s:B.get_time_s ~buf is with
1029+
match
1030+
Request.parse_req_start ~client_addr ~get_time_s:B.get_time_s ~buf is
1031+
with
10291032
| Ok None -> continue := false (* client is done *)
10301033
| Error (c, s) ->
10311034
(* connection error, close *)
@@ -1042,13 +1045,7 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
10421045
let base_handler =
10431046
match find_map (fun ph -> ph req) self.path_handlers with
10441047
| Some f -> unwrap_resp_result f
1045-
| None ->
1046-
fun _oc req ~resp ->
1047-
let body_str =
1048-
Pool.with_resource self.buf_pool @@ fun buf ->
1049-
Request.read_body_full ~buf req
1050-
in
1051-
resp (self.handler body_str)
1048+
| None -> fun _oc req ~resp -> resp (self.handler req)
10521049
in
10531050
10541051
(* handle expect/continue *)

src/Tiny_httpd_server.mli

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ module Request : sig
6767
meth: Meth.t; (** HTTP method for this request. *)
6868
host: string;
6969
(** Host header, mandatory. It can also be found in {!headers}. *)
70-
client_addr : Unix.sockaddr; (** Client address. Available since NEXT_RELEASE. *)
70+
client_addr: Unix.sockaddr;
71+
(** Client address. Available since NEXT_RELEASE. *)
7172
headers: Headers.t; (** List of headers. *)
7273
http_version: int * int;
7374
(** HTTP version. This should be either [1, 0] or [1, 1]. *)
@@ -162,7 +163,11 @@ module Request : sig
162163
(* for testing purpose, do not use. There is no guarantee of stability. *)
163164
module Internal_ : sig
164165
val parse_req_start :
165-
?buf:buf -> client_addr:Unix.sockaddr -> get_time_s:(unit -> float) -> byte_stream -> unit t option
166+
?buf:buf ->
167+
client_addr:Unix.sockaddr ->
168+
get_time_s:(unit -> float) ->
169+
byte_stream ->
170+
unit t option
166171

167172
val parse_body : ?buf:buf -> unit t -> byte_stream -> byte_stream t
168173
end
@@ -527,7 +532,7 @@ val add_middleware :
527532

528533
(** {2 Request handlers} *)
529534

530-
val set_top_handler : t -> (string Request.t -> Response.t) -> unit
535+
val set_top_handler : t -> (byte_stream Request.t -> Response.t) -> unit
531536
(** Setup a handler called by default.
532537
533538
This handler is called with any request not accepted by any handler

0 commit comments

Comments
 (0)