Cohttp_lwt_unix.Server
The Server
module implements the full UNIX HTTP server interface, including the UNIX-specific functions defined in S
.
The Logs
source name for this module logger is "cohttp.lwt.server"
. Refer to the Debug
module for further details.
include Cohttp_lwt.S.Server with module IO = Cohttp_lwt_unix__.Io
module IO : sig ... end
The Io
module contains the IO implementation for cohttp-lwt-unix
.
type conn = IO.conn * Cohttp.Connection.t
type response_action = [
| `Expert of Cohttp.Response.t * (IO.ic -> IO.oc -> unit Lwt.t)
| `Response of Cohttp.Response.t * Cohttp_lwt.Body.t
]
A request handler can respond in two ways:
`Response
, with a Response.t
and a Body.t
.`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 unitLwt.t
is resolved. The connection can be closed by closing the !IO.ic
.val make_response_action :
?conn_closed:(conn -> unit) ->
?sleep_fn:(unit -> unit Lwt.t) ->
callback:
(conn -> Cohttp.Request.t -> Cohttp_lwt.Body.t -> response_action Lwt.t) ->
unit ->
t
make_response_action
creates a set of callbacks used by Cohttp Server.
callback
is called when a new connection is accepted by the server socket.conn_closed
if provided, will be called when the connection is closed, e.g. when an EOF is received.sleep_fn
if provided, will be used for periodic checks for EOF from the client. If this callback is not provided, Cohttp will not detect and notify the client about EOF received from the peer while the client is handling the new connection. This can lead to a resource leak if the callback
is designed to never resolve. If the connection is closed locally, Cohttp will stop waiting for EOF and will wait the promise to be cancelled.val make :
?conn_closed:(conn -> unit) ->
?sleep_fn:(unit -> unit Lwt.t) ->
callback:
(conn ->
Cohttp.Request.t ->
Cohttp_lwt.Body.t ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t) ->
unit ->
t
Resolve a URI and a docroot into a concrete local filename.
Deprecated. Please use Cohttp.Path.resolve_local_file.
val respond :
?headers:Cohttp.Header.t ->
?flush:bool ->
status:Cohttp.Code.status_code ->
body:Cohttp_lwt.Body.t ->
unit ->
(Cohttp.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. 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.
val respond_string :
?flush:bool ->
?headers:Cohttp.Header.t ->
status:Cohttp.Code.status_code ->
body:string ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
val respond_error :
?headers:Cohttp.Header.t ->
?status:Cohttp.Code.status_code ->
body:string ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
val respond_redirect :
?headers:Cohttp.Header.t ->
uri:Uri.t ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
val respond_need_auth :
?headers:Cohttp.Header.t ->
auth:Cohttp.Auth.challenge ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
val respond_not_found :
?uri:Uri.t ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
Deprecated. Please use Cohttp.Path.resolve_local_file.
val respond_file :
?headers:Cohttp.Header.t ->
fname:string ->
unit ->
(Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t
val create :
?timeout:int ->
?backlog:int ->
?stop:unit Lwt.t ->
?on_exn:(exn -> unit) ->
?ctx:Net.ctx ->
?mode:Conduit_lwt_unix.server ->
t ->
unit Lwt.t
create ?timeout ?backlog ?stop ?on_exn ?mode t
is a new HTTP server.
The user can decide to start a simple HTTP server (without encryption) or one with TLS encryption. It depends on what the user gives as mode
and how conduit-unix
is configured.
To create a simple HTTP server listening on port 8089:
let run = create (`TCP 8080)
When provided, the stop
thread will terminate the server if it ever becomes determined.
When provided, backlog
will limit the number of open connections.
Every connection will be served in a new lightweight thread that is invoked via the callback defined in t
. If the callback raises an exception, it is passed to on_exn
(by default, to a function that logs the exception using the Logs
library).