package cohttp-eio

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Server is a HTTP 1.1 server.

The request headers, a reader for the socket, and the address of the client. To read the request body, use read_fixed or read_chunked.

type response = Http.Response.t * Body.t
type handler = request -> response
type 'a env = < domain_mgr : Eio.Domain_manager.t ; net : Eio.Net.t ; clock : Eio.Time.clock.. > as 'a

Request Body

val read_fixed : Http.Request.t -> Eio.Buf_read.t -> string option

read_fixed (request, buf_read) is Some content, where content is of length n if "Content-Length" header is a valid integer value n in request.

buf_read is updated to reflect that n bytes was read.

If "Content-Length" header is missing or is an invalid value in request OR if the request http method is not one of POST, PUT or PATCH, then None is returned.

val read_chunked : Http.Request.t -> Eio.Buf_read.t -> (Body.chunk -> unit) -> Http.Header.t option

read_chunked request buf_read chunk_handler is Some updated_headers if "Transfer-Encoding" header value is "chunked" in request and all chunks in buf_read are read successfully. updated_headers is the updated headers as specified by the chunked encoding algorithm in https: //datatracker.ietf.org/doc/html/rfc7230#section-4.1.3.

buf_read is updated to reflect the number of bytes read. Returns None if Transfer-Encoding header in headers is not specified as "chunked"

Response

val text_response : string -> response

text t s returns a HTTP/1.1, 200 status response with "Content-Type" header set to "text/plain".

val html_response : string -> response

html t s returns a HTTP/1.1, 200 status response with header set to "Content-Type: text/html".

val not_found_response : response

not_found t returns a HTTP/1.1, 404 status response.

val internal_server_error_response : response

internal_server_error returns a HTTP/1.1, 500 status response.

val bad_request_response : response

Run Server

val run : ?socket_backlog:int -> ?domains:int -> port:int -> 'a env -> handler -> 'b

run ~socket_backlog ~domains ~port env handler runs a HTTP/1.1 server executing handler and listening on port. env corresponds to Eio.Stdenv.t.

socket_backlog is the number of pending connections for tcp server socket. The default is 128.

domains is the number of OCaml 5.0 domains the server will use. The default is 1. You may use Domain.recommended_domain_count to configure a multicore capable server.

val connection_handler : handler -> Eio.Time.clock -> Eio.Flow.two_way -> Eio.Net.Sockaddr.stream -> unit

connection_handler request_handler is a connection handler, suitable for passing to Eio.Net.accept_fork.

Basic Handlers

val not_found_handler : handler

not_found_handler return HTTP 404 response.

OCaml

Innovation. Community. Security.