Skip to content

Commit 188c21c

Browse files
authored
Merge branch 'c-cube:master' into client-ip
2 parents 1e50abb + 5721689 commit 188c21c

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/Tiny_httpd_server.ml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,6 @@ module Request = struct
351351
end
352352
end
353353
354-
(*$R
355-
let q = "GET hello HTTP/1.1\r\nHost: coucou\r\nContent-Length: 11\r\n\r\nsalutationsSOMEJUNK" in
356-
let str = Tiny_httpd.Byte_stream.of_string q in
357-
let r = Request.Internal_.parse_req_start ~get_time_s:(fun _ -> 0.) str in
358-
match r with
359-
| None -> assert_failure "should parse"
360-
| Some req ->
361-
assert_equal (Some "coucou") (Headers.get "Host" req.Request.headers);
362-
assert_equal (Some "coucou") (Headers.get "host" req.Request.headers);
363-
assert_equal (Some "11") (Headers.get "content-length" req.Request.headers);
364-
assert_equal "hello" req.Request.path;
365-
let req = Request.Internal_.parse_body req str |> Request.read_body_full in
366-
assert_equal ~printer:(fun s->s) "salutations" req.Request.body;
367-
()
368-
*)
369-
370354
module Response = struct
371355
type body =
372356
[ `String of string
@@ -958,20 +942,28 @@ module Unix_tcp_server_ = struct
958942
try
959943
let client_sock, client_addr = Unix.accept sock in
960944
Unix.setsockopt client_sock Unix.TCP_NODELAY true;
945+
(* Block INT/HUP while cloning to avoid children handling them.
946+
When thread gets them, our Unix.accept raises neatly. *)
947+
ignore Unix.(sigprocmask SIG_BLOCK Sys.[ sigint; sighup ]);
961948
self.new_thread (fun () ->
962949
try
963950
handle_client_unix_ client_sock client_addr;
964951
Sem_.release 1 self.sem_max_connections
965952
with e ->
966953
(try Unix.close client_sock with _ -> ());
967954
Sem_.release 1 self.sem_max_connections;
968-
raise e)
955+
raise e);
956+
ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ])
969957
with e ->
970958
Sem_.release 1 self.sem_max_connections;
971959
_debug (fun k ->
972960
k "Unix.accept or Thread.create raised an exception: %s"
973961
(Printexc.to_string e))
974962
done;
963+
964+
(* Wait for all threads to be done: this only works if all threads are done. *)
965+
Unix.close sock;
966+
Sem_.acquire self.sem_max_connections.max self.sem_max_connections;
975967
());
976968
}
977969
end

tests/unit/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
(tests
3-
(names t_util t_buf)
3+
(names t_util t_buf t_server)
44
(package tiny_httpd)
55
(libraries tiny_httpd qcheck-core qcheck-core.runner test_util))

tests/unit/t_server.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
open Test_util
2+
open Tiny_httpd_server
3+
4+
let () =
5+
let q =
6+
"GET hello HTTP/1.1\r\n\
7+
Host: coucou\r\n\
8+
Content-Length: 11\r\n\
9+
\r\n\
10+
salutationsSOMEJUNK"
11+
in
12+
let str = Tiny_httpd.Byte_stream.of_string q in
13+
let r = Request.Internal_.parse_req_start ~get_time_s:(fun _ -> 0.) str in
14+
match r with
15+
| None -> failwith "should parse"
16+
| Some req ->
17+
assert_eq (Some "coucou") (Headers.get "Host" req.Request.headers);
18+
assert_eq (Some "coucou") (Headers.get "host" req.Request.headers);
19+
assert_eq (Some "11") (Headers.get "content-length" req.Request.headers);
20+
assert_eq "hello" req.Request.path;
21+
let req = Request.Internal_.parse_body req str |> Request.read_body_full in
22+
assert_eq ~to_string:(fun s -> s) "salutations" req.Request.body;
23+
()

0 commit comments

Comments
 (0)