Up

module Cohttp_lwt

: sig

Portable Lwt implementation of HTTP client and server, without depending on a particular I/O implementation. The various Make functors must be instantiated by an implementation that provides a concrete IO monad.

#
module type Net = sig

The Net module type defines how to connect to a remote node and close the resulting channels to clean up.

#
module IO : Cohttp.S.IO
#
type ctx
#
val default_ctx : ctx
#
val connect_uri : ctx:ctx -> Uri.t -> (IO.conn * IO.ic * IO.oc) Lwt.t
#
val close_in : IO.ic -> unit
#
val close_out : IO.oc -> unit
#
val close : IO.ic -> IO.oc -> unit
#
val sexp_of_ctx : ctx -> Sexplib.Sexp.t
end
#
module type Request = sig

The Request module combines the [root:Cohttp].Request module with the IO functions, to have them conveniently in one place.

#
type t = Cohttp.Request.t
include Cohttp.S.Request with type t := Cohttp.Request.t
include Cohttp.S.Http_io with type t := Cohttp.Request.t
#
val t_of_sexp : Sexplib.Sexp.t -> t
#
val sexp_of_t : t -> Sexplib.Sexp.t
end
#
module Make_request : functor (IO : Cohttp.S.IO) -> Request with module IO = IO

Functor to build a concrete Request from an IO implementation

#
module type Response = sig

The Response module combines the [root:Cohttp].Request module with the IO functions, to have them conveniently in one place.

#
type t = Cohttp.Response.t
include Cohttp.S.Response with type t := Cohttp.Response.t
include Cohttp.S.Http_io with type t := Cohttp.Response.t
#
val t_of_sexp : Sexplib.Sexp.t -> t
#
val sexp_of_t : t -> Sexplib.Sexp.t
end
#
module Make_response : functor (IO : Cohttp.S.IO) -> Response with module IO = IO

Functor to build a concrete Response from an IO implementation

#
module type Client = sig

The Client module implements non-pipelined single HTTP client calls. Each call will open a separate Net connection. For best results, the [root:Cohttp_lwt_body] that is returned should be consumed in order to close the file descriptor in a timely fashion. It will still be finalized by a GC hook if it is not used up, but this can take some additional time to happen.

#
module IO : Cohttp.S.IO
#
module Request : Request
#
module Response : Response
#
type ctx
#
val default_ctx : ctx
#
val call : ?ctx:ctx -> ?headers:Cohttp.Header.t -> ?body:Cohttp_lwt_body.t -> ?chunked:bool -> Cohttp.Code.meth -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t

call ?ctx ?headers ?body ?chunked meth uri will resolve the uri to a concrete network endpoint using the resolver initialized in ctx. It will then issue an HTTP request with method meth, adding request headers from headers if present. If a body is specified then that will be included with the request, using chunked encoding if chunked is true. The default is to disable chunked encoding for HTTP request bodies for compatibility reasons.

In most cases you should use the more specific helper calls in the interface rather than invoke this function directly. See head, get and post for some examples.

#
val head : ?ctx:ctx -> ?headers:Cohttp.Header.t -> Uri.t -> Response.t Lwt.t
#
val get : ?ctx:ctx -> ?headers:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val delete : ?ctx:ctx -> ?headers:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val post : ?ctx:ctx -> ?body:Cohttp_lwt_body.t -> ?chunked:bool -> ?headers:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val put : ?ctx:ctx -> ?body:Cohttp_lwt_body.t -> ?chunked:bool -> ?headers:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val patch : ?ctx:ctx -> ?body:Cohttp_lwt_body.t -> ?chunked:bool -> ?headers:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val post_form : ?ctx:ctx -> ?headers:Cohttp.Header.t -> params:Cohttp.Header.t -> Uri.t -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val sexp_of_ctx : ctx -> Sexplib.Sexp.t
end
#
module Make_client : functor (IO : Cohttp.S.IO with type 'a t = 'a Lwt.t) -> functor (Request : Request with module IO = IO ) -> functor (Response : Response with module IO = IO ) -> functor (Net : Net with module IO = IO ) -> Client with module IO = IO and module Request = Request and module Response = Response and type ctx = Net.ctx

The Make_client functor glues together a [root:Cohttp].S.IO implementation with [root:Cohttp].Request and [root:Cohttp].Response to send requests down a connection that is established by the Net module. The resulting module satisfies the Client module type.

#
module type Server = sig

The Server module implements a pipelined HTTP/1.1 server.

#
module IO : Cohttp.S.IO
#
module Request : Request
#
module Response : Response
#
type ctx
#
val default_ctx : ctx
#
type conn = IO.conn * Cohttp.Connection.t
#
type t
#
val make : ?conn_closed:(conn -> unit) -> callback:(conn -> Cohttp.Request.t -> Cohttp_lwt_body.t -> (Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t) -> unit -> t
#
val resolve_local_file : docroot:string -> uri:Uri.t -> string

Resolve a URI and a docroot into a concrete local filename.

#
val respond : ?headers:Cohttp.Header.t -> ?flush:bool -> status:Cohttp.Code.status_code -> body:Cohttp_lwt_body.t -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t

respond ?headers ?flush ~status ~body will respond to an HTTP request with the given status code and response body. If flush is true, then every response chunk will be flushed to the network rather than being buffered. flush is true by default.

#
val respond_string : ?headers:Cohttp.Header.t -> status:Cohttp.Code.status_code -> body:string -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val respond_error : ?headers:Cohttp.Header.t -> ?status:Cohttp.Code.status_code -> body:string -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val respond_redirect : ?headers:Cohttp.Header.t -> uri:Uri.t -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val respond_need_auth : ?headers:Cohttp.Header.t -> auth:Cohttp.Auth.challenge -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val respond_not_found : ?uri:Uri.t -> unit -> (Response.t * Cohttp_lwt_body.t) Lwt.t
#
val callback : t -> IO.conn -> IO.ic -> IO.oc -> unit Lwt.t
#
val sexp_of_ctx : ctx -> Sexplib.Sexp.t
end
#
module Make_server : functor (IO : Cohttp.S.IO with type 'a t = 'a Lwt.t) -> functor (Request : Request with module IO = IO ) -> functor (Response : Response with module IO = IO ) -> functor (Net : Net with module IO = IO ) -> Server with module IO = IO and module Request = Request and module Response = Response and type ctx = Net.ctx

The Make_server functor glues together a [root:Cohttp].S.IO implementation with [root:Cohttp].Request and [root:Cohttp].Response to send requests down a connection that is established by the Net module. The resulting module satisfies the Server module type.

end