Repository navigation
Expand file tree
/
Copy patheio_example.ml
More file actions
43 lines (42 loc) · 1.67 KB
/
Copy patheio_example.ml
File metadata and controls
43 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module A = Httpkit_transport_eio
module E = Httpkit_engine
let () =
Eio_main.run (fun env ->
Eio.Switch.run (fun sw ->
let a, b = Eio_unix.Net.socketpair_stream ~sw () in
let clock = Eio.Stdenv.mono_clock env in
Eio.Fiber.both
(fun () ->
A.with_connection ~clock (A.of_flow a)
(Result.get_ok (E.server ()))
(fun c ->
let id, request =
match A.next_event c with
| E.Request (id, r) -> (id, r)
| _ -> assert false
in
ignore (A.collect_body c id);
let response = Transform.handle request in
A.respond c id response;
A.send c id (Httpkit_core.Response.body response);
A.finish c id))
(fun () ->
A.with_connection ~clock (A.of_flow b)
(Result.get_ok (E.client ()))
(fun c ->
let open Httpkit_core in
let request =
Request.create ~meth:Method.get
~target:(Result.get_ok (Target.of_string "/"))
~headers:
(Result.get_ok
(Headers.of_list [ ("host", "localhost") ]))
()
in
let id = A.submit_request c request in
A.finish c id;
(match A.next_event c with
| E.Response _ -> ()
| _ -> assert false);
let body, _ = A.collect_body c id in
print_string body))))