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

Eio backend using stock parsing, serialization and signatures from Cohttp. #984

Merged
merged 30 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d9038c8
Add eio backend using parsing and serialization from Http and Cohttp.
mefyl Jun 13, 2023
47ac2d7
Move server generic signature from cohttp-lwt to cohttp.
mefyl Jun 14, 2023
06feafb
cohttp-eio: Honor the server signature from cohttp.
mefyl Jun 16, 2023
ab94910
Extract generic client signature from cohttp-lwt to cohttp, honor it …
mefyl Jun 16, 2023
9533819
Apply suggestions from code review
mefyl Jun 19, 2023
535334b
cohttp-eio.Server: Make connection the peer address, not the socket i…
mefyl Jun 16, 2023
cf88655
cohttp-eio.Server: Send a bad request response in case of client error.
mefyl Jun 16, 2023
8b7d57a
Do not link libraries with `eio.mock`.
mefyl Jun 19, 2023
7a12cdd
cohttp-eio: Make example multi-domain proof.
mefyl Jun 19, 2023
e06abd4
cohttp-eio: Accept `\n` without `\r` as a line terminator.
mefyl Jun 19, 2023
87e50fa
cohttp-eio: Simplify `Server.run`.
mefyl Jun 19, 2023
59b19ca
Do not omit null content-length headers.
mefyl Jun 15, 2023
8151742
Run eio tests only for cohttp-eio package.
mefyl Jun 30, 2023
2ea9669
Add missing log.threaded library to eio examples.
mefyl Jun 30, 2023
cbc1a7d
Fix rebase.
mefyl Jun 30, 2023
6319289
cohttp-eio: Expose IO.
adatario Jun 30, 2023
fb5a55b
cohttp-eio: Pass a switch to the request callback.
mefyl Jul 19, 2023
09ff049
cohttp-eio: Expose more options from `run_server`.
mefyl Jul 19, 2023
bd3a672
Merge master into eio.
mefyl Jul 20, 2023
3bd95ac
Merge master into eio.
mefyl Aug 17, 2023
882bbce
Update to Eio 0.12
talex5 Sep 1, 2023
f957e4d
cohttp-eio: require `on_error` argument
talex5 Sep 1, 2023
2a079ad
Merge talex5/eio into mefyl/eio.
mefyl Sep 15, 2023
ada1d0b
Merge branch 'master' into eio
mefyl Sep 27, 2023
1bf3a4f
Changelog: Document new eio implementation.
mefyl Sep 27, 2023
aa82db6
Format.
mefyl Sep 27, 2023
969cda2
Require eio 0.12.
mefyl Sep 27, 2023
7a598e5
update CI workflow
mseri Sep 28, 2023
704538a
Remove upper bound on eio constraint.
mefyl Sep 30, 2023
b8a964f
cohttp-eio: Add missing tests dependencies.
mefyl Sep 30, 2023
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 .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jobs:
local-packages:
- |
http.opam
cohttp.opam
cohttp-eio.opam

