mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-07 11:45:36 -05:00
feat: support ipv6 address
This commit is contained in:
parent
3e28be80ec
commit
b7e458c0c4
3 changed files with 11 additions and 3 deletions
|
|
@ -518,6 +518,7 @@ type cb_path_handler = string Request.t -> Response.t
|
||||||
type t = {
|
type t = {
|
||||||
addr: string;
|
addr: string;
|
||||||
port: int;
|
port: int;
|
||||||
|
ipv6: bool;
|
||||||
sem_max_connections: Sem_.t;
|
sem_max_connections: Sem_.t;
|
||||||
new_thread: (unit -> unit) -> unit;
|
new_thread: (unit -> unit) -> unit;
|
||||||
masksigpipe: bool;
|
masksigpipe: bool;
|
||||||
|
|
@ -557,13 +558,14 @@ let add_path_handler
|
||||||
self.path_handlers <- ph :: self.path_handlers
|
self.path_handlers <- ph :: self.path_handlers
|
||||||
|
|
||||||
let create
|
let create
|
||||||
|
?(ipv6=false)
|
||||||
?(masksigpipe=true)
|
?(masksigpipe=true)
|
||||||
?(max_connections=32)
|
?(max_connections=32)
|
||||||
?(new_thread=(fun f -> ignore (Thread.create f () : Thread.t)))
|
?(new_thread=(fun f -> ignore (Thread.create f () : Thread.t)))
|
||||||
?(addr="127.0.0.1") ?(port=8080) () : t =
|
?(addr="127.0.0.1") ?(port=8080) () : t =
|
||||||
let handler _req = Response.fail ~code:404 "no top handler" in
|
let handler _req = Response.fail ~code:404 "no top handler" in
|
||||||
let max_connections = max 4 max_connections in
|
let max_connections = max 4 max_connections in
|
||||||
{ new_thread; addr; port; masksigpipe; handler;
|
{ new_thread; addr; port; masksigpipe; handler; ipv6;
|
||||||
running= true; sem_max_connections=Sem_.create max_connections;
|
running= true; sem_max_connections=Sem_.create max_connections;
|
||||||
path_handlers=[];
|
path_handlers=[];
|
||||||
cb_encode_resp=[]; cb_decode_req=[];
|
cb_encode_resp=[]; cb_decode_req=[];
|
||||||
|
|
@ -659,7 +661,9 @@ let run (self:t) : (unit,_) result =
|
||||||
if self.masksigpipe then (
|
if self.masksigpipe then (
|
||||||
ignore (Unix.sigprocmask Unix.SIG_BLOCK [Sys.sigpipe] : _ list);
|
ignore (Unix.sigprocmask Unix.SIG_BLOCK [Sys.sigpipe] : _ list);
|
||||||
);
|
);
|
||||||
let sock = Unix.socket PF_INET Unix.SOCK_STREAM 0 in
|
let sock =
|
||||||
|
Unix.socket (if self.ipv6 then Unix.PF_INET6 else Unix.PF_INET)
|
||||||
|
Unix.SOCK_STREAM 0 in
|
||||||
Unix.clear_nonblock sock;
|
Unix.clear_nonblock sock;
|
||||||
Unix.setsockopt sock Unix.SO_REUSEADDR true;
|
Unix.setsockopt sock Unix.SO_REUSEADDR true;
|
||||||
Unix.setsockopt_optint sock Unix.SO_LINGER None;
|
Unix.setsockopt_optint sock Unix.SO_LINGER None;
|
||||||
|
|
|
||||||
|
|
@ -322,6 +322,7 @@ type t
|
||||||
(** A HTTP server. See {!create} for more details. *)
|
(** A HTTP server. See {!create} for more details. *)
|
||||||
|
|
||||||
val create :
|
val create :
|
||||||
|
?ipv6:bool ->
|
||||||
?masksigpipe:bool ->
|
?masksigpipe:bool ->
|
||||||
?max_connections:int ->
|
?max_connections:int ->
|
||||||
?new_thread:((unit -> unit) -> unit) ->
|
?new_thread:((unit -> unit) -> unit) ->
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ type config = {
|
||||||
mutable max_upload_size: int;
|
mutable max_upload_size: int;
|
||||||
mutable delete: bool;
|
mutable delete: bool;
|
||||||
mutable j: int;
|
mutable j: int;
|
||||||
|
mutable ipv6: bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_config () : config = {
|
let default_config () : config = {
|
||||||
|
|
@ -18,6 +19,7 @@ let default_config () : config = {
|
||||||
upload=true;
|
upload=true;
|
||||||
max_upload_size = 10 * 1024 * 1024;
|
max_upload_size = 10 * 1024 * 1024;
|
||||||
j=32;
|
j=32;
|
||||||
|
ipv6=false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let contains_dot_dot s =
|
let contains_dot_dot s =
|
||||||
|
|
@ -100,7 +102,7 @@ let date_of_time (f:float) : string =
|
||||||
|
|
||||||
let serve ~config (dir:string) : _ result =
|
let serve ~config (dir:string) : _ result =
|
||||||
Printf.printf "serve directory %s on http://%s:%d\n%!" dir config.addr config.port;
|
Printf.printf "serve directory %s on http://%s:%d\n%!" dir config.addr config.port;
|
||||||
let server = S.create ~max_connections:config.j ~addr:config.addr ~port:config.port () in
|
let server = S.create ~ipv6:config.ipv6 ~max_connections:config.j ~addr:config.addr ~port:config.port () in
|
||||||
if config.delete then (
|
if config.delete then (
|
||||||
S.add_path_handler server ~meth:`DELETE "/%s"
|
S.add_path_handler server ~meth:`DELETE "/%s"
|
||||||
(fun path _req ->
|
(fun path _req ->
|
||||||
|
|
@ -209,6 +211,7 @@ let main () =
|
||||||
"--delete", Unit (fun () -> config.delete <- true), " enable `delete` on files";
|
"--delete", Unit (fun () -> config.delete <- true), " enable `delete` on files";
|
||||||
"--no-delete", Unit (fun () -> config.delete <- false), " disable `delete` on files";
|
"--no-delete", Unit (fun () -> config.delete <- false), " disable `delete` on files";
|
||||||
"-j", Int (fun j->config.j <- j), " maximum number of simultaneous connections";
|
"-j", Int (fun j->config.j <- j), " maximum number of simultaneous connections";
|
||||||
|
"-6", Unit (fun () -> config.ipv6 <- true), " allow ipv6";
|
||||||
]) (fun s -> dir_ := s) "http_of_dir [options] [dir]";
|
]) (fun s -> dir_ := s) "http_of_dir [options] [dir]";
|
||||||
match serve ~config !dir_ with
|
match serve ~config !dir_ with
|
||||||
| Ok () -> ()
|
| Ok () -> ()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue