Skip to content

Commit d851d6f

Browse files
committed
Remove assertion in Ocsigen_cohttp_server
Fix a misconception between the initialization of server in Ocsigen_server and the main loop in Ocsigen_cohttp_server about the type of address (before this patch, we cast an inet_addr to a string in initialization, and we cast this string (with possible fail) to inet_addr, now we keep inet_addr).
1 parent c8d04e0 commit d851d6f

File tree

5 files changed

+17
-26
lines changed

5 files changed

+17
-26
lines changed

src/server/ocsigen_cohttp_server.ml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,16 @@ let service ?ssl ~address ~port ~connector () =
322322
in
323323
(* We create a specific context for Conduit and Cohttp. *)
324324
Conduit_lwt_unix.init
325-
~src:address
325+
~src:(Ocsigen_socket.string_of_socket_type address)
326326
~tls_server_key ()
327327
>>= fun conduit_ctx ->
328328
Lwt.return
329329
(Cohttp_lwt_unix_net.init
330330
~ctx:conduit_ctx ())
331331
(* We catch the INET_ADDR of the server *)
332332
>>= fun ctx ->
333-
Lwt_unix.getaddrinfo
334-
address
335-
"0"
336-
[Unix.AI_PASSIVE; Unix.AI_SOCKTYPE Unix.SOCK_STREAM]
337-
>>= function
338-
| { ai_addr = ADDR_INET (ai_addr, _); } :: _ ->
339333
let callback =
340-
handler ~address:ai_addr ~port ~extensions_connector:connector
334+
handler ~address:(Ocsigen_socket.to_inet_addr address) ~port ~extensions_connector:connector
341335
in
342336
let config = Server.make ~conn_closed ~callback () in
343337
let mode =
@@ -349,10 +343,3 @@ let service ?ssl ~address ~port ~connector () =
349343
Server.create ~stop ~ctx ~mode config
350344
>>= fun () ->
351345
Lwt.return (Lwt.wakeup stop_wakener ())
352-
| _ ->
353-
(* This case is not possible:
354-
- Conduit raise an error if we have an [ADDR_UNIX].
355-
- The result of [getaddrinfo] must not be empty
356-
(or Conduit would also raise an error).
357-
*)
358-
assert false

src/server/ocsigen_cohttp_server.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ val shutdown_server : float option -> unit
2121
(** initialize a main loop of http server *)
2222
val service :
2323
?ssl:string * string * (bool -> string) option ->
24-
address:string ->
24+
address:Ocsigen_socket.socket_type ->
2525
port:int ->
2626
connector:(Ocsigen_request_info.request_info -> unit -> Ocsigen_http_frame.result Lwt.t) ->
2727
unit -> unit Lwt.t

src/server/ocsigen_server.ml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,8 @@ let start_server () = try
203203
(fun a i -> (listen true i wait_end_init extensions_connector)@a) [] sslports;
204204
*)
205205

206-
let l =
207-
List.map (fun (a, p) -> Ocsigen_socket.string_of_socket_type a, p)
208-
ports
209-
in
210-
let connection = match l with
211-
| [] -> [("0.0.0.0", 80)]
206+
let connection = match ports with
207+
| [] -> [(Ocsigen_socket.All, 80)]
212208
| l -> l
213209
in
214210

@@ -221,10 +217,8 @@ let start_server () = try
221217
raise (Ocsigen_config.Config_file_error "SSL key is missing")
222218
| Some (None, Some _) ->
223219
raise (Ocsigen_config.Config_file_error "SSL certificate is missing")
224-
in match List.map
225-
(fun (a, p) -> Ocsigen_socket.string_of_socket_type a, p)
226-
sslports, ssl with
227-
| [], Some (crt, key) -> [("0.0.0.0", 443, (crt, key))]
220+
in match sslports, ssl with
221+
| [], Some (crt, key) -> [(Ocsigen_socket.All, 443, (crt, key))]
228222
| l, Some (crt, key) ->
229223
List.map (fun (a, p) -> (a, p, (crt, key))) l
230224
| _ -> []

src/server/ocsigen_socket.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ let string_of_socket_type = function
6464
| All -> Unix.string_of_inet_addr Unix.inet_addr_any
6565
| IPv4 u -> Unix.string_of_inet_addr u
6666
| IPv6 u -> Unix.string_of_inet_addr u
67+
68+
let to_inet_addr = function
69+
| All -> Unix.inet_addr_any
70+
| IPv4 u -> u
71+
| IPv6 u -> u

src/server/ocsigen_socket.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ val port_of_sockaddr : Unix.sockaddr -> int
2626
@param A socket_type
2727
*)
2828
val string_of_socket_type : socket_type -> string
29+
30+
(** to_inet_addr accessor of inet addr
31+
@param A socket_type
32+
*)
33+
val to_inet_addr : socket_type -> Unix.inet_addr

0 commit comments

Comments
 (0)