hostname, ipv4, or ipv6: whatever a URL's host actually is (RFC 3986's host grammar), wrapped in a uniform API that matches hostname/ipv4/ipv6/url themselves: a constructor that just parses, plus from_url() and str().
More...
#include <urlparser.h>
Collaboration diagram for urlparser::host:Public Types | |
| using | variant_type = std::variant< hostname, ipv4, ipv6 > |
Public Member Functions | |
| host () noexcept=default | |
| Default-constructs an empty hostname. | |
| host (const char *host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept | |
| Classify a host string literal. Disambiguation overload: without this, host("literal") would be ambiguous between the string_view and string&& overloads. | |
| host (std::string &&host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept | |
| Classify an already-isolated host string, taking ownership of it. When the text turns out to be a domain name, this reuses its existing allocation to build the resulting hostname instead of copying into a new one. | |
| host (std::string_view host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept | |
| Classify and parse an already-isolated host string (no scheme, no path - e.g. what url::host_text() gives you) as whichever of hostname/ipv4/ipv6 it actually is. Never throws: anything that isn't a valid IPv4/IPv6 address becomes a hostname. | |
| const hostname & | get_hostname () const |
| const ipv4 & | get_ipv4 () const |
| const ipv6 & | get_ipv6 () const |
| bool | is_hostname () const noexcept |
| bool | is_ip () const noexcept |
| True for either IP family - the opposite of is_hostname(). | |
| bool | is_ipv4 () const noexcept |
| bool | is_ipv6 () const noexcept |
| std::string | str () const |
| The text form, whichever of hostname/ipv4/ipv6 this is. | |
| const hostname * | try_hostname () const noexcept |
| nullptr if this isn't a hostname. | |
| const ipv4 * | try_ipv4 () const noexcept |
| nullptr if this isn't an ipv4. | |
| const ipv6 * | try_ipv6 () const noexcept |
| nullptr if this isn't an ipv6. | |
| const variant_type & | variant () const noexcept |
| Direct access to the underlying variant - for std::visit, std::get, etc. | |
Static Public Member Functions | |
| static host | from_url (const char *url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept |
| Classify and parse the host of a URL string literal. Disambiguation overload, same reason as host(const char*, bool). | |
| static host | from_url (std::string &&url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept |
| Extract the host from a full URL and classify/parse it, taking ownership of the URL string - reuses its allocation end-to-end (extract, then classify) when the result is a hostname. | |
| static host | from_url (std::string_view url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept |
| Extract the host from a full URL and classify/parse it, in one step. | |
| static host | wrap (hostname h) noexcept |
| Wraps an already-classified hostname/ipv4/ipv6 as a host. | |
| static host | wrap (ipv4 v) noexcept |
| static host | wrap (ipv6 v) noexcept |
Friends | |
| bool | operator!= (const host &a, const host &b) noexcept |
| bool | operator== (const host &a, const host &b) noexcept |
hostname, ipv4, or ipv6: whatever a URL's host actually is (RFC 3986's host grammar), wrapped in a uniform API that matches hostname/ipv4/ipv6/url themselves: a constructor that just parses, plus from_url() and str().
Internally this is a std::variant<hostname, ipv4, ipv6> held by value (composition, not inheritance - deriving public classes from a standard container is a well-known footgun: no virtual destructor, and an interface you don't fully control). is_hostname()/is_ipv4()/is_ipv6(), get_hostname()/get_ipv4()/get_ipv6() (throwing, like std::get<T>()), and try_hostname()/try_ipv4()/try_ipv6() (non-throwing, like std::get_if<T>()) cover the common cases without needing std::variant at all; variant() gives direct access to the underlying std::variant for std::visit or structured, exhaustive handling.
| using urlparser::host::variant_type = std::variant<hostname, ipv4, ipv6> |
|
defaultnoexcept |
Default-constructs an empty hostname.
|
noexcept |
Classify and parse an already-isolated host string (no scheme, no path - e.g. what url::host_text() gives you) as whichever of hostname/ipv4/ipv6 it actually is. Never throws: anything that isn't a valid IPv4/IPv6 address becomes a hostname.
| host_text | The host text, e.g. "example.com", "192.0.2.1", or "[2001:db8::1]" (a bracketed IPv6 literal, as it appears in a URL). |
| ignore_www | Only meaningful if the result is a hostname; ignored for IPs. |
|
noexcept |
Classify a host string literal. Disambiguation overload: without this, host("literal") would be ambiguous between the string_view and string&& overloads.
|
noexcept |
Classify an already-isolated host string, taking ownership of it. When the text turns out to be a domain name, this reuses its existing allocation to build the resulting hostname instead of copying into a new one.
| host_text | The host text. Left in a valid but unspecified state. |
|
staticnoexcept |
Classify and parse the host of a URL string literal. Disambiguation overload, same reason as host(const char*, bool).
|
staticnoexcept |
Extract the host from a full URL and classify/parse it, taking ownership of the URL string - reuses its allocation end-to-end (extract, then classify) when the result is a hostname.
| url | The URL string. Left in a valid but unspecified state. |
|
staticnoexcept |
Extract the host from a full URL and classify/parse it, in one step.
| std::bad_variant_access | if this isn't a hostname. |
| std::bad_variant_access | if this isn't an ipv4. |
| std::bad_variant_access | if this isn't an ipv6. |
|
inlinenoexcept |
|
inlinenoexcept |
True for either IP family - the opposite of is_hostname().
|
inlinenoexcept |
|
inlinenoexcept |
| std::string urlparser::host::str | ( | ) | const |
The text form, whichever of hostname/ipv4/ipv6 this is.
nullptr if this isn't a hostname.
|
inlinenoexcept |
Direct access to the underlying variant - for std::visit, std::get, etc.
Wraps an already-classified hostname/ipv4/ipv6 as a host.