Module Http.Body

Message bodies.

Bytes readers and writers

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

The type for reading bytes. Pull control flow.

  • The function returns Some (bytes, first, length) for letting the client read bytes from first to first+length valid until the next call to the function. The bytes value must not be modified by the client.
  • The function returns None when there are no longer any bytes to read and subsequent calls always return None.
type byte_writer = (bytes * int * int) option -> unit

The type for writing bytes. Push control flow.

  • The function should be called with Some (byte, first, length) to write the bytes from first to first+length. These bytes should not be modified until the function returns and the function must only read the given bytes.
  • The function gets called with None to signal that there is no longer any bytes to write.

Body contents

type 'a writer = 'a -> unit

The type for body writers. These are functions defining bodies by writing them on the structure 'a given to them. Typically for service responses the connector provides a structure to write on.

type custom_content = ..

The type for custom body contents.

This allows bodies to expose connector specific readable or writable representations. For example the Webs_unix.Fd.Writer custom content defines a body content by a function that writes directly on an output file descriptor provided by the connector.

type content =
  1. | Empty

    Empty body.

  2. | Byte_reader of byte_reader

    Byte reader, pulls bytes.

  3. | Byte_writer of byte_writer writer

    Function that pushes bytes.

  4. | Custom of custom_content

    Custom content.


The type for body contents.


type t

The type for bodies.

val make : ?content_length:int -> ?content_type:Media_type.t -> content -> t

make c is a body with content c and

Raises Invalid_argument if content_length is negative.

val empty : t

empty s is a body with Empty content. The content_type is Media_type.none, the content_length is 0 and close is a nop.

val of_custom_content : ?content_length:int -> ?content_type:Media_type.t -> custom_content -> t

of_custom_content c is a body defined by the custom content c.

val of_byte_writer : ?content_length:int -> ?content_type:Media_type.t -> byte_writer writer -> t

of_byte_writer w is a body written by w.

val of_byte_reader : ?content_length:int -> ?content_type:Media_type.t -> byte_reader -> t

of_byte_reader b is a body from the given byte reader.

val of_string : ?content_type:Media_type.t -> string -> t

of_string s is a body made of string s (uses a Byte_writer). content_length is set to the length of s.

val to_byte_reader : t -> (byte_reader, string) Stdlib.result

to_byte_reader b is a byte reader on the inbound body b. This errors on Custom content. It works on Byte_writer but entails a full copy in memory.

val to_string : t -> (string, string) Stdlib.result

to_string b reads the body to a string. This errors on Custom content.

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

pp formats bodies for inspection. It guarantees not to touch the content.



val is_empty : t -> bool

is_empty b is true iff content b is Empty. Note that this does not rule out a writer that doesn't write any data.


val content : t -> content

content b is the content of b.

val content_type : t -> Media_type.t

content_type b is the media type of b.

val content_length : t -> int option

content_length b is the content length of b, if known.