Loading...
Searching...
No Matches
TLD::Host Class Reference

Represents a Host part of a URL. More...

#include <urlparser.h>

+ Collaboration diagram for TLD::Host:

Public Member Functions

 Host ()=default
 Default constructor for the Host class. Creates an empty Host object.
 
 Host (const std::string &host, const bool ignore_www=DEFAULT_IGNORE_WWW)
 Construct a Host object from a hostname string.
 
const std::string & domain () const noexcept
 Get the domain of the host.
 
std::string domainName () const noexcept
 Get the domain name of the host.
 
const std::string & fulldomain () const noexcept
 Get the full domain of the host.
 
bool operator== (const Host &other) const
 Equality operator for comparing two Host objects.
 
bool operator== (const std::string &other) const
 Equality operator for comparing a Host object with a string.
 
const std::string & str () const noexcept
 Get the complete host as a string.
 
const std::string & subdomain () const noexcept
 Get the subdomain of the host.
 
const std::string & suffix () const noexcept
 Get the suffix of the host.
 

Static Public Member Functions

static Host fromUrl (const std::string &url, const bool ignore_www=DEFAULT_IGNORE_WWW)
 Create a Host object from a URL string.
 
static bool isPslLoaded () noexcept
 Check if the Public Suffix List (PSL) is loaded.
 
static void loadPslFromPath (const std::string &filepath)
 Load the Public Suffix List from a file.
 
static void loadPslFromString (const std::string &filestr)
 Load the Public Suffix List from a string.
 
static std::string_view removeWWW (const std::string_view &host) noexcept
 Remove "www." from the beginning of a hostname.
 

Detailed Description

Represents a Host part of a URL.

The Host class encapsulates functionalities for handling the host component of a URL. It provides methods to extract domain-specific details such as suffix, domain, subdomain, and the full domain.

Constructor & Destructor Documentation

◆ Host() [1/2]

TLD::Host::Host ( const std::string &  host,
const bool  ignore_www = DEFAULT_IGNORE_WWW 
)

Construct a Host object from a hostname string.

Parameters
hostThe hostname to parse.
ignore_wwwWhether to ignore the "www" subdomain. Default is false.
Exceptions
std::invalid_argumentIf the hostname is malformed or cannot be parsed.

◆ Host() [2/2]

TLD::Host::Host ( )
default

Default constructor for the Host class. Creates an empty Host object.

Member Function Documentation

◆ domain()

const std::string & TLD::Host::domain ( ) const
noexcept

Get the domain of the host.

Returns
The domain of the host (e.g., "example" in "www.example.com").

◆ domainName()

std::string TLD::Host::domainName ( ) const
noexcept

Get the domain name of the host.

Returns
The domain name, which is the same as domain().

◆ fromUrl()

static Host TLD::Host::fromUrl ( const std::string &  url,
const bool  ignore_www = DEFAULT_IGNORE_WWW 
)
static

Create a Host object from a URL string.

Parameters
urlThe URL string to parse.
ignore_wwwWhether to ignore the "www" subdomain. Default is false.
Returns
A Host object representing the host part of the URL.

◆ fulldomain()

const std::string & TLD::Host::fulldomain ( ) const
noexcept

Get the full domain of the host.

Returns
The full domain of the host (e.g., "example.com").

◆ isPslLoaded()

static bool TLD::Host::isPslLoaded ( )
staticnoexcept

Check if the Public Suffix List (PSL) is loaded.

Returns
true if the PSL is loaded, false otherwise.

◆ loadPslFromPath()

static void TLD::Host::loadPslFromPath ( const std::string &  filepath)
static

Load the Public Suffix List from a file.

Parameters
filepathPath to the PSL file.
Exceptions
std::runtime_errorIf the file cannot be opened or parsed.

◆ loadPslFromString()

static void TLD::Host::loadPslFromString ( const std::string &  filestr)
static

Load the Public Suffix List from a string.

Parameters
filestrThe PSL content as a string.
Exceptions
std::runtime_errorIf the content cannot be parsed.

◆ operator==() [1/2]

bool TLD::Host::operator== ( const Host other) const

Equality operator for comparing two Host objects.

Parameters
otherThe Host object to compare with.
Returns
true if the hosts are equal, false otherwise.

◆ operator==() [2/2]

bool TLD::Host::operator== ( const std::string &  other) const

Equality operator for comparing a Host object with a string.

Parameters
otherThe string to compare with.
Returns
true if the host string is equal to the given string, false otherwise.

◆ removeWWW()

static std::string_view TLD::Host::removeWWW ( const std::string_view &  host)
staticnoexcept

Remove "www." from the beginning of a hostname.

Parameters
hostThe hostname to process.
Returns
The hostname without "www." if it was present.

◆ str()

const std::string & TLD::Host::str ( ) const
noexcept

Get the complete host as a string.

Returns
The complete host string.

◆ subdomain()

const std::string & TLD::Host::subdomain ( ) const
noexcept

Get the subdomain of the host.

Returns
The subdomain of the host (e.g., "www" in "www.example.com").

◆ suffix()

const std::string & TLD::Host::suffix ( ) const
noexcept

Get the suffix of the host.

Returns
The suffix of the host (e.g., "com" in "example.com").

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