-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1002 from talex5/eio-tls
cohttp-eio: add Client.make_generic and HTTPS support
- Loading branch information
Showing
10 changed files
with
121 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
open Cohttp_eio | ||
|
||
let () = | ||
Logs.set_reporter (Logs_fmt.reporter ()); | ||
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 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 | ||
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 | ||
Eio.Switch.run @@ fun sw -> | ||
let resp, body = | ||
Client.get ~sw client (Uri.of_string "https://example.com") | ||
in | ||
if Http.Status.compare resp.status `OK = 0 then | ||
print_string @@ Eio.Buf_read.(parse_exn take_all) body ~max_size:max_int | ||
else Fmt.epr "Unexpected HTTP status: %a" Http.Status.pp resp.status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
open Eio.Std | ||
|
||
type t | ||
|
||
include | ||
Cohttp.Client.S | ||
with type 'a with_context = t -> sw:Eio.Switch.t -> 'a | ||
with type 'a with_context = t -> sw:Switch.t -> 'a | ||
and type 'a io = 'a | ||
and type body = Body.t | ||
|
||
val make : _ Eio.Net.t -> 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. | ||
- 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 | ||
connection to use for a given URI. *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters