Headers

class Headers

A map of the key value pairs from the header portion of an HTTP request or response. The fields listed below are some common headers but the list is not exhaustive.

When each header is serialized, it is added as a property with lower_snake_case. For example, X-Forwarded-For becomes x_forwarded_for.

Typically each header’s value will be a string, however if there are multiple entries for any header, it will be a table of strings.

static serialize_header(key, value)

Serialize a key value pair w/o the trailing new line

If the provided value is a string[], it will be joined with rn into one string, though no trailing new line will be provided

Parameters:
  • key (str) –

  • value (str or list[str]) –

Return type:

str

serialize()

Serialize the whole set of headers separating them with a ‘\r\n’

Return type:

str

append_chunk(text)

Append a chunk of headers to this map

Parameters:

text (str) –

Returns:

success 1 if successful

Return type:

integer or nil

Returns:

err if ret1 is nil an error message

Return type:

nil or str

static from_chunk(text)

Constructor for a Headers instance with the provided text

Parameters:

text (str) –

Return type:

Headers or nil

Return type:

nil or str

static new()

Bare constructor

Return type:

Headers

static normalize_key(key)

Convert a standard header key to the normalized

lua identifer used by this collection

Parameters:

key (str) –

Return type:

str

append(key, value)

Insert a single key value pair to the collection will duplicate existing keys

Parameters:
  • key (str) –

  • value (str or nil) –

Return type:

Headers

replace(key, value)

Insert a single key value pair to the collection will not duplicate keys

Parameters:
  • key (str) –

  • value (str) –

Return type:

Headers

get_one(key)

Get a header from the map of headers

This will first normalize the provided key. For example ‘Content-Type’ will be normalized to content_type. If more than one value is provided for that header, the last value will be provided

Parameters:

key (str) –

Return type:

str

get_all(key)

Get a header from the map of headers as a list of strings.

In the event that a header’s key is duplicated, the value is stored internally as a list of values. This method is useful for getting that list.

This will first normalize the provided key. For example ‘Content-Type’ will be normalized to content_type.

Parameters:

key (str) –

Return type:

list[str]

iter()

Return a lua iterator over the key/value pairs in this header map

Returns:

():string|nil

Return type:

function