runs-on: ${{ matrix.os }}
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased
- cohttp-eio: Complete rewrite to follow common interfaces and behaviors. (mefyl #984)

## v6.0.0~alpha2 (2023-08-08)
- cohttp-lwt: Do not leak exceptions to `Lwt.async_exception_hook`. (mefyl #992, #995)
- http.header, cohttp, cohttp-eio: remove "first" and "move_to_first" and the special treatment of the "host" header (mseri #988, #986)
Expand Down
3 changes: 2 additions & 1 deletion cohttp-eio.opam
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ bug-reports: "https://github.com/mirage/ocaml-cohttp/issues"
depends: [
"dune" {>= "3.0"}
"base-domains"
"eio" {>= "0.10" & < "0.12"}
"cohttp" {= version}
"eio" {>= "0.12" & < "0.13"}
"eio_main" {with-test}
"mdx" {with-test}
"uri" {with-test}
Expand Down
16 changes: 14 additions & 2 deletions cohttp-eio/examples/client1.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
open Cohttp_eio

let () = Logs.set_reporter (Logs_fmt.reporter ())
mefyl marked this conversation as resolved.
Show resolved Hide resolved

and () =
(* The eio backend does not leverage domains yet, but might in the near future *)
Logs_threaded.enable ()

and () = Logs.Src.set_level Cohttp_eio.src (Some Debug)

let () =
Eio_main.run @@ fun env ->
let res = Client.get env ~host:"www.example.org" "/" in
print_string @@ Client.read_fixed res
let client = Client.make env#net in
Eio.Switch.run @@ fun sw ->
let resp, body = Client.get ~sw client (Uri.of_string "http://example.com") in
if Http.Status.compare resp.status `OK = 0 then
print_string @@ Eio.Buf_read.(parse_exn take_all) body ~max_size:max_int
else Fmt.epr "Unexpected HTTP status: %a" Http.Status.pp resp.status
17 changes: 8 additions & 9 deletions cohttp-eio/examples/client_timeout.ml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
open Eio
open Cohttp_eio

let () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
let client = Client.make env#net in
(* Increment/decrement this value to see success/failure. *)
let timeout_s = 0.01 in
Eio.Time.with_timeout env#clock timeout_s (fun () ->
let host, port = ("www.example.org", 80) in
let he = Unix.gethostbyname host in
let addr = `Tcp (Eio_unix.Net.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)
Eio.Switch.run @@ fun sw ->
let _, body =
Client.get client ~sw (Uri.of_string "http://www.example.org")
in
Eio.Buf_read.(of_flow ~max_size:max_int body |> take_all) |> Result.ok)
|> function
| Ok s -> print_string s
| Error `Timeout -> print_string "Connection timed out"
| Error (`Fatal e) -> Fmt.epr "fatal error: %s@." e
| Error `Timeout -> Fmt.epr "Connection timed out@."
20 changes: 13 additions & 7 deletions cohttp-eio/examples/docker_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ module Client = Cohttp_eio.Client
module Response = Http.Response
module Status = Http.Status

let () = Logs.set_reporter (Logs_fmt.reporter ())
and () = Logs.Src.set_level Cohttp_eio.src (Some Debug)

let () =
Eio_main.run @@ fun env ->
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 client = Client.make env#net in
Eio.Switch.run @@ fun sw ->
let response, body =
Client.get client ~sw
@@ Uri.make ~scheme:"httpunix" ~host:"/var/run/docker.sock" ~path:"/version"
()
in
let code = response |> Response.status |> 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
(response |> Response.headers |> Http.Header.to_string);
let body = Eio.Buf_read.(of_flow ~max_size:max_int body |> take_all) in
Printf.printf "Body of length: %d\n" (String.length body);
print_endline ("Received body\n" ^ body)
2 changes: 1 addition & 1 deletion cohttp-eio/examples/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(executables
(names server1 client1 docker_client client_timeout)
(libraries cohttp-eio eio_main eio.unix unix))
(libraries cohttp-eio eio_main eio.unix fmt unix logs.fmt logs.threaded))

(alias
(name runtest)
Expand Down
28 changes: 19 additions & 9 deletions cohttp-eio/examples/server1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ let text =
was coming to, but it was too dark to see anything; then she looked at the \
sides of the well, and noticed that they were filled with cupboards......"

open Cohttp_eio
let () = Logs.set_reporter (Logs_fmt.reporter ())
and () = Logs.Src.set_level Cohttp_eio.src (Some Debug)

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 handler _socket request _body =
match Http.Request.resource request with
| "/" -> (Http.Response.make (), Cohttp_eio.Body.of_string text)
| "/html" ->
( Http.Response.make
~headers:(Http.Header.of_list [ ("content-type", "text/html") ])
(),
(* Use a plain flow to test chunked encoding *)
Eio.Flow.string_source text )
| _ -> (Http.Response.make ~status:`Not_found (), Cohttp_eio.Body.of_string "")

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 ->
Eio.Switch.run @@ fun sw ->
let socket =
Eio.Net.listen env#net ~sw ~backlog:128 ~reuse_addr:true
(`Tcp (Eio.Net.Ipaddr.V4.loopback, !port))
and server = Cohttp_eio.Server.make ~callback:handler () in
Cohttp_eio.Server.run socket server ~on_error:raise
Loading
Loading