@@ -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 *)
0 commit comments