package tiny_httpd
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=23feafd49bcb14fd43b7af513ba8d02d53ee07e2ae544195d65082a8fd69c07e
sha512=64bdc8a5608c923fdfe807dfdb7b43a7b3aadf68f854190d331552cbf12cc43d6cf63d885f07d1d1f8eaa5edf93d96656f68f7d219ac10898d8b3c7d33d1b095
doc/tiny_httpd/Tiny_httpd_server/index.html
Module Tiny_httpd_server
Source
HTTP server.
This module implements a very simple, basic HTTP/1.1 server using blocking IOs and threads.
It is possible to use a thread pool, see create
's argument new_thread
.
HTTP Methods
Headers
Headers are metadata associated with a request or response.
Requests
Requests are sent by a client, e.g. a web browser or cURL. From the point of view of the server, they're inputs.
Response Codes
Responses
Responses are what a http server, such as Tiny_httpd
, send back to the client to answer a Request.t
Routing
Basic type-safe routing of handlers based on URL paths. This is optional, it is possible to only define the root handler with something like Routes.
Middlewares
A middleware can be inserted in a handler to modify or observe its behavior.
Main Server type
val create :
?masksigpipe:bool ->
?max_connections:int ->
?timeout:float ->
?buf_size:int ->
?get_time_s:(unit -> float) ->
?new_thread:((unit -> unit) -> unit) ->
?addr:string ->
?port:int ->
?sock:Unix.file_descr ->
?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list ->
unit ->
t
Create a new webserver using UNIX abstractions.
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.
A backend that provides IO operations, network operations, etc.
val create_from :
?buf_size:int ->
?middlewares:([ `Encoding | `Stage of int ] * Middleware.t) list ->
backend:(module IO_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.
is_ipv6 server
returns true
iff the address of the server is an IPv6 address.
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).
val add_decode_request_cb :
t ->
(unit Request.t -> (unit Request.t * (byte_stream -> byte_stream)) option) ->
unit
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.
Add a middleware to every request/response pair.
Request handlers
Setup a handler called by default.
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.
val add_route_handler :
?accept:(unit Request.t -> (unit, Response_code.t * string) result) ->
?middlewares:Middleware.t list ->
?meth:Meth.t ->
t ->
('a, string Request.t -> Response.t) Route.t ->
'a ->
unit
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.
val add_route_handler_stream :
?accept:(unit Request.t -> (unit, Response_code.t * string) result) ->
?middlewares:Middleware.t list ->
?meth:Meth.t ->
t ->
('a, byte_stream Request.t -> Response.t) Route.t ->
'a ->
unit
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.
Server-sent events
EXPERIMENTAL: this API is not stable yet.
A server-side function to generate of Server-sent events.
Server-sent event generator. This generates events that are forwarded to the client (e.g. the browser).
val add_route_server_sent_handler :
?accept:(unit Request.t -> (unit, Response_code.t * string) result) ->
t ->
('a, string Request.t -> server_sent_generator -> unit) Route.t ->
'a ->
unit
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).
Run the server
Ask the server to stop. This might not have an immediate effect as run
might currently be waiting on IO.
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.