Skip to content

Commit ac469b1

Browse files
committed
server: Listen on a unix domain socket
The server can now listen on a local unix domain socket when started with: Ocsigen_server.start ~ports:[`File "./local.sock", 0] This can allow testing. `Socket_type.to_inet_addr` is removed as it's only used to later obtain a string.
1 parent 395125c commit ac469b1

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

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+
| `File _ -> 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+
| (`File _ as s), _ -> `Unix_domain_socket s
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 | `File 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+
| `File 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 | `File 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ 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

src/server/ocsigen_request.mli

Lines changed: 2 additions & 2 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

0 commit comments

Comments
 (0)