Skip to content

Commit

Permalink
eio(client): use Eio.Flow.two_way in call()
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Aug 3, 2022
1 parent becd6ab commit a59fa0b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 56 deletions.
1 change: 0 additions & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version=0.21.0
profile=conventional
break-infix=fit-or-vertical
parse-docstrings=true
1 change: 0 additions & 1 deletion cohttp-eio.opam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ depends: [
"cstruct"
"bigstringaf"
"fmt"
"uri"
"eio_main" {with-test}
"http" {= version}
"odoc" {with-doc}
Expand Down
19 changes: 9 additions & 10 deletions cohttp-eio/examples/client1.ml
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
open Eio
open Cohttp_eio

let connect_info url =
let connect_info env sw url =
let uri = Uri.of_string url in
let host = Uri.host uri |> Option.get in
let port = Uri.port uri |> Option.value ~default:80 in
let path = Uri.of_string (Uri.path uri) in
let addr =
let path = Uri.path uri in
let flow =
let he = Unix.gethostbyname host in
he.h_addr_list.(0)
let addr = `Tcp (Eio_unix.Ipaddr.of_unix he.h_addr_list.(0), port) in
(Net.connect ~sw (Stdenv.net env) addr :> Eio.Flow.two_way)
in
(Eio_unix.Ipaddr.of_unix addr, port, path)
(flow, path)

let () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
let addr, port, path = connect_info "http://www.reddit.com/" in
let flow, _path = connect_info env sw "http://www.example.org/" in
let res =
Client.get
~headers:(Http.Header.of_list [ ("Accept", "application/json") ])
env sw
(`Tcp (addr, port))
path
~headers:(Http.Header.of_list [ ("Host", "www.example.org") ])
sw flow "/"
in
match Client.read_fixed res with Some b -> print_string b | None -> ()
48 changes: 22 additions & 26 deletions cohttp-eio/src/client.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module Buf_read = Eio.Buf_read

type response = Http.Response.t * Buf_read.t
type env = < net : Eio.Net.t >
type resource_path = string

