Loading...
Searching...
No Matches
urlparser::url Class Reference

Represents a URL. More...

#include <urlparser.h>

Collaboration diagram for urlparser::url:

Public Types

using search_params = std::unordered_map<std::string, std::vector<std::string>>
 A decoded key → values map of the query string (like JS's URLSearchParams, or ada_url.parse_search_params()). A repeated key (e.g. "a=1&a=3") collects every value under that key, in order; a key with no '=' maps to one empty string.

Public Member Functions

 url () noexcept=default
 Default constructor for the url class. Creates an empty URL object.
 url (const char *text, const bool ignore_www=DEFAULT_IGNORE_WWW)
 url (const std::string &text, const bool ignore_www=DEFAULT_IGNORE_WWW)
 Same as the string_view constructor. These two overloads exist so a std::string or a string literal converts to a url implicitly (one user conversion): a function taking const urlparser::url& can then be called as f("http://example.com/") or f(some_std_string), which the string_view constructor alone does not allow (std::string -> string_view -> url is two).
 url (std::string_view url, const bool ignore_www=DEFAULT_IGNORE_WWW)
 Construct a url object from a given URL string.
const std::string & abspath () const noexcept
 The path, with '.'/'..' segments resolved and repeated '/' collapsed. A trailing '/' is kept (RFC 3986 5.2.4): "/a/" stays "/a/", and a path that ends in a dot segment ends in '/' too ("/a/b/.." is "/a/").
int effective_port () const noexcept
 The port a connection to this URL uses: port() when the URL names one, else the default port of its scheme (see default_port()), else 0. (Called port_or_default() up to 2.3.1.).
std::string_view fragment () const noexcept
 The fragment of the URL (the part after '#').
search_params get_search_params () const
 Build the decoded key → values map described on search_params above.
const urlparser::host & host () const noexcept
 The host part of the URL, classified as a hostname, ipv4, or ipv6. Use std::get_if<hostname>/<ipv4>/<ipv6>(&url.host()) or std::visit to access it - see the file-level docs for an example.
std::string_view host_text () const noexcept
 The raw host text (e.g., "example.com", "192.0.2.1", or "[::1]"), before hostname/ip classification.
std::string normalized () const
 The URL's canonical, normalized form (roughly WHATWG URL "serialize a URL" / ada's normalize_url()): scheme/host already lowercase at parse time here (nothing to do for those), plus what str() alone doesn't give you - the scheme's default port omitted (e.g. no ":443" for an explicit "https://host:443/..."), the host IDNA-normalized to ASCII/Punycode (see idna::to_ascii - so a Unicode host normalizes the same as its already-ASCII form), and the path with '.'/'..' segments resolved (str() keeps the raw path; this uses abspath() instead - so this also inherits abspath()'s collapsing of repeated slashes ("/a//b" -> "/a/b"), deliberate there (see AbspathCollapsesRepeatedSlashes in tests/cpp/test_url.cpp) but a real, known difference from ada's normalize_url(), which keeps them as-is. A trailing '/' is kept, as in ada: "/a" and "/a/" stay two different paths. Query and fragment are kept exactly as given - unlike a path, there's no single canonical form for a query string's parameter order to collapse to, so this doesn't try. Leading/trailing whitespace around the original input is already handled at parse time (see the url(...) constructor), not here.
bool operator== (const url &other) const noexcept
 Equality operator for comparing two url objects.
query_params params () const noexcept
 The '&'-separated query parameters, split into a vector.
std::string_view password () const noexcept
 The part of userinfo() after the first ':', raw and still percent-encoded; empty when userinfo() has no ':'. Decode it with percent_codec::decode() when needed.
std::string_view path () const noexcept
 The raw path of the URL, exactly as it appears in the source (no dot-segment resolution - see abspath() for that).
int port () const noexcept
 The port number of the URL, or 0 if not specified.
std::string_view protocol () const noexcept
 The protocol of the URL (e.g., "http", "https", "ftp").
std::string_view query () const noexcept
 The query string of the URL (e.g., "a=1&b=2").
std::string str () const noexcept
 The complete URL as a string.
std::string_view userinfo () const noexcept
 The userinfo of the URL (e.g., "username:password").
std::string_view username () const noexcept
 The part of userinfo() before the first ':' (the whole userinfo when it has no ':'), raw and still percent-encoded like path() and query(). Decode it with percent_codec::decode() when needed.

Static Public Member Functions

static int default_port (std::string_view scheme) noexcept
 The default port of a scheme, or 0 when the scheme has none. Covers the WHATWG "special" schemes (http 80, https 443, ws 80, wss 443, ftp 21) and the SOCKS proxy schemes (socks5 and socks5h, 1080, the port RFC 1928 and curl give them) - the same table normalized() uses to drop a redundant port, so "socks5://h:1080" normalizes to "socks5://h". Any other scheme has none (0), whatever the protocol's own convention (ssh 22, ...): the table lists the schemes a connection can be made to by this URL alone. The scheme is matched as given (lowercase, as protocol() returns it).
static std::string extract_host (const char *url) noexcept
 Extract the host from a given URL literal. Disambiguation overload: without this, extract_host("literal") would be an ambiguous call between the string_view and string&& overloads (a const char* converts equally well to either). Routes to the string_view overload, since a literal has no heap buffer to reuse.
static std::string extract_host (std::string &&url) noexcept
 Extract the host from a given URL, taking ownership of it. Reuses url's own buffer (via erase()) to build the result instead of allocating a new string, so prefer this overload when you have an owned/temporary std::string you don't need afterward.
static std::string extract_host (std::string_view url) noexcept
 Extract the host from a given URL, without fully parsing it.
static bool is_psl_loaded () noexcept
 Check if the Public Suffix List (PSL) is loaded.

Detailed Description

Represents a URL.

The url class provides functionalities for parsing and managing URLs. It allows access to various components of a URL such as protocol, host, query, fragment, userinfo, port, and parameters.

Like hostname, url is a plain value type - parsing happens once in the constructor; the only deferred work is classifying/building the (cheap) host, which only happens if .host() is actually called.

Internally, all string fields (scheme, userinfo, host, path, params, query, fragment) are carved out of a single owned buffer, sized exactly once at construction - instead of each field being its own separately heap-allocated std::string. Since fields are stored as (offset, length) pairs into that one buffer rather than raw pointers, copying a url is automatically correct (no dangling views) with no custom copy/move logic needed.

Member Typedef Documentation

◆ search_params

using urlparser::url::search_params = std::unordered_map<std::string, std::vector<std::string>>

A decoded key → values map of the query string (like JS's URLSearchParams, or ada_url.parse_search_params()). A repeated key (e.g. "a=1&a=3") collects every value under that key, in order; a key with no '=' maps to one empty string.

Decoding follows application/x-www-form-urlencoded, as URLSearchParams does: a literal '+' is a space ("q=hello+world" -> "hello world"; "%2B" is how a real plus is written), XX becomes its byte, and malformed escapes are left as-is. That's deliberately different from percent_codec::decode(), the generic RFC 3986 "unquote", which leaves '+' alone.

Gap this closes: params() above only ever gave the raw, still percent-encoded "key=value" strings (by design - see percent_codec's own doc comment on why liburlparser doesn't decode implicitly); there was no built-in way to get "the value of query parameter x" without manually splitting on '=' and decoding yourself.

Constructor & Destructor Documentation

◆ url() [1/4]

urlparser::url::url ( std::string_view url,
const bool ignore_www = DEFAULT_IGNORE_WWW )

Construct a url object from a given URL string.

Parameters
urlThe URL string to parse.
ignore_wwwWhether to ignore the "www" subdomain. Default is false.
Exceptions
std::invalid_argumentIf the URL is malformed or cannot be parsed.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url() [2/4]

urlparser::url::url ( const std::string & text,
const bool ignore_www = DEFAULT_IGNORE_WWW )
inline

Same as the string_view constructor. These two overloads exist so a std::string or a string literal converts to a url implicitly (one user conversion): a function taking const urlparser::url& can then be called as f("http://example.com/") or f(some_std_string), which the string_view constructor alone does not allow (std::string -> string_view -> url is two).

Here is the call graph for this function:

◆ url() [3/4]

urlparser::url::url ( const char * text,
const bool ignore_www = DEFAULT_IGNORE_WWW )
inline
Here is the call graph for this function:

◆ url() [4/4]

urlparser::url::url ( )
defaultnoexcept

Default constructor for the url class. Creates an empty URL object.

Member Function Documentation

◆ abspath()

const std::string & urlparser::url::abspath ( ) const
noexcept

The path, with '.'/'..' segments resolved and repeated '/' collapsed. A trailing '/' is kept (RFC 3986 5.2.4): "/a/" stays "/a/", and a path that ends in a dot segment ends in '/' too ("/a/b/.." is "/a/").

◆ default_port()

int urlparser::url::default_port ( std::string_view scheme)
staticnoexcept

The default port of a scheme, or 0 when the scheme has none. Covers the WHATWG "special" schemes (http 80, https 443, ws 80, wss 443, ftp 21) and the SOCKS proxy schemes (socks5 and socks5h, 1080, the port RFC 1928 and curl give them) - the same table normalized() uses to drop a redundant port, so "socks5://h:1080" normalizes to "socks5://h". Any other scheme has none (0), whatever the protocol's own convention (ssh 22, ...): the table lists the schemes a connection can be made to by this URL alone. The scheme is matched as given (lowercase, as protocol() returns it).

Here is the caller graph for this function:

◆ effective_port()

int urlparser::url::effective_port ( ) const
inlinenoexcept

The port a connection to this URL uses: port() when the URL names one, else the default port of its scheme (see default_port()), else 0. (Called port_or_default() up to 2.3.1.).

Here is the call graph for this function:

◆ extract_host() [1/3]

std::string urlparser::url::extract_host ( const char * url)
staticnoexcept

Extract the host from a given URL literal. Disambiguation overload: without this, extract_host("literal") would be an ambiguous call between the string_view and string&& overloads (a const char* converts equally well to either). Routes to the string_view overload, since a literal has no heap buffer to reuse.

Here is the call graph for this function:

◆ extract_host() [2/3]

std::string urlparser::url::extract_host ( std::string && url)
staticnoexcept

Extract the host from a given URL, taking ownership of it. Reuses url's own buffer (via erase()) to build the result instead of allocating a new string, so prefer this overload when you have an owned/temporary std::string you don't need afterward.

Parameters
urlThe URL string, left in a valid but unspecified state.
Here is the call graph for this function:

◆ extract_host() [3/3]

std::string urlparser::url::extract_host ( std::string_view url)
staticnoexcept

Extract the host from a given URL, without fully parsing it.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fragment()

std::string_view urlparser::url::fragment ( ) const
inlinenoexcept

The fragment of the URL (the part after '#').

◆ get_search_params()

search_params urlparser::url::get_search_params ( ) const

Build the decoded key → values map described on search_params above.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host()

const urlparser::host & urlparser::url::host ( ) const
noexcept

The host part of the URL, classified as a hostname, ipv4, or ipv6. Use std::get_if<hostname>/<ipv4>/<ipv6>(&url.host()) or std::visit to access it - see the file-level docs for an example.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_text()

std::string_view urlparser::url::host_text ( ) const
inlinenoexcept

The raw host text (e.g., "example.com", "192.0.2.1", or "[::1]"), before hostname/ip classification.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_psl_loaded()

bool urlparser::url::is_psl_loaded ( )
staticnoexcept

Check if the Public Suffix List (PSL) is loaded.

◆ normalized()

std::string urlparser::url::normalized ( ) const

The URL's canonical, normalized form (roughly WHATWG URL "serialize a URL" / ada's normalize_url()): scheme/host already lowercase at parse time here (nothing to do for those), plus what str() alone doesn't give you - the scheme's default port omitted (e.g. no ":443" for an explicit "https://host:443/..."), the host IDNA-normalized to ASCII/Punycode (see idna::to_ascii - so a Unicode host normalizes the same as its already-ASCII form), and the path with '.'/'..' segments resolved (str() keeps the raw path; this uses abspath() instead - so this also inherits abspath()'s collapsing of repeated slashes ("/a//b" -> "/a/b"), deliberate there (see AbspathCollapsesRepeatedSlashes in tests/cpp/test_url.cpp) but a real, known difference from ada's normalize_url(), which keeps them as-is. A trailing '/' is kept, as in ada: "/a" and "/a/" stay two different paths. Query and fragment are kept exactly as given - unlike a path, there's no single canonical form for a query string's parameter order to collapse to, so this doesn't try. Leading/trailing whitespace around the original input is already handled at parse time (see the url(...) constructor), not here.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator==()

bool urlparser::url::operator== ( const url & other) const
noexcept

Equality operator for comparing two url objects.

Here is the call graph for this function:

◆ params()

query_params urlparser::url::params ( ) const
noexcept

The '&'-separated query parameters, split into a vector.

◆ password()

std::string_view urlparser::url::password ( ) const
inlinenoexcept

The part of userinfo() after the first ':', raw and still percent-encoded; empty when userinfo() has no ':'. Decode it with percent_codec::decode() when needed.

Here is the call graph for this function:

◆ path()

std::string_view urlparser::url::path ( ) const
inlinenoexcept

The raw path of the URL, exactly as it appears in the source (no dot-segment resolution - see abspath() for that).

◆ port()

int urlparser::url::port ( ) const
inlinenoexcept

The port number of the URL, or 0 if not specified.

◆ protocol()

std::string_view urlparser::url::protocol ( ) const
inlinenoexcept

The protocol of the URL (e.g., "http", "https", "ftp").

Here is the call graph for this function:
Here is the caller graph for this function:

◆ query()

std::string_view urlparser::url::query ( ) const
inlinenoexcept

The query string of the URL (e.g., "a=1&b=2").

◆ str()

std::string urlparser::url::str ( ) const
noexcept

The complete URL as a string.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ userinfo()

std::string_view urlparser::url::userinfo ( ) const
inlinenoexcept

The userinfo of the URL (e.g., "username:password").

Here is the caller graph for this function:

◆ username()

std::string_view urlparser::url::username ( ) const
inlinenoexcept

The part of userinfo() before the first ':' (the whole userinfo when it has no ':'), raw and still percent-encoded like path() and query(). Decode it with percent_codec::decode() when needed.

Here is the call graph for this function:

The documentation for this class was generated from the following file:
  • /home/runner/work/liburlparser/liburlparser/include/urlparser.h