HTTP module

URL

An RFC 3986 based [URL]({#ref classicy_1_1http_1_1URL #}) parser.

URL

#include <icy/http/url.h>

An RFC 3986 based URL parser. Constructors and assignment operators will throw a SyntaxException if the URL is invalid.

Public Methods

ReturnNameDescription
URLCreates an empty URL.
URLParses the URL from a null-terminated string.
URLParses the URL from a std::string.
URLConstructs a URL from scheme and authority components.
URLConstructs a URL from scheme, authority, and path+query+fragment.
URLConstructs a URL from individual components.
URLDefaulted constructor.
URL &operator=Assigns a URL from another URL instance.
URL &operator=Assigns a URL from a null-terminated string.
URL &operator=Assigns a URL from a std::string.
boolparseParses and assigns a URL from the given string view, resetting all components first.
std::stringscheme constReturns the URL scheme (e.g. "http", "https", "ws"). Always lowercase.
std::stringuserInfo constReturns the user info component (e.g. "user:pass" from "http://user:pass@host/"). Returns an empty string if not present.
std::stringhost constReturns the host component (e.g. "example.com"). Returns an empty string if not present.
uint16_tport constReturns the port number. If no explicit port was in the URL, returns the default port for the scheme (80 for http, 443 for https), or 0 if the scheme is unknown.
std::stringauthority constReturns the authority component (userinfo@host:port). Only includes components that are present.
std::stringpath constReturns the path component (e.g. "/index.html"). Returns an empty string if not present.
std::stringpathEtc constReturns the path, query and fragment combined (e.g. "/path?q=1#frag").
std::stringquery constReturns the query string without the leading '?' (e.g. "key=value&foo=bar"). Returns an empty string if not present.
std::stringfragment constReturns the fragment identifier without the leading '#'. Returns an empty string if not present.
boolhasSchema constReturns true if the URL has a scheme component.
boolhasUserInfo constReturns true if the URL has a user info component.
boolhasHost constReturns true if the URL has a host component.
boolhasPort constReturns true if an explicit port was specified in the URL.
boolhasPath constReturns true if the URL has a path component.
boolhasQuery constReturns true if the URL has a query component.
boolhasFragment constReturns true if the URL has a fragment component.
boolvalid constReturns true if the URL is non-empty and was successfully parsed.
std::stringstr constReturns the original URL string as parsed.

URL

URL()

Creates an empty URL.


URL

URL(const char * url)

Parses the URL from a null-terminated string.

Parameters

  • url Null-terminated URL string to parse.

URL

URL(const std::string & url)

Parses the URL from a std::string.

Parameters

  • url URL string to parse.

URL

URL(const std::string & scheme, const std::string & authority)

Constructs a URL from scheme and authority components.

Parameters

  • scheme URL scheme (e.g. "http", "https").

  • authority Host and optional port (e.g. "example.com:8080").


URL

URL(const std::string & scheme, const std::string & authority, const std::string & pathEtc)

Constructs a URL from scheme, authority, and path+query+fragment.

Parameters

  • scheme URL scheme (e.g. "http").

  • authority Host and optional port.

  • pathEtc Path, query and fragment combined (e.g. "/path?q=1#frag").


URL

URL(const std::string & scheme, const std::string & authority, const std::string & path, const std::string & query, const std::string & fragment)

Constructs a URL from individual components.

Parameters

  • scheme URL scheme (e.g. "http").

  • authority Host and optional port.

  • path URL path (e.g. "/index.html").

  • query Query string without leading '?' (e.g. "key=value").

  • fragment Fragment identifier without leading '#'.


URL

URL(const URL &) = default

Defaulted constructor.


operator=

URL & operator=(const URL & uri)

Assigns a URL from another URL instance.

Parameters

  • uri Source URL to copy from.

Returns

Reference to this URL.


operator=

URL & operator=(const char * uri)

Assigns a URL from a null-terminated string.

Parameters

  • uri Null-terminated URL string to parse.

Returns

Reference to this URL.


operator=

URL & operator=(const std::string & uri)

Assigns a URL from a std::string.

Parameters

  • uri URL string to parse.

Returns

Reference to this URL.


parse

bool parse(std::string_view url, bool whiny)

Parses and assigns a URL from the given string view, resetting all components first.

Parameters

  • url URL string to parse.

  • whiny If true, throws std::runtime_error on an invalid URL; otherwise returns false.

Returns

true if the URL was parsed successfully; false if invalid and whiny is false.


scheme

const

std::string scheme() const

Returns the URL scheme (e.g. "http", "https", "ws"). Always lowercase.


userInfo

const

std::string userInfo() const

Returns the user info component (e.g. "user:pass" from "http://user:pass@host/"). Returns an empty string if not present.


host

const

std::string host() const

Returns the host component (e.g. "example.com"). Returns an empty string if not present.


port

const

uint16_t port() const

Returns the port number. If no explicit port was in the URL, returns the default port for the scheme (80 for http, 443 for https), or 0 if the scheme is unknown.


authority

const

std::string authority() const

Returns the authority component (userinfo@host:port). Only includes components that are present.


path

const

std::string path() const

Returns the path component (e.g. "/index.html"). Returns an empty string if not present.


pathEtc

const

std::string pathEtc() const

Returns the path, query and fragment combined (e.g. "/path?q=1#frag").


query

const

std::string query() const

Returns the query string without the leading '?' (e.g. "key=value&foo=bar"). Returns an empty string if not present.


fragment

const

std::string fragment() const

Returns the fragment identifier without the leading '#'. Returns an empty string if not present.


hasSchema

const

bool hasSchema() const

Returns true if the URL has a scheme component.


hasUserInfo

const

bool hasUserInfo() const

Returns true if the URL has a user info component.


hasHost

const

bool hasHost() const

Returns true if the URL has a host component.


hasPort

const

bool hasPort() const

Returns true if an explicit port was specified in the URL.


hasPath

const

bool hasPath() const

Returns true if the URL has a path component.


hasQuery

const

bool hasQuery() const

Returns true if the URL has a query component.


hasFragment

const

bool hasFragment() const

Returns true if the URL has a fragment component.


valid

const

bool valid() const

Returns true if the URL is non-empty and was successfully parsed.


str

const

std::string str() const

Returns the original URL string as parsed.

Public Static Methods

ReturnNameDescription
std::stringencode staticPercent-encodes a string per RFC 3986, preserving unreserved characters (A-Z, a-z, 0-9, '-', '_', '.', '~'). Equivalent to JavaScript's encodeURIComponent().
std::stringdecode staticDecodes a percent-encoded string per RFC 3986. Equivalent to JavaScript's decodeURIComponent().

encode

static

static std::string encode(std::string_view str)

Percent-encodes a string per RFC 3986, preserving unreserved characters (A-Z, a-z, 0-9, '-', '_', '.', '~'). Equivalent to JavaScript's encodeURIComponent().

Parameters

  • str Input string to encode.

Returns

Percent-encoded string.


decode

static

static std::string decode(std::string_view str)

Decodes a percent-encoded string per RFC 3986. Equivalent to JavaScript's decodeURIComponent().

Parameters

  • str Percent-encoded input string.

Returns

Decoded string.

Protected Attributes

ReturnNameDescription
std::string_buf
std::string_scheme
std::string_userInfo
std::string_host
uint16_t_port
std::string_path
std::string_query
std::string_fragment
bool_hasPort

_buf

std::string _buf

_scheme

std::string _scheme

_userInfo

std::string _userInfo

_host

std::string _host

_port

uint16_t _port

_path

std::string _path

_query

std::string _query

_fragment

std::string _fragment

_hasPort

bool _hasPort