Skip to content

Commit 3658895

Browse files
committed
format code
1 parent 2a7e597 commit 3658895

30 files changed

+525
-522
lines changed

examples/dune

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
(name echo)
1313
(flags :standard -warn-error -a+8)
1414
(modules echo vfs)
15-
(libraries tiny_httpd logs tiny_httpd_camlzip tiny_httpd.multipart-form-data))
15+
(libraries
16+
tiny_httpd
17+
logs
18+
tiny_httpd_camlzip
19+
tiny_httpd.multipart-form-data))
1620

1721
(executable
1822
(name writer)

examples/echo.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ let () =
142142
"-p", Arg.Set_int port_, " set port";
143143
"--debug", Arg.Unit setup_logging, " enable debug";
144144
"-j", Arg.Set_int j, " maximum number of connections";
145-
"--addr", Arg.Set_string addr, " binding address";
145+
"--addr", Arg.Set_string addr, " binding address";
146146
])
147147
(fun _ -> raise (Arg.Bad ""))
148148
"echo [option]*";
149149

150-
let server = Tiny_httpd.create ~addr:!addr ~port:!port_ ~max_connections:!j () in
150+
let server =
151+
Tiny_httpd.create ~addr:!addr ~port:!port_ ~max_connections:!j ()
152+
in
151153

152154
Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16 * 1024) server;
153155
let m_stats, get_stats = middleware_stat () in

examples/echo_ws.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ let setup_logging ~debug () =
66
Logs.set_level ~all:true
77
@@ Some
88
(if debug then
9-
Logs.Debug
10-
else
11-
Logs.Info)
9+
Logs.Debug
10+
else
11+
Logs.Info)
1212

1313
let handle_ws (req : unit Request.t) ic oc =
1414
Log.info (fun k ->

examples/sse_server.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ let () =
3636
EV.send_event
3737
~event:
3838
(if !tick then
39-
"tick"
40-
else
41-
"tock")
39+
"tick"
40+
else
41+
"tock")
4242
~data:(Ptime.to_rfc3339 now) ();
4343
tick := not !tick;
4444

src/Tiny_httpd.mli

