mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
breaking: set_top_handler takes a stream request, for more generality
This commit is contained in:
parent
20b85c9926
commit
402e3f72e4
2 changed files with 15 additions and 13 deletions
|
|
@ -659,7 +659,7 @@ type t = {
|
||||||
backend: (module IO_BACKEND);
|
backend: (module IO_BACKEND);
|
||||||
mutable tcp_server: IO.TCP_server.t option;
|
mutable tcp_server: IO.TCP_server.t option;
|
||||||
buf_size: int;
|
buf_size: int;
|
||||||
mutable handler: string Request.t -> Response.t;
|
mutable handler: byte_stream Request.t -> Response.t;
|
||||||
(** toplevel handler, if any *)
|
(** toplevel handler, if any *)
|
||||||
mutable middlewares: (int * Middleware.t) list; (** Global middlewares *)
|
mutable middlewares: (int * Middleware.t) list; (** Global middlewares *)
|
||||||
mutable middlewares_sorted: (int * Middleware.t) list lazy_t;
|
mutable middlewares_sorted: (int * Middleware.t) list lazy_t;
|
||||||
|
|
@ -920,7 +920,8 @@ module Unix_tcp_server_ = struct
|
||||||
after_init tcp_server;
|
after_init tcp_server;
|
||||||
|
|
||||||
(* how to handle a single client *)
|
(* 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_RCVTIMEO self.timeout);
|
||||||
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
|
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
|
||||||
let oc =
|
let oc =
|
||||||
|
|
@ -1025,7 +1026,9 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
|
||||||
while !continue && running self do
|
while !continue && running self do
|
||||||
_debug (fun k -> k "read next request");
|
_debug (fun k -> k "read next request");
|
||||||
let (module B) = self.backend in
|
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 *)
|
| Ok None -> continue := false (* client is done *)
|
||||||
| Error (c, s) ->
|
| Error (c, s) ->
|
||||||
(* connection error, close *)
|
(* connection error, close *)
|
||||||
|
|
@ -1042,13 +1045,7 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
|
||||||
let base_handler =
|
let base_handler =
|
||||||
match find_map (fun ph -> ph req) self.path_handlers with
|
match find_map (fun ph -> ph req) self.path_handlers with
|
||||||
| Some f -> unwrap_resp_result f
|
| Some f -> unwrap_resp_result f
|
||||||
| None ->
|
| None -> fun _oc req ~resp -> resp (self.handler req)
|
||||||
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)
|
|
||||||
in
|
in
|
||||||
|
|
||||||
(* handle expect/continue *)
|
(* handle expect/continue *)
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,8 @@ module Request : sig
|
||||||
meth: Meth.t; (** HTTP method for this request. *)
|
meth: Meth.t; (** HTTP method for this request. *)
|
||||||
host: string;
|
host: string;
|
||||||
(** Host header, mandatory. It can also be found in {!headers}. *)
|
(** 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. *)
|
headers: Headers.t; (** List of headers. *)
|
||||||
http_version: int * int;
|
http_version: int * int;
|
||||||
(** HTTP version. This should be either [1, 0] or [1, 1]. *)
|
(** 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. *)
|
(* for testing purpose, do not use. There is no guarantee of stability. *)
|
||||||
module Internal_ : sig
|
module Internal_ : sig
|
||||||
val parse_req_start :
|
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
|
val parse_body : ?buf:buf -> unit t -> byte_stream -> byte_stream t
|
||||||
end
|
end
|
||||||
|
|
@ -527,7 +532,7 @@ val add_middleware :
|
||||||
|
|
||||||
(** {2 Request handlers} *)
|
(** {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.
|
(** Setup a handler called by default.
|
||||||
|
|
||||||
This handler is called with any request not accepted by any handler
|
This handler is called with any request not accepted by any handler
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue