Skip to content

Commit 163d720

Browse files
committed
Merge branch 'ocamlformat'
2 parents 61ee931 + aa27963 commit 163d720

37 files changed

Lines changed: 2232 additions & 2084 deletions

.ocamlformat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version = 0.24.1
2+
profile=conventional
3+
margin=80
4+
if-then-else=k-r
5+
parens-ite=true
6+
parens-tuple=multi-line-only
7+
sequence-style=terminator
8+
type-decl=compact
9+
break-cases=toplevel
10+
cases-exp-indent=2
11+
field-space=tight-decl
12+
leading-nested-match-parens=true
13+
module-item-spacing=compact
14+
quiet=true

examples/dune

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
1-
21
(executable
3-
(name sse_server)
4-
(modules sse_server)
5-
(libraries tiny_httpd unix ptime ptime.clock.os))
2+
(name sse_server)
3+
(modules sse_server)
4+
(libraries tiny_httpd unix ptime ptime.clock.os))
65

76
(executable
8-
(name sse_client)
9-
(modules sse_client)
10-
(libraries unix))
7+
(name sse_client)
8+
(modules sse_client)
9+
(libraries unix))
1110

1211
(executable
13-
(name echo)
14-
(flags :standard -warn-error -a+8)
15-
(modules echo vfs)
16-
(libraries tiny_httpd tiny_httpd_camlzip))
12+
(name echo)
13+
(flags :standard -warn-error -a+8)
14+
(modules echo vfs)
15+
(libraries tiny_httpd tiny_httpd_camlzip))
1716

1817
(rule
19-
(targets test_output.txt)
20-
(deps (:script ./run_test.sh) ./sse_client.exe ./sse_server.exe)
21-
(enabled_if (= %{system} "linux"))
22-
(package tiny_httpd)
23-
(action
24-
(with-stdout-to %{targets} (run %{script}))))
18+
(targets test_output.txt)
19+
(deps
20+
(:script ./run_test.sh)
21+
./sse_client.exe
22+
./sse_server.exe)
23+
(enabled_if
24+
(= %{system} "linux"))
25+
(package tiny_httpd)
26+
(action
27+
(with-stdout-to
28+
%{targets}
29+
(run %{script}))))
2530

2631
(rule
27-
(alias runtest)
28-
(package tiny_httpd)
29-
(enabled_if (= %{system} "linux"))
30-
(deps test_output.txt)
31-
(action
32-
(diff test_output.txt.expected test_output.txt)))
32+
(alias runtest)
33+
(package tiny_httpd)
34+
(enabled_if
35+
(= %{system} "linux"))
36+
(deps test_output.txt)
37+
(action
38+
(diff test_output.txt.expected test_output.txt)))
3339

3440
; produce an embedded FS
41+
3542
(rule
36-
(targets vfs.ml)
37-
(deps (source_tree files) (:out test_output.txt.expected))
38-
(enabled_if (= %{system} "linux"))
39-
(action (run %{bin:tiny-httpd-vfs-pack} -o %{targets}
40-
--mirror=files/
41-
--file=test_out.txt,%{out}
42-
; --url=example_dot_com,http://example.com ; this breaks tests in opam sandbox 😢
43-
)))
43+
(targets vfs.ml)
44+
(deps
45+
(source_tree files)
46+
(:out test_output.txt.expected))
47+
(enabled_if
48+
(= %{system} "linux"))
49+
(action
50+
(run
51+
%{bin:tiny-httpd-vfs-pack}
52+
-o
53+
%{targets}
54+
--mirror=files/
55+
--file=test_out.txt,%{out}
56+
; --url=example_dot_com,http://example.com ; this breaks tests in opam sandbox 😢
57+
)))
4458

