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