Skip to content

Commit

Permalink
eio(client): define env type for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Jul 25, 2022
1 parent b300c71 commit 6b44cb2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
16 changes: 16 additions & 0 deletions cohttp-eio/examples/client1.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(* Contributed by @patricoferris *)

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 -> ()
27 changes: 15 additions & 12 deletions cohttp-eio/src/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --*)
Expand All @@ -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 =
Expand Down
11 changes: 7 additions & 4 deletions cohttp-eio/src/client.ml
Original file line number Diff line number Diff line change
@@ -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 ->
Expand Down
23 changes: 12 additions & 11 deletions cohttp-eio/src/cohttp_eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,24 @@ 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 ->
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 ->
Expand All @@ -126,23 +127,23 @@ 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 ->
response

(** {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} *)

Expand Down

0 comments on commit 6b44cb2

Please sign in to comment.