4559
(rule
46-
(targets vfs.ml)
47-
(enabled_if (<> %{system} "linux"))
48-
(action
49-
(with-stdout-to
50-
%{targets}
51-
(progn
52-
(echo "let embedded_fs = Tiny_httpd_dir.Embedded_fs.create ~mtime:0. ()")
53-
(echo "let vfs = Tiny_httpd_dir.Embedded_fs.to_vfs embedded_fs")))))
60+
(targets vfs.ml)
61+
(enabled_if
62+
(<> %{system} "linux"))
63+
(action
64+
(with-stdout-to
65+
%{targets}
66+
(progn
67+
(echo "let embedded_fs = Tiny_httpd_dir.Embedded_fs.create ~mtime:0. ()")
68+
(echo "let vfs = Tiny_httpd_dir.Embedded_fs.to_vfs embedded_fs")))))

examples/echo.ml

Lines changed: 114 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
module S = Tiny_httpd
32

43
let now_ = Unix.gettimeofday
@@ -22,118 +21,162 @@ let middleware_stat () : S.Middleware.t * (unit -> string) =
2221
total_time_ := !total_time_ +. (t4 -. t1);
2322
parse_time_ := !parse_time_ +. (t2 -. t1);
2423
build_time_ := !build_time_ +. (t3 -. t2);
25-
write_time_ := !write_time_ +. (t4 -. t3);
26-
)
24+
write_time_ := !write_time_ +. (t4 -. t3))
2725
and get_stat () =
28-
Printf.sprintf "%d requests (average response time: %.3fms = %.3fms + %.3fms + %.3fms)"
29-
!n_req (!total_time_ /. float !n_req *. 1e3)
30-
(!parse_time_ /. float !n_req *. 1e3)
31-
(!build_time_ /. float !n_req *. 1e3)
32-
(!write_time_ /. float !n_req *. 1e3)
26+
Printf.sprintf
27+
"%d requests (average response time: %.3fms = %.3fms + %.3fms + %.3fms)"
28+
!n_req
29+
(!total_time_ /. float !n_req *. 1e3)
30+
(!parse_time_ /. float !n_req *. 1e3)
31+
(!build_time_ /. float !n_req *. 1e3)
32+
(!write_time_ /. float !n_req *. 1e3)
3333
in
3434
m, get_stat
3535

36-
3736
let () =
3837
let port_ = ref 8080 in
3938
let j = ref 32 in
40-
Arg.parse (Arg.align [
41-
"--port", Arg.Set_int port_, " set port";
42-
"-p", Arg.Set_int port_, " set port";
43-
"--debug", Arg.Unit (fun () -> S._enable_debug true), " enable debug";
44-
"-j", Arg.Set_int j, " maximum number of connections";
45-
]) (fun _ -> raise (Arg.Bad "")) "echo [option]*";
39+
Arg.parse
40+
(Arg.align
41+
[
42+
"--port", Arg.Set_int port_, " set port";
43+
"-p", Arg.Set_int port_, " set port";
44+
"--debug", Arg.Unit (fun () -> S._enable_debug true), " enable debug";
45+
"-j", Arg.Set_int j, " maximum number of connections";
46+
])
47+
(fun _ -> raise (Arg.Bad ""))
48+
"echo [option]*";
4649

4750
let server = S.create ~port:!port_ ~max_connections:!j () in
48-
Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16*1024) server;
51+
Tiny_httpd_camlzip.setup ~compress_above:1024 ~buf_size:(16 * 1024) server;
4952

