Header store#

Stores header fields back to back as NUL-terminated name and value pairs in a caller-supplied buffer. Lookups are case-insensitive and return the first match.

struct lwhttp_headers_t#

Header store: name/value pairs packed into a caller-supplied buffer.

Each field takes strlen(name) + strlen(value) + 2 bytes. Lookups are case-insensitive and duplicate names are kept as separate entries in insertion order. Initialise with lwhttp_headers_init(); the fields are read-only for applications.

char *data#

Storage buffer.

size_t size#

Size of lwhttp_headers_t.data in bytes.

size_t len#

Bytes of lwhttp_headers_t.data in use.

size_t count#

Number of stored fields.

void lwhttp_headers_init(lwhttp_headers_t *headers, char *data, size_t size)#

Initialise an empty header store over a caller-supplied buffer.

Parameters:
  • headers – Store to initialise.

  • data – Storage buffer; must stay valid while the store is used.

  • size – Size of data in bytes.

void lwhttp_headers_clear(lwhttp_headers_t *headers)#

Remove all fields. The buffer is kept and pointers previously returned become stale.

Parameters:
  • headers – The store.

lwhttp_error_t lwhttp_headers_add(lwhttp_headers_t *headers, const char *name, const char *value)#

Append a copy of a field. Names and values are not validated, and existing fields with the same name are kept.

Parameters:
  • headers – The store.

  • name – NUL-terminated field name.

  • value – NUL-terminated field value.

Returns:

LWHTTP_OK, or LWHTTP_ERR_HEADERS_TOO_LARGE if it does not fit (the store is unchanged).

const char *lwhttp_headers_get(const lwhttp_headers_t *headers, const char *name)#

Find the first field with a given name, ignoring case.

Parameters:
  • headers – The store, may be NULL.

  • name – Field name, may be NULL.

Returns:

NUL-terminated value inside the store, or NULL if absent or an argument is NULL. Valid until the store is cleared or its buffer reused.

bool lwhttp_headers_at(const lwhttp_headers_t *headers, size_t index, const char **name, const char **value)#

Get a field by position, in insertion order. Use it to read repeated fields such as Set-Cookie.

Parameters:
  • headers – The store, may be NULL.

  • index – Zero-based position.

  • name – Receives the NUL-terminated name, or NULL if not wanted.

  • value – Receives the NUL-terminated value, or NULL if not wanted.

Returns:

true on success; false if index is out of range or headers is NULL.

size_t lwhttp_headers_count(const lwhttp_headers_t *headers)#

Number of stored fields.

Parameters:
  • headers – The store, may be NULL.

Returns:

Field count, 0 for NULL.