Compare commits

...

21 commits

Author SHA1 Message Date
Simon Cruanes
236c93ea4f
Merge a56dd0ec65 into 6203e7a4a7 2025-04-24 03:07:01 +00:00
Simon Cruanes
6203e7a4a7
prepare for 0.19
Some checks failed
github pages / deploy (push) Has been cancelled
build / build (4.08.x, ubuntu-latest) (push) Has been cancelled
build / build (4.14.x, ubuntu-latest) (push) Has been cancelled
build / build (5.03.x, ubuntu-latest) (push) Has been cancelled
2025-04-18 09:37:27 -04:00
Simon Cruanes
d7a5cca1d4
feat(headers): set will not reallocate whole list if not needed 2025-04-18 09:37:27 -04:00
Simon Cruanes
cdac33689a
add basic test for response 2025-04-18 09:37:27 -04:00
Simon Cruanes
4c8cc8ba5a
test: update test 2025-04-18 09:37:27 -04:00
Simon Cruanes
173e5fef6e
feat(headers): use case insensitive comparison 2025-04-18 09:37:27 -04:00
Simon Cruanes
94c9239d64
fix(response): do not override "content-length" in raw response
close #92
2025-04-18 09:37:27 -04:00
Simon Cruanes
c55e3a2dfc
feat pool: expose acquire/release 2025-04-18 09:37:27 -04:00
Simon Cruanes
f6daff24c0
prepare for 0.18 2025-04-18 09:37:27 -04:00
Simon Cruanes
3c9e505a45
fix test 2025-04-18 09:37:27 -04:00
Simon Cruanes
44002fc355
detail 2025-03-25 15:01:17 -04:00
Simon Cruanes
f3461cfd21
detail in example 2025-03-21 08:37:16 -04:00
Simon Cruanes
075ad0825a
feat: add ?head_middlewares to create 2025-03-19 13:40:49 -04:00
Simon Cruanes
75d90559bd
fix warnings 2025-03-19 13:39:15 -04:00
Simon Cruanes
e177153f10
remove dead comment 2025-03-14 11:12:11 -04:00
Jonah Beckford
1e0bbc7f39 Processing to fix incompatible -O and gcc flags
Some checks failed
github pages / deploy (push) Has been cancelled
build / build (4.08.x, ubuntu-latest) (push) Has been cancelled
build / build (4.14.x, ubuntu-latest) (push) Has been cancelled
build / build (5.03.x, ubuntu-latest) (push) Has been cancelled
Two changes:
1. Accept BUILD_TINY_HTTPD_OPTLEVEL envvar to adjust the -O<num> level. Defaults to 2. Can be negative to remove it entirely, which fixes errors with MSVC which will bail on incompatible options.
2. Do not use -fPIC with MSVC
2025-02-15 20:06:44 -05:00
Simon Cruanes
a56dd0ec65
add echo_mio.sh 2024-09-06 17:19:56 -04:00
Simon Cruanes
7f9fae1fc8
test: echo_mio: add a heavier endpoint 2024-09-03 15:51:15 -04:00
Simon Cruanes
e199162e1f
fix: also make server socket nonblocking 2024-09-03 15:40:26 -04:00
Simon Cruanes
cf9c14b1c2
basic test for moonpool-io 2024-09-03 15:17:25 -04:00
Simon Cruanes
c43ffb5ff4
feat: add tiny_httpd_moonpool library 2024-09-03 15:16:33 -04:00
34 changed files with 836 additions and 56 deletions

View file

@ -1,4 +1,23 @@
## 0.19
- feat(headers): `set` will not reallocate whole list if not needed
- feat(headers): use case insensitive comparison
- fix(response): do not override "content-length" in raw response
- feat pool: expose `acquire/release` for advanced uses
## 0.18
- feat: add ?head_middlewares to `create`
- add content-type header for prometheus endpoint
- new flag ?enable_logging to disable regular logs (not debug)
- new sublibrary to deal with multipart-form-data
- feat response: add `pp_with`; have `pp` hide set-cookie headers
- fix percent encoding for < 0x10 chars
- Processing to fix incompatible -O and gcc flags
- fix: make check for 'Connection: Upgrade' header case-insensitive
## 0.17
- add optional middlewares to tiny_httpd_ws

View file

@ -1,10 +1,10 @@
(lang dune 2.9)
(lang dune 3.2)
(name tiny_httpd)
(generate_opam_files true)
(authors c-cube)
(maintainers c-cube)
(version 0.17)
(version 0.19)
(source (github c-cube/tiny_httpd))
(homepage https://github.com/c-cube/tiny_httpd/)
(license MIT)
@ -39,3 +39,14 @@
(iostream-camlzip (>= 0.2.1))
(logs :with-test)
(odoc :with-doc)))
(package
(name tiny_httpd_moonpool)
(synopsis "Moonpool+picos_stdio backend for Tiny_httpd")
(depends
seq
(tiny_httpd (= :version))
(moonpool (>= 0.7))
(moonpool-io (>= 0.7))
(ocaml (>= 5.0))
(odoc :with-doc)))

2
echo_mio.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/sh
exec dune exec --display=quiet --profile=release "examples/echo_mio.exe" -- $@

View file

