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

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
 

Detailed Description

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.

Member Typedef Documentation

◆ variant_type

Constructor & Destructor Documentation

◆ host() [1/4]

urlparser::host::host ( )
defaultnoexcept

Default-constructs an empty hostname.

◆ host() [2/4]

urlparser::host::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.

Parameters
host_textThe 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_wwwOnly meaningful if the result is a hostname; ignored for IPs.

◆ host() [3/4]

urlparser::host::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() [4/4]

urlparser::host::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.

Parameters
host_textThe host text. Left in a valid but unspecified state.

Member Function Documentation

◆ from_url() [1/3]

static host urlparser::host::from_url ( const char *  url,
bool  ignore_www = DEFAULT_IGNORE_WWW 
)
staticnoexcept

Classify and parse the host of a URL string literal. Disambiguation overload, same reason as host(const char*, bool).

◆ from_url() [2/3]

static host urlparser::host::from_url ( std::string &&  url,
bool  ignore_www = DEFAULT_IGNORE_WWW 
)
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.

Parameters
urlThe URL string. Left in a valid but unspecified state.

◆ from_url() [3/3]

static host urlparser::host::from_url ( std::string_view  url,
bool  ignore_www = DEFAULT_IGNORE_WWW 
)
staticnoexcept

Extract the host from a full URL and classify/parse it, in one step.

◆ get_hostname()

const hostname & urlparser::host::get_hostname ( ) const
inline
Exceptions
std::bad_variant_accessif this isn't a hostname.

◆ get_ipv4()

const ipv4 & urlparser::host::get_ipv4 ( ) const
inline
Exceptions
std::bad_variant_accessif this isn't an ipv4.

◆ get_ipv6()

const ipv6 & urlparser::host::get_ipv6 ( ) const
inline
Exceptions
std::bad_variant_accessif this isn't an ipv6.

◆ is_hostname()

bool urlparser::host::is_hostname ( ) const
inlinenoexcept

◆ is_ip()

bool urlparser::host::is_ip ( ) const
inlinenoexcept

True for either IP family - the opposite of is_hostname().

◆ is_ipv4()

bool urlparser::host::is_ipv4 ( ) const
inlinenoexcept

◆ is_ipv6()

bool urlparser::host::is_ipv6 ( ) const
inlinenoexcept

◆ str()

std::string urlparser::host::str ( ) const

The text form, whichever of hostname/ipv4/ipv6 this is.

◆ try_hostname()

const hostname * urlparser::host::try_hostname ( ) const
inlinenoexcept

nullptr if this isn't a hostname.

◆ try_ipv4()

const ipv4 * urlparser::host::try_ipv4 ( ) const
inlinenoexcept

nullptr if this isn't an ipv4.

◆ try_ipv6()

const ipv6 * urlparser::host::try_ipv6 ( ) const
inlinenoexcept

nullptr if this isn't an ipv6.

◆ variant()

const variant_type & urlparser::host::variant ( ) const
inlinenoexcept

Direct access to the underlying variant - for std::visit, std::get, etc.

◆ wrap() [1/3]

static host urlparser::host::wrap ( hostname  h)
inlinestaticnoexcept

Wraps an already-classified hostname/ipv4/ipv6 as a host.

◆ wrap() [2/3]

static host urlparser::host::wrap ( ipv4  v)
inlinestaticnoexcept

◆ wrap() [3/3]

static host urlparser::host::wrap ( ipv6  v)
inlinestaticnoexcept

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( const host &  a,
const host &  b 
)
friend

◆ operator==

bool operator== ( const host &  a,
const host &  b 
)
friend

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