Skip to content

Commit

Permalink
feat(h2-eio): track sw for gluten (#215)
Browse files Browse the repository at this point in the history
* feat(h2-eio): track `sw` for gluten

* dont require Eio.Flow.close

* wip

* wip

* wip

* fix: tests
  • Loading branch information
anmonteiro authored Jul 9, 2023
1 parent 827922d commit 4274321
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ocamlVersion: [4_13, 4_14, 5_00]
ocamlVersion: [4_14, 5_0, 5_1]
steps:
- uses: actions/checkout@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions eio/h2_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Server = struct
?(config = H2.Config.default)
~request_handler
~error_handler
~sw
client_addr
socket
=
Expand All @@ -47,6 +48,7 @@ module Server = struct
Gluten_eio.Server.create_connection_handler
~read_buffer_size:config.read_buffer_size
~protocol:(module H2.Server_connection)
~sw
connection
client_addr
socket
Expand Down
5 changes: 3 additions & 2 deletions eio/h2_eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ module Server : sig
-> request_handler:(Eio.Net.Sockaddr.stream -> H2.Reqd.t -> unit)
-> error_handler:
(Eio.Net.Sockaddr.stream -> H2.Server_connection.error_handler)
-> sw:Eio.Switch.t
-> Eio.Net.Sockaddr.stream
-> Eio.Flow.two_way
-> #Eio.Flow.two_way
-> unit
end

Expand All @@ -53,7 +54,7 @@ module Client : sig
(H2.Request.t -> (H2.Client_connection.response_handler, unit) result)
-> sw:Eio.Switch.t
-> error_handler:H2.Client_connection.error_handler
-> Eio.Flow.two_way
-> #Eio.Flow.two_way
-> t

val request :
Expand Down
4 changes: 3 additions & 1 deletion examples/alpn/unix/alpn_server_ssl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ let start_https_server () =
let server_ctx =
Ssl.create_context Ssl.TLSv1_3 Ssl.Server_context
in
Ssl.disable_protocols server_ctx [ Ssl.SSLv23; Ssl.TLSv1_1 ];
Ssl.disable_protocols
server_ctx
([ Ssl.SSLv23; Ssl.TLSv1_1 ] [@ocaml.alert "-deprecated"]);
Ssl.use_certificate server_ctx cert priv_key;
let protos = [ "h2"; "http/1.1" ] in
Ssl.set_context_alpn_protos server_ctx protos;
Expand Down
2 changes: 1 addition & 1 deletion examples/eio/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(executable
(name eio_get)
(libraries h2 h2-eio eio-ssl))
(libraries h2 h2-eio eio-ssl eio_main eio.unix))

(alias
(name examples)
Expand Down
40 changes: 26 additions & 14 deletions examples/eio/eio_get.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ let response_handler ~on_eof response response_body =
in
read_response ()

let error_handler err =
match err with
let error_handler u err =
(match err with
| `Exn exn -> Format.eprintf "wut %S@." (Printexc.to_string exn)
| `Invalid_response_body_length res ->
Format.eprintf "invalid res: %a@." Response.pp_hum res
| `Malformed_response str -> Format.eprintf "malformed %S@." str
| `Protocol_error (err, s) ->
Format.eprintf "wut %a %s@." H2.Error_code.pp_hum err s
Format.eprintf "wut %a %S@." H2.Error_code.pp_hum err s);
Eio.Promise.resolve u ()

let () =
let[@ocaml.alert "-deprecated"] () =
Ssl_threads.init ();
Ssl.init ~thread_safe:true ();
let host = ref None in
let port = ref 443 in
Arg.parse
[ "-p", Set_int port, " Port number (443 by default)" ]
(fun host_argument -> host := Some host_argument)
"lwt_get.exe [-p N] HOST";
"eio_get.exe [-p N] HOST";
let host =
match !host with
| None -> failwith "No hostname provided"
Expand All @@ -65,7 +66,7 @@ let () =
in
let addr =
let inet, port = List.hd addrs in
`Tcp (Eio_unix.Ipaddr.of_unix inet, port)
`Tcp (Eio_unix.Net.Ipaddr.of_unix inet, port)
in
let socket = Eio.Net.connect ~sw network addr in

Expand All @@ -74,30 +75,39 @@ let () =
`GET
"/"
~scheme:"https"
~headers:Headers.(add_list empty [ ":authority", host ])
~headers:
Headers.(
add_list
empty
[ "user-agent", "carl/0.0.0-experimental"
; ":authority", host
])
in

let ctx = Ssl.create_context Ssl.SSLv23 Ssl.Client_context in
Ssl.disable_protocols ctx [ Ssl.SSLv23 ];
(* Ssl.disable_protocols ctx [ Ssl.SSLv23 ]; *)
Ssl.honor_cipher_order ctx;
Ssl.set_context_alpn_protos ctx [ "h2" ];

Ssl.set_min_protocol_version ctx TLSv1_3;
Ssl.set_max_protocol_version ctx TLSv1_3;

let ssl_ctx = Eio_ssl.Context.create ~ctx socket in
let ssl_sock = Eio_ssl.Context.ssl_socket ssl_ctx in
Ssl.set_client_SNI_hostname ssl_sock host;
Ssl.set_hostflags ssl_sock [ No_partial_wildcards ];
Ssl.set_host ssl_sock host;
let ssl_sock = Eio_ssl.connect ssl_ctx in

let shut_p, shut_u = Eio.Promise.create () in
let error_handler = error_handler shut_u in
let connection =
Client.create_connection
~sw
~error_handler
(ssl_sock :> Eio.Flow.two_way)
Client.create_connection ~sw ~error_handler ssl_sock
in
let response_handler =
response_handler ~on_eof:(fun () ->
Format.eprintf "eof@.";
Client.shutdown connection)
Eio.Promise.resolve shut_u ())
in
let request_body =
Client.request
Expand All @@ -107,4 +117,6 @@ let () =
~response_handler
~flush_headers_immediately:true
in
Body.Writer.close request_body))
Body.Writer.close request_body;
Eio.Promise.await shut_p;
Eio.Promise.await (Client.shutdown connection)))
45 changes: 32 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

inputs.nix-filter.url = "github:numtide/nix-filter";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:anmonteiro/nix-overlays";
inputs.nixpkgs.url = "github:anmonteiro/nix-overlays/anmonteiro/update-gluten-piaf";
inputs.nixpkgs.inputs.flake-utils.follows = "flake-utils";

outputs = { self, nixpkgs, flake-utils, nix-filter }:
Expand Down
2 changes: 1 addition & 1 deletion nix/ci/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ stdenv.mkDerivation {
mkdir h2-tests
chmod u+w -R testdir-*
mv -n testdir-*/* h2-tests
mv --backup=numbered testdir-*/* h2-tests
'';
dontBuild = true;
installPhase = ''
Expand Down

0 comments on commit 4274321

Please sign in to comment.