2 (** raised when a client timeouts *)
5 (** if timeout is given (Some _) @return a new callback which establish
6 timeout_callback as callback for signal Sys.sigalrm and register an alarm
7 (expiring after timeout seconds) before invoking the real callback given. If
8 timeout is None, callback is returned unchanged. *)
9 let wrap_callback_w_timeout ~callback ~timeout ~timeout_callback =
12 | Some timeout -> (* wrap callback setting an handler for ALRM signal and an
13 alarm that ring after timeout seconds *)
14 (fun inchan outchan ->
15 ignore (Sys.signal Sys.sigalrm (Sys.Signal_handle ~timeout_callback));
16 ignore (Unix.alarm timeout);
17 callback inchan outchan)
19 (** Http_daemon.start function low level which use Unix.establish_server which
20 in turn forks a child for each request *)
21 let ocaml_builtin ~sockaddr ~timeout callback =
22 let timeout_callback signo =
23 if signo = Sys.sigalrm then
27 (wrap_callback_w_timeout ~callback ~timeout ~timeout_callback)
30 (** Http_daemon.start function low level which doesn't fork, requests are
31 server sequentially and in the same address space of the calling process *)
32 let simple ~sockaddr ~timeout callback =
33 let suck = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
34 Unix.setsockopt suck Unix.SO_REUSEADDR true;
35 Unix.bind suck sockaddr;
37 let timeout_callback signo =
38 if signo = Sys.sigalrm then
41 let callback = wrap_callback_w_timeout ~callback ~timeout ~timeout_callback in
43 let (suck, _) = Unix.accept suck in
44 (* client is now connected *)
45 let (inchan, outchan) =
46 (Unix.in_channel_of_descr suck, Unix.out_channel_of_descr suck)
49 callback inchan outchan;
52 close_out outchan (* this close also inchan, because socket is the same *)