howdy/context

Request context shared by handlers and guards.

Types

The body of an incoming request. Under ewe it wraps the live connection and is read from the socket on demand. In tests it holds the bytes directly, so handlers that read bodies run unchanged. Build test bodies with howdy/testing.

pub opaque type Body

Why a request body could not be read.

pub type BodyError {
  BodyTooLarge
  InvalidBody
}

Constructors

  • BodyTooLarge

    The body is larger than the limit given.

  • InvalidBody

    The body was malformed, or the client stopped sending it.

guard holds the successful controller guard value, or Nil for an ordinary controller. version is the API version resolved by howdy/version, or None for routes outside a version group.

pub type Context(guarded) {
  Context(
    request: request.Request(Body),
    params: dict.Dict(String, String),
    guard: guarded,
    version: option.Option(String),
  )
}

Constructors

A request body being read a piece at a time, so that a large one need not be held in memory. Start one with reader or controller.body_reader, then call read_chunk until it gives None. Like read_body, a body can only be read once.

pub opaque type Reader

Values

pub fn client_ip(
  request: request.Request(Body),
) -> option.Option(String)

The address the request came from: an IP for TCP connections, the socket path for Unix sockets, or None when it cannot be determined.

pub fn read_body(
  request: request.Request(Body),
  limit limit: Int,
) -> Result(BitArray, BodyError)

Read the whole request body, up to limit bytes.

pub fn read_chunk(
  reader: Reader,
  max_bytes max_bytes: Int,
) -> Result(option.Option(#(BitArray, Reader)), BodyError)

The next piece of the body, of at most max_bytes, with the reader for the piece after it, or None once the body has been read to the end. A body over the reader’s limit gives BodyTooLarge once it gets there.

fn count(reader: context.Reader, total: Int) -> Result(Int, context.BodyError) {
  case context.read_chunk(reader, max_bytes: 65_536) {
    Ok(Some(#(chunk, reader))) -> count(reader, total + bit_array.byte_size(chunk))
    Ok(None) -> Ok(total)
    Error(error) -> Error(error)
  }
}
pub fn reader(
  request: request.Request(Body),
  limit limit: Int,
) -> Reader

Start reading the body of request in pieces, refusing one larger than limit bytes in all.

✨ Search Document