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 (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.
 
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 own fuzz-tested behavior of trimming a trailing '/' on a non-root path ("/a/" -> "/a") and collapsing repeated slashes ("/a//b" -> "/a/b"), both deliberate there (see AbspathDropsTrailingSlashOnNonRootPath / AbspathCollapsesRepeatedSlashes in tests/cpp/test_url.cpp) but a real, known difference from ada's normalize_url(), which keeps both as-is). 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 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").
 

Static Public Member Functions

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/2]

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.

◆ url() [2/2]

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.

◆ extract_host() [1/3]

static 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.

◆ extract_host() [2/3]

static 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.

◆ extract_host() [3/3]

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

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

◆ 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.

◆ 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.

◆ 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.

◆ is_psl_loaded()

static 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 own fuzz-tested behavior of trimming a trailing '/' on a non-root path ("/a/" -> "/a") and collapsing repeated slashes ("/a//b" -> "/a/b"), both deliberate there (see AbspathDropsTrailingSlashOnNonRootPath / AbspathCollapsesRepeatedSlashes in tests/cpp/test_url.cpp) but a real, known difference from ada's normalize_url(), which keeps both as-is). 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 caller graph for this function:

◆ operator==()

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

Equality operator for comparing two url objects.

◆ params()

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

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

◆ 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").

◆ 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.

◆ userinfo()

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

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


The documentation for this class was generated from the following file: