Skip to content

Commit

Permalink
Add cohttp-lwt-unix-nossl and cohttp-lwt-unix-ssl (update cohttp-lwt-…
Browse files Browse the repository at this point in the history
…unix to use cohttp-lwt-unix-nossl)
  • Loading branch information
dinosaure committed Oct 14, 2020
1 parent 7d56281 commit 1c976f0
Show file tree
Hide file tree
Showing 26 changed files with 447 additions and 22 deletions.
50 changes: 50 additions & 0 deletions cohttp-lwt-unix-nossl.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
opam-version: "2.0"
maintainer: "[email protected]"
authors: [
"Anil Madhavapeddy"
"Stefano Zacchiroli"
"David Sheets"
"Thomas Gazagnaire"
"David Scott"
"Rudi Grinberg"
"Andy Ray"
]
synopsis: "CoHTTP implementation for Unix and Windows using Lwt"
description: """
An implementation of an HTTP client and server using the Lwt
concurrency library. See the `Cohttp_lwt_unix` module for information
on how to use this. The package also installs `cohttp-curl-lwt`
and a `cohttp-server-lwt` binaries for quick uses of a HTTP(S)
client and server respectively.

Although the name implies that this only works under Unix, it
should also be fine under Windows too."""
license: "ISC"
tags: ["org:mirage" "org:xapi-project"]
homepage: "https://github.com/mirage/ocaml-cohttp"
doc: "https://mirage.github.io/ocaml-cohttp/"
bug-reports: "https://github.com/mirage/ocaml-cohttp/issues"
depends: [
"ocaml" {>= "4.04.1"}
"dune" {>= "1.1.0"}
"conduit-lwt" {>= "1.0.3"}
"ca-certs"
"cmdliner"
"magic-mime"
"logs"
"fmt" {>= "0.8.2"}
"cohttp-lwt" {=version}
"lwt" {>= "3.0.0"}
"base-unix"
"ounit" {with-test}
]
build: [
["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs]
["dune" "runtest" "-p" name "-j" jobs] {with-test}
]
dev-repo: "git+https://github.com/mirage/ocaml-cohttp.git"
pin-depends: [
[ "conduit.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
[ "conduit-lwt.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
]
2 changes: 2 additions & 0 deletions cohttp-lwt-unix-nossl/src/client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include Cohttp_lwt.Make_client(Io)(Net)
5 changes: 5 additions & 0 deletions cohttp-lwt-unix-nossl/src/client.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

(** The [Client] module implements the full UNIX HTTP client interface,
including the UNIX-specific functions defined in {!C }. *)

include Cohttp_lwt.S.Client with type ctx = Conduit.resolvers
33 changes: 33 additions & 0 deletions cohttp-lwt-unix-nossl/src/cohttp_lwt_unix_nossl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(*{{{ Copyright (c) 2012 Anil Madhavapeddy <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
}}}*)

module Request = struct
include Cohttp.Request
include (Make(Io)
: module type of Make(Io) with type t := t)
end

module Response = struct
include Cohttp.Response
include (Make(Io)
: module type of Make(Io) with type t := t)
end

module Client = Client
module Server = Server
module Debug = Debug
module Net = Net
module IO = Io
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions cohttp-lwt-unix-nossl/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(library
(name cohttp_lwt_unix_nossl)
(public_name cohttp-lwt-unix-nossl)
(synopsis "Lwt/Unix backend for Cohttp")
(preprocess
(pps ppx_sexp_conv))
(libraries fmt logs logs.lwt conduit-lwt magic-mime lwt.unix
cohttp cohttp-lwt))
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions cohttp-lwt-unix-nossl/src/net.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(*{{{ Copyright (c) 2012 Anil Madhavapeddy <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
}}}*)

(* Miscellaneous net-helpers used by Cohttp. Ideally, these will disappear
* into some connection-management framework such as andrenth/release *)

open Lwt.Infix

module IO = Io

type ctx = (Conduit.resolvers[@sexp.opaque]) [@@deriving sexp]

let default_ctx = Conduit_lwt.empty

let failwith fmt = Format.kasprintf (fun err -> Lwt.fail (Failure err)) fmt

let uri_to_endpoint uri =
(match Uri.host uri with
| None -> failwith "Invalid uri: no host component in %a" Uri.pp uri
| Some h -> Lwt.return h) >>= fun v ->
let ( >>= ) x f = match x with Ok x -> f x | Error err -> Error err in
match Domain_name.(of_string v >>= host), Ipaddr.of_string v with
| Ok domain_name, _ -> Lwt.return (Conduit.Endpoint.domain domain_name)
| Error _, Ok v -> Lwt.return (Conduit.Endpoint.ip v)
| Error _, Error _ -> failwith "Invalid uri: %a" Uri.pp uri

let connect_uri ~ctx uri =
uri_to_endpoint uri >>= fun edn ->
let ctx = match Uri.scheme uri with
| (Some "http" | None) ->
let port = Option.value ~default:80 (Uri.port uri) in
Conduit_lwt.add Conduit_lwt.TCP.protocol (Conduit_lwt.TCP.resolve ~port) ctx
| _ -> ctx in
Conduit_lwt.resolve ctx edn >>= function
| Ok flow ->
let ic, oc = Conduit_lwt.io_of_flow flow in
Lwt.return (flow, ic, oc)
| Error err ->
failwith "%a" Conduit_lwt.pp_error err

let close c = Lwt.catch
(fun () -> Lwt_io.close c)
(fun e ->
Logs.warn (fun f -> f "Closing channel failed: %s" (Printexc.to_string e));
Lwt.return_unit
)

let close_in ic = Lwt.ignore_result (close ic)

let close_out oc = Lwt.ignore_result (close oc)

let close ic oc = Lwt.ignore_result (close ic >>= fun () -> close oc)
38 changes: 38 additions & 0 deletions cohttp-lwt-unix-nossl/src/net.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(*{{{ Copyright (c) 2015 David Sheets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
}}}*)

(** Basic satisfaction of {! Cohttp_lwt.Net } *)

module IO = Io

type ctx = (Conduit.resolvers[@sexp.opaque]) [@@deriving sexp]

val default_ctx : ctx

(** Exceptions from [conduit].
When the [recv] or the [send] {i syscalls} return an error,
[conduit] will reraise it. *)

val connect_uri :
ctx:ctx ->
Uri.t ->
(Conduit_lwt.flow * Lwt_io.input Lwt_io.channel * Lwt_io.output Lwt_io.channel) Lwt.t

val close_in : 'a Lwt_io.channel -> unit
val close_out : 'a Lwt_io.channel -> unit

val close : 'a Lwt_io.channel -> 'b Lwt_io.channel -> unit
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions cohttp-lwt-unix-ssl.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
opam-version: "2.0"
maintainer: "[email protected]"
authors: [
"Anil Madhavapeddy"
"Stefano Zacchiroli"
"David Sheets"
"Thomas Gazagnaire"
"David Scott"
"Rudi Grinberg"
"Andy Ray"
]
synopsis: "CoHTTP implementation for Unix and Windows using Lwt"
description: """
An implementation of an HTTP client and server using the Lwt
concurrency library. See the `Cohttp_lwt_unix` module for information
on how to use this. The package also installs `cohttp-curl-lwt`
and a `cohttp-server-lwt` binaries for quick uses of a HTTP(S)
client and server respectively.

Although the name implies that this only works under Unix, it
should also be fine under Windows too."""
license: "ISC"
tags: ["org:mirage" "org:xapi-project"]
homepage: "https://github.com/mirage/ocaml-cohttp"
doc: "https://mirage.github.io/ocaml-cohttp/"
bug-reports: "https://github.com/mirage/ocaml-cohttp/issues"
depends: [
"ocaml" {>= "4.04.1"}
"dune" {>= "1.1.0"}
"conduit-lwt" {>= "1.0.3"}
"conduit-lwt-ssl"
"ca-certs"
"cmdliner"
"magic-mime"
"logs"
"fmt" {>= "0.8.2"}
"cohttp-lwt" {=version}
"cohttp-lwt-unix-nossl" {=version}
"lwt" {>= "3.0.0"}
"base-unix"
"ounit" {with-test}
]
build: [
["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs]
["dune" "runtest" "-p" name "-j" jobs] {with-test}
]
dev-repo: "git+https://github.com/mirage/ocaml-cohttp.git"
pin-depends: [
[ "conduit.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
[ "conduit-tls.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
[ "conduit-lwt-ssl.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
[ "conduit-lwt.dev" "git+https://github.com/mirage/ocaml-conduit.git#4b9e8b0f8e96459c8e27ca27ed7416b8d1b5ba23" ]
]
2 changes: 2 additions & 0 deletions cohttp-lwt-unix-ssl/src/client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include Cohttp_lwt.Make_client(Cohttp_lwt_unix_nossl.IO)(Net)
5 changes: 5 additions & 0 deletions cohttp-lwt-unix-ssl/src/client.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

(** The [Client] module implements the full UNIX HTTP client interface,
including the UNIX-specific functions defined in {!C }. *)

include Cohttp_lwt.S.Client with type ctx = Conduit.resolvers
33 changes: 33 additions & 0 deletions cohttp-lwt-unix-ssl/src/cohttp_lwt_unix_ssl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(*{{{ Copyright (c) 2012 Anil Madhavapeddy <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
}}}*)

module Request = struct
include Cohttp.Request
include (Make(Cohttp_lwt_unix_nossl.IO)
: module type of Make(Cohttp_lwt_unix_nossl.IO) with type t := t)
end

module Response = struct
include Cohttp.Response
include (Make(Cohttp_lwt_unix_nossl.IO)
: module type of Make(Cohttp_lwt_unix_nossl.IO) with type t := t)
end

module Client = Client
module Server = Cohttp_lwt_unix_nossl.Server
module Debug = Cohttp_lwt_unix_nossl.Debug
module Net = Net
module IO = Cohttp_lwt_unix_nossl.IO
8 changes: 8 additions & 0 deletions cohttp-lwt-unix-ssl/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(library
(name cohttp_lwt_unix_ssl)
(public_name cohttp-lwt-unix-ssl)
(synopsis "Lwt/Unix (with OpenSSL) backend for Cohttp")
(preprocess
(pps ppx_sexp_conv))
(libraries fmt logs logs.lwt conduit-lwt conduit-lwt-ssl magic-mime lwt.unix
cohttp cohttp-lwt cohttp-lwt-unix-nossl))
Loading

0 comments on commit 1c976f0

Please sign in to comment.