@ -14,6 +14,12 @@
(modules echo vfs)
(libraries tiny_httpd logs tiny_httpd_camlzip tiny_httpd.multipart-form-data))
(executable
(name echo_mio)
(flags :standard -warn-error -a+8)
(modules echo_mio)
(libraries tiny_httpd tiny_httpd_moonpool logs))
(executable
(name writer)
(flags :standard -warn-error -a+8)

View file

@ -134,6 +134,7 @@ let setup_upload server : unit =
let () =
let port_ = ref 8080 in
let j = ref 32 in
let addr = ref "127.0.0.1" in
Arg.parse
(Arg.align
[
@ -141,11 +142,12 @@ let () =
"-p", Arg.Set_int port_, " set port";
"--debug", Arg.Unit setup_logging, " enable debug";
"-j", Arg.Set_int j, " maximum number of connections";
"--addr", Arg.Set_string addr, " binding address";
])
(fun _ -> raise (Arg.Bad ""))
"echo [option]*";
let server = Tiny_httpd.create ~port:!port_ ~max_connections:!j () in
let server = Tiny_httpd.create ~addr:!addr ~port:!port_ ~max_connections:!j () in
Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16 * 1024) server;
let m_stats, get_stats = middleware_stat () in

294
examples/echo_mio.ml Normal file
View file

