@@ -53,7 +53,9 @@ type t =
5353 ; r_port : int
5454 ; r_ssl : bool
5555 ; r_filenames : string list ref
56- ; r_remote_ip : string
56+ ; r_sockaddr : Lwt_unix .sockaddr
57+ ; r_remote_ip : string Lazy .t
58+ ; r_remote_ip_parsed : [`Ip of Ipaddr .t | `Unix of string ] Lazy .t
5759 ; r_forward_ip : string list
5860 ; r_uri : uri
5961 ; r_meth : Cohttp.Code .meth
@@ -84,11 +86,26 @@ let make
8486 ~connection_closed
8587 request
8688 =
89+ let r_remote_ip =
90+ lazy
91+ (match sockaddr with
92+ | Unix. ADDR_INET (ip , _port ) -> Unix. string_of_inet_addr ip
93+ | ADDR_UNIX f -> f)
94+ in
95+ let r_remote_ip_parsed =
96+ lazy
97+ (match sockaddr with
98+ | Unix. ADDR_INET (ip , _port ) ->
99+ `Ip (Ipaddr. of_string_exn (Unix. string_of_inet_addr ip))
100+ | ADDR_UNIX f -> `Unix f)
101+ in
87102 { r_address = address
88103 ; r_port = port
89104 ; r_ssl = ssl
90105 ; r_filenames = filenames
91- ; r_remote_ip = sockaddr
106+ ; r_sockaddr = sockaddr
107+ ; r_remote_ip
108+ ; r_remote_ip_parsed
92109 ; r_forward_ip = forward_ip
93110 ; r_uri = make_uri (Cohttp.Request. uri request)
94111 ; r_encoding = Cohttp.Request. encoding request
@@ -123,6 +140,7 @@ let update
123140 ; r_meth
124141 ; r_forward_ip
125142 ; r_remote_ip
143+ ; r_remote_ip_parsed
126144 ; r_cookies_override
127145 ; r_body
128146 ; r_sub_path
@@ -132,8 +150,11 @@ let update
132150 let r_ssl = match ssl with Some ssl -> ssl | None -> r_ssl
133151 and r_forward_ip =
134152 match forward_ip with Some forward_ip -> forward_ip | None -> r_forward_ip
135- and r_remote_ip =
136- match remote_ip with Some remote_ip -> remote_ip | None -> r_remote_ip
153+ and r_remote_ip, r_remote_ip_parsed =
154+ match remote_ip with
155+ | Some remote_ip ->
156+ lazy remote_ip, lazy (`Ip (Ipaddr. of_string_exn remote_ip))
157+ | None -> r_remote_ip, r_remote_ip_parsed
137158 and r_sub_path = match sub_path with Some _ -> sub_path | None -> r_sub_path
138159 and r_body =
139160 match post_data with
@@ -172,6 +193,7 @@ let update
172193 ; r_meth
173194 ; r_forward_ip
174195 ; r_remote_ip
196+ ; r_remote_ip_parsed
175197 ; r_body
176198 ; r_cookies_override
177199 ; r_sub_path
@@ -270,19 +292,8 @@ let post_params r s i =
270292let files r s i =
271293 match force_post_data r s i with Some v -> Some (v > |= snd) | None -> None
272294
273- let remote_ip {r_remote_ip; _} = r_remote_ip
274-
275- let remote_ip_parsed {r_remote_ip; _} =
276- let is_prefix prefix s =
277- (* TODO: Naive version to be swapped with [String.starts_with ~prefix s]
278- when the dependency on OCaml >= 4.13 is acceptable. *)
279- let plen = String. length prefix in
280- String. length s > = plen && String. sub s 0 plen = prefix
281- in
282- if is_prefix " unix://" r_remote_ip
283- then `Unix r_remote_ip
284- else `Ip (Ipaddr. of_string_exn r_remote_ip)
285-
295+ let remote_ip {r_remote_ip; _} = Lazy. force r_remote_ip
296+ let remote_ip_parsed {r_remote_ip_parsed; _} = Lazy. force r_remote_ip_parsed
286297let forward_ip {r_forward_ip; _} = r_forward_ip
287298let request_cache {r_request_cache; _} = r_request_cache
288299let tries {r_tries; _} = r_tries
0 commit comments