From 402e3f72e453b624a96f7466472811622682cd66 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 8 Aug 2023 16:36:17 -0400 Subject: [PATCH] breaking: `set_top_handler` takes a stream request, for more generality --- src/Tiny_httpd_server.ml | 17 +++++++---------- src/Tiny_httpd_server.mli | 11 ++++++++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Tiny_httpd_server.ml b/src/Tiny_httpd_server.ml index a8b727d2..2b95a7d3 100644 --- a/src/Tiny_httpd_server.ml +++ b/src/Tiny_httpd_server.ml @@ -659,7 +659,7 @@ type t = { backend: (module IO_BACKEND); mutable tcp_server: IO.TCP_server.t option; buf_size: int; - mutable handler: string Request.t -> Response.t; + mutable handler: byte_stream Request.t -> Response.t; (** toplevel handler, if any *) mutable middlewares: (int * Middleware.t) list; (** Global middlewares *) mutable middlewares_sorted: (int * Middleware.t) list lazy_t; @@ -920,7 +920,8 @@ module Unix_tcp_server_ = struct after_init tcp_server; (* how to handle a single client *) - let handle_client_unix_ (client_sock : Unix.file_descr) (client_addr : Unix.sockaddr) : unit = + let handle_client_unix_ (client_sock : Unix.file_descr) + (client_addr : Unix.sockaddr) : unit = Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout); Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout); let oc = @@ -1025,7 +1026,9 @@ let client_handle_for (self : t) ~client_addr ic oc : unit = while !continue && running self do _debug (fun k -> k "read next request"); let (module B) = self.backend in - match Request.parse_req_start ~client_addr ~get_time_s:B.get_time_s ~buf is with + match + Request.parse_req_start ~client_addr ~get_time_s:B.get_time_s ~buf is + with | Ok None -> continue := false (* client is done *) | Error (c, s) -> (* connection error, close *) @@ -1042,13 +1045,7 @@ let client_handle_for (self : t) ~client_addr ic oc : unit = let base_handler = match find_map (fun ph -> ph req) self.path_handlers with | Some f -> unwrap_resp_result f - | None -> - fun _oc req ~resp -> - let body_str = - Pool.with_resource self.buf_pool @@ fun buf -> - Request.read_body_full ~buf req - in - resp (self.handler body_str) + | None -> fun _oc req ~resp -> resp (self.handler req) in (* handle expect/continue *) diff --git a/src/Tiny_httpd_server.mli b/src/Tiny_httpd_server.mli index b96c6a1d..1910b32b 100644 --- a/src/Tiny_httpd_server.mli +++ b/src/Tiny_httpd_server.mli @@ -67,7 +67,8 @@ module Request : sig meth: Meth.t; (** HTTP method for this request. *) host: string; (** Host header, mandatory. It can also be found in {!headers}. *) - client_addr : Unix.sockaddr; (** Client address. Available since NEXT_RELEASE. *) + client_addr: Unix.sockaddr; + (** Client address. Available since NEXT_RELEASE. *) headers: Headers.t; (** List of headers. *) http_version: int * int; (** HTTP version. This should be either [1, 0] or [1, 1]. *) @@ -162,7 +163,11 @@ module Request : sig (* for testing purpose, do not use. There is no guarantee of stability. *) module Internal_ : sig val parse_req_start : - ?buf:buf -> client_addr:Unix.sockaddr -> get_time_s:(unit -> float) -> byte_stream -> unit t option + ?buf:buf -> + client_addr:Unix.sockaddr -> + get_time_s:(unit -> float) -> + byte_stream -> + unit t option val parse_body : ?buf:buf -> unit t -> byte_stream -> byte_stream t end @@ -527,7 +532,7 @@ val add_middleware : (** {2 Request handlers} *) -val set_top_handler : t -> (string Request.t -> Response.t) -> unit +val set_top_handler : t -> (byte_stream Request.t -> Response.t) -> unit (** Setup a handler called by default. This handler is called with any request not accepted by any handler