From 809fa5cd33920b48857d4b73149f529a804ac4ac Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Mon, 17 Oct 2022 14:36:15 +0200 Subject: [PATCH] Improve the API to be more user-friendly (@pitag-ha) --- src/http_mirage_client.ml | 52 +++++++++++++++++++++----------------- src/http_mirage_client.mli | 12 ++++----- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/http_mirage_client.ml b/src/http_mirage_client.ml index c1713ff..1c14f73 100644 --- a/src/http_mirage_client.ml +++ b/src/http_mirage_client.ml @@ -5,16 +5,23 @@ let tls_config = Mimic.make ~name:"tls-config" open Lwt.Infix +type t = + { ctx : Mimic.ctx + ; alpn_protocol : Mimic.flow -> string option + ; authenticator : (X509.Authenticator.t, [ `Msg of string ]) result } + module type S = sig - val connect : Mimic.ctx -> Mimic.ctx Lwt.t - val alpn_protocol : Mimic.flow -> string option - val authenticator : (X509.Authenticator.t, [> `Msg of string ]) result + type nonrec t = t + + val connect : Mimic.ctx -> t Lwt.t end module Make (Pclock : Mirage_clock.PCLOCK) (TCP : Tcpip.Tcp.S) (Happy_eyeballs : Mimic_happy_eyeballs.S with type flow = TCP.flow) : S = struct + type nonrec t = t + module TCP = struct include TCP type endpoint = Happy_eyeballs.t * string * int @@ -60,23 +67,6 @@ module Make let tls_edn, tls_protocol = Mimic.register ~name:"tls" (module TLS) - let connect ctx = - let k0 happy_eyeballs http_scheme http_hostname http_port = match http_scheme with - | "http" -> Lwt.return_some (happy_eyeballs, http_hostname, http_port) - | _ -> Lwt.return_none in - let k1 happy_eyeballs http_scheme http_hostname http_port tls_config = match http_scheme with - | "https" -> Lwt.return_some (happy_eyeballs, tls_config, http_hostname, http_port) - | _ -> Lwt.return_none in - let ctx = Mimic.fold tcp_edn - Mimic.Fun.[ req Happy_eyeballs.happy_eyeballs - ; req http_scheme; req http_hostname; dft http_port 80 ] - ~k:k0 ctx in - Lwt.return (Mimic.fold tls_edn - Mimic.Fun.[ req Happy_eyeballs.happy_eyeballs - ; req http_scheme; req http_hostname; dft http_port 443 - ; req tls_config ] - ~k:k1 ctx) - let alpn_protocol flow = let module M = (val (Mimic.repr tls_protocol)) in match flow with @@ -89,6 +79,24 @@ module Make let authenticator = let module V = Ca_certs_nss.Make (Pclock) in V.authenticator () + + let connect ctx = + let k0 happy_eyeballs http_scheme http_hostname http_port = match http_scheme with + | "http" -> Lwt.return_some (happy_eyeballs, http_hostname, http_port) + | _ -> Lwt.return_none in + let k1 happy_eyeballs http_scheme http_hostname http_port tls_config = match http_scheme with + | "https" -> Lwt.return_some (happy_eyeballs, tls_config, http_hostname, http_port) + | _ -> Lwt.return_none in + let ctx = Mimic.fold tcp_edn + Mimic.Fun.[ req Happy_eyeballs.happy_eyeballs + ; req http_scheme; req http_hostname; dft http_port 80 ] + ~k:k0 ctx in + let ctx = Mimic.fold tls_edn + Mimic.Fun.[ req Happy_eyeballs.happy_eyeballs + ; req http_scheme; req http_hostname; dft http_port 443 + ; req tls_config ] + ~k:k1 ctx in + Lwt.return { ctx; alpn_protocol; authenticator; } end module Version = Httpaf.Version @@ -310,9 +318,7 @@ let resolve_location ~uri ~location = let one_request ?config ?tls_config:cfg - ~ctx - ~alpn_protocol - ~authenticator + { ctx; alpn_protocol; authenticator; } ?(meth= `GET) ?(headers= []) ?body diff --git a/src/http_mirage_client.mli b/src/http_mirage_client.mli index 55c5574..12303c0 100644 --- a/src/http_mirage_client.mli +++ b/src/http_mirage_client.mli @@ -1,7 +1,9 @@ +type t + module type S = sig - val connect : Mimic.ctx -> Mimic.ctx Lwt.t - val alpn_protocol : Mimic.flow -> string option - val authenticator : (X509.Authenticator.t, [> `Msg of string ]) result + type nonrec t = t + + val connect : Mimic.ctx -> t Lwt.t end module Make @@ -22,9 +24,7 @@ type response = val one_request : ?config:[ `H2 of H2.Config.t | `HTTP_1_1 of Httpaf.Config.t ] -> ?tls_config:Tls.Config.client -> - ctx:Mimic.ctx -> - alpn_protocol:(Mimic.flow -> string option) -> - authenticator:(X509.Authenticator.t, [> `Msg of string ]) result -> + t -> ?meth:Httpaf.Method.t -> ?headers:(string * string) list -> ?body:string ->