Skip to content

Commit d818e36

Browse files
committed
Merge branch 'to-eio-base' into to-eio
2 parents 7f44cba + 3b12a12 commit d818e36

File tree

6 files changed

+64
-44
lines changed

6 files changed

+64
-44
lines changed

src/extensions/accesscontrol.ml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,22 @@ let ip s =
3939
in
4040
fun ri ->
4141
let r =
42-
match Ocsigen_request.remote_ip_parsed ri with
43-
| `Ip ip -> Ipaddr.Prefix.mem ip prefix
44-
| `Unix _ -> false
42+
match Ocsigen_request.client_conn ri with
43+
| `Inet (ip, _) -> (
44+
match Ipaddr.of_string (ip :> string) with
45+
| Ok ip -> Ipaddr.Prefix.mem ip prefix
46+
| Error _ -> false)
47+
| _ -> false
4548
in
4649
if r
4750
then
4851
Logs.info ~src:section (fun fmt ->
49-
fmt "IP: %s matches %s" (Ocsigen_request.remote_ip ri) s)
52+
fmt "IP: %s matches %s" (Ocsigen_request.client_conn_to_string ri) s)
5053
else
5154
Logs.info ~src:section (fun fmt ->
52-
fmt "IP: %s does not match %s" (Ocsigen_request.remote_ip ri) s);
55+
fmt "IP: %s does not match %s"
56+
(Ocsigen_request.client_conn_to_string ri)
57+
s);
5358
r
5459

5560
let port port ri =
@@ -220,24 +225,23 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
220225
match Ocsigen_lib.Netstring_pcre.split comma_space_regexp header with
221226
| original_ip :: proxies ->
222227
let last_proxy = List.last proxies in
223-
let proxy_ip = Ipaddr.of_string_exn last_proxy in
224228
let equal_ip =
225-
match Ocsigen_request.remote_ip_parsed request_info with
226-
| `Ip r_ip -> Ipaddr.compare proxy_ip r_ip = 0
227-
| `Unix _ -> false
229+
match Ocsigen_request.client_conn request_info with
230+
| `Inet (r_ip, _) -> last_proxy = (r_ip :> string)
231+
| _ -> false
228232
in
229233
if equal_ip || not check_equal_ip
230234
then
231235
{ request with
232236
Ocsigen_extensions.request_info =
233237
Ocsigen_request.update ~forward_ip:proxies
234-
~remote_ip:original_ip request_info }
238+
~client_conn:(`Forwarded_for original_ip) request_info }
235239
else (
236240
(* the announced ip of the proxy is not its real ip *)
237241
Logs.warn ~src:section (fun fmt ->
238242
fmt
239243
"X-Forwarded-For: host ip (%s) does not match the header (%s)"
240-
(Ocsigen_request.remote_ip request_info)
244+
(Ocsigen_request.client_conn_to_string request_info)
241245
header);
242246
request)
243247
| _ ->

src/extensions/revproxy.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ let gen dir = function
107107
(Ocsigen_request.address request_info)
108108
in
109109
String.concat ", "
110-
(Ocsigen_request.remote_ip request_info
110+
(Ocsigen_request.client_conn_to_string request_info
111111
:: Ocsigen_request.forward_ip request_info
112112
@ [address])
113113
in

src/server/ocsigen_cohttp.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ let handler
6565
body
6666
=
6767
let filenames = ref [] in
68-
let sockaddr =
68+
let client_conn =
6969
match eio_stream with
70-
| `Unix s -> "unix://" ^ s
71-
| `Tcp (ip, _port) -> (ip : _ Eio.Net.Ipaddr.t :> string)
70+
| `Tcp (ip, port) -> `Inet (ip, port)
71+
| `Unix _ as p -> p
7272
in
7373
let connection_closed =
7474
try fst (Hashtbl.find connections conn)
@@ -108,7 +108,7 @@ let handler
108108
in
109109
(* TODO: equivalent of Ocsigen_range *)
110110
let request =
111-
Ocsigen_request.make ~address ~port ~ssl ~filenames ~sockaddr ~body
111+
Ocsigen_request.make ~address ~port ~ssl ~filenames ~client_conn ~body
112112
~connection_closed request
113113
in
114114
Fun.protect
@@ -128,7 +128,7 @@ let handler
128128
(match Ocsigen_request.host request with
129129
| None -> "<host not specified in the request>"
130130
| Some h -> h)
131-
(Ocsigen_request.remote_ip request)
131+
(Ocsigen_request.client_conn_to_string request)
132132
(Option.value ~default:""
133133
(Ocsigen_request.header request Ocsigen_header.Name.user_agent))
134134
(Option.fold ~none:""

src/server/ocsigen_request.ml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ let make_uri u =
4848
and u_get_params_flat = lazy (flatten_get_params (Lazy.force u_get_params)) in
4949
{u_uri; u_get_params; u_get_params_flat; u_path; u_path_string}
5050

51+
type client_conn =
52+
[ `Inet of Eio.Net.Ipaddr.v4v6 * int
53+
| `Unix of string
54+
| `Forwarded_for of string
55+
| `Unknown ]
56+
5157
type t =
5258
{ r_address : Ocsigen_config.Socket_type.t
5359
; r_port : int
5460
; r_ssl : bool
5561
; r_filenames : string list ref
56-
; r_remote_ip : string
62+
; r_client_conn : client_conn
5763
; r_forward_ip : string list
5864
; r_uri : uri
5965
; r_meth : Cohttp.Code.meth
@@ -79,7 +85,7 @@ let make
7985
~port
8086
~ssl
8187
~filenames
82-
~sockaddr
88+
~client_conn
8389
~body
8490
~connection_closed
8591
request
@@ -88,7 +94,7 @@ let make
8894
; r_port = port
8995
; r_ssl = ssl
9096
; r_filenames = filenames
91-
; r_remote_ip = sockaddr
97+
; r_client_conn = client_conn
9298
; r_forward_ip = forward_ip
9399
; r_uri = make_uri (Cohttp.Request.uri request)
94100
; r_encoding = Cohttp.Request.encoding request
@@ -110,7 +116,7 @@ let path {r_uri = {u_path; _}; _} = Lazy.force u_path
110116
let update
111117
?ssl
112118
?forward_ip
113-
?remote_ip
119+
?client_conn
114120
?sub_path
115121
?meth
116122
?get_params_flat
@@ -122,7 +128,7 @@ let update
122128
; r_uri = {u_uri; _} as r_uri
123129
; r_meth
124130
; r_forward_ip
125-
; r_remote_ip
131+
; r_client_conn
126132
; r_cookies_override
127133
; r_body
128134
; r_sub_path
@@ -132,8 +138,8 @@ let update
132138
let r_ssl = match ssl with Some ssl -> ssl | None -> r_ssl
133139
and r_forward_ip =
134140
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
141+
and r_client_conn =
142+
match client_conn with Some c -> c | None -> r_client_conn
137143
and r_sub_path = match sub_path with Some _ -> sub_path | None -> r_sub_path
138144
and r_body =
139145
match post_data with
@@ -171,7 +177,7 @@ let update
171177
; r_uri
172178
; r_meth
173179
; r_forward_ip
174-
; r_remote_ip
180+
; r_client_conn
175181
; r_body
176182
; r_cookies_override
177183
; r_sub_path
@@ -270,18 +276,14 @@ let post_params r s i =
270276
let files r s i =
271277
match force_post_data r s i with Some v -> Some (snd v) | None -> None
272278

273-
let remote_ip {r_remote_ip; _} = r_remote_ip
279+
let client_conn {r_client_conn = c; _} = c
274280

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)
281+
let client_conn_to_string {r_client_conn = c; _} =
282+
match c with
283+
| `Inet (ip, _) -> (ip :> string)
284+
| `Unix path -> "unix:" ^ path
285+
| `Forwarded_for ip -> "forwarded:" ^ ip
286+
| `Unknown -> "unknown"
285287

286288
let forward_ip {r_forward_ip; _} = r_forward_ip
287289
let request_cache {r_request_cache; _} = r_request_cache

src/server/ocsigen_request.mli

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ type file_info = Ocsigen_multipart.file_info =
1111

1212
type post_data = (string * string) list * (string * file_info) list
1313

14+
type client_conn =
15+
[ `Inet of Eio.Net.Ipaddr.v4v6 * int
16+
| `Unix of string
17+
| `Forwarded_for of string
18+
| `Unknown ]
19+
(** Type of connection used by the client. [`Inet] means the client connected
20+
through the Internet. [`Forwarded_for] means that the client connected
21+
through a proxy and carries the IP address reported in the HTTP headers. *)
22+
1423
val make :
1524
?forward_ip:string list
1625
-> ?sub_path:string
@@ -21,7 +30,7 @@ val make :
2130
-> port:int
2231
-> ssl:bool
2332
-> filenames:string list ref
24-
-> sockaddr:string
33+
-> client_conn:client_conn
2534
-> body:Cohttp_eio.Body.t
2635
-> connection_closed:unit Promise.t
2736
-> Cohttp.Request.t
@@ -30,7 +39,7 @@ val make :
3039
val update :
3140
?ssl:bool
3241
-> ?forward_ip:string list
33-
-> ?remote_ip:string
42+
-> ?client_conn:client_conn
3443
-> ?sub_path:string
3544
-> ?meth:Cohttp.Code.meth
3645
-> ?get_params_flat:(string * string) list
@@ -76,8 +85,13 @@ val post_params :
7685
-> Int64.t option
7786
-> (string * string) list option
7887

79-
val remote_ip : t -> string
80-
val remote_ip_parsed : t -> [`Ip of Ipaddr.t | `Unix of string]
88+
val client_conn : t -> client_conn
89+
(** The way the client connects to the server (for example, its IP address if
90+
connected over the internet). *)
91+
92+
val client_conn_to_string : t -> string
93+
(** A textual representation of [client_conn] suitable for use in logs. *)
94+
8195
val forward_ip : t -> string list
8296
val content_type : t -> content_type option
8397
val request_cache : t -> Polytables.t

test/extensions/deflatemod.t/run.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$ run_server ./test.exe
33
ocsigen:main: [WARNING] Command pipe created
44
cohttp.eio: [INFO] unix:: accept connection
5-
ocsigen:access: connection for local-test from unix:// (): /index.html
5+
ocsigen:access: connection for local-test from unix: (): /index.html
66
ocsigen:ext: [INFO] host found! local-test:0 matches .*
77
ocsigen:ext:staticmod: [INFO] Is it a static file?
88
ocsigen:local-file: [INFO] Testing "./index.html".
@@ -11,23 +11,23 @@
1111
ocsigen:local-file: [INFO] Returning "./index.html".
1212
cohttp.eio: [INFO] unix:: disconnected
1313
cohttp.eio: [INFO] unix:: accept connection
14-
ocsigen:access: connection for local-test from unix:// (): /index.html
14+
ocsigen:access: connection for local-test from unix: (): /index.html
1515
ocsigen:ext: [INFO] host found! local-test:0 matches .*
1616
ocsigen:ext:staticmod: [INFO] Is it a static file?
1717
ocsigen:local-file: [INFO] Testing "./index.html".
1818
ocsigen:local-file: [INFO] checking if file index.html can be sent
1919
ocsigen:local-file: [INFO] Returning "./index.html".
2020
cohttp.eio: [INFO] unix:: disconnected
2121
cohttp.eio: [INFO] unix:: accept connection
22-
ocsigen:access: connection for local-test from unix:// (): /empty_dir/
22+
ocsigen:access: connection for local-test from unix: (): /empty_dir/
2323
ocsigen:ext: [INFO] host found! local-test:0 matches .*
2424
ocsigen:ext:staticmod: [INFO] Is it a static file?
2525
ocsigen:local-file: [INFO] Testing "./empty_dir/".
2626
ocsigen:local-file: [INFO] Testing "./empty_dir/index.html" as possible index.
2727
ocsigen:local-file: [INFO] No index and no listing
2828
cohttp.eio: [INFO] unix:: disconnected
2929
cohttp.eio: [INFO] unix:: accept connection
30-
ocsigen:access: connection for local-test from unix:// (): /doesnt_exists.html
30+
ocsigen:access: connection for local-test from unix: (): /doesnt_exists.html
3131
ocsigen:ext: [INFO] host found! local-test:0 matches .*
3232
ocsigen:ext:staticmod: [INFO] Is it a static file?
3333
ocsigen:local-file: [INFO] Testing "./doesnt_exists.html".

0 commit comments

Comments
 (0)