-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cohttp-eio: add Client.make_generic and HTTPS support #1002
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,9 +1,29 @@ | ||||||
open Eio.Std | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid opening here, and leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a bit ugly - several other places would get a fair bit longer too. |
||||||
|
||||||
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 -> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since it is no longer optional, why not dropping the option from the signature? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https support is optional (you don't have to use it); see #984 (comment):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the plan is to have a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. I was recalling what we had discussed incorrectly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this signature ok as is, or do we need to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I would prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave it as is. We can make it optional in due course once there is the tls package if we can think of a better way to deal with it |
||||||
_ 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. *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit sad with the amount of boilerplate we have to come up to do a simple
https
call. Could we somehow ship a default authenticator as well? The current API doesn't look very simple to use.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't ship a default authenticator without adding extra dependencies to cohttp-eio. The plan is to have another package for that later (either cohttp-eio-tls, or maybe something conduit-like); this PR is just making that possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could merge this and release it as beta, so that people can test it out in the meantime and inform possible api refinements. But I agree with @samoht, and I think it would be nice to have
cohttp-eio-tls
before we release the new stable 6.0.0There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a good plan. I'm already using that PR in a project of mine and it's working great 👍 Thanks for this!