Skip to content
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

Renovate Server/Client API #969

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- cohttp-eio: generate Date header in responses (bikallem #955)
- cohttp-eio: further improve Cohttp_eio.Client ergonomics (bikallem #?)
- cohttp-eio: server api improvements (bikallem #962)
- cohttp-eio: renovate Client/Server API (bikallem #969)

## v6.0.0~alpha0 (2022-10-24)
- cohttp-eio: ensure "Host" header is the first header in http client requests (bikallem #939)
Expand Down
3 changes: 2 additions & 1 deletion cohttp-eio.opam
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ depends: [
"eio" {>= "0.7"}
"eio_main" {with-test}
"mdx" {with-test}
"uri" {with-test}
"uri"
"fmt"
"easy-format" {>= "1.3.4"}
"ptime"
"http" {= version}
"odoc" {with-doc}
Expand Down
8 changes: 6 additions & 2 deletions cohttp-eio/examples/client1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ open Cohttp_eio

let () =
Eio_main.run @@ fun env ->
let res = Client.get env ~host:"www.example.org" "/" in
print_string @@ Client.read_fixed res
Eio.Switch.run @@ fun sw ->
let client = Client.make sw env#net in
let res = Client.get client "www.example.org" in
match Body.read_content res with
| Some body -> print_string body
| None -> print_string "no body"
6 changes: 4 additions & 2 deletions cohttp-eio/examples/client_timeout.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ let () =
let he = Unix.gethostbyname host in
let addr = `Tcp (Eio_unix.Ipaddr.of_unix he.h_addr_list.(0), port) in
let conn = Net.connect ~sw env#net addr in
let res = Client.get ~conn ~port env ~host "/" in
Client.read_fixed res |> Result.ok)
let req = Request.get "www.example.org" in
let res = Client.call ~conn req in
Option.to_result ~none:`No_body (Body.read_content res))
|> function
| Ok s -> print_string s
| Error `No_body -> ()
| Error `Timeout -> print_string "Connection timed out"
30 changes: 15 additions & 15 deletions cohttp-eio/examples/docker_client.ml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module Switch = Eio.Switch
module Net = Eio.Net
module Stdenv = Eio.Stdenv
module Client = Cohttp_eio.Client
module Response = Http.Response
module Status = Http.Status
open Cohttp_eio

let () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
Eio.Switch.run @@ fun sw ->
let addr = `Unix "/var/run/docker.sock" in
let conn = Net.connect ~sw env#net addr in
let res = Client.get ~conn ~host:"docker" env "/version" in
let code = fst res |> Response.status |> Status.to_int in
let conn = Eio.Net.connect ~sw env#net addr in
let req =
Request.client_request ~host:"docker" ~resource:"/version" Method.Get
Body.none
in
let res = Client.call ~conn req in
let code = Response.status res |> Http.Status.to_int in
Printf.printf "Response code: %d\n" code;
Printf.printf "Headers: %s\n"
(fst res |> Response.headers |> Http.Header.to_string);
let body = Client.read_fixed res in
Printf.printf "Body of length: %d\n" (String.length body);
print_endline ("Received body\n" ^ body)
Printf.printf "Headers: %s\n" (Response.headers res |> Http.Header.to_string);
match Body.read_content res with
| Some body ->
Printf.printf "Body of length: %d\n" (String.length body);
print_endline ("Received body\n" ^ body)
| None -> print_string "no body"
14 changes: 7 additions & 7 deletions cohttp-eio/examples/server1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ let text =

open Cohttp_eio

let app : Server.request -> Server.response =
fun ((req, _, _) : Server.request) ->
match Http.Request.resource req with
| "/" -> Server.text_response text
| "/html" -> Server.html_response text
| _ -> Server.not_found_response
let app req =
match Request.resource req with
| "/" -> Response.text text
| "/html" -> Response.html text
| _ -> Response.not_found

let () =
let port = ref 8080 in
Arg.parse
[ ("-p", Arg.Set_int port, " Listening port number(8080 by default)") ]
ignore "An HTTP/1.1 server";

Eio_main.run @@ fun env -> Server.run ~port:!port env app
Eio_main.run @@ fun env ->
Server.run ~port:!port ~on_error:raise env#domain_mgr env#net env#clock app
Loading