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