diff --git a/src/Tiny_httpd_server.ml b/src/Tiny_httpd_server.ml index 43b7cfd5..30c35d11 100644 --- a/src/Tiny_httpd_server.ml +++ b/src/Tiny_httpd_server.ml @@ -897,7 +897,7 @@ let handle_client_ (self : t) (client_sock : Unix.file_descr) : unit = let is_ipv6 self = String.contains self.addr ':' -let run (self : t) : (unit, _) result = +let run ?(after_init = ignore) (self : t) : (unit, _) result = try if self.masksigpipe then ignore (Unix.sigprocmask Unix.SIG_BLOCK [ Sys.sigpipe ] : _ list); @@ -925,6 +925,7 @@ let run (self : t) : (unit, _) result = Unix.listen sock (2 * self.sem_max_connections.Sem_.n) ); self.sock <- Some sock; + after_init (); while self.running do (* limit concurrency *) Sem_.acquire 1 self.sem_max_connections; diff --git a/src/Tiny_httpd_server.mli b/src/Tiny_httpd_server.mli index f4a41864..8f0e90a1 100644 --- a/src/Tiny_httpd_server.mli +++ b/src/Tiny_httpd_server.mli @@ -542,13 +542,17 @@ val stop : t -> unit (** Ask the server to stop. This might not have an immediate effect as {!run} might currently be waiting on IO. *) -val run : t -> (unit, exn) result +val run : ?after_init:(unit -> unit) -> t -> (unit, exn) result (** Run the main loop of the server, listening on a socket described at the server's creation time, using [new_thread] to start a thread for each new client. This returns [Ok ()] if the server exits gracefully, or [Error e] if - it exits with an error. *) + it exits with an error. + + @param after_init is called after the server starts listening. @since NEXT_RELEASE . + +*) (**/**) diff --git a/src/bin/http_of_dir.ml b/src/bin/http_of_dir.ml index 9d192fdf..87a96811 100644 --- a/src/bin/http_of_dir.ml +++ b/src/bin/http_of_dir.ml @@ -5,15 +5,17 @@ module Pf = Printf let serve ~config (dir : string) addr port j : _ result = let server = S.create ~max_connections:j ~addr ~port () in - Printf.printf "serve directory %s on http://%(%s%):%d\n%!" dir - (if S.is_ipv6 server then - "[%s]" - else - "%s") - addr port; + let after_init () = + Printf.printf "serve directory %s on http://%(%s%):%d\n%!" dir + (if S.is_ipv6 server then + "[%s]" + else + "%s") + addr (S.port server) + in D.add_dir_path ~config ~dir ~prefix:"" server; - S.run server + S.run ~after_init server let parse_size s : int = try Scanf.sscanf s "%dM" (fun n -> n * 1_024 * 1_024)