5053
let m_stats, get_stats = middleware_stat () in
5154
S.add_middleware server ~stage:(`Stage 1) m_stats;
5255

5356
(* say hello *)
5457
S.add_route_handler ~meth:`GET server
5558
S.Route.(exact "hello" @/ string @/ return)
56-
(fun name _req -> S.Response.make_string (Ok ("hello " ^name ^"!\n")));
59+
(fun name _req -> S.Response.make_string (Ok ("hello " ^ name ^ "!\n")));
5760

5861
(* compressed file access *)
5962
S.add_route_handler ~meth:`GET server
6063
S.Route.(exact "zcat" @/ string_urlencoded @/ return)
6164
(fun path _req ->
62-
let ic = open_in path in
63-
let str = S.Byte_stream.of_chan ic in
64-
let mime_type =
65+
let ic = open_in path in
66+
let str = S.Byte_stream.of_chan ic in
67+
let mime_type =
68+
try
69+
let p = Unix.open_process_in (Printf.sprintf "file -i -b %S" path) in
6570
try
66-
let p = Unix.open_process_in (Printf.sprintf "file -i -b %S" path) in
67-
try
68-
let s = ["Content-Type", String.trim (input_line p)] in
69-
ignore @@ Unix.close_process_in p;
70-
s
71-
with _ -> ignore @@ Unix.close_process_in p; []
72-
with _ -> []
73-
in
74-
S.Response.make_stream ~headers:mime_type (Ok str)
75-
);
71+
let s = [ "Content-Type", String.trim (input_line p) ] in
72+
ignore @@ Unix.close_process_in p;
73+
s
74+
with _ ->
75+
ignore @@ Unix.close_process_in p;
76+
[]
77+
with _ -> []
78+
in
79+
S.Response.make_stream ~headers:mime_type (Ok str));
7680

7781
(* echo request *)
7882
S.add_route_handler server
7983
S.Route.(exact "echo" @/ return)
8084
(fun req ->
81-
let q =
82-
S.Request.query req |> List.map (fun (k,v) -> Printf.sprintf "%S = %S" k v)
83-
|> String.concat ";"
84-
in
85-
S.Response.make_string
86-
(Ok (Format.asprintf "echo:@ %a@ (query: %s)@." S.Request.pp req q)));
85+
let q =
86+
S.Request.query req
87+
|> List.map (fun (k, v) -> Printf.sprintf "%S = %S" k v)
88+
|> String.concat ";"
89+
in
90+
S.Response.make_string
91+
(Ok (Format.asprintf "echo:@ %a@ (query: %s)@." S.Request.pp req q)));
8792

8893
(* file upload *)
8994
S.add_route_handler_stream ~meth:`PUT server
9095
S.Route.(exact "upload" @/ string @/ return)
9196
(fun path req ->
92-
S._debug (fun k->k "start upload %S, headers:\n%s\n\n%!" path
93-
(Format.asprintf "%a" S.Headers.pp (S.Request.headers req)));
94-
try
95-
let oc = open_out @@ "/tmp/" ^ path in
96-
S.Byte_stream.to_chan oc req.S.Request.body;
97-
flush oc;
98-
S.Response.make_string (Ok "uploaded file")
99-
with e ->
100-
S.Response.fail ~code:500 "couldn't upload file: %s" (Printexc.to_string e)
101-
);
97+
S._debug (fun k ->
98+
k "start upload %S, headers:\n%s\n\n%!" path
99+
(Format.asprintf "%a" S.Headers.pp (S.Request.headers req)));
100+
try
101+
let oc = open_out @@ "/tmp/" ^ path in
102+
S.Byte_stream.to_chan oc req.S.Request.body;
103+
flush oc;
104+
S.Response.make_string (Ok "uploaded file")
105+
with e ->
106+
S.Response.fail ~code:500 "couldn't upload file: %s"
107+
(Printexc.to_string e));
102108

103109
(* stats *)
104-
S.add_route_handler server S.Route.(exact "stats" @/ return)
110+
S.add_route_handler server
111+
S.Route.(exact "stats" @/ return)
105112
(fun _req ->
106-
let stats = get_stats() in
107-
S.Response.make_string @@ Ok stats
108-
);
113+
let stats = get_stats () in
114+
S.Response.make_string @@ Ok stats);
109115

110116
(* VFS *)
111117
Tiny_httpd_dir.add_vfs server
112-
~config:(Tiny_httpd_dir.config ~download:true
113-
~dir_behavior:Tiny_httpd_dir.Index_or_lists ())
118+
~config:
119+
(Tiny_httpd_dir.config ~download:true
120+
~dir_behavior:Tiny_httpd_dir.Index_or_lists ())
114121
~vfs:Vfs.vfs ~prefix:"vfs";
115122

