From 70cb9f19489038c9f44654895aca5ed219a17028 Mon Sep 17 00:00:00 2001 From: Bikal Lem Date: Mon, 25 Jul 2022 13:54:09 +0100 Subject: [PATCH] eio(client): define env type for Client --- .ocamlformat | 2 +- cohttp-eio/examples/client1.ml | 14 ++++++++++++++ cohttp-eio/src/body.ml | 27 +++++++++++++++------------ cohttp-eio/src/client.ml | 11 +++++++---- cohttp-eio/src/cohttp_eio.mli | 23 ++++++++++++----------- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/.ocamlformat b/.ocamlformat index ccb7749a80..1ba7448c15 100644 --- a/.ocamlformat +++ b/.ocamlformat @@ -1,4 +1,4 @@ -version=0.21.0 +version=0.24.1 profile=conventional break-infix=fit-or-vertical parse-docstrings=true diff --git a/cohttp-eio/examples/client1.ml b/cohttp-eio/examples/client1.ml index e69de29bb2..a88fe4e53c 100644 --- a/cohttp-eio/examples/client1.ml +++ b/cohttp-eio/examples/client1.ml @@ -0,0 +1,14 @@ +open Eio +open Cohttp_eio + +let () = + Eio_main.run @@ fun env -> + Switch.run @@ fun sw -> + let res = + Client.get + ~headers:(Http.Header.of_list [ ("Accept", "application/json") ]) + env sw + (`Tcp (Eio.Net.Ipaddr.V4.loopback, 8080)) + (Uri.of_string "/") + in + match Client.read_fixed res with Some b -> print_string b | None -> () diff --git a/cohttp-eio/src/body.ml b/cohttp-eio/src/body.ml index 3dbc3f26d9..fd982ebb51 100644 --- a/cohttp-eio/src/body.ml +++ b/cohttp-eio/src/body.ml @@ -70,8 +70,7 @@ let quoted_char = (*-- qdtext = HTAB / SP /%x21 / %x23-5B / %x5D-7E / obs-text -- *) let qdtext = function - | '\t' | ' ' | '\x21' | '\x23' .. '\x5B' - | '\x5D' .. '\x7E' as c -> c + | ('\t' | ' ' | '\x21' | '\x23' .. '\x5B' | '\x5D' .. '\x7E') as c -> c | c -> failwith (Printf.sprintf "Invalid quoted character %C" c) (*-- quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE --*) @@ -81,31 +80,35 @@ let quoted_string r = let rec aux () = match any_char r with | '"' -> Buffer.contents buf - | '\\' -> Buffer.add_char buf (quoted_char r); aux () - | c -> Buffer.add_char buf (qdtext c); aux () + | '\\' -> + Buffer.add_char buf (quoted_char r); + aux () + | c -> + Buffer.add_char buf (qdtext c); + aux () in aux () let optional c x r = let c2 = peek_char r in - if Some c = c2 then (consume r 1; Some (x r)) + if Some c = c2 then ( + consume r 1; + Some (x r)) else None (*-- https://datatracker.ietf.org/doc/html/rfc7230#section-4.1 --*) let chunk_ext_val = let* c = peek_char in - match c with - | Some '"' -> quoted_string - | _ -> token + match c with Some '"' -> quoted_string | _ -> token let rec chunk_exts r = let c = peek_char r in match c with | Some ';' -> - consume r 1; - let name = token r in - let value = optional '=' chunk_ext_val r in - { name; value } :: chunk_exts r + consume r 1; + let name = token r in + let value = optional '=' chunk_ext_val r in + { name; value } :: chunk_exts r | _ -> [] let chunk_size = diff --git a/cohttp-eio/src/client.ml b/cohttp-eio/src/client.ml index 5fdd6d7ff9..1364076db3 100644 --- a/cohttp-eio/src/client.ml +++ b/cohttp-eio/src/client.ml @@ -1,21 +1,24 @@ module Buf_read = Eio.Buf_read type response = Http.Response.t * Buf_read.t +type env = < net : Eio.Net.t > -type body_disallowed_call = +type 'a body_disallowed_call = ?version:Http.Version.t -> ?headers:Http.Header.t -> - Eio.Stdenv.t -> + (< env ; .. > as 'a) -> Eio.Switch.t -> Eio.Net.Sockaddr.stream -> Uri.t -> response +(** [body_disallowed_call] denotes HTTP client calls where a request is not + allowed to have a request body. *) -type body_allowed_call = +type 'a body_allowed_call = ?version:Http.Version.t -> ?headers:Http.Header.t -> ?body:Body.t -> - Eio.Stdenv.t -> + (< env ; .. > as 'a) -> Eio.Switch.t -> Eio.Net.Sockaddr.stream -> Uri.t -> diff --git a/cohttp-eio/src/cohttp_eio.mli b/cohttp-eio/src/cohttp_eio.mli index 6efbe91faa..7ed5d84d84 100644 --- a/cohttp-eio/src/cohttp_eio.mli +++ b/cohttp-eio/src/cohttp_eio.mli @@ -95,11 +95,12 @@ end module Client : sig type response = Http.Response.t * Eio.Buf_read.t + type env = < net : Eio.Net.t > - type body_disallowed_call = + type 'a body_disallowed_call = ?version:Http.Version.t -> ?headers:Http.Header.t -> - Eio.Stdenv.t -> + (< env ; .. > as 'a) -> Eio.Switch.t -> Eio.Net.Sockaddr.stream -> Uri.t -> @@ -107,11 +108,11 @@ module Client : sig (** [body_disallowed_call] denotes HTTP client calls where a request is not allowed to have a request body. *) - type body_allowed_call = + type 'a body_allowed_call = ?version:Http.Version.t -> ?headers:Http.Header.t -> ?body:Body.t -> - Eio.Stdenv.t -> + (< env ; .. > as 'a) -> Eio.Switch.t -> Eio.Net.Sockaddr.stream -> Uri.t -> @@ -126,7 +127,7 @@ module Client : sig ?version:Http.Version.t -> ?headers:Http.Header.t -> ?body:Body.t -> - Eio.Stdenv.t -> + < env ; .. > -> Eio.Switch.t -> Eio.Net.Sockaddr.stream -> Uri.t -> @@ -134,15 +135,15 @@ module Client : sig (** {1 HTTP Calls with Body Disallowed} *) - val get : body_disallowed_call - val head : body_disallowed_call - val delete : body_disallowed_call + val get : 'a body_disallowed_call + val head : 'a body_disallowed_call + val delete : 'a body_disallowed_call (** {1 HTTP Calls with Body Allowed} *) - val post : body_allowed_call - val put : body_allowed_call - val patch : body_allowed_call + val post : 'a body_allowed_call + val put : 'a body_allowed_call + val patch : 'a body_allowed_call (** {1 Response Body} *)