FakeRiak.HTTP (fake_riak v0.1.0)

Copy Markdown View Source

A minimal HTTP/1.1 request parser and response builder shared by the Riak and etcd handlers (both of which speak HTTP over TCP).

It only implements the slice of HTTP these fake services need: a request line, headers, and a Content-Length-delimited body. Chunked transfer encoding is not supported.

Summary

Functions

Parse a single request out of buffer.

Parse an application/x-www-form-urlencoded body into a map.

Build an HTTP/1.1 response. Content-Length is always set; extra headers can be supplied as a keyword list.

Percent-decode a URI component (e.g. %2F -> /, + -> space).

Types

request()

@type request() :: %{
  method: String.t(),
  path: String.t(),
  query: String.t(),
  headers: %{optional(String.t()) => String.t()},
  body: binary()
}

Functions

parse(buffer)

@spec parse(binary()) :: {:ok, request(), binary()} | :incomplete | :error

Parse a single request out of buffer.

Returns {:ok, request, rest} when a full request (headers + declared body) is present, :incomplete when more bytes are needed, or :error when the request is malformed (e.g. a garbage Content-Length) and the connection should be dropped — without a valid length the request boundary is unknowable.

parse_form(body)

@spec parse_form(binary()) :: %{optional(String.t()) => String.t()}

Parse an application/x-www-form-urlencoded body into a map.

response(status, reason, body, headers \\ [])

@spec response(non_neg_integer(), String.t(), iodata(), keyword()) :: iodata()

Build an HTTP/1.1 response. Content-Length is always set; extra headers can be supplied as a keyword list.

uri_decode(bin)

@spec uri_decode(binary()) :: binary()

Percent-decode a URI component (e.g. %2F -> /, + -> space).