URLs#

RFC 3986 parsing into pointer and length pairs that point into the original string, and reference resolution.

struct lwhttp_url_t#

Components of a URI reference split by lwhttp_url_parse().

All pointers point into the parsed string and are not NUL-terminated; use the matching _len field. Components are not decoded or case-normalised. A pointer is meaningful only when its has_ flag is set (path is always set but may be empty; without an authority host is NULL with length 0).

const char *scheme#

Scheme without the :.

const char *authority#

Authority without the leading //: userinfo, host and port.

const char *userinfo#

Userinfo before the last @ of the authority.

const char *host#

Host, possibly empty; for an IP literal, the address without the brackets.

const char *path#

Path, possibly empty.

const char *query#

Query without the ?.

const char *fragment#

Fragment without the #.

size_t scheme_len#

Length of lwhttp_url_t.scheme.

size_t authority_len#

Length of lwhttp_url_t.authority.

size_t userinfo_len#

Length of lwhttp_url_t.userinfo.

size_t host_len#

Length of lwhttp_url_t.host.

size_t path_len#

Length of lwhttp_url_t.path.

size_t query_len#

Length of lwhttp_url_t.query.

size_t fragment_len#

Length of lwhttp_url_t.fragment.

uint16_t port#

Port number; valid only when lwhttp_url_t.has_port is set.

bool has_scheme#

A scheme is present (the reference is absolute).

bool has_authority#

An authority (//...) is present.

bool has_userinfo#

The authority contains userinfo.

bool has_port#

The authority contains a non-empty port (host: with nothing after the colon does not count).

bool has_query#

A query is present, possibly empty.

bool has_fragment#

A fragment is present, possibly empty.

bool ipv6#

The host was written as a bracketed IP literal ([...]).

lwhttp_error_t lwhttp_url_parse(const char *s, lwhttp_url_t *url)#

Split a URI reference (absolute URL or relative reference) into its components, following RFC 3986.

Nothing is copied or decoded; the result points into s, which must outlive it. A reference without // has no authority, so "example.com/" is parsed as a path.

Parameters:
  • s – NUL-terminated URL.

  • url – Receives the components.

Returns:

LWHTTP_OK, LWHTTP_ERR_ARG if an argument is NULL, or LWHTTP_ERR_URL if s contains spaces or control characters, the port is not a number up to 65535, or an IP literal is malformed.

lwhttp_error_t lwhttp_url_resolve(const char *base, const char *reference, char *out, size_t size)#

Resolve a reference against a base URL (RFC 3986 section 5), removing dot segments.

The fragment of the result comes from reference only. This is how the client resolves redirect Location values.

Parameters:
  • base – Absolute base URL (must have a scheme).

  • reference – Absolute URL or relative reference.

  • out – Receives the NUL-terminated result.

  • size – Size of out including the terminator.

Returns:

LWHTTP_OK, LWHTTP_ERR_ARG if a pointer is NULL or size is 0, LWHTTP_ERR_URL if either input fails lwhttp_url_parse() or base has no scheme, or LWHTTP_ERR_TOO_LARGE if the result does not fit out.