Module Webs.Resp

HTTP responses.

Response bodies

type connection = ..

The type for direct response connection. This is connector dependent.

type consumer = (bytes * int * int) option -> unit

The type for response consumers.

Services call consumers with Some (byte, first len) to output the corresponding data and None when they are finished.

Response consumers are provided by the connector to pull the body produced by a response. If you are writing a consumer, the bytes MUST NOT be modified by the consumer and only read from first to first+len.

type body =
| Empty
| Stream of consumer -> unit
| Direct of connection -> unit

The type for response bodies. This is either:

  • An empty body.
  • A stream to which a consumer will be passed by the connector.
  • A direct connection handler, the connector will pass it's connection representation.
val empty_body : body

empty_body s is an empty body.

val stream_body : (consumer -> unit) -> body

stream_body producer is a response body stream produced by producer on the consumer it will be given to.

val direct_body : (connection -> unit) -> body

direct_body producer is a response body produced by producer on the given (backend specific) connection.

val body_of_string : string -> body

body_of_string s is a reponse body made of string s.

val pp_body : Stdlib.Format.formatter -> body -> unit

pp_body ppf b prints an unspecified representation of b's specification on ppf but guarantees not to consume the body.


type t

The type for responses.

val v : ?version:Http.version -> ?explain:string -> ?reason:string -> ?body:body -> ?headers:Http.headers -> Http.status -> t

v status headers body is a response with given status, headers (defaults to Http.H.empty), body (defaults to empty_body, reason (defaults to Http.status_reason_phrase) and version (defaults to (1,1)), explain is a server side reason it is not put on the wire.

Note. If body is body_empty (default) a Http.H.content_length of 0 is automatically added to headers.

val version : t -> Http.version

version r is r's version.

val status : t -> Http.status

status r is r's status.

val reason : t -> string

reason r is r's reason phrase.

val headers : t -> Http.headers

headers r is r's headers.

val body : t -> body

body r is r's body.

val explain : t -> string

explain r is r's explanation. In contrast to reason this remains on the server and can be, for example logged.

val with_status : ?explain:string -> ?reason:string -> Http.status -> t -> t

with_status st r is r with status st and reason phrase reason (defaults to Http.status_reason_phrase.

val with_headers : Http.headers -> t -> t

with_headers hs r is r with headers hs.

val override_headers : by:Http.headers -> t -> t

override_headers by r is r with headers H.override (headers r) ~by.

val with_body : body -> t -> t

with_body b r is r with body b.

val pp : Stdlib.Format.formatter -> t -> unit

pp ppf t prints an unspecified representation of r on ppf but guarantees not to consume the body.

Pre-canned responses

The optional set argument of the functions below always Http.H.override those the function computed.

See also request deconstruction combinators.

val result : ('a'a) Stdlib.result -> 'a

result r is Result.fold

Content responses

val content : ?explain:string -> ?set:Http.headers -> mime_type:Http.mime_type -> int -> string -> t

content ~mime_type st s responds s with content type mime_type and status st. Sets Http.H.content_type and Http.H.content_length appropriately.

val text : ?explain:string -> ?set:Http.headers -> int -> string -> t

text responds with UTF-8 encoded plain text, i.e. content with Http.Mime_type.text_plain.

val html : ?explain:string -> ?set:Http.headers -> int -> string -> t

html responds with UTF-8 encoded HTML text, i.e. content with Http.Mime_type.text_html.

val json : ?explain:string -> ?set:Http.headers -> int -> string -> t

json responds with JSON text, i.e. content with Http.Mime_type.application_json.

val octets : ?explain:string -> ?set:Http.headers -> int -> string -> t

octets responds with octets, i.e. content with Http.Mime_type.application_octet_stream.

Redirect responses

val redirect : ?explain:string -> ?set:Http.headers -> int -> string -> t

redirect status loc redirects to location loc with status status (defaults to Http.s302_found). See also Req.service_redirect.

Errors responses

val method_not_allowed : ?explain:string -> ?reason:string -> ?set:Http.headers -> allowed:Http.meth list -> unit -> t

method_not_allowed ~allowed is an empty response with status Http.s405_method_not_allowed. It sets Http.H.allow to the allowed methods (which can be empty).