Http.Resp
HTTP responses.
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
.
The type for response bodies. This is either:
val empty_body : body
empty_body s
is an empty 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 = resp
The type for responses.
val v : ?version:version -> ?explain:string -> ?reason:string -> ?body:body -> ?headers:headers -> status -> resp
v status headers body
is a response with given status
, headers
(defaults to Http.Headers.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.
FIXME. Maybe make body
non-optional to encourage use of empty
which is clearer in code.
Note. If body
is body_empty
(default) a Http.content_length
of 0
is automatically added to headers
.
val reason : resp -> string
reason r
is r
's reason phrase.
val explain : resp -> string
explain r
is r
's explanation. In contrast to reason
this remains on the server and can be, for example logged.
with_status st r
is r
with status st
and reason phrase reason
(defaults to Http
.status_reason_phrase.
override_headers by r
is r
with headers H.override (headers r) ~by
.
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
.
The optional headers
argument of the functions below always Http
.override those the function computed.
See also request deconstruction combinators.
FIXME. Do a better compositional design, e.g. easily use the error responses with content responses. *
empty ?explain ?reason ?headers st
is v ?explain ?reason ?headers st
.
val content : ?explain:string -> ?reason:string -> ?headers:headers -> mime_type:mime_type ->
int -> string -> resp
content ~mime_type st s
responds s
with content type mime_type
and status st
. Sets Http.content_type
and Http.content_length
appropriately.
text
responds with UTF-8 encoded plain text, i.e. content
with Http.Mime_type.text_plain
.
html
responds with UTF-8 encoded HTML text, i.e. content
with Http.Mime_type.text_html
.
json
responds with JSON text, i.e. content
with Http.Mime_type.application_json
.
octets
responds with octets, i.e. content
with Http.Mime_type.application_octet_stream
.
redirect status loc
redirects to location loc
with status status
(defaults to Http.found_302
). See also Req.service_redirect
.
val bad_request_400 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
bad_request ?explain ?reason ()
is an empty
response with status Http.bad_request_400
.
val unauthorized_401 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
unauthorized ?explain ?reason ()
is an empty
response with status Http.unauthorized_401
.
val forbidden_403 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
forbidden ?explain ?reason
is an empty
response with status Http.forbidden_403
.
val not_found_404 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
not_found ?explain ?reason
is an empty
response with status Http.not_found_404
.
val method_not_allowed_405 : ?explain:string -> ?reason:string -> ?headers:headers ->
allowed:meth list -> unit -> ('a, t) Stdlib.result
method_not_allowed ~allowed
is an empty
response with status Http.method_not_allowed_405
. It sets the Http.allow
header to the allow
ed methods (which can be empty).
val gone_410 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
not_found ?explain ?reason
is an empty
response with status Http.gone_410
.
val server_error_500 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
server_error ?explain ?reason
is an empty
response with status Http.server_error_500
.
val not_implemented_501 : ?explain:string -> ?reason:string -> ?headers:headers ->
unit -> ('a, resp) Stdlib.result
server_error ?explain ?reason
is an empty
response with status Http.not_implemented_501
.