Lines changed: 86 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,80 @@
11
(** Tiny Http Server
22
33
This library implements a very simple, basic HTTP/1.1 server using blocking
4-
IOs and threads. Basic routing based is provided for convenience,
5-
so that several handlers can be registered.
4+
IOs and threads. Basic routing based is provided for convenience, so that
5+
several handlers can be registered.
66
77
It is possible to use a thread pool, see {!create}'s argument [new_thread].
88
99
The [echo] example (see [src/examples/echo.ml]) demonstrates some of the
1010
features by declaring a few endpoints, including one for uploading files:
1111
1212
{[
13-
module S = Tiny_httpd
14-
15-
let () =
16-
let server = S.create () in
17-
18-
(* say hello *)
19-
S.add_route_handler ~meth:`GET server
20-
S.Route.(exact "hello" @/ string @/ return)
21-
(fun name _req -> S.Response.make_string (Ok ("hello " ^name ^"!\n")));
22-
23-
(* echo request *)
24-
S.add_route_handler server
25-
S.Route.(exact "echo" @/ return)
26-
(fun req -> S.Response.make_string
27-
(Ok (Format.asprintf "echo:@ %a@." S.Request.pp req)));
28-
29-
(* file upload *)
30-
S.add_route_handler ~meth:`PUT server
31-
S.Route.(exact "upload" @/ string_urlencoded @/ return)
32-
(fun path req ->
33-
try
34-
let oc = open_out @@ "/tmp/" ^ path in
35-
output_string oc req.S.Request.body;
36-
flush oc;
37-
S.Response.make_string (Ok "uploaded file")
38-
with e ->
39-
S.Response.fail ~code:500 "couldn't upload file: %s"
40-
(Printexc.to_string e)
41-
);
42-
43-
(* run the server *)
44-
Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server);
45-
match S.run server with
46-
| Ok () -> ()
47-
| Error e -> raise e
13+
module S = Tiny_httpd
14+
15+
let () =
16+
let server = S.create () in
17+
18+
(* say hello *)
19+
S.add_route_handler ~meth:`GET server
20+
S.Route.(exact "hello" @/ string @/ return)
21+
(fun name _req ->
22+
S.Response.make_string (Ok ("hello " ^ name ^ "!\n")));
23+
24+
(* echo request *)
25+
S.add_route_handler server
26+
S.Route.(exact "echo" @/ return)
27+
(fun req ->
28+
S.Response.make_string
29+
(Ok (Format.asprintf "echo:@ %a@." S.Request.pp req)));
30+
31+
(* file upload *)
32+
S.add_route_handler ~meth:`PUT server
33+
S.Route.(exact "upload" @/ string_urlencoded @/ return)
34+
(fun path req ->
35+
try
36+
let oc = open_out @@ "/tmp/" ^ path in
37+
output_string oc req.S.Request.body;
38+
flush oc;
39+
S.Response.make_string (Ok "uploaded file")
40+
with e ->
41+
S.Response.fail ~code:500 "couldn't upload file: %s"
42+
(Printexc.to_string e));
43+
44+
(* run the server *)
45+
Printf.printf "listening on http://%s:%d\n%!" (S.addr server)
46+
(S.port server);
47+
match S.run server with
48+
| Ok () -> ()
49+
| Error e -> raise e
4850
]}
4951
5052
It is then possible to query it using curl:
5153
5254
{[
53-
$ dune exec src/examples/echo.exe &
54-
listening on http://127.0.0.1:8080
55-
56-
# the path "hello/name" greets you.
57-
$ curl -X GET http://localhost:8080/hello/quadrarotaphile
58-
hello quadrarotaphile!
59-
60-
# the path "echo" just prints the request.
61-
$ curl -X GET http://localhost:8080/echo --data "howdy y'all"
62-
echo:
63-
{meth=GET;
64-
headers=Host: localhost:8080
65-
User-Agent: curl/7.66.0
66-
Accept: */*
67-
Content-Length: 10
68-
Content-Type: application/x-www-form-urlencoded;
69-
path="/echo"; body="howdy y'all"}
70-
71-
72-
]}
73-
74-
*)
55+
$ dune exec src/examples/echo.exe &
56+
listening on http://127.0.0.1:8080
57+
58+
# the path "hello/name" greets you.
59+
$ curl -X GET http://localhost:8080/hello/quadrarotaphile
60+
hello quadrarotaphile!
61+
62+
# the path "echo" just prints the request.
63+
$ curl -X GET http://localhost:8080/echo --data "howdy y'all"
64+
echo:
65+
{meth=GET;
66+
headers=Host: localhost:8080
67+
User-Agent: curl/7.66.0
68+
Accept: */*
69+
Content-Length: 10
70+
Content-Type: application/x-www-form-urlencoded;
71+
path="/echo"; body="howdy y'all"}
72+
]} *)
7573

7674
(** {2 Tiny buffer implementation}
7775
7876
These buffers are used to avoid allocating too many byte arrays when
79-
processing streams and parsing requests.
80-
*)
77+
processing streams and parsing requests. *)
8178

8279
module Buf = Buf
8380

