A backend that provides IO operations, network operations, etc.
val create_from :
?buf_size:int ->?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list->backend:(moduleIO_BACKEND)->unit ->t
Create a new webserver using provided backend.
The server will not do anything until run is called on it. Before starting the server, one can use add_path_handler and set_top_handler to specify how to handle incoming requests.
Port on which the server listens. Note that this might be different than the port initially given if the port was 0 (meaning that the OS picks a port for us).
Add a callback for every request. The callback can provide a stream transformer and a new request (with modified headers, typically). A possible use is to handle decompression by looking for a Transfer-Encoding header and returning a stream transformer that decompresses on the fly.
Add a callback for every request/response pair. Similarly to add_encode_response_cb the callback can return a new response, for example to compress it. The callback is given the query with only its headers, as well as the current response.
This handler is called with any request not accepted by any handler installed via add_path_handler. If no top handler is installed, unhandled paths will return a 404 not found
This used to take a string Request.t but it now takes a byte_stream Request.t since 0.14 . Use Request.read_body_full to read the body into a string if needed.
add_route_handler server Route.(exact "path" @/ string @/ int @/ return) f calls f "foo" 42 request when a request with path "path/foo/42/" is received.
Note that the handlers are called in the reverse order of their addition, so the last registered handler can override previously registered ones.
parametermeth
if provided, only accept requests with the given method. Typically one could react to `GET or `PUT.
parameteraccept
should return Ok() if the given request (before its body is read) should be accepted, Error (code,message) if it's to be rejected (e.g. because its content is too big, or for some permission error). See the http_of_dir program for an example of how to use accept to filter uploads that are too large before the upload even starts. The default always returns Ok(), i.e. it accepts all requests.
Similar to add_route_handler, but where the body of the request is a stream of bytes that has not been read yet. This is useful when one wants to stream the body directly into a parser, json decoder (such as Jsonm) or into a file.
Add a handler on an endpoint, that serves server-sent events.
The callback is given a generator that can be used to send events as it pleases. The connection is always closed by the client, and the accepted method is always GET. This will set the header "content-type" to "text/event-stream" automatically and reply with a 200 immediately. See server_sent_generator for more details.
This handler stays on the original thread (it is synchronous).
since 0.9
Upgrade handlers
These handlers upgrade the connection to another protocol.
Ask the server to stop. This might not have an immediate effect as run might currently be waiting on IO.
val run : ?after_init:(unit -> unit)->t->(unit, exn)result
Run the main loop of the server, listening on a socket described at the server's creation time, using new_thread to start a thread for each new client.
This returns Ok () if the server exits gracefully, or Error e if it exits with an error.
parameterafter_init
is called after the server starts listening. since 0.13 .
val run_exn : ?after_init:(unit -> unit)->t-> unit
run_exn s is like run s but re-raises an exception if the server exits with an error.