From 4274321af21bcebda11d2cdc5cafa146bbfdb912 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 8 Jul 2023 21:26:13 -0700 Subject: [PATCH] feat(h2-eio): track `sw` for gluten (#215) * feat(h2-eio): track `sw` for gluten * dont require Eio.Flow.close * wip * wip * wip * fix: tests --- .github/workflows/test.yml | 2 +- eio/h2_eio.ml | 2 ++ eio/h2_eio.mli | 5 +-- examples/alpn/unix/alpn_server_ssl.ml | 4 ++- examples/eio/dune | 2 +- examples/eio/eio_get.ml | 40 +++++++++++++++--------- flake.lock | 45 +++++++++++++++++++-------- flake.nix | 2 +- nix/ci/test.nix | 2 +- 9 files changed, 70 insertions(+), 34 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c37964de..500a60ab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/eio/h2_eio.ml b/eio/h2_eio.ml index 43705778..b3b3b7f0 100644 --- a/eio/h2_eio.ml +++ b/eio/h2_eio.ml @@ -35,6 +35,7 @@ module Server = struct ?(config = H2.Config.default) ~request_handler ~error_handler + ~sw client_addr socket = @@ -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 diff --git a/eio/h2_eio.mli b/eio/h2_eio.mli index a1beefb2..9e949a80 100644 --- a/eio/h2_eio.mli +++ b/eio/h2_eio.mli @@ -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 @@ -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 : diff --git a/examples/alpn/unix/alpn_server_ssl.ml b/examples/alpn/unix/alpn_server_ssl.ml index f7891e1b..5e96eaf9 100644 --- a/examples/alpn/unix/alpn_server_ssl.ml +++ b/examples/alpn/unix/alpn_server_ssl.ml @@ -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; diff --git a/examples/eio/dune b/examples/eio/dune index 985debb3..e86b3a3b 100644 --- a/examples/eio/dune +++ b/examples/eio/dune @@ -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) diff --git a/examples/eio/eio_get.ml b/examples/eio/eio_get.ml index d9b4f0a3..d5fabd05 100644 --- a/examples/eio/eio_get.ml +++ b/examples/eio/eio_get.ml @@ -22,16 +22,17 @@ 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 @@ -39,7 +40,7 @@ let () = 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" @@ -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 @@ -74,13 +75,23 @@ 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; @@ -88,16 +99,15 @@ let () = 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 @@ -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))) diff --git a/flake.lock b/flake.lock index 307dd40b..1a0fe38b 100644 --- a/flake.lock +++ b/flake.lock @@ -1,12 +1,15 @@ { "nodes": { "flake-utils": { + "inputs": { + "systems": "systems" + }, "locked": { - "lastModified": 1678901627, - "narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=", + "lastModified": 1687709756, + "narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=", "owner": "numtide", "repo": "flake-utils", - "rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6", + "rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7", "type": "github" }, "original": { @@ -17,11 +20,11 @@ }, "nix-filter": { "locked": { - "lastModified": 1678109515, - "narHash": "sha256-C2X+qC80K2C1TOYZT8nabgo05Dw2HST/pSn6s+n6BO8=", + "lastModified": 1687178632, + "narHash": "sha256-HS7YR5erss0JCaUijPeyg2XrisEb959FIct3n2TMGbE=", "owner": "numtide", "repo": "nix-filter", - "rev": "aa9ff6ce4a7f19af6415fb3721eaa513ea6c763c", + "rev": "d90c75e8319d0dd9be67d933d8eb9d0894ec9174", "type": "github" }, "original": { @@ -38,32 +41,33 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1679257633, - "narHash": "sha256-b1nQZAofP8Gkxge51j0O/F/LM8wH0bMCNZVDXr5k2Rs=", + "lastModified": 1688874406, + "narHash": "sha256-D+cbqCLZ7bOlSbf+lshMuBwQ5uXcp8IG/4tAfA32KjY=", "owner": "anmonteiro", "repo": "nix-overlays", - "rev": "d767c2bd906de48e16c2a9610f81ac69d91114d7", + "rev": "7f1558c4fef22bd20cbe636d9f86942281bf24f2", "type": "github" }, "original": { "owner": "anmonteiro", + "ref": "anmonteiro/update-gluten-piaf", "repo": "nix-overlays", "type": "github" } }, "nixpkgs_2": { "locked": { - "lastModified": 1679220753, - "narHash": "sha256-PTIvwZPtF1/IuJL8PAlqJ13lpRPCdzwiTKHidE8VvkE=", + "lastModified": 1688784496, + "narHash": "sha256-s/EWMtzAJhS0gicB3epoKpAEooRdcZwsS24yqOQsX4s=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "1283da924bcb995d5a2dfd853f3527fa6f82ac2e", + "rev": "038752b14eab3aa316537ae67d7c470b37e27fa3", "type": "github" }, "original": { "owner": "NixOS", "repo": "nixpkgs", - "rev": "1283da924bcb995d5a2dfd853f3527fa6f82ac2e", + "rev": "038752b14eab3aa316537ae67d7c470b37e27fa3", "type": "github" } }, @@ -73,6 +77,21 @@ "nix-filter": "nix-filter", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 44534a22..0037631a 100644 --- a/flake.nix +++ b/flake.nix @@ -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 }: diff --git a/nix/ci/test.nix b/nix/ci/test.nix index 2b7d8ece..30138db3 100644 --- a/nix/ci/test.nix +++ b/nix/ci/test.nix @@ -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 = ''