@ -0,0 +1,294 @@
open Tiny_httpd_core
module Log = Tiny_httpd.Log
let now_ = Unix.gettimeofday
let alice_text =
"CHAPTER I. Down the Rabbit-Hole Alice was beginning to get very tired of \
sitting by her sister on the bank, and of having nothing to do: once or \
twice she had peeped into the book her sister was reading, but it had no \
pictures or conversations in it, <and what is the use of a book,> thought \
Alice <without pictures or conversations?> So she was considering in her \
own mind (as well as she could, for the hot day made her feel very sleepy \
and stupid), whether the pleasure of making a daisy-chain would be worth \
the trouble of getting up and picking the daisies, when suddenly a White \
Rabbit with pink eyes ran close by her. There was nothing so very \
remarkable in that; nor did Alice think it so very much out of the way to \
hear the Rabbit say to itself, <Oh dear! Oh dear! I shall be late!> (when \
she thought it over afterwards, it occurred to her that she ought to have \
wondered at this, but at the time it all seemed quite natural); but when \
the Rabbit actually took a watch out of its waistcoat-pocket, and looked at \
it, and then hurried on, Alice started to her feet, for it flashed across \
her mind that she had never before seen a rabbit with either a \
waistcoat-pocket, or a watch to take out of it, and burning with curiosity, \
she ran across the field after it, and fortunately was just in time to see \
it pop down a large rabbit-hole under the hedge. In another moment down \
went Alice after it, never once considering how in the world she was to get \
out again. The rabbit-hole went straight on like a tunnel for some way, and \
then dipped suddenly down, so suddenly that Alice had not a moment to think \
about stopping herself before she found herself falling down a very deep \
well. Either the well was very deep, or she fell very slowly, for she had \
plenty of time as she went down to look about her and to wonder what was \
going to happen next. First, she tried to look down and make out what she \
was coming to, but it was too dark to see anything; then she looked at the \
sides of the well, and noticed that they were filled with cupboards......"
(* util: a little middleware collecting statistics *)
let middleware_stat () : Server.Middleware.t * (unit -> string) =
let n_req = ref 0 in
let total_time_ = ref 0. in
let parse_time_ = ref 0. in
let build_time_ = ref 0. in
let write_time_ = ref 0. in
let m h req ~resp =
incr n_req;
let t1 = Request.start_time req in
let t2 = now_ () in
h req ~resp:(fun response ->
let t3 = now_ () in
resp response;
let t4 = now_ () in
total_time_ := !total_time_ +. (t4 -. t1);
parse_time_ := !parse_time_ +. (t2 -. t1);
build_time_ := !build_time_ +. (t3 -. t2);
write_time_ := !write_time_ +. (t4 -. t3))
and get_stat () =
Printf.sprintf
"%d requests (average response time: %.3fms = %.3fms + %.3fms + %.3fms)"
!n_req
(!total_time_ /. float !n_req *. 1e3)
(!parse_time_ /. float !n_req *. 1e3)
(!build_time_ /. float !n_req *. 1e3)
(!write_time_ /. float !n_req *. 1e3)
in
m, get_stat
(* ugly AF *)
let base64 x =
let ic, oc = Unix.open_process "base64" in
output_string oc x;
flush oc;
close_out oc;
let r = input_line ic in
ignore (Unix.close_process (ic, oc));
r
let setup_logging () =
Logs.set_reporter @@ Logs.format_reporter ();
Logs.set_level ~all:true (Some Logs.Debug)
let () =
Moonpool_fib.main @@ fun _ ->
let port_ = ref 8080 in
let max_conn = ref 800 in
let j = ref 16 in
Arg.parse
(Arg.align
[
"--port", Arg.Set_int port_, " set port";
"-p", Arg.Set_int port_, " set port";
"--debug", Arg.Unit setup_logging, " enable debug";
( "--max-connections",
Arg.Set_int max_conn,
" maximum number of connections" );
"-j", Arg.Set_int j, " Size of thread pool";
])
(fun _ -> raise (Arg.Bad ""))
"echo [option]*";
let runner = Moonpool.Ws_pool.create ~num_threads:!j () in
let server : Server.t =
Tiny_httpd_moonpool.create ~runner ~port:!port_ ~max_connections:!max_conn
()
in
let m_stats, get_stats = middleware_stat () in
Server.add_middleware server ~stage:(`Stage 1) m_stats;
(* say hello *)
Server.add_route_handler ~meth:`GET server
Route.(exact "hello" @/ string @/ return)
(fun name _req -> Response.make_string (Ok ("hello " ^ name ^ "!\n")));
(* compressed file access *)
Server.add_route_handler ~meth:`GET server
Route.(exact "zcat" @/ string_urlencoded @/ return)
(fun path _req ->
let ic = open_in path in
let str = IO.Input.of_in_channel ic in
let mime_type =
try
let p = Unix.open_process_in (Printf.sprintf "file -i -b %S" path) in
try
let s = [ "Content-Type", String.trim (input_line p) ] in
ignore @@ Unix.close_process_in p;
s
with _ ->
ignore @@ Unix.close_process_in p;
[]
with _ -> []
in
Response.make_stream ~headers:mime_type (Ok str));
(* echo request *)
Server.add_route_handler server
Route.(exact "echo" @/ return)
(fun req ->
let q =
Request.query req
|> List.map (fun (k, v) -> Printf.sprintf "%S = %S" k v)
|> String.concat ";"
in
Response.make_string
(Ok (Format.asprintf "echo:@ %a@ (query: %s)@." Request.pp req q)));
(* file upload *)
Server.add_route_handler_stream ~meth:`PUT server
Route.(exact "upload" @/ string @/ return)
(fun path req ->
Log.debug (fun k ->
k "start upload %S, headers:\n%s\n\n%!" path
(Format.asprintf "%a" Headers.pp (Request.headers req)));
try
let oc = open_out @@ "/tmp/" ^ path in
IO.Input.to_chan oc req.Request.body;
flush oc;
Response.make_string (Ok "uploaded file")
with e ->
Response.fail ~code:500 "couldn't upload file: %s"
(Printexc.to_string e));
(* protected by login *)
Server.add_route_handler server
Route.(exact "protected" @/ return)
(fun req ->
let ok =
match Request.get_header req "authorization" with
| Some v ->
Log.debug (fun k -> k "authenticate with %S" v);
v = "Basic " ^ base64 "user:foobar"
| None -> false
in
if ok then (
(* FIXME: a logout link *)
let s =
"<p>hello, this is super secret!</p><a href=\"/logout\">log out</a>"
in
Response.make_string (Ok s)
) else (
let headers =
Headers.(empty |> set "www-authenticate" "basic realm=\"echo\"")
in
Response.fail ~code:401 ~headers "invalid"
));
(* logout *)
Server.add_route_handler server
Route.(exact "logout" @/ return)
(fun _req -> Response.fail ~code:401 "logged out");
(* stats *)
Server.add_route_handler server
Route.(exact "stats" @/ return)
(fun _req ->
let stats = get_stats () in
Response.make_string @@ Ok stats);
Server.add_route_handler server
Route.(exact "alice" @/ return)
(fun _req -> Response.make_string (Ok alice_text));
Server.add_route_handler server
Route.(exact "alice10" @/ return)
(fun _req ->
let writer =
IO.Writer.make () ~write:(fun oc ->
for _i = 1 to 10 do
IO.Output.output_string oc alice_text;
IO.Output.flush oc
done)
in
Response.make_writer (Ok writer));
(* main page *)
Server.add_route_handler server
Route.(return)
(fun _req ->
let open Tiny_httpd_html in
let h =
html []
[
head [] [ title [] [ txt "index of echo" ] ];
body []
[
h3 [] [ txt "welcome!" ];
p [] [ b [] [ txt "endpoints are:" ] ];
ul []
[
li [] [ pre [] [ txt "/hello/:name (GET)" ] ];
li []
[
pre []
[
a [ A.href "/echo/" ] [ txt "echo" ];
txt " echo back query";
];
];
li []
[ pre [] [ txt "/upload/:path (PUT) to upload a file" ] ];
li []
[
pre []
[
txt
"/zcat/:path (GET) to download a file (deflate \
transfer-encoding)";
];
];
li []
[
pre []
[
a [ A.href "/stats/" ] [ txt "/stats/" ];
txt " (GET) to access statistics";
];
];
li []
[
pre []
[
a [ A.href "/vfs/" ] [ txt "/vfs" ];
txt " (GET) to access a VFS embedded in the binary";
];
];
li []
[
pre []
[
a [ A.href "/protected" ] [ txt "/protected" ];
txt
" (GET) to see a protected page (login: user, \
password: foobar)";
];
];
li []
[
pre []
[
a [ A.href "/logout" ] [ txt "/logout" ];
txt " (POST) to log out";
];
];
];
];
]
in
let s = to_string_top h in
Response.make_string ~headers:[ "content-type", "text/html" ] @@ Ok s);
Printf.printf "listening on http://%s:%d\n%!" (Server.addr server)
(Server.port server);
match Server.run server with
| Ok () -> ()
| Error e -> raise e

View file

@ -30,7 +30,8 @@ end
let create ?enable_logging ?(masksigpipe = not Sys.win32) ?max_connections
?(timeout = 0.0) ?buf_size ?(get_time_s = Unix.gettimeofday)
?(new_thread = fun f -> ignore (Thread.create f () : Thread.t))
?(addr = "127.0.0.1") ?(port = 8080) ?sock ?middlewares () : t =
?(addr = "127.0.0.1") ?(port = 8080) ?sock ?head_middlewares ?middlewares ()
: t =
let max_connections = get_max_connection_ ?max_connections () in
let server =
{
@ -65,4 +66,5 @@ let create ?enable_logging ?(masksigpipe = not Sys.win32) ?max_connections
let tcp_server () = tcp_server_builder
end in
let backend = (module B : IO_BACKEND) in
Server.create_from ?enable_logging ?buf_size ?middlewares ~backend ()
Server.create_from ?enable_logging ?buf_size ?head_middlewares ?middlewares
~backend ()

View file

@ -135,6 +135,7 @@ val create :
?addr:string ->
?port:int ->
?sock:Unix.file_descr ->
?head_middlewares:Head_middleware.t list ->
?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list ->
unit ->
t
@ -169,7 +170,7 @@ val create :
used instead of the [addr] and [port]. If not passed in, those will be
used. This parameter exists since 0.10.
@param enable_logging if true and [Logs] is installed, log requests. Default true.
This parameter exists since NEXT_RELEASE. Does not affect debug-level logs.
This parameter exists since 0.18. Does not affect debug-level logs.
@param get_time_s obtain the current timestamp in seconds.
This parameter exists since 0.11.

View file

@ -1,7 +1,5 @@
module S = Tiny_httpd
module U = Tiny_httpd.Util
module D = Tiny_httpd.Dir
module Pf = Printf
module Log = Tiny_httpd.Log
let serve ~config ~timeout (dir : string) addr port j : _ result =
@ -9,9 +7,9 @@ let serve ~config ~timeout (dir : string) addr port j : _ result =
let after_init () =
Printf.printf "serve directory %s on http://%(%s%):%d\n%!" dir
(if S.is_ipv6 server then
"[%s]"
else
"%s")
"[%s]"
else
"%s")
addr (S.port server)
in
@ -63,9 +61,9 @@ let main () =
(fun b ->
config.dir_behavior <-
(if b then
Index_or_lists
else
Lists)),
Index_or_lists
else
Lists)),
" <bool> automatically redirect to index.html if present" );
( "--delete",
Unit (fun () -> config.delete <- true),

View file

@ -4,24 +4,46 @@ type t = (string * string) list
let empty = []
let contains name headers =
let name' = String.lowercase_ascii name in
List.exists (fun (n, _) -> name' = n) headers
(* [Char.lowercase_ascii] but easier to inline *)
let[@inline] lower_char_ = function
| 'A' .. 'Z' as c -> Char.unsafe_chr (Char.code c + 32)
| c -> c
let get_exn ?(f = fun x -> x) x h =
let x' = String.lowercase_ascii x in
List.assoc x' h |> f
(** Are these two header names equal? This is case insensitive *)
let equal_name_ (s1 : string) (s2 : string) : bool =
String.length s1 = String.length s2
&&
try
for i = 0 to String.length s1 - 1 do
let c1 = String.unsafe_get s1 i |> lower_char_ in
let c2 = String.unsafe_get s2 i |> lower_char_ in
if c1 <> c2 then raise_notrace Exit
done;
true
with Exit -> false
let contains name headers =
List.exists (fun (n, _) -> equal_name_ name n) headers
let rec get_exn ?(f = fun x -> x) x h =
match h with
| [] -> raise Not_found
| (k, v) :: _ when equal_name_ x k -> f v
| _ :: tl -> get_exn ~f x tl
let get ?(f = fun x -> x) x h =
try Some (get_exn ~f x h) with Not_found -> None
let remove x h =
let x' = String.lowercase_ascii x in
List.filter (fun (k, _) -> k <> x') h
let remove x h = List.filter (fun (k, _) -> not (equal_name_ k x)) h
let set x y h =
let x' = String.lowercase_ascii x in
(x', y) :: List.filter (fun (k, _) -> k <> x') h
let h =
if contains x h then
remove x h
else
h
in
(x, y) :: h
let pp out l =
let pp_pair out (k, v) = Format.fprintf out "@[<h>%s: %s@]" k v in
@ -76,6 +98,6 @@ let parse_ ~(buf : Buf.t) (bs : IO.Input.t) : t =
| Error msg ->
bad_reqf 400 "invalid header line: %s\nline is: %S" msg line
in
loop ((String.lowercase_ascii k, v) :: acc)
loop ((k, v) :: acc)
in
loop []

View file

@ -32,8 +32,9 @@ val contains : string -> t -> bool
val pp : Format.formatter -> t -> unit
(** Pretty print the headers. *)
val parse_ : buf:Buf.t -> IO.Input.t -> t
(**/*)
val parse_ : buf:Buf.t -> IO.Input.t -> t
val parse_line_ : string -> (string * string, string) result
(**/*)

View file

@ -14,4 +14,4 @@ val dummy : bool
val fully_disable : unit -> unit
(** Totally silence logs for tiny_httpd. With [Logs] installed this means setting
the level of the tiny_httpd source to [None].
@since NEXT_RELEASE *)
@since 0.18 *)

View file

@ -12,20 +12,20 @@ type 'a t = {
let create ?(clear = ignore) ~mk_item ?(max_size = 512) () : _ t =
{ mk_item; clear; max_size; items = A.make Nil }
let rec acquire_ self =
let rec acquire self =
match A.get self.items with
| Nil -> self.mk_item ()
| Cons (_, x, tl) as l ->
if A.compare_and_set self.items l tl then
x
else
acquire_ self
acquire self
let[@inline] size_ = function
| Cons (sz, _, _) -> sz
| Nil -> 0
let release_ self x : unit =
let release self x : unit =
let rec loop () =
match A.get self.items with
| Cons (sz, _, _) when sz >= self.max_size ->
@ -40,12 +40,17 @@ let release_ self x : unit =
loop ()
let with_resource (self : _ t) f =
let x = acquire_ self in
let x = acquire self in
try
let res = f x in
release_ self x;
release self x;
res
with e ->
let bt = Printexc.get_raw_backtrace () in
release_ self x;
release self x;
Printexc.raise_with_backtrace e bt
module Raw = struct
let release = release
let acquire = acquire
end

View file

@ -23,3 +23,12 @@ val with_resource : 'a t -> ('a -> 'b) -> 'b
(** [with_resource pool f] runs [f x] with [x] a resource;
when [f] fails or returns, [x] is returned to the pool for
future reuse. *)
(** Low level control over the pool.
This is easier to get wrong (e.g. releasing the same resource twice)
so use with caution.
@since 0.18 *)
module Raw : sig
val acquire : 'a t -> 'a
val release : 'a t -> 'a -> unit
end

View file

@ -15,7 +15,11 @@ let set_code code self = { self with code }
let make_raw ?(headers = []) ~code body : t =
(* add content length to response *)
let headers =
Headers.set "Content-Length" (string_of_int (String.length body)) headers
if Headers.contains "content-length" headers then
(* do not override user-provided headers (e.g. in HEAD), see #92 *)
headers
else
Headers.set "Content-Length" (string_of_int (String.length body)) headers
in
{ code; headers; body = `String body }

View file

@ -126,7 +126,7 @@ val pp_with :
Default is ["set-cookie"].
@param pp_body body printer
(default fully prints String bodies, but omits stream bodies)
@since NEXT_RELEASE *)
@since 0.18 *)
val pp : Format.formatter -> t -> unit
(** Pretty print the response. The exact format is not specified. *)

View file

@ -5,6 +5,7 @@ let not_found = 404
let descr = function
| 100 -> "Continue"
| 101 -> "Switching Protocols"
| 200 -> "OK"
| 201 -> "Created"
| 202 -> "Accepted"

View file

@ -88,6 +88,7 @@ type t = {
mutable tcp_server: IO.TCP_server.t option;
mutable handler: IO.Input.t Request.t -> Response.t;
(** toplevel handler, if any *)
mutable head_middlewares: Head_middleware.t list;
mutable middlewares: (int * Middleware.t) list; (** Global middlewares *)
mutable middlewares_sorted: (int * Middleware.t) list lazy_t;
(** sorted version of {!middlewares} *)
@ -128,6 +129,9 @@ let add_middleware ~stage self m =
self.middlewares <- (stage, m) :: self.middlewares;
self.middlewares_sorted <- lazy (sort_middlewares_ self.middlewares)
let add_head_middleware (self : t) m : unit =
self.head_middlewares <- m :: self.head_middlewares
let add_decode_request_cb self f =
(* turn it into a middleware *)
let m h req ~resp =
@ -258,6 +262,7 @@ let add_route_server_sent_handler ?accept ?(middlewares = []) self route f =
let add_upgrade_handler ?(accept = fun _ -> Ok ()) ?(middlewares = [])
(self : t) route f : unit =
let ph req : handler_result option =
let middlewares = List.rev_append self.head_middlewares middlewares in
if req.Request.meth <> `GET then
None
else (
@ -274,7 +279,7 @@ let add_upgrade_handler ?(accept = fun _ -> Ok ()) ?(middlewares = [])
let clear_bytes_ bs = Bytes.fill bs 0 (Bytes.length bs) '\x00'
let create_from ?(enable_logging = not Log.dummy) ?(buf_size = 16 * 1_024)
?(middlewares = []) ~backend () : t =
?(head_middlewares = []) ?(middlewares = []) ~backend () : t =
let handler _req = Response.fail ~code:404 "no top handler" in
let self =
{
@ -283,6 +288,7 @@ let create_from ?(enable_logging = not Log.dummy) ?(buf_size = 16 * 1_024)
tcp_server = None;
handler;
path_handlers = [];
head_middlewares;
middlewares = [];
middlewares_sorted = lazy [];
bytes_pool =

View file

@ -83,6 +83,7 @@ end
val create_from :
?enable_logging:bool ->
?buf_size:int ->
?head_middlewares:Head_middleware.t list ->
?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list ->
backend:(module IO_BACKEND) ->
unit ->
@ -94,9 +95,10 @@ val create_from :
{!set_top_handler} to specify how to handle incoming requests.
@param buf_size size for buffers (since 0.11)
@param head_middlewares see {!add_head_middleware} for details (since 0.18)
@param middlewares see {!add_middleware} for more details.
@param enable_logging if true and [Logs] is installed,
emit logs via Logs (since NEXT_RELEASE).
emit logs via Logs (since 0.18).
Default [true].
@since 0.14
@ -152,6 +154,12 @@ val add_middleware :
@since 0.11
*)
val add_head_middleware : t -> Head_middleware.t -> unit
(** Add a request-header only {!Head_middleware.t}.
This is called on requests, to modify them, and returns a new request
immediately.
@since 0.18 *)
(** {2 Request handlers} *)
val set_top_handler : t -> (IO.Input.t Request.t -> Response.t) -> unit

6
src/moonpool-io/dune Normal file
View file

@ -0,0 +1,6 @@
(library
(name tiny_httpd_moonpool)
(public_name tiny_httpd_moonpool)
(libraries tiny_httpd moonpool moonpool.sync moonpool.fib moonpool-io))

View file

@ -0,0 +1,220 @@
open Tiny_httpd_core
module A = Atomic
module MIO = Moonpool_io
module Sem = Moonpool_sync.Semaphore.Counting
module Fd = Moonpool_io.Fd
module IO_helper = struct
module Slice = Iostream.Slice
module Output = struct
include IO.Output
class of_unix_fd ?(close_noerr = false) ~closed ~(buf : Slice.t) (fd : Fd.t) :
t =
object
inherit t_from_output ~bytes:buf.bytes ()
method private output_underlying bs i len0 =
let i = ref i in
let len = ref len0 in
while !len > 0 do
match MIO.Unix.write fd bs !i !len with
| 0 -> failwith "write failed"
| n ->
i := !i + n;
len := !len - n
done
method private close_underlying () =
if not !closed then (
closed := true;
if close_noerr then (
try MIO.Unix.close fd with _ -> ()
) else
MIO.Unix.close fd
)
end
end
module Input = struct
include IO.Input
let of_unix_fd ?(close_noerr = false) ~closed ~(buf : Slice.t) (fd : Fd.t) :
t =
let eof = ref false in
object
inherit Iostream.In_buf.t_from_refill ~bytes:buf.bytes ()
method private refill (slice : Slice.t) =
if not !eof then (
slice.off <- 0;
let continue = ref true in
while !continue do
match
MIO.Unix.read fd slice.bytes 0 (Bytes.length slice.bytes)
with
| n ->
slice.len <- n;
continue := false
done;
(* Printf.eprintf "read returned %d B\n%!" !n; *)
if slice.len = 0 then eof := true
)
method close () =
if not !closed then (
closed := true;
eof := true;
if close_noerr then (
try MIO.Unix.close fd with _ -> ()
) else
MIO.Unix.close fd
)
end
end
end
open struct
let get_addr_ (fd : Fd.t) =
match Unix.getsockname (Fd.unsafe_get fd) with
| Unix.ADDR_INET (addr, port) -> addr, port
| _ -> invalid_arg "httpd: address is not INET"
let shutdown_silent_ (fd : Fd.t) : unit =
try MIO.Unix.shutdown fd Unix.SHUTDOWN_ALL with _ -> ()
let close_silent_ (fd : Fd.t) : unit = try MIO.Unix.close fd with _ -> ()
end
type t = {
addr: string;
port: int;
buf_pool: Buf.t Pool.t;
slice_pool: IO.Slice.t Pool.t;
max_connections: int;
sem_max_connections: Sem.t;
(** semaphore to restrict the number of active concurrent connections *)
mutable sock: Fd.t option; (** Socket *)
new_thread: (unit -> unit) -> unit;
timeout: float;
running: bool A.t; (* TODO: use an atomic? *)
}
let to_tcp_server (self : t) : IO.TCP_server.builder =
{
IO.TCP_server.serve =
(fun ~after_init ~handle () : unit ->
let sock, should_bind =
match self.sock with
| Some s ->
(* Because we're getting a socket from the caller (e.g. systemd) *)
s, false
| None ->
let sock =
Unix.socket
(if Util.is_ipv6_str self.addr then
Unix.PF_INET6
else
Unix.PF_INET)
Unix.SOCK_STREAM 0
in
let fd = Fd.create sock in
fd, true (* Because we're creating the socket ourselves *)
in
MIO.Unix.set_nonblock sock;
MIO.Unix.setsockopt_optint sock Unix.SO_LINGER None;
if should_bind then (
let inet_addr = Unix.inet_addr_of_string self.addr in
MIO.Unix.setsockopt sock Unix.SO_REUSEADDR true;
MIO.Unix.bind sock (Unix.ADDR_INET (inet_addr, self.port));
let n_listen = 2 * self.max_connections in
MIO.Unix.listen sock n_listen
);
self.sock <- Some sock;
let tcp_server =
{
IO.TCP_server.stop = (fun () -> Atomic.set self.running false);
running = (fun () -> Atomic.get self.running);
active_connections =
(fun () ->
self.max_connections - Sem.get_value self.sem_max_connections);
endpoint =
(fun () ->
let addr, port = get_addr_ sock in
Unix.string_of_inet_addr addr, port);
}
in
after_init tcp_server;
(* how to handle a single client *)
let handle_client_ (client_sock : Fd.t) (client_addr : Unix.sockaddr) :
unit =
Log.debug (fun k ->
k "t[%d]: serving new client on %s"
(Thread.id @@ Thread.self ())
(Util.show_sockaddr client_addr));
MIO.Unix.set_nonblock client_sock;
MIO.Unix.setsockopt client_sock Unix.TCP_NODELAY true;
MIO.Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout);
MIO.Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
Pool.with_resource self.slice_pool @@ fun ic_buf ->
Pool.with_resource self.slice_pool @@ fun oc_buf ->
let closed = ref false in
let oc =
new IO_helper.Output.of_unix_fd
~close_noerr:true ~closed ~buf:oc_buf client_sock
in
let ic =
IO_helper.Input.of_unix_fd ~close_noerr:true ~closed ~buf:ic_buf
client_sock
in
handle.handle ~client_addr ic oc
in
MIO.Unix.set_nonblock sock;
while Atomic.get self.running do
match MIO.Unix.accept sock with
| client_sock, client_addr ->
(* limit concurrency *)
Sem.acquire self.sem_max_connections;
self.new_thread (fun () ->
try
handle_client_ client_sock client_addr;
Log.debug (fun k ->
k "t[%d]: done with client on %s, exiting"
(Thread.id @@ Thread.self ())
@@ Util.show_sockaddr client_addr);
shutdown_silent_ client_sock;
close_silent_ client_sock;
Sem.release self.sem_max_connections
with e ->
let bt = Printexc.get_raw_backtrace () in
shutdown_silent_ client_sock;
close_silent_ client_sock;
Sem.release self.sem_max_connections;
Log.error (fun k ->
k
"@[<v>Handler: uncaught exception for client %s:@ %s@ \
%s@]"
(Util.show_sockaddr client_addr)
(Printexc.to_string e)
(Printexc.raw_backtrace_to_string bt)))
| exception e ->
Log.error (fun k ->
k "Unix.accept raised an exception: %s" (Printexc.to_string e));
Atomic.set self.running false
done;
(* Wait for all threads to be done: this only works if all threads are done. *)
MIO.Unix.close sock;
while Sem.get_value self.sem_max_connections < self.max_connections do
Sem.acquire self.sem_max_connections
done;
());
}

View file

@ -0,0 +1,52 @@
include Tiny_httpd
module Fd = Io_server.Fd
open struct
let get_max_connection_ ?(max_connections = 64) () : int =
let max_connections = max 4 max_connections in
max_connections
let clear_slice (slice : IO.Slice.t) =
Bytes.fill slice.bytes 0 (Bytes.length slice.bytes) '\x00';
slice.off <- 0;
slice.len <- 0
end
let create ?max_connections ?(timeout = 0.0) ?buf_size
?(get_time_s = Unix.gettimeofday) ?(addr = "127.0.0.1") ?(port = 8080)
?(sock : Fd.t option) ?middlewares ~(runner : Moonpool.Runner.t) () : t =
let new_thread f =
ignore (Moonpool_fib.spawn_top ~on:runner f : _ Moonpool_fib.t)
in
let max_connections = get_max_connection_ ?max_connections () in
let server =
{
Io_server.addr;
new_thread;
buf_pool =
Pool.create ~clear:Buf.clear_and_zero
~mk_item:(fun () -> Buf.create ?size:buf_size ())
();
slice_pool =
Pool.create ~clear:clear_slice
~mk_item:
(let buf_size = Option.value buf_size ~default:4096 in
fun () -> IO.Slice.create buf_size)
();
running = Atomic.make true;
port;
sock;
max_connections;
sem_max_connections = Io_server.Sem.make max_connections;
timeout;
}
in
let tcp_server_builder = Io_server.to_tcp_server server in
let module B = struct
let init_addr () = addr
let init_port () = port
let get_time_s = get_time_s
let tcp_server () = tcp_server_builder
end in
let backend = (module B : IO_BACKEND) in
Server.create_from ?buf_size ?middlewares ~backend ()

View file

@ -72,11 +72,6 @@ module Histogram : sig
val add : t -> float -> unit
end
(* TODO:
module Histogram : sig
end
*)
val http_middleware : Registry.t -> Server.Middleware.t
(** Middleware to get basic metrics about HTTP requests *)

View file

@ -1,3 +1,28 @@
; Set BUILD_TINY_HTTPD_OPTLEVEL to the -O<num> level.
; Defaults to 2, which means -O2 is the default C optimization flag.
; Use -1 to remove the -O<num> flag entirely.
(rule
(enabled_if (>= %{env:BUILD_TINY_HTTPD_OPTLEVEL=2} 0))
(target optlevel.string)
(deps (env_var BUILD_TINY_HTTPD_OPTLEVEL))
(action (with-stdout-to %{target} (echo "-O%{env:BUILD_TINY_HTTPD_OPTLEVEL=2}"))))
(rule
(enabled_if (< %{env:BUILD_TINY_HTTPD_OPTLEVEL=2} 0))
(target optlevel.string)
(deps (env_var BUILD_TINY_HTTPD_OPTLEVEL))
(action (with-stdout-to %{target} (echo ""))))
; All compilers will include the optimization level.
; Non-MSVC compilers will include `-std=c99 -fPIC`.
(rule
(enabled_if (= %{ocaml-config:ccomp_type} msvc))
(target cflags.sexp)
(action (with-stdout-to %{target} (echo "(%{read:optlevel.string})"))))
(rule
(enabled_if (not (= %{ocaml-config:ccomp_type} msvc)))
(target cflags.sexp)
(action (with-stdout-to %{target} (echo "(-std=c99 -fPIC %{read:optlevel.string})"))))
(library
(name tiny_httpd_ws)
(public_name tiny_httpd.ws)
@ -7,7 +32,7 @@
(foreign_stubs
(language c)
(names tiny_httpd_ws_stubs)
(flags :standard -std=c99 -fPIC -O2))
(flags :standard (:include cflags.sexp)))
(libraries
(re_export tiny_httpd.core)
threads))

View file

@ -19,6 +19,27 @@
(action
(diff echo1.expect echo1.out)))
(rule
(targets echo_mio1.out)
(deps
(:bin ../examples/echo_mio.exe))
(locks /port)
(enabled_if
(= %{system} "linux"))
(package tiny_httpd_moonpool)
(action
(with-stdout-to
%{targets}
(run ./echo_mio1.sh %{bin}))))
(rule
(alias runtest)
(package tiny_httpd_moonpool)
(enabled_if
(= %{system} "linux"))
(action
(diff echo_mio1.expect echo_mio1.out)))
(rule
(targets sse_count.out)
(deps

View file

@ -2,8 +2,8 @@ listening on http://127.0.0.1:8085
echo:
{meth=GET; host=localhost:8085;
headers=[user-agent: test
accept: */*
host: localhost:8085];
Accept: */*
Host: localhost:8085];
path="/echo/?a=b&c=d"; body=""; path_components=["echo"];
query=["c","d";"a","b"]}
(query: "c" = "d";"a" = "b")

10
tests/echo_mio1.expect Normal file
View file

@ -0,0 +1,10 @@
listening on http://127.0.0.1:8085
test moonpool_io
echo:
{meth=GET; host=localhost:8085;
headers=[user-agent: test
accept: */*
host: localhost:8085];
path="/echo/?a=b&c=d"; body=""; path_components=["echo"];
query=["c","d";"a","b"]}
(query: "c" = "d";"a" = "b")

12
tests/echo_mio1.sh Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env sh
ECHO=$1
PORT=8085
"$ECHO" -p $PORT &
PID=$!
sleep 0.1
echo "test moonpool_io"
curl -N "http://localhost:${PORT}/echo/?a=b&c=d" -H user-agent:test --max-time 5
kill $PID

View file

@ -1,4 +1,4 @@
(tests
(names t_util t_buf t_server t_io)
(names t_util t_buf t_server t_io t_response)
(package tiny_httpd)
(libraries tiny_httpd.core qcheck-core qcheck-core.runner test_util))

17
tests/unit/t_response.ml Normal file
View file

@ -0,0 +1,17 @@
open Test_util
open Tiny_httpd_core
module U = Util
let () =
let res =
Response.make_raw ~code:200 ~headers:[ "content-length", "42" ] ""
in
let h = Headers.get_exn "content-length" res.headers in
assert_eq "42" h
let () =
let res =
Response.make_raw ~code:200 ~headers:[ "Content-Length", "42" ] ""
in
let h = Headers.get_exn "content-length" res.headers in
assert_eq "42" h

View file

@ -5,7 +5,7 @@ if [ -f data ]; then rm data ; fi
SERVER=$1
PORT=8087
"$SERVER" . -p $PORT --upload --max-upload 100000000000 &
"$SERVER" . -p $PORT --upload --max-upload 1000M &
PID=$!
sleep 0.1

View file

@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.17"
version: "0.19"
synopsis: "Minimal HTTP server using threads"
maintainer: ["c-cube"]
authors: ["c-cube"]
@ -11,7 +11,7 @@ tags: [
homepage: "https://github.com/c-cube/tiny_httpd/"
bug-reports: "https://github.com/c-cube/tiny_httpd/issues"
depends: [
"dune" {>= "2.9"}
"dune" {>= "3.2"}
"seq"
"base-threads"
"result"
@ -38,11 +38,9 @@ build: [
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/c-cube/tiny_httpd.git"

View file

@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.17"
version: "0.19"
synopsis: "Interface to camlzip for tiny_httpd"
maintainer: ["c-cube"]
authors: ["c-cube"]
@ -8,7 +8,7 @@ license: "MIT"
homepage: "https://github.com/c-cube/tiny_httpd/"
bug-reports: "https://github.com/c-cube/tiny_httpd/issues"
depends: [
"dune" {>= "2.9"}
"dune" {>= "3.2"}
"tiny_httpd" {= version}
"camlzip" {>= "1.06"}
"iostream-camlzip" {>= "0.2.1"}
@ -24,11 +24,9 @@ build: [
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/c-cube/tiny_httpd.git"

35
tiny_httpd_moonpool.opam Normal file
View file

@ -0,0 +1,35 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.17"
synopsis: "Moonpool+picos_stdio backend for Tiny_httpd"
maintainer: ["c-cube"]
authors: ["c-cube"]
license: "MIT"
homepage: "https://github.com/c-cube/tiny_httpd/"
bug-reports: "https://github.com/c-cube/tiny_httpd/issues"
depends: [
"dune" {>= "2.9"}
"seq"
"tiny_httpd" {= version}
"moonpool" {>= "0.7"}
"moonpool-io" {>= "0.7"}
"ocaml" {>= "5.0"}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/c-cube/tiny_httpd.git"