Skip to content

Commit 6bfe3a2

Browse files
authored
Merge pull request #261 from Julow/server-test
Add server tests
2 parents e7bfc58 + a552264 commit 6bfe3a2

File tree

18 files changed

+168
-39
lines changed

18 files changed

+168
-39
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ Build instructions:
2424
Local testings:
2525
===============
2626

27+
To run the automated tests, do:
28+
29+
* dune runtest --auto-promote
30+
31+
* this will update the files at test/extensions/*/run.t to reflect the
32+
behavior of ocsigenserver. The test files don't change if the server did not
33+
change behavior. Use Git to see the eventual changes.
34+
35+
Alternatively, you can also test the 'ocsigenserver' program using a config file:
36+
2737
* run "make run.local" or "make run.opt.local"
2838
in the ocsigen source directory.
2939

@@ -32,6 +42,8 @@ Local testings:
3242
* if it does not work, look at the logs (see 'local/var/log/' in the
3343
ocsgigen source directory) or run ocsigen with options -v.
3444

45+
* this will use the config file at 'local/etc/ocsigenserver.conf'.
46+
3547
------------------------------------------------------------------
3648

3749
Authors:

src/baselib/ocsigen_lib.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ module Ip_address = struct
3737
else Lwt_unix.AI_FAMILY Lwt_unix.PF_INET) ]
3838
in
3939
Lwt.bind (Lwt_unix.getaddrinfo host "" options) aux
40-
41-
let of_sockaddr = function
42-
| Unix.ADDR_INET (ip, _port) -> ip
43-
| _ -> raise (Ocsigen_Internal_Error "ip of unix socket")
4440
end
4541

4642
(*****************************************************************************)

src/baselib/ocsigen_lib.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ module Ip_address : sig
4040
exception No_such_host
4141

4242
val get_inet_addr : ?v6:bool -> string -> Unix.inet_addr Lwt.t
43-
val of_sockaddr : Unix.sockaddr -> Unix.inet_addr
4443
end
4544

4645
module Filename : sig

src/extensions/accesscontrol.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ let ip s =
3838
Ocsigen_extensions.badconfig "Bad ip/netmask [%s] in <ip> condition" s)
3939
in
4040
fun ri ->
41-
let r = Ipaddr.Prefix.mem (Ocsigen_request.remote_ip_parsed ri) prefix in
41+
let r =
42+
match Ocsigen_request.remote_ip_parsed ri with
43+
| `Ip ip -> Ipaddr.Prefix.mem ip prefix
44+
| `Unix _ -> false
45+
in
4246
if r
4347
then
4448
Logs.info ~src:section (fun fmt ->
@@ -218,7 +222,9 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
218222
let last_proxy = List.last proxies in
219223
let proxy_ip = Ipaddr.of_string_exn last_proxy in
220224
let equal_ip =
221-
proxy_ip = Ocsigen_request.remote_ip_parsed request_info
225+
match Ocsigen_request.remote_ip_parsed request_info with
226+
| `Ip r_ip -> Ipaddr.compare proxy_ip r_ip = 0
227+
| `Unix _ -> false
222228
in
223229
if equal_ip || not check_equal_ip
224230
then

src/extensions/revproxy.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ let gen dir = function
105105
let h =
106106
let forward =
107107
let address =
108-
Unix.string_of_inet_addr
108+
Ocsigen_config.Socket_type.to_string
109109
(Ocsigen_request.address request_info)
110110
in
111111
String.concat ", "

src/server/ocsigen_cohttp.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,24 @@ let service ?ssl ~address ~port ~connector () =
224224
| None -> `None
225225
in
226226
(* We create a specific context for Conduit and Cohttp. *)
227-
Conduit_lwt_unix.init
228-
~src:(Ocsigen_config.Socket_type.to_string address)
229-
~tls_own_key ()
230-
>>= fun conduit_ctx ->
227+
let src =
228+
match address with
229+
| `Unix _ -> None
230+
| _ -> Some (Ocsigen_config.Socket_type.to_string address)
231+
in
232+
Conduit_lwt_unix.init ?src ~tls_own_key () >>= fun conduit_ctx ->
231233
Lwt.return (Cohttp_lwt_unix.Net.init ~ctx:conduit_ctx ()) >>= fun ctx ->
232234
(* We catch the INET_ADDR of the server *)
233235
let callback =
234-
let address = Ocsigen_config.Socket_type.to_inet_addr address
235-
and ssl = match ssl with Some _ -> true | None -> false in
236+
let ssl = match ssl with Some _ -> true | None -> false in
236237
handler ~ssl ~address ~port ~connector
237238
in
238239
let config = Cohttp_lwt_unix.Server.make ~conn_closed ~callback () in
239240
let mode =
240-
match tls_own_key with
241-
| `None -> `TCP (`Port port)
242-
| `TLS (crt, key, pass) -> `OpenSSL (crt, key, pass, `Port port)
241+
match address, tls_own_key with
242+
| `Unix f, _ -> `Unix_domain_socket (`File f)
243+
| _, `None -> `TCP (`Port port)
244+
| _, `TLS (crt, key, pass) -> `OpenSSL (crt, key, pass, `Port port)
243245
in
244246
Cohttp_lwt_unix.Server.create ~stop ~ctx ~mode config >>= fun () ->
245247
Lwt.return (Lwt.wakeup stop_wakener ())

src/server/ocsigen_config.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ type ssl_info =
2828
; ssl_curve : string option }
2929

3030
module Socket_type = struct
31-
type t = [`All | `IPv4 of Unix.inet_addr | `IPv6 of Unix.inet_addr]
31+
type t =
32+
[`All | `IPv4 of Unix.inet_addr | `IPv6 of Unix.inet_addr | `Unix of string]
3233

3334
let to_string = function
3435
| `All -> Unix.string_of_inet_addr Unix.inet_addr_any
3536
| `IPv4 u -> Unix.string_of_inet_addr u
3637
| `IPv6 u -> Unix.string_of_inet_addr u
37-
38-
let to_inet_addr = function
39-
| `All -> Unix.inet_addr_any
40-
| `IPv4 u -> u
41-
| `IPv6 u -> u
38+
| `Unix s -> s
4239
end
4340

4441
type socket_type = Socket_type.t

src/server/ocsigen_config.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ type ssl_info =
2828
; ssl_curve : string option }
2929

3030
module Socket_type : sig
31-
type t = [`All | `IPv4 of Unix.inet_addr | `IPv6 of Unix.inet_addr]
31+
type t =
32+
[`All | `IPv4 of Unix.inet_addr | `IPv6 of Unix.inet_addr | `Unix of string]
3233

3334
val to_string : t -> string
34-
val to_inet_addr : t -> Unix.inet_addr
3535
end
3636

3737
type socket_type = Socket_type.t

src/server/ocsigen_request.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ let make_uri u =
4949
{u_uri; u_get_params; u_get_params_flat; u_path; u_path_string}
5050

5151
type t =
52-
{ r_address : Unix.inet_addr
52+
{ r_address : Ocsigen_config.Socket_type.t
5353
; r_port : int
5454
; r_ssl : bool
5555
; r_filenames : string list ref
5656
; r_sockaddr : Lwt_unix.sockaddr
5757
; r_remote_ip : string Lazy.t
58-
; r_remote_ip_parsed : Ipaddr.t Lazy.t
58+
; r_remote_ip_parsed : [`Ip of Ipaddr.t | `Unix of string] Lazy.t
5959
; r_forward_ip : string list
6060
; r_uri : uri
6161
; r_meth : Cohttp.Code.meth
@@ -88,10 +88,16 @@ let make
8888
=
8989
let r_remote_ip =
9090
lazy
91-
(Unix.string_of_inet_addr (Ocsigen_lib.Ip_address.of_sockaddr sockaddr))
91+
(match sockaddr with
92+
| Unix.ADDR_INET (ip, _port) -> Unix.string_of_inet_addr ip
93+
| ADDR_UNIX f -> f)
9294
in
9395
let r_remote_ip_parsed =
94-
lazy (Ipaddr.of_string_exn (Lazy.force r_remote_ip))
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)
95101
in
96102
{ r_address = address
97103
; r_port = port
@@ -146,7 +152,8 @@ let update
146152
match forward_ip with Some forward_ip -> forward_ip | None -> r_forward_ip
147153
and r_remote_ip, r_remote_ip_parsed =
148154
match remote_ip with
149-
| Some remote_ip -> lazy remote_ip, lazy (Ipaddr.of_string_exn remote_ip)
155+
| Some remote_ip ->
156+
lazy remote_ip, lazy (`Ip (Ipaddr.of_string_exn remote_ip))
150157
| None -> r_remote_ip, r_remote_ip_parsed
151158
and r_sub_path = match sub_path with Some _ -> sub_path | None -> r_sub_path
152159
and r_body =

src/server/ocsigen_request.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val make :
1515
-> ?original_full_path:string
1616
-> ?request_cache:Polytables.t
1717
-> ?cookies_override:string Ocsigen_cookie_map.Map_inner.t
18-
-> address:Unix.inet_addr
18+
-> address:Ocsigen_config.Socket_type.t
1919
-> port:int
2020
-> ssl:bool
2121
-> filenames:string list ref
@@ -42,7 +42,7 @@ val update :
4242
val to_cohttp : t -> Cohttp.Request.t
4343
val uri : t -> Uri.t
4444
val body : t -> Cohttp_lwt.Body.t
45-
val address : t -> Unix.inet_addr
45+
val address : t -> Ocsigen_config.Socket_type.t
4646
val host : t -> string option
4747
val meth : t -> Cohttp.Code.meth
4848
val port : t -> int
@@ -75,7 +75,7 @@ val post_params :
7575
-> (string * string) list Lwt.t option
7676

7777
val remote_ip : t -> string
78-
val remote_ip_parsed : t -> Ipaddr.t
78+
val remote_ip_parsed : t -> [`Ip of Ipaddr.t | `Unix of string]
7979
val forward_ip : t -> string list
8080
val content_type : t -> content_type option
8181
val request_cache : t -> Polytables.t

0 commit comments

Comments
 (0)