-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Eio >= 0.12 and Cohttp_eio >= 6.0.0~beta1
- Loading branch information
1 parent
43e8a5d
commit d28c905
Showing
10 changed files
with
97 additions
and
222 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 |
---|---|---|
@@ -1,60 +1,33 @@ | ||
open Util | ||
|
||
let authenticator = | ||
(* FIXME *) | ||
let null_auth ?ip:_ ~host:_ _ = Ok None in | ||
null_auth | ||
let null_auth ?ip:_ ~host:_ _ = | ||
Ok None (* Warning: use a real authenticator in your code! *) | ||
|
||
let request_https socket host = | ||
let host = | ||
Result.(host |> Domain_name.of_string >>= Domain_name.host |> to_option) | ||
in | ||
let client = | ||
let open Tls.Config in | ||
client ~version:(`TLS_1_0, `TLS_1_3) ~authenticator | ||
~ciphers:Ciphers.supported () | ||
in | ||
Tls_eio.client_of_flow client ?host socket | ||
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 request ?headers ?body ~meth env ~sw url = | ||
let url = Uri.of_string url in | ||
let host = Uri.host url |> Option.get in | ||
let port = Uri.port url in | ||
let service = Uri.scheme url |> Option.get in | ||
let path = Uri.path_and_query url in | ||
let request ?headers ?body ~meth env ~sw (url : string) = | ||
let headers = headers |> Option.map Cohttp.Header.of_list in | ||
let body = | ||
body |> Option.map (function `Fixed src -> Cohttp_eio.Body.Fixed src) | ||
body |> Option.map (function `Fixed src -> Cohttp_eio.Body.of_string src) | ||
in | ||
let addr = | ||
match Eio.Net.getaddrinfo_stream (Eio.Stdenv.net env) host ~service with | ||
| [] -> failwith "getaddrinfo failed" | ||
| addr :: _ -> ( | ||
match (addr, port) with | ||
| `Tcp (ip, _), Some port -> `Tcp (ip, port) | ||
| _ -> addr) | ||
in | ||
let net = Eio.Stdenv.net env in | ||
let socket = Eio.Net.connect ~sw net addr in | ||
let conn : Eio.Flow.two_way = | ||
match service with | ||
| "https" -> (request_https socket host :> Eio.Flow.two_way) | ||
| "http" -> (socket :> Eio.Flow.two_way) | ||
| _ -> failwith "unsupported scheme" | ||
let client = | ||
Cohttp_eio.Client.make | ||
~https:(Some (https ~authenticator:null_auth)) | ||
(Eio.Stdenv.net env) | ||
in | ||
Cohttp_eio.Client.call ~meth env ~host ?port ?headers ?body ~conn path | ||
Cohttp_eio.Client.call ~sw ?headers ?body client meth (Uri.of_string url) | ||
|
||
let get = request ~meth:`GET | ||
let post = request ~meth:`POST | ||
let put = request ~meth:`PUT | ||
let delete = request ~meth:`DELETE | ||
|
||
let drain_resp_body resp = | ||
let body = ref [] in | ||
match | ||
Cohttp_eio.Client.read_chunked resp (function | ||
| Chunk { data; _ } -> body := data :: !body | ||
| Last_chunk _ -> ()) | ||
with | ||
| None -> Cohttp_eio.Client.read_fixed resp | ||
| Some _ -> String.concat "" (List.rev !body) | ||
let drain_resp_body (_, body) = | ||
Eio.Buf_read.(parse_exn take_all) body ~max_size:max_int |
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
Oops, something went wrong.