Skip to content

Commit

Permalink
Revert conduit 3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
samoht committed Jan 29, 2021
1 parent 5df2dd1 commit 9fa8152
Show file tree
Hide file tree
Showing 53 changed files with 286 additions and 919 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ jobs:
- run: |
opam pin add cohttp-async.dev . --no-action
opam pin add cohttp-lwt-jsoo.dev . --no-action
opam pin add cohttp-lwt-unix-nossl.dev . --no-action
opam pin add cohttp-lwt-unix-ssl.dev . --no-action
opam pin add cohttp-lwt-unix.dev . --no-action
opam pin add cohttp-lwt.dev . --no-action
opam pin add cohttp-mirage.dev . --no-action
Expand All @@ -50,8 +48,6 @@ jobs:
cohttp-lwt \
cohttp-lwt-jsoo \
cohttp-lwt-unix \
cohttp-lwt-unix-nossl \
cohttp-lwt-unix-ssl \
cohttp-mirage \
cohttp-top \
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- lwt_jsoo: Forward exceptions to caller when response is null (@mefyl #738)
- Remove wrapped false (@rgrinberg #734)
- Use implicit executable dependency for generate.exe (@TheLortex #735)
- Revert the changes to adapt to conduit 3.0.0 (#741, @samoht)

## v3.0.0 (2020-10-02) -- unreleased

Expand Down Expand Up @@ -44,7 +45,7 @@
let cfg =
{ Conduit_lwt.TCP.sockaddr= Unix.ADDR_INET (Unix.inet_addr_loopback, 8080)
; capacity= 40 }
let run cohttp_config =
Cohttp_lwt_unix.Server.create cfg Conduit_lwt.TCP.protocol Conduit_lwt.TCP.service
cohttp_config
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ findlib (`ocamlfind`) libraries:
* `cohttp` - Base `Cohttp` module. No platform specific functionality
* `cohttp-async` - Async backend `Cohttp_async`
* `cohttp-lwt` - Lwt backend without unix specifics
* `cohttp-lwt-unix` - Unix based lwt backend with `tls` support
* `cohttp-lwt-unix-ssl` - Unix based lwt backend with `lwt_ssl` support
* `cohttp-lwt-unix-nossl` - Unix based lwt backend (only `http`)
* `cohttp-lwt-unix` - Unix based lwt backend
* `cohttp-lwt-jsoo` - Jsoo (XHR) client
* `cohttp-top` - Print cohttp types in the toplevel (`#require "cohttp-top"`)

Expand All @@ -160,7 +158,7 @@ You can use [`Lwt.pick`](https://ocsigen.org/lwt/4.1.0/api/Lwt) to set a timeout
on the execution of a thread. For example, say that you want to set a timeout on
the `Client.get` thread in the example above, then you could modify the get call
as follows

```ocaml
(* [...] *)
Expand Down Expand Up @@ -247,7 +245,7 @@ To build and execute with `dune`, first create the following `dune` file
```
cat - > dune <<EOF
(executable
;(public_name docker_example)
;(public_name docker_example)
(name docker_example)
(libraries cohttp-lwt-unix conduit-lwt))
EOF
Expand All @@ -256,7 +254,7 @@ then run the example with
```
dune exec ./docker_example.exe
```
Even though conduit is transitively there, for this example we are explicitly
Even though conduit is transitively there, for this example we are explicitly
mentioning it to emphasize that we are creating a new Conduit resolver. Refer to
[conduit's README](https://github.com/mirage/ocaml-conduit/) for examples of use and
links to up-to-date conduit documentation.
Expand Down
3 changes: 1 addition & 2 deletions cohttp-async.opam
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ depends: [
"base" {>= "v0.11.0"}
"core" {with-test}
"cohttp" {=version}
"conduit-async" {>="3.0.0"}
"conduit-async-ssl"
"conduit-async" {>="1.2.0"}
"magic-mime"
"mirage-crypto" {with-test}
"logs"
Expand Down
19 changes: 7 additions & 12 deletions cohttp-async/bin/cohttp_server_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ let determine_mode cert_file_path key_file_path =
| None, None -> `TCP
| _ -> failwith "Error: must specify both certificate and key for HTTPS"

let start_server docroot port index cert_file key_file verbose =
let start_server docroot port index cert_file key_file verbose () =
(* enable logging to stdout *)
Fmt_tty.setup_std_outputs ();
Logs.set_level @@ if verbose then (Some Logs.Debug) else (Some Logs.Info);
Expand All @@ -116,19 +116,14 @@ let start_server docroot port index cert_file key_file verbose =
let mode_str = (match mode with `OpenSSL _ -> "HTTPS" | `TCP -> "HTTP") in
Logs.info (fun f -> f "Listening for %s requests on %d" mode_str port);
let info = Printf.sprintf "Served by Cohttp/Async listening on %d" port in
let _never, server = Server.create
~on_handler_error:(`Call (fun flow exn ->
let addr = match Conduit_async.cast flow Conduit_async.TCP.protocol with
| Some flow -> Conduit_async.TCP.Protocol.address flow
| None -> assert false (* XXX(dinosaure): safe when we initialize the server with
[Conduit_async_tcp.service] *) in
Server.create
~on_handler_error:(`Call (fun addr exn ->
Logs.err (fun f -> f "Error from %s" (Socket.Address.to_string addr));
Logs.err (fun f -> f "%s" @@ Exn.to_string exn)))
~protocol:Conduit_async.TCP.protocol
~service:Conduit_async.TCP.service
(Conduit_async.TCP.Listen (None, Tcp.Where_to_listen.of_port port))
(handler ~info ~docroot ~index) in
server
~mode
(Tcp.Where_to_listen.of_port port)
(handler ~info ~docroot ~index) >>= fun _serv ->
Deferred.never ()

let () =
let open Async_command in
Expand Down
93 changes: 37 additions & 56 deletions cohttp-async/src/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,24 @@ module Net = struct
Or_error.return (host, Ipaddr_unix.of_inet_addr addr, port)
| _ -> Or_error.error "Failed to resolve Uri" uri Uri_sexp.sexp_of_t

let connect_uri ?ssl_ctx uri =
match (Uri.scheme uri, ssl_ctx) with
| Some "httpunix", _ ->
let connect_uri ?interrupt ?ssl_config uri =
(match Uri.scheme uri with
| Some "httpunix" ->
let host = Uri.host_with_default ~default:"localhost" uri in
let tcp_cfg = Conduit_async.TCP.Unix (Socket.Address.Unix.create host) in
Conduit_async.connect tcp_cfg Conduit_async.TCP.protocol
| Some "https", Some ctx ->
lookup uri
|> Deferred.Or_error.ok_exn
>>= fun (_, addr, port) ->
let tcp_cfg =
let addr = Ipaddr_unix.to_inet_addr addr in
Conduit_async.TCP.Inet (Socket.Address.Inet.create addr ~port) in
Conduit_async.connect (ctx, tcp_cfg) Conduit_async_ssl.TCP.protocol
| Some "https", None ->
return @@ `Unix_domain_socket host
| _ ->
lookup uri
|> Deferred.Or_error.ok_exn
>>= fun (host, addr, port) ->
let tcp_cfg =
let addr = Ipaddr_unix.to_inet_addr addr in
Conduit_async.TCP.Inet (Socket.Address.Inet.create addr ~port) in
let ctx = Conduit_async_ssl.context ~hostname:host () in
Conduit_async.connect (ctx, tcp_cfg) Conduit_async_ssl.TCP.protocol
| _ ->
lookup uri
|> Deferred.Or_error.ok_exn
>>= fun (_, addr, port) ->
let tcp_cfg =
let addr = Ipaddr_unix.to_inet_addr addr in
Conduit_async.TCP.Inet (Socket.Address.Inet.create addr ~port) in
Conduit_async.connect tcp_cfg Conduit_async.TCP.protocol

let failwith fmt = Stdlib.Format.kasprintf failwith fmt

let connect_uri ?ssl_ctx uri =
connect_uri ?ssl_ctx uri >>= function
| Ok flow -> Conduit_async.reader_and_writer_of_flow flow
| Error err -> failwith "%a" Conduit_async.pp_error err
return @@ match (Uri.scheme uri, ssl_config) with
| Some "https", Some config ->
`OpenSSL (addr, port, config)
| Some "https", None ->
let config = Conduit_async.V2.Ssl.Config.create ~hostname:host () in
`OpenSSL (addr, port, config)
| _ -> `TCP (addr, port))
>>= fun mode ->
Conduit_async.V2.connect ?interrupt mode
end

let read_response ic =
Expand All @@ -85,13 +65,14 @@ let read_response ic =
(res, pipe)
end

let request ?ssl_ctx ?uri ?(body=`Empty) req =
let request ?interrupt ?ssl_config ?uri ?(body=`Empty) req =
(* Connect to the remote side *)
let uri =
match uri with
| Some t -> t
| None -> Request.uri req in
Net.connect_uri ?ssl_ctx uri >>= fun (ic, oc) ->
Net.connect_uri ?interrupt ?ssl_config uri
>>= fun (ic, oc) ->
try_with (fun () ->
Request.write (fun writer ->
Body_raw.write_body Request.write_body body writer) req oc
Expand All @@ -116,8 +97,8 @@ module Connection = struct
(* we can't send concurrent requests over HTTP/1 *)
type t = t' Sequencer.t

let connect ?ssl_ctx uri =
Net.connect_uri ?ssl_ctx uri
let connect ?interrupt ?ssl_config uri =
Net.connect_uri ?interrupt ?ssl_config uri
>>| fun (ic, oc) ->
let t =
{ ic ; oc }
Expand Down Expand Up @@ -154,8 +135,8 @@ module Connection = struct
Ivar.read res
end

let callv ?ssl_ctx uri reqs =
Connection.connect ?ssl_ctx uri
let callv ?interrupt ?ssl_config uri reqs =
Connection.connect ?interrupt ?ssl_config uri
>>| fun connection ->
let responses =
Pipe.map' ~max_queue_length:1 reqs ~f:(fun reqs ->
Expand All @@ -165,7 +146,7 @@ let callv ?ssl_ctx uri reqs =
(Pipe.closed responses >>= fun () -> Connection.close connection) |> don't_wait_for;
responses

let call ?ssl_ctx ?headers ?(chunked=false) ?(body=`Empty) meth uri =
let call ?interrupt ?ssl_config ?headers ?(chunked=false) ?(body=`Empty) meth uri =
(* Create a request, then make the request. Figure out an appropriate
transfer encoding *)
begin
Expand All @@ -180,33 +161,33 @@ let call ?ssl_ctx ?headers ?(chunked=false) ?(body=`Empty) meth uri =
| false -> (* Use chunked encoding if there is a body *)
Request.make_for_client ?headers ~chunked:true meth uri, body
end
end >>= fun (req, body) -> request ?ssl_ctx ~body ~uri req
end >>= fun (req, body) -> request ?interrupt ?ssl_config ~body ~uri req

let get ?ssl_ctx ?headers uri =
call ?ssl_ctx ?headers ~chunked:false `GET uri
let get ?interrupt ?ssl_config ?headers uri =
call ?interrupt ?ssl_config ?headers ~chunked:false `GET uri

let head ?ssl_ctx ?headers uri =
call ?ssl_ctx ?headers ~chunked:false `HEAD uri
let head ?interrupt ?ssl_config ?headers uri =
call ?interrupt ?ssl_config ?headers ~chunked:false `HEAD uri
>>| fun (res, body) ->
(match body with
| `Pipe p -> Pipe.close_read p;
| _ -> ());
res

let post ?ssl_ctx ?headers ?(chunked=false) ?body uri =
call ?ssl_ctx ?headers ~chunked ?body `POST uri
let post ?interrupt ?ssl_config ?headers ?(chunked=false) ?body uri =
call ?interrupt ?ssl_config ?headers ~chunked ?body `POST uri

let post_form ?ssl_ctx ?headers ~params uri =
let post_form ?interrupt ?ssl_config ?headers ~params uri =
let headers = Cohttp.Header.add_opt_unless_exists headers
"content-type" "application/x-www-form-urlencoded" in
let body = Body.of_string (Uri.encoded_of_query params) in
post ?ssl_ctx ~headers ~chunked:false ~body uri
post ?interrupt ?ssl_config ~headers ~chunked:false ~body uri

let put ?ssl_ctx ?headers ?(chunked=false) ?body uri =
call ?ssl_ctx ?headers ~chunked ?body `PUT uri
let put ?interrupt ?ssl_config ?headers ?(chunked=false) ?body uri =
call ?interrupt ?ssl_config ?headers ~chunked ?body `PUT uri

let patch ?ssl_ctx ?headers ?(chunked=false) ?body uri =
call ?ssl_ctx ?headers ~chunked ?body `PATCH uri
let patch ?interrupt ?ssl_config ?headers ?(chunked=false) ?body uri =
call ?interrupt ?ssl_config ?headers ~chunked ?body `PATCH uri

let delete ?ssl_ctx ?headers ?(chunked=false) ?body uri =
call ?ssl_ctx ?headers ~chunked ?body `DELETE uri
let delete ?interrupt ?ssl_config ?headers ?(chunked=false) ?body uri =
call ?interrupt ?ssl_config ?headers ~chunked ?body `DELETE uri
33 changes: 22 additions & 11 deletions cohttp-async/src/client.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(** Send an HTTP request with an arbitrary body
The request is sent as-is. *)
val request :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?uri:Uri.t ->
?body:Body.t ->
Cohttp.Request.t ->
Expand All @@ -20,7 +21,8 @@ val request :
default port ([*:80]) or the specified one.}}
*)
val call :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
?chunked:bool ->
?body:Body.t ->
Expand All @@ -33,7 +35,8 @@ module Connection : sig
type t

val connect :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
Uri.t ->
t Async_kernel.Deferred.t

Expand All @@ -49,28 +52,32 @@ module Connection : sig
end

val callv :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
Uri.t ->
(Cohttp.Request.t * Body.t) Async_kernel.Pipe.Reader.t ->
(Cohttp.Response.t * Body.t) Async_kernel.Pipe.Reader.t Async_kernel.Deferred.t

(** Send an HTTP GET request *)
val get :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
Uri.t ->
(Cohttp.Response.t * Body.t) Async_kernel.Deferred.t

(** Send an HTTP HEAD request *)
val head :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
Uri.t ->
Cohttp.Response.t Async_kernel.Deferred.t

(** Send an HTTP DELETE request *)
val delete :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
?chunked:bool ->
?body:Body.t ->
Expand All @@ -81,7 +88,8 @@ val delete :
[chunked] encoding is off by default as not many servers support it
*)
val post :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
?chunked:bool ->
?body:Body.t ->
Expand All @@ -92,7 +100,8 @@ val post :
[chunked] encoding is off by default as not many servers support it
*)
val put :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
?chunked:bool ->
?body:Body.t ->
Expand All @@ -103,7 +112,8 @@ val put :
[chunked] encoding is off by default as not many servers support it
*)
val patch :
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
?chunked:bool ->
?body:Body.t ->
Expand All @@ -112,7 +122,8 @@ val patch :

(** Send an HTTP POST request in form format *)
val post_form:
?ssl_ctx:Conduit_async_ssl.context ->
?interrupt:unit Async_kernel.Deferred.t ->
?ssl_config:Conduit_async.V2.Ssl.Config.t ->
?headers:Cohttp.Header.t ->
params:(string * string list) list ->
Uri.t ->
Expand Down
2 changes: 1 addition & 1 deletion cohttp-async/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
(synopsis "Async backend")
(public_name cohttp-async)
(libraries logs.fmt base fmt async_unix async_kernel uri uri.services
uri-sexp ipaddr.unix conduit-async conduit-async-ssl magic-mime cohttp)
uri-sexp ipaddr.unix conduit-async magic-mime cohttp)
(preprocess
(pps ppx_sexp_conv)))
Loading

0 comments on commit 9fa8152

Please sign in to comment.