116123
(* main page *)
117-
S.add_route_handler server S.Route.(return)
124+
S.add_route_handler server
125+
S.Route.(return)
118126
(fun _req ->
119-
let open Tiny_httpd_html in
120-
let h = html [] [
121-
head[][title[][txt "index of echo"]];
122-
body[][
123-
h3[] [txt "welcome!"];
124-
p[] [b[] [txt "endpoints are:"]];
125-
ul[] [
126-
li[][pre[][txt "/hello/:name (GET)"]];
127-
li[][pre[][a[A.href "/echo/"][txt "echo"]; txt " echo back query"]];
128-
li[][pre[][txt "/upload/:path (PUT) to upload a file"]];
129-
li[][pre[][txt "/zcat/:path (GET) to download a file (deflate transfer-encoding)"]];
130-
li[][pre[][a[A.href "/stats/"][txt"/stats/"]; txt" (GET) to access statistics"]];
131-
li[][pre[][a[A.href "/vfs/"][txt"/vfs"]; txt" (GET) to access a VFS embedded in the binary"]];
132-
]
133-
]
134-
] in
135-
let s = to_string_top h in
136-
S.Response.make_string ~headers:["content-type", "text/html"] @@ Ok s);
127+
let open Tiny_httpd_html in
128+
let h =
129+
html []
130+
[
131+
head [] [ title [] [ txt "index of echo" ] ];
132+
body []
133+
[
134+
h3 [] [ txt "welcome!" ];
135+
p [] [ b [] [ txt "endpoints are:" ] ];
136+
ul []
137+
[
138+
li [] [ pre [] [ txt "/hello/:name (GET)" ] ];
139+
li []
140+
[
141+
pre []
142+
[
143+
a [ A.href "/echo/" ] [ txt "echo" ];
144+
txt " echo back query";
145+
];
146+
];
147+
li []
148+
[ pre [] [ txt "/upload/:path (PUT) to upload a file" ] ];
149+
li []
150+
[
151+
pre []
152+
[
153+
txt
154+
"/zcat/:path (GET) to download a file (deflate \
155+
transfer-encoding)";
156+
];
157+
];
158+
li []
159+
[
160+
pre []
161+
[
162+
a [ A.href "/stats/" ] [ txt "/stats/" ];
163+
txt " (GET) to access statistics";
164+
];
165+
];
166+
li []
167+
[
168+
pre []
169+
[
170+
a [ A.href "/vfs/" ] [ txt "/vfs" ];
171+
txt " (GET) to access a VFS embedded in the binary";
172+
];
173+
];
174+
];
175+
];
176+
]
177+
in
178+
let s = to_string_top h in
179+
S.Response.make_string ~headers:[ "content-type", "text/html" ] @@ Ok s);
137180

138181
Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server);
139182
match S.run server with

examples/sse_client.ml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
let addr = ref "127.0.0.1"
22
let port = ref 8080
33
let path = ref "/clock"
4-
54
let bufsize = 1024
65

76
let () =
8-
Arg.parse (Arg.align [
9-
"-h", Arg.Set_string addr, " address to connect to";
10-
"-p", Arg.Set_int port, " port to connect to";
11-
"--alarm", Arg.Int (fun i->Unix.alarm i|>ignore), " set alarm (in seconds)";
12-
]) (fun s -> path := s) "sse_client [opt]* path?";
7+
Arg.parse
8+
(Arg.align
9+
[
10+
"-h", Arg.Set_string addr, " address to connect to";
11+
"-p", Arg.Set_int port, " port to connect to";
12+
( "--alarm",
13+
Arg.Int (fun i -> Unix.alarm i |> ignore),
14+
" set alarm (in seconds)" );
15+
])
16+
(fun s -> path := s)
17+
"sse_client [opt]* path?";
1318

1419
Format.printf "connect to %s:%d@." !addr !port;
1520
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
@@ -25,7 +30,8 @@ let () =
2530
let buf = Bytes.create bufsize in
2631
while !continue do
2732
let n = input ic buf 0 bufsize in
28-
if n=0 then continue := false;
29-
output stdout buf 0 n; flush stdout
33+
if n = 0 then continue := false;
34+
output stdout buf 0 n;
35+
flush stdout
3036
done;
3137
Format.printf "exit!@."

0 commit comments

Comments
 (0)