123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231(** 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. *)(** The IO module is specialized for the [Lwt] monad. *)moduletypeIO=sigincludeCohttp.S.IOwithtype'at='aLwt.ttypeerrorvalcatch:(unit->'at)->('a,error)resultt(** [catch f] is [f () >|= Result.ok], unless [f] fails with an IO error, in
which case it returns the error. *)valpp_error:Format.formatter->error->unitend(** The [Net] module type defines how to connect to a remote node and close the
resulting channels to clean up. *)moduletypeNet=sigmoduleIO:IOtypectx[@@derivingsexp_of]valdefault_ctx:ctxvalconnect_uri:ctx:ctx->Uri.t->(IO.conn*IO.ic*IO.oc)Lwt.tvalclose_in:IO.ic->unitvalclose_out:IO.oc->unitvalclose:IO.ic->IO.oc->unitend(** The [Client] module implements non-pipelined single HTTP client calls. Each
call will open a separate {!Net} connection. For best results, the {!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. *)moduletypeClient=sigtypectxvalcall:?ctx:ctx->?headers:Cohttp.Header.t->?body:Body.t->?chunked:bool->Cohttp.Code.meth->Uri.t->(Cohttp.Response.t*Body.t)Lwt.t(** [call ?ctx ?headers ?body ?chunked meth uri] will resolve the [uri] to a
concrete network endpoint using context [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.
To avoid leaks, the body needs to be consumed, using the functions
provided in the {!Body} module and, if not necessary, should be explicitly
drained calling {!Body.drain_body}. Leaks are logged as debug messages by
the client, these can be enabled activating the debug logging. For
example, this can be done as follows in [cohttp-lwt-unix]
{[
Cohttp_lwt_unix.Debug.activate_debug ();
Logs.set_level (Some Logs.Warning)
]}
Depending on [ctx], the library is able to send a simple HTTP request or
an encrypted one with a secured protocol (such as TLS). Depending on how
conduit is configured, [ctx] might initiate a secured connection with TLS
(using [ocaml-tls]) or SSL (using [ocaml-ssl]), on [*:443] or on the
specified port by the user. If neitehr [ocaml-tls] or [ocaml-ssl] are
installed on the system, [cohttp]/[conduit] tries the usual ([*:80]) or
the specified port by the user in a non-secured way. *)valhead:?ctx:ctx->?headers:Cohttp.Header.t->Uri.t->Cohttp.Response.tLwt.tvalget:?ctx:ctx->?headers:Cohttp.Header.t->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvaldelete:?ctx:ctx->?body:Body.t->?chunked:bool->?headers:Cohttp.Header.t->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvalpost:?ctx:ctx->?body:Body.t->?chunked:bool->?headers:Cohttp.Header.t->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvalput:?ctx:ctx->?body:Body.t->?chunked:bool->?headers:Cohttp.Header.t->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvalpatch:?ctx:ctx->?body:Body.t->?chunked:bool->?headers:Cohttp.Header.t->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvalpost_form:?ctx:ctx->?headers:Cohttp.Header.t->params:(string*stringlist)list->Uri.t->(Cohttp.Response.t*Body.t)Lwt.tvalcallv:?ctx:ctx->Uri.t->(Cohttp.Request.t*Body.t)Lwt_stream.t->(Cohttp.Response.t*Body.t)Lwt_stream.tLwt.tend(** The [Server] module implements a pipelined HTTP/1.1 server. *)moduletypeServer=sigmoduleIO:IOtypeconn=IO.conn*Cohttp.Connection.ttyperesponse_action=[`ExpertofCohttp.Response.t*(IO.ic->IO.oc->unitLwt.t)|`ResponseofCohttp.Response.t*Body.t](** A request handler can respond in two ways:
- Using [`Response], with a {!Response.t} and a {!Body.t}.
- Using [`Expert], with a {!Response.t} and an IO function that is
expected to write the response body. The IO function has access to the
underlying {!IO.ic} and {!IO.oc}, which allows writing a response body
more efficiently, stream a response or to switch protocols entirely
(e.g. websockets). Processing of pipelined requests continue after the
{!unit Lwt.t} is resolved. The connection can be closed by closing the
{!IO.ic}. *)typetvalmake_response_action:?conn_closed:(conn->unit)->callback:(conn->Cohttp.Request.t->Body.t->response_actionLwt.t)->unit->tvalmake_expert:?conn_closed:(conn->unit)->callback:(conn->Cohttp.Request.t->Body.t->(Cohttp.Response.t*(IO.ic->IO.oc->unitLwt.t))Lwt.t)->unit->tvalmake:?conn_closed:(conn->unit)->callback:(conn->Cohttp.Request.t->Body.t->(Cohttp.Response.t*Body.t)Lwt.t)->unit->tvalresolve_local_file:docroot:string->uri:Uri.t->string(** Resolve a URI and a docroot into a concrete local filename.
Deprecated. Please use Cohttp.Path.resolve_local_file. *)valrespond:?headers:Cohttp.Header.t->?flush:bool->status:Cohttp.Code.status_code->body:Body.t->unit->(Cohttp.Response.t*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. The transfer encoding will be
detected from the [body] value and set to chunked encoding if it cannot be
determined immediately. You can override the encoding by supplying an
appropriate [Content-length] or [Transfer-encoding] in the [headers]
parameter. *)valrespond_string:?flush:bool->?headers:Cohttp.Header.t->status:Cohttp.Code.status_code->body:string->unit->(Cohttp.Response.t*Body.t)Lwt.tvalrespond_error:?headers:Cohttp.Header.t->?status:Cohttp.Code.status_code->body:string->unit->(Cohttp.Response.t*Body.t)Lwt.tvalrespond_redirect:?headers:Cohttp.Header.t->uri:Uri.t->unit->(Cohttp.Response.t*Body.t)Lwt.tvalrespond_need_auth:?headers:Cohttp.Header.t->auth:Cohttp.Auth.challenge->unit->(Cohttp.Response.t*Body.t)Lwt.tvalrespond_not_found:?uri:Uri.t->unit->(Cohttp.Response.t*Body.t)Lwt.tvalcallback:t->IO.conn->IO.ic->IO.oc->unitLwt.tend