@@ -141,37 +138,42 @@ val create :
141138
t
142139
(** Create a new webserver using UNIX abstractions.
143140
144-
The server will not do anything until {!run} is called on it.
145-
Before starting the server, one can use {!add_path_handler} and
146-
{!set_top_handler} to specify how to handle incoming requests.
141+
The server will not do anything until {!run} is called on it. Before
142+
starting the server, one can use {!add_path_handler} and {!set_top_handler}
143+
to specify how to handle incoming requests.
147144
148-
@param masksigpipe if true, block the signal {!Sys.sigpipe} which otherwise
149-
tends to kill client threads when they try to write on broken sockets.
150-
Default: [true] except when on Windows, which defaults to [false].
145+
@param masksigpipe
146+
if true, block the signal {!Sys.sigpipe} which otherwise tends to kill
147+
client threads when they try to write on broken sockets. Default: [true]
148+
except when on Windows, which defaults to [false].
151149
152150
@param buf_size size for buffers (since 0.11)
153151
154-
@param new_thread a function used to spawn a new thread to handle a
155-
new client connection. By default it is {!Thread.create} but one
156-
could use a thread pool instead.
157-
See for example {{: https://github.com/c-cube/tiny-httpd-moonpool-bench/blob/0dcbbffb4fe34ea4ad79d46343ad0cebb69ca69f/examples/t1.ml#L31}
158-
this use of moonpool}.
152+
@param new_thread
153+
a function used to spawn a new thread to handle a new client connection.
154+
By default it is {!Thread.create} but one could use a thread pool instead.
155+
See for example
156+
{{:https://github.com/c-cube/tiny-httpd-moonpool-bench/blob/0dcbbffb4fe34ea4ad79d46343ad0cebb69ca69f/examples/t1.ml#L31}
157+
this use of moonpool}.
159158
160159
@param middlewares see {!add_middleware} for more details.
161160
162161
@param max_connections maximum number of simultaneous connections.
163-
@param timeout connection is closed if the socket does not do read or
164-
write for the amount of second. Default: 0.0 which means no timeout.
165-
timeout is not recommended when using proxy.
162+
@param timeout
163+
connection is closed if the socket does not do read or write for the
164+
amount of second. Default: 0.0 which means no timeout. timeout is not
165+
recommended when using proxy.
166166
@param addr address (IPv4 or IPv6) to listen on. Default ["127.0.0.1"].
167167
@param port to listen on. Default [8080].
168-
@param sock an existing socket given to the server to listen on, e.g. by
169-
systemd on Linux (or launchd on macOS). If passed in, this socket will be
170-
used instead of the [addr] and [port]. If not passed in, those will be
171-
used. This parameter exists since 0.10.
172-
@param enable_logging if true and [Logs] is installed, log requests. Default true.
173-
This parameter exists since 0.18. Does not affect debug-level logs.
174-
175-
@param get_time_s obtain the current timestamp in seconds.
176-
This parameter exists since 0.11.
168+
@param sock
169+
an existing socket given to the server to listen on, e.g. by systemd on
170+
Linux (or launchd on macOS). If passed in, this socket will be used
171+
instead of the [addr] and [port]. If not passed in, those will be used.
172+
This parameter exists since 0.10.
173+
@param enable_logging
174+
if true and [Logs] is installed, log requests. Default true. This
175+
parameter exists since 0.18. Does not affect debug-level logs.
176+
177+
@param get_time_s
178+
obtain the current timestamp in seconds. This parameter exists since 0.11.
177179
*)

src/bin/curly.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Result = struct
22
include Result
33

4-
let ( >>= ) :
5-
type a b e. (a, e) result -> (a -> (b, e) result) -> (b, e) result =
4+
let ( >>= ) : type a b e.
5+
(a, e) result -> (a -> (b, e) result) -> (b, e) result =
66
fun r f ->
77
match r with
88
| Ok x -> f x
@@ -121,9 +121,9 @@ module Request = struct
121121
Header.to_cmd t.headers;
122122
[ t.url ];
123123
(if has_body t then
124-
[ "--data-binary"; "@-" ]
125-
else
126-
[]);
124+
[ "--data-binary"; "@-" ]
125+
else
126+
[]);
127127
]
128128

129129
let pp fmt t =

src/camlzip/Tiny_httpd_camlzip.mli

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
(** Middleware for compression.
22
3-
This uses camlzip to provide deflate compression/decompression.
4-
If installed, the middleware will compress responses' bodies
5-
when they are streams or fixed-size above a given limit
6-
(but it will not compress small, fixed-size bodies).
7-
*)
3+
This uses camlzip to provide deflate compression/decompression. If
4+
installed, the middleware will compress responses' bodies when they are
5+
streams or fixed-size above a given limit (but it will not compress small,
6+
fixed-size bodies). *)
87

98
val middleware :
109
?compress_above:int -> ?buf_size:int -> unit -> Server.Middleware.t
1110
(** Middleware responsible for deflate compression/decompression.
12-
@param compress_above threshold, in bytes, above which a response body
13-
that has a known content-length is compressed. Stream bodies
14-
are always compressed.
11+
@param compress_above
12+
threshold, in bytes, above which a response body that has a known
13+
content-length is compressed. Stream bodies are always compressed.
1514
@param buf_size size of the underlying buffer for compression/decompression
1615
@since 0.11 *)
1716

1817
val setup : ?compress_above:int -> ?buf_size:int -> Server.t -> unit
19-
(** Install middleware for tiny_httpd to be able to encode/decode
20-
compressed streams
18+
(** Install middleware for tiny_httpd to be able to encode/decode compressed
19+
streams
2120
@param compress_above threshold above with string responses are compressed
22-
@param buf_size size of the underlying buffer for compression/decompression *)
21+
@param buf_size size of the underlying buffer for compression/decompression
22+
*)

0 commit comments

Comments
 (0)