diff --git a/cohttp-eio/examples/client_tls.ml b/cohttp-eio/examples/client_tls.ml index 3b54a07d5..3ccba76ba 100644 --- a/cohttp-eio/examples/client_tls.ml +++ b/cohttp-eio/examples/client_tls.ml @@ -5,18 +5,24 @@ let () = Logs_threaded.enable (); Logs.Src.set_level Cohttp_eio.src (Some Debug) -let null_auth ?ip:_ ~host:_ _ = Ok None (* Warning: use a real authenticator in your code! *) +let null_auth ?ip:_ ~host:_ _ = + Ok None (* Warning: use a real authenticator in your code! *) let https ~authenticator = let tls_config = Tls.Config.client ~authenticator () in fun uri raw -> - let host = Uri.host uri |> Option.map (fun x -> Domain_name.(host_exn (of_string_exn x))) in + let host = + Uri.host uri + |> Option.map (fun x -> Domain_name.(host_exn (of_string_exn x))) + in Tls_eio.client_of_flow ?host tls_config raw let () = Eio_main.run @@ fun env -> Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () -> - let client = Client.make ~https:(Some (https ~authenticator:null_auth)) env#net in + let client = + Client.make ~https:(Some (https ~authenticator:null_auth)) env#net + in Eio.Switch.run @@ fun sw -> let resp, body = Client.get ~sw client (Uri.of_string "https://example.com") diff --git a/cohttp-eio/examples/dune b/cohttp-eio/examples/dune index 4f0a2dfb7..7e5f48ac5 100644 --- a/cohttp-eio/examples/dune +++ b/cohttp-eio/examples/dune @@ -1,6 +1,15 @@ (executables (names server1 client1 docker_client client_timeout client_tls) - (libraries cohttp-eio eio_main eio.unix fmt unix logs.fmt logs.threaded tls-eio mirage-crypto-rng-eio)) + (libraries + cohttp-eio + eio_main + eio.unix + fmt + unix + logs.fmt + logs.threaded + tls-eio + mirage-crypto-rng-eio)) (alias (name runtest) diff --git a/cohttp-eio/src/client.ml b/cohttp-eio/src/client.ml index c01a978a1..7525b074f 100644 --- a/cohttp-eio/src/client.ml +++ b/cohttp-eio/src/client.ml @@ -3,7 +3,6 @@ include Client_intf open Utils type connection = Eio.Flow.two_way_ty r - type t = sw:Switch.t -> Uri.t -> connection include @@ -15,7 +14,7 @@ include let map_context v f t ~sw = f (v t ~sw) - let call (t:t) ~sw ?headers ?body ?(chunked = false) meth uri = + let call (t : t) ~sw ?headers ?body ?(chunked = false) meth uri = let socket = t ~sw uri in let body_length = if chunked then None @@ -84,16 +83,23 @@ let tcp_address ~net uri = let make ~https net : t = let net = (net :> [ `Generic ] Eio.Net.ty r) in - let https = (https :> (Uri.t -> [ `Generic ] Eio.Net.stream_socket_ty r -> connection) option) in + let https = + (https + :> (Uri.t -> [ `Generic ] Eio.Net.stream_socket_ty r -> connection) option) + in fun ~sw uri -> match Uri.scheme uri with | Some "httpunix" -> - (* FIXME: while there is no standard, http+unix seems more widespread *) - (Eio.Net.connect ~sw net (unix_address uri) :> connection) - | Some "http" -> (Eio.Net.connect ~sw net (tcp_address ~net uri) :> connection) - | Some "https" -> - (match https with - | Some wrap -> wrap uri @@ Eio.Net.connect ~sw net (tcp_address ~net uri) - | None -> Fmt.failwith "HTTPS not enabled (for %a)" Uri.pp uri) + (* FIXME: while there is no standard, http+unix seems more widespread *) + (Eio.Net.connect ~sw net (unix_address uri) :> connection) + | Some "http" -> + (Eio.Net.connect ~sw net (tcp_address ~net uri) :> connection) + | Some "https" -> ( + match https with + | Some wrap -> + wrap uri @@ Eio.Net.connect ~sw net (tcp_address ~net uri) + | None -> Fmt.failwith "HTTPS not enabled (for %a)" Uri.pp uri) | x -> - Fmt.failwith "Unknown scheme %a" Fmt.(option ~none:(any "None") Dump.string) x + Fmt.failwith "Unknown scheme %a" + Fmt.(option ~none:(any "None") Dump.string) + x diff --git a/cohttp-eio/src/client.mli b/cohttp-eio/src/client.mli index f9f676ecc..e3276211f 100644 --- a/cohttp-eio/src/client.mli +++ b/cohttp-eio/src/client.mli @@ -9,16 +9,20 @@ include and type body = Body.t val make : - https:(Uri.t -> [`Generic] Eio.Net.stream_socket_ty r -> _ Eio.Flow.two_way) option -> - _ Eio.Net.t -> t -(** [make ~https net] is a convenience wrapper around {!make_generic} that - uses [net] to make connections. + https: + (Uri.t -> [ `Generic ] Eio.Net.stream_socket_ty r -> _ Eio.Flow.two_way) + option -> + _ Eio.Net.t -> + t +(** [make ~https net] is a convenience wrapper around {!make_generic} that uses + [net] to make connections. - - URIs of the form "http://host:port/..." connect to the given TCP host and port. - - URIs of the form "https://host:port/..." connect to the given TCP host and port, - and are then wrapped by [https] (or rejected if that is [None]). - - URIs of the form "httpunix://unix-path/http-path" connect to the given Unix path. -*) + - URIs of the form "http://host:port/..." connect to the given TCP host and + port. + - URIs of the form "https://host:port/..." connect to the given TCP host and + port, and are then wrapped by [https] (or rejected if that is [None]). + - URIs of the form "httpunix://unix-path/http-path" connect to the given + Unix path. *) val make_generic : (sw:Switch.t -> Uri.t -> _ Eio.Net.stream_socket) -> t (** [make_generic connect] is an HTTP client that uses [connect] to get the