Skip to content

Commit 148315a

Browse files
authored
Merge branch 'master' into compat_with_cohttp_610
2 parents b040aa9 + c39e80f commit 148315a

File tree

10 files changed

+66
-67
lines changed

10 files changed

+66
-67
lines changed

src/baselib/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
cryptokit
2727
re
2828
ocsigen_lib_base
29+
cohttp-lwt
2930
logs
3031
(select
3132
dynlink_wrapper.ml

src/baselib/ocsigen_stream.ml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,7 @@ let of_lwt_stream stream =
239239
in
240240
make aux
241241

242-
(** Convert an {!Ocsigen_stream.t} into a {!Lwt_stream.t}.
243-
@param is_empty function to skip empty chunk.
244-
*)
245-
let to_lwt_stream ?(is_empty = fun _ -> false) o_stream =
246-
let stream = ref (get o_stream) in
247-
let rec wrap () =
248-
next !stream >>= function
249-
| Finished None -> o_stream.finalizer `Success >>= fun () -> Lwt.return None
250-
| Finished (Some next) ->
251-
stream := next;
252-
wrap ()
253-
| Cont (value, next) ->
254-
stream := next;
255-
if is_empty value then wrap () else Lwt.return (Some value)
256-
in
257-
Lwt_stream.from wrap
242+
let of_cohttp_body body = Cohttp_lwt.Body.to_stream body |> of_lwt_stream
258243

259244
module StringStream = struct
260245
type out = string t

src/baselib/ocsigen_stream.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,9 @@ val of_file : string -> string t
106106
val of_string : string -> string t
107107
(** returns a stream containing a string. *)
108108

109-
val of_lwt_stream : 'a Lwt_stream.t -> 'a t
109+
val of_cohttp_body : Cohttp_lwt.Body.t -> string t
110110
(** Convert a {!Lwt_stream.t} to an {!Ocsigen_stream.t}. *)
111111

112-
val to_lwt_stream : ?is_empty:('a -> bool) -> 'a t -> 'a Lwt_stream.t
113-
(** Convert an {!Ocsigen_stream.t} into a {!Lwt_stream.t}.
114-
@param is_empty function to skip empty chunk.
115-
*)
116-
117112
module StringStream : sig
118113
type out = string t
119114
(** Interface for stream creation (for tyxml) *)

src/extensions/accesscontrol.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ 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, _) -> Ipaddr.Prefix.mem ip prefix
44+
| _ -> false
4545
in
4646
if r
4747
then
4848
Logs.info ~src:section (fun fmt ->
49-
fmt "IP: %s matches %s" (Ocsigen_request.remote_ip ri) s)
49+
fmt "IP: %s matches %s" (Ocsigen_request.client_conn_to_string ri) s)
5050
else
5151
Logs.info ~src:section (fun fmt ->
52-
fmt "IP: %s does not match %s" (Ocsigen_request.remote_ip ri) s);
52+
fmt "IP: %s does not match %s"
53+
(Ocsigen_request.client_conn_to_string ri)
54+
s);
5355
r
5456

5557
let port port ri =
@@ -222,22 +224,22 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
222224
let last_proxy = List.last proxies in
223225
let proxy_ip = Ipaddr.of_string_exn last_proxy in
224226
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
227+
match Ocsigen_request.client_conn request_info with
228+
| `Inet (r_ip, _) -> Ipaddr.compare proxy_ip r_ip = 0
229+
| _ -> false
228230
in
229231
if equal_ip || not check_equal_ip
230232
then
231233
{ request with
232234
Ocsigen_extensions.request_info =
233235
Ocsigen_request.update ~forward_ip:proxies
234-
~remote_ip:original_ip request_info }
236+
~client_conn:(`Forwarded_for original_ip) request_info }
235237
else (
236238
(* the announced ip of the proxy is not its real ip *)
237239
Logs.warn ~src:section (fun fmt ->
238240
fmt
239241
"X-Forwarded-For: host ip (%s) does not match the header (%s)"
240-
(Ocsigen_request.remote_ip request_info)
242+
(Ocsigen_request.client_conn_to_string request_info)
241243
header);
242244
request)
243245
| _ ->

src/extensions/revproxy.ml

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

src/server/ocsigen_cohttp.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ end
5757
let handler ~ssl ~address ~port ~connector (flow, conn) request body =
5858
let filenames = ref [] in
5959
let edn = Conduit_lwt_unix.endp_of_flow flow in
60-
let getsockname = function
61-
| `TCP (ip, _) | `TLS (_, `TCP (ip, _)) -> Ipaddr.to_string ip
60+
let client_conn =
61+
match edn with
62+
| `TCP (ip, port) | `TLS (_, `TCP (ip, port)) -> `Inet (ip, port)
6263
| `Unix_domain_socket path | `TLS (_, `Unix_domain_socket path) ->
63-
"unix://" ^ path
64-
| _ -> "unknown"
64+
`Unix path
65+
| _ -> `Unknown
6566
in
66-
let sockaddr = getsockname edn in
6767
let connection_closed =
6868
try fst (Hashtbl.find connections conn)
6969
with Not_found ->
@@ -108,7 +108,7 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
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
Lwt.finalize
@@ -118,7 +118,7 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
118118
(match Ocsigen_request.host request with
119119
| None -> "<host not specified in the request>"
120120
| Some h -> h)
121-
(Ocsigen_request.remote_ip request)
121+
(Ocsigen_request.client_conn_to_string request)
122122
(Option.value ~default:""
123123
(Ocsigen_request.header request Ocsigen_header.Name.user_agent))
124124
(Option.fold ~none:""

src/server/ocsigen_multipart.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ let post_params ~content_type body_gen =
365365
match String.lowercase_ascii ct, String.lowercase_ascii cst with
366366
| "application", "x-www-form-urlencoded" ->
367367
Some
368-
(body_gen |> Cohttp_lwt.Body.to_stream |> Ocsigen_stream.of_lwt_stream
368+
(body_gen |> Ocsigen_stream.of_cohttp_body
369369
|> post_params_form_urlencoded)
370370
| "multipart", "form-data" ->
371371
Some
372-
(body_gen |> Cohttp_lwt.Body.to_stream |> Ocsigen_stream.of_lwt_stream
372+
(body_gen |> Ocsigen_stream.of_cohttp_body
373373
|> post_params_multipart_form_data ctparams)
374374
| _ -> 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 Ipaddr.t * 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 (v >|= snd) | 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, _) -> Ipaddr.to_string ip
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
@@ -9,6 +9,15 @@ type file_info = Ocsigen_multipart.file_info =
99

1010
type post_data = (string * string) list * (string * file_info) list
1111

12+
type client_conn =
13+
[ `Inet of Ipaddr.t * int
14+
| `Unix of string
15+
| `Forwarded_for of string
16+
| `Unknown ]
17+
(** Type of connection used by the client. [`Inet] means the client connected
18+
through the Internet. [`Forwarded_for] means that the client connected
19+
through a proxy and carries the IP address reported in the HTTP headers. *)
20+
1221
val make :
1322
?forward_ip:string list
1423
-> ?sub_path:string
@@ -19,7 +28,7 @@ val make :
1928
-> port:int
2029
-> ssl:bool
2130
-> filenames:string list ref
22-
-> sockaddr:string
31+
-> client_conn:client_conn
2332
-> body:Cohttp_lwt.Body.t
2433
-> connection_closed:unit Lwt.t
2534
-> Cohttp.Request.t
@@ -28,7 +37,7 @@ val make :
2837
val update :
2938
?ssl:bool
3039
-> ?forward_ip:string list
31-
-> ?remote_ip:string
40+
-> ?client_conn:client_conn
3241
-> ?sub_path:string
3342
-> ?meth:Cohttp.Code.meth
3443
-> ?get_params_flat:(string * string) list
@@ -74,8 +83,13 @@ val post_params :
7483
-> Int64.t option
7584
-> (string * string) list Lwt.t option
7685

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

test/extensions/deflatemod.t/run.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
$ source ../../server-test-helpers.sh
22
$ run_server ./test.exe
33
ocsigen:main: [WARNING] Command pipe created
4-
ocsigen:access: connection for local-test from unix:// (): /index.html
4+
ocsigen:access: connection for local-test from unix: (): /index.html
55
ocsigen:ext: [INFO] host found! local-test:0 matches .*
66
ocsigen:ext:staticmod: [INFO] Is it a static file?
77
ocsigen:local-file: [INFO] Testing "./index.html".
88
ocsigen:local-file: [INFO] checking if file index.html can be sent
99
ocsigen:ext: [INFO] Compiling exclusion regexp $^
1010
ocsigen:local-file: [INFO] Returning "./index.html".
11-
ocsigen:access: connection for local-test from unix:// (): /index.html
11+
ocsigen:access: connection for local-test from unix: (): /index.html
1212
ocsigen:ext: [INFO] host found! local-test:0 matches .*
1313
ocsigen:ext:staticmod: [INFO] Is it a static file?
1414
ocsigen:local-file: [INFO] Testing "./index.html".

0 commit comments

Comments
 (0)