type 'a body_disallowed_call =
?version:Http.Version.t ->
?headers:Http.Header.t ->
(< env ; .. > as 'a) ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
(#Eio.Flow.two_way as 'a) ->
resource_path ->
response
(** [body_disallowed_call] denotes HTTP client calls where a request is not
allowed to have a request body. *)
Expand All @@ -18,17 +17,16 @@ type 'a body_allowed_call =
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
(< env ; .. > as 'a) ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
(#Eio.Flow.two_way as 'a) ->
resource_path ->
response

(* Request line https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1 *)
let write_request writer (meth, version, headers, uri, body) =
let write_request writer (meth, version, headers, resource_path, body) =
Writer.write_string writer (Http.Method.to_string meth);
Writer.write_char writer ' ';
Writer.write_string writer (Uri.path_and_query uri);
Writer.write_string writer resource_path;
Writer.write_char writer ' ';
Writer.write_string writer (Http.Version.to_string version);
Writer.write_string writer "\r\n";
Expand Down Expand Up @@ -66,11 +64,9 @@ let response buf_read =
(* Generic HTTP call *)

let call ?(meth = `GET) ?(version = `HTTP_1_1) ?(headers = Http.Header.init ())
?(body = Body.Empty) env sw stream uri =
let open Eio in
let flow = Net.connect ~sw (Stdenv.net env) stream in
let writer = Writer.create (flow :> Flow.sink) in
Fiber.fork ~sw (fun () -> Writer.run writer);
?(body = Body.Empty) sw flow uri =
let writer = Writer.create (flow :> Eio.Flow.sink) in
Eio.Fiber.fork ~sw (fun () -> Writer.run writer);
write_request writer (meth, version, headers, uri, body);
Writer.wakeup writer;
let reader =
Expand All @@ -82,25 +78,25 @@ let call ?(meth = `GET) ?(version = `HTTP_1_1) ?(headers = Http.Header.init ())

(* HTTP Calls with Body Disallowed *)

let get ?version ?headers env sw stream uri =
call ~meth:`GET ?version ?headers env sw stream uri
let get ?version ?headers sw stream uri =
call ~meth:`GET ?version ?headers sw stream uri

let head ?version ?headers env sw stream uri =
call ~meth:`HEAD ?version ?headers env sw stream uri
let head ?version ?headers sw stream uri =
call ~meth:`HEAD ?version ?headers sw stream uri

let delete ?version ?headers env sw stream uri =
call ~meth:`DELETE ?version ?headers env sw stream uri
let delete ?version ?headers sw stream uri =
call ~meth:`DELETE ?version ?headers sw stream uri

(* HTTP Calls with Body Allowed *)

let post ?version ?headers ?body env sw stream uri =
call ~meth:`POST ?version ?headers ?body env sw stream uri
let post ?version ?headers ?body sw stream uri =
call ~meth:`POST ?version ?headers ?body sw stream uri

let put ?version ?headers ?body env sw stream uri =
call ~meth:`PUT ?version ?headers ?body env sw stream uri
let put ?version ?headers ?body sw stream uri =
call ~meth:`PUT ?version ?headers ?body sw stream uri

let patch ?version ?headers ?body env sw stream uri =
call ~meth:`PATCH ?version ?headers ?body env sw stream uri
let patch ?version ?headers ?body sw stream uri =
call ~meth:`PATCH ?version ?headers ?body sw stream uri

(* Response Body *)

Expand Down
19 changes: 9 additions & 10 deletions cohttp-eio/src/cohttp_eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ end

module Client : sig
type response = Http.Response.t * Eio.Buf_read.t
type env = < net : Eio.Net.t >

type resource_path = string
(** Represents the HTTP resource path. *)

type 'a body_disallowed_call =
?version:Http.Version.t ->
?headers:Http.Header.t ->
(< env ; .. > as 'a) ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
(#Eio.Flow.two_way as 'a) ->
resource_path ->
response
(** [body_disallowed_call] denotes HTTP client calls where a request is not
allowed to have a request body. *)
Expand All @@ -112,10 +113,9 @@ module Client : sig
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
(< env ; .. > as 'a) ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
(#Eio.Flow.two_way as 'a) ->
resource_path ->
response
(** [body_allowed_call] denotes HTTP client calls where a request is allowed
to have a request body. *)
Expand All @@ -127,10 +127,9 @@ module Client : sig
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
< env ; .. > ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
#Eio.Flow.two_way ->
resource_path ->
response

(** {1 HTTP Calls with Body Disallowed} *)
Expand Down
2 changes: 1 addition & 1 deletion cohttp-eio/src/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name cohttp_eio)
(public_name cohttp-eio)
(libraries eio cstruct http bigstringaf fmt uri))
(libraries eio cstruct http bigstringaf fmt))
13 changes: 7 additions & 6 deletions cohttp-eio/tests/test_client.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
open Eio
open Cohttp_eio

let get_conn env sw =
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8080) in
let stream = Net.connect ~sw (Stdenv.net env) addr in
(stream :> Eio.Flow.two_way)

let get () =
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 "/get")
sw (get_conn env sw) "/get"
in
match Client.read_fixed res with Some s -> print_string s | None -> ()

Expand All @@ -25,9 +28,7 @@ let post () =
[
("Accept", "application/json"); ("Content-Length", content_length);
])
~body:(Body.Fixed content) env sw
(`Tcp (Eio.Net.Ipaddr.V4.loopback, 8080))
(Uri.of_string "/post")
~body:(Body.Fixed content) sw (get_conn env sw) "/post"
in
match Client.read_fixed res with Some s -> print_string s | None -> ()

Expand Down
1 change: 0 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ should also be fine under Windows too.
cstruct
bigstringaf
fmt
uri
(eio_main :with-test)
(http
(= :version))))

0 comments on commit a59fa0b

Please sign in to comment.