Skip to content

Commit 20b85c9

Browse files
committed
Merge commit '03596c1a08f9b9fa063f22c40d80afc73d14ed08'
2 parents b7eb8f5 + 03596c1 commit 20b85c9

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/Tiny_httpd_io.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ end
187187
(** A TCP server abstraction. *)
188188
module TCP_server = struct
189189
type conn_handler = {
190-
handle: Input.t -> Output.t -> unit; (** Handle client connection *)
190+
handle: client_addr:Unix.sockaddr -> Input.t -> Output.t -> unit; (** Handle client connection *)
191191
}
192192

193193
type t = {

src/Tiny_httpd_server.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ module Request = struct
164164
type 'body t = {
165165
meth: Meth.t;
166166
host: string;
167+
client_addr: Unix.sockaddr;
167168
headers: Headers.t;
168169
http_version: int * int;
169170
path: string;
@@ -245,7 +246,7 @@ module Request = struct
245246
bad_reqf 400 "body is too short by %d bytes" size)
246247
247248
(* parse request, but not body (yet) *)
248-
let parse_req_start ~get_time_s ~buf (bs : byte_stream) :
249+
let parse_req_start ~client_addr ~get_time_s ~buf (bs : byte_stream) :
249250
unit t option resp_result =
250251
try
251252
let line = Byte_stream.read_line ~buf bs in
@@ -281,6 +282,7 @@ module Request = struct
281282
meth;
282283
query;
283284
host;
285+
client_addr;
284286
path;
285287
path_components;
286288
headers;
@@ -340,8 +342,8 @@ module Request = struct
340342
| e -> bad_reqf 500 "failed to read body: %s" (Printexc.to_string e)
341343
342344
module Internal_ = struct
343-
let parse_req_start ?(buf = Buf.create ()) ~get_time_s bs =
344-
parse_req_start ~get_time_s ~buf bs |> unwrap_resp_result
345+
let parse_req_start ?(buf = Buf.create ()) ~client_addr ~get_time_s bs =
346+
parse_req_start ~client_addr ~get_time_s ~buf bs |> unwrap_resp_result
345347
346348
let parse_body ?(buf = Buf.create ()) req bs : _ t =
347349
parse_body_ ~tr_stream:(fun s -> s) ~buf { req with body = bs }
@@ -918,14 +920,14 @@ module Unix_tcp_server_ = struct
918920
after_init tcp_server;
919921
920922
(* how to handle a single client *)
921-
let handle_client_unix_ (client_sock : Unix.file_descr) : unit =
923+
let handle_client_unix_ (client_sock : Unix.file_descr) (client_addr : Unix.sockaddr) : unit =
922924
Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout);
923925
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
924926
let oc =
925927
IO.Output.of_out_channel @@ Unix.out_channel_of_descr client_sock
926928
in
927929
let ic = IO.Input.of_unix_fd client_sock in
928-
handle.handle ic oc;
930+
handle.handle ~client_addr ic oc;
929931
_debug (fun k -> k "done with client, exiting");
930932
(try Unix.close client_sock
931933
with e ->
@@ -938,14 +940,14 @@ module Unix_tcp_server_ = struct
938940
(* limit concurrency *)
939941
Sem_.acquire 1 self.sem_max_connections;
940942
try
941-
let client_sock, _ = Unix.accept sock in
943+
let client_sock, client_addr = Unix.accept sock in
942944
Unix.setsockopt client_sock Unix.TCP_NODELAY true;
943945
(* Block INT/HUP while cloning to avoid children handling them.
944946
When thread gets them, our Unix.accept raises neatly. *)
945947
ignore Unix.(sigprocmask SIG_BLOCK Sys.[ sigint; sighup ]);
946948
self.new_thread (fun () ->
947949
try
948-
handle_client_unix_ client_sock;
950+
handle_client_unix_ client_sock client_addr;
949951
Sem_.release 1 self.sem_max_connections
950952
with e ->
951953
(try Unix.close client_sock with _ -> ());
@@ -1015,15 +1017,15 @@ let find_map f l =
10151017
aux f l
10161018
10171019
(* handle client on [ic] and [oc] *)
1018-
let client_handle_for (self : t) ic oc : unit =
1020+
let client_handle_for (self : t) ~client_addr ic oc : unit =
10191021
Pool.with_resource self.buf_pool @@ fun buf ->
10201022
Pool.with_resource self.buf_pool @@ fun buf_res ->
10211023
let is = Byte_stream.of_input ~buf_size:self.buf_size ic in
10221024
let continue = ref true in
10231025
while !continue && running self do
10241026
_debug (fun k -> k "read next request");
10251027
let (module B) = self.backend in
1026-
match Request.parse_req_start ~get_time_s:B.get_time_s ~buf is with
1028+
match Request.parse_req_start ~client_addr ~get_time_s:B.get_time_s ~buf is with
10271029
| Ok None -> continue := false (* client is done *)
10281030
| Error (c, s) ->
10291031
(* connection error, close *)

src/Tiny_httpd_server.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ 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. *)
7071
headers: Headers.t; (** List of headers. *)
7172
http_version: int * int;
7273
(** HTTP version. This should be either [1, 0] or [1, 1]. *)
@@ -161,7 +162,7 @@ module Request : sig
161162
(* for testing purpose, do not use. There is no guarantee of stability. *)
162163
module Internal_ : sig
163164
val parse_req_start :
164-
?buf:buf -> get_time_s:(unit -> float) -> byte_stream -> unit t option
165+
?buf:buf -> client_addr:Unix.sockaddr -> get_time_s:(unit -> float) -> byte_stream -> unit t option
165166

166167
val parse_body : ?buf:buf -> unit t -> byte_stream -> byte_stream t
167168
end

tests/unit/t_server.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ let () =
1010
salutationsSOMEJUNK"
1111
in
1212
let str = Tiny_httpd.Byte_stream.of_string q in
13-
let r = Request.Internal_.parse_req_start ~get_time_s:(fun _ -> 0.) str in
13+
let client_addr = Unix.(ADDR_INET (inet_addr_loopback, 1024)) in
14+
let r = Request.Internal_.parse_req_start ~client_addr ~get_time_s:(fun _ -> 0.) str in
1415
match r with
1516
| None -> failwith "should parse"
1617
| Some req ->

0 commit comments

Comments
 (0)