Module Http.Body

Message bodies.

Important. Bodies are ressources, they must be properly consumed or dismissed if not used.

Body contents

type bytes_writer = eod:bool -> Bytesrw.Bytes.Writer.t -> unit

The type for functions writing on the given bytes writer. The bytes writer must write an Bytesrw.Bytes.Slice.eod before returning if and only if eod is true.

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. | Bytes_reader of Bytesrw.Bytes.Reader.t
    (*

    Bytes reader, pulls bytes.

    *)
  3. | Bytes_writer of bytes_writer
    (*

    Function that pushes bytes.

    *)
  4. | Custom of custom_content
    (*

    Custom content.

    *)

The type for body contents.

Bodies

type t

The type for bodies.

val make : ?content_length:int -> ?content_type:Media_type.t -> ?finally:(unit -> unit) -> 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 -> ?finally:(unit -> unit) -> custom_content -> t

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

val of_bytes_writer : ?content_length:int -> ?content_type:Media_type.t -> ?finally:(unit -> unit) -> bytes_writer -> t

of_byte_writer w is a body written by w.

val of_bytes_reader : ?content_length:int -> ?content_type:Media_type.t -> ?finally:(unit -> unit) -> Bytesrw.Bytes.Reader.t -> 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.

Properties

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.

val finally : t -> unit -> unit

finally b is the function called whenever the body contents has been consumed. Unless otherwise noted this is called automatically by consuming functions.

Consuming

val dismiss : t -> unit

dismiss b must be called if the body will not be consumed.

val write : eod:bool -> Bytesrw.Bytes.Writer.t -> t -> (unit, string) Stdlib.result

write ~eod w body writes the body on w. This errors on Custom content.

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

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

val to_bytes_reader : t -> (Bytesrw.Bytes.Reader.t, string) Stdlib.result

to_bytes_reader b is a bytes reader on the body b. This errors on Custom content. It works on Bytes_writers but entails a full copy in memory.

Important. After the reader has returned Bytesrw.Bytes.Slice.eod it is the client's duty to call finally b ().

Predicates

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 is_custom : t -> bool

is_custom b is true iff content b is Custom.

Formatting

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

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