URL

Detailed Description

Represents an immutable Uniform Resource Locator.

A Uniform Resource Locator (URL), is used to uniquely identify a resource on the Internet. The URL is a compact text string with a restricted syntax that consists of four main components:

protocol://<authority><path><query>
constexpr std::string_view protocol() const noexcept
Gets the protocol of the URL.
Definition zdbpp.h:402

The protocol part is mandatory, the other components may or may not be present in an URL string. For instance the file protocol only use the path component while a http protocol may use all components.

The following URL components are automatically unescaped according to the escaping mechanism defined in RFC 2396; credentials, path and parameter values. If you use a password with non-URL safe characters, you must URL escape the value.

An IPv6 address can be used for host as defined in RFC2732 by enclosing the address in [brackets]. For instance, mysql://[2010:836B:4179::836B:4179]:3306/test

For more information about the URL syntax and specification, see, RFC2396 - Uniform Resource Identifiers (URI): Generic Syntax

Example:

URL url("postgresql://user:password@example.com:5432/database?use-ssl=true");
// Retrieve and print various components of the URL
std::cout << "Protocol: " << url.protocol() << std::endl;
std::cout << "Host: " << url.host().value_or("Not specified") << std::endl;
std::cout << "Port: " << url.port() << std::endl;
std::cout << "User: " << url.user().value_or("Not specified") << std::endl;
std::cout << "Password: " << (url.password().has_value() ? "Specified" : "Not specified") << std::endl;
std::cout << "Path: " << url.path().value_or("Not specified") << std::endl;
// Get a specific parameter value
auto use_ssl = url.parameter("use-ssl").value_or("false");
std::cout << "SSL Enabled: " << (use_ssl == "true") << std::endl;
Represents an immutable Uniform Resource Locator.
Definition zdbpp.h:333

Public Member Functions

 URL (const std::string &url)
 Creates a new URL object from the given URL string.
 
Properties
constexpr std::string_view protocol () const noexcept
 Gets the protocol of the URL.
 
constexpr std::optional< std::string_view > user () const noexcept
 Gets the username from the URL's authority part.
 
constexpr std::optional< std::string_view > password () const noexcept
 Gets the password from the URL's authority part.
 
constexpr std::optional< std::string_view > host () const noexcept
 Gets the hostname of the URL.
 
constexpr int port () const noexcept
 Gets the port of the URL.
 
constexpr std::optional< std::string_view > path () const noexcept
 Gets the path of the URL.
 
constexpr std::optional< std::string_view > queryString () const noexcept
 Gets the query string of the URL.
 
std::vector< std::string_view > parameterNames () const noexcept
 Gets the names of parameters contained in this URL.
 
std::optional< std::string_view > parameter (const std::string &name) const noexcept
 Gets the value of the specified URL parameter.
 
Functions
constexpr std::string_view toString () const noexcept
 Returns a string representation of this URL object.
 

Constructor & Destructor Documentation

◆ URL()

URL ( const std::string & url)
explicit

Creates a new URL object from the given URL string.

Parameters
urlA string specifying the URL.
Exceptions
sql_exceptionif the URL cannot be parsed.

Member Function Documentation

◆ protocol()

std::string_view protocol ( ) const
nodiscardconstexprnoexcept

Gets the protocol of the URL.

Returns
The protocol name.

◆ user()

std::optional< std::string_view > user ( ) const
nodiscardconstexprnoexcept

Gets the username from the URL's authority part.

Returns
An optional containing the username or std::nullopt if not found.

◆ password()

std::optional< std::string_view > password ( ) const
nodiscardconstexprnoexcept

Gets the password from the URL's authority part.

Returns
An optional containing the password or std::nullopt if not found.

◆ host()

std::optional< std::string_view > host ( ) const
nodiscardconstexprnoexcept

Gets the hostname of the URL.

Returns
An optional containing the hostname or std::nullopt if not found.

◆ port()

int port ( ) const
nodiscardconstexprnoexcept

Gets the port of the URL.

Returns
The port number of the URL or -1 if not specified.

◆ path()

std::optional< std::string_view > path ( ) const
nodiscardconstexprnoexcept

Gets the path of the URL.

Returns
An optional containing the path or std::nullopt if not found.

◆ queryString()

std::optional< std::string_view > queryString ( ) const
nodiscardconstexprnoexcept

Gets the query string of the URL.

Returns
An optional containing the query string or std::nullopt if not found.

◆ parameterNames()

std::vector< std::string_view > parameterNames ( ) const
nodiscardnoexcept

Gets the names of parameters contained in this URL.

Returns
A vector of parameter names, or an empty vector if no parameters.

◆ parameter()

std::optional< std::string_view > parameter ( const std::string & name) const
nodiscardnoexcept

Gets the value of the specified URL parameter.

Parameters
nameThe parameter name to lookup.
Returns
An optional containing the parameter value, or std::nullopt if not found.

◆ toString()

std::string_view toString ( ) const
nodiscardconstexprnoexcept

Returns a string representation of this URL object.

Returns
The URL string.

Copyright © Tildeslash Ltd. All rights reserved.