Ada 3.4.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
ada::url Struct Reference

Represents a parsed URL with individual string components. More...

#include <url.h>

Inheritance diagram for ada::url:
ada::url_base

Public Member Functions

 url ()=default
 
 url (const url &u)=default
 
 url (url &&u) noexcept=default
 
urloperator= (url &&u) noexcept=default
 
urloperator= (const url &u)=default
 
 ~url () override=default
 
bool has_empty_hostname () const noexcept
 
bool has_port () const noexcept
 
bool has_hostname () const noexcept
 
bool has_valid_domain () const noexcept override
 
std::string to_string () const override
 
ada_really_inline std::string get_href () const noexcept
 
std::string get_origin () const noexcept override
 
std::string get_protocol () const noexcept
 
std::string get_host () const noexcept
 
std::string get_hostname () const noexcept
 
constexpr std::string_view get_pathname () const noexcept
 
ada_really_inline size_t get_pathname_length () const noexcept
 
std::string get_search () const noexcept
 
const std::string & get_username () const noexcept
 
bool set_username (std::string_view input)
 
bool set_password (std::string_view input)
 
bool set_port (std::string_view input)
 
void set_hash (std::string_view input)
 
void set_search (std::string_view input)
 
bool set_pathname (std::string_view input)
 
bool set_host (std::string_view input)
 
bool set_hostname (std::string_view input)
 
bool set_protocol (std::string_view input)
 
bool set_href (std::string_view input)
 
const std::string & get_password () const noexcept
 
std::string get_port () const noexcept
 
std::string get_hash () const noexcept
 
ada_really_inline bool has_credentials () const noexcept
 
ada_really_inline ada::url_components get_components () const noexcept
 
constexpr bool has_hash () const noexcept override
 
constexpr bool has_search () const noexcept override
 
- Public Member Functions inherited from ada::url_base
virtual ~url_base ()=default
 
ada_really_inline constexpr bool is_special () const noexcept
 

Friends

ada::url ada::parser::parse_url (std::string_view, const ada::url *)
 
ada::url_aggregator ada::parser::parse_url (std::string_view, const ada::url_aggregator *)
 
void ada::helpers::strip_trailing_spaces_from_opaque_path (ada::url &url) noexcept
 
ada::url ada::parser::parse_url_impl (std::string_view, const ada::url *)
 
ada::url_aggregator ada::parser::parse_url_impl (std::string_view, const ada::url_aggregator *)
 

Additional Inherited Members

- Public Attributes inherited from ada::url_base
bool is_valid {true}
 
bool has_opaque_path {false}
 
url_host_type host_type = url_host_type::DEFAULT
 

Detailed Description

Represents a parsed URL with individual string components.

The url struct stores each URL component (scheme, username, password, host, port, path, query, fragment) as a separate std::string. This provides flexibility but incurs more memory allocations compared to url_aggregator.

When to use ada::url:

  • When you need to frequently modify individual URL components
  • When you want independent ownership of component strings

When to use ada::url_aggregator instead:

  • For read-mostly operations on parsed URLs
  • When memory efficiency is important
  • When you only need string_view access to components
Note
This type is returned when parsing with ada::parse<ada::url>(). By default, ada::parse() returns ada::url_aggregator.
See also
url_aggregator For a more memory-efficient URL representation
https://url.spec.whatwg.org/#url-representation

Definition at line 62 of file url.h.

Constructor & Destructor Documentation

◆ url() [1/3]

◆ url() [2/3]

ada::url::url ( const url & u)
default

References url().

◆ url() [3/3]

ada::url::url ( url && u)
defaultnoexcept

References url().

◆ ~url()

ada::url::~url ( )
overridedefault

Member Function Documentation

◆ get_components()

ada_really_inline ada::url_components ada::url::get_components ( ) const
nodiscardnoexcept

Returns the URL component offsets for efficient serialization.

The components represent byte offsets into the serialized URL:

https://user:[email protected]:1234/foo/bar?baz#quux
| | | | ^^^^| | |
| | | | | | | `----- hash_start
| | | | | | `--------- search_start
| | | | | `----------------- pathname_start
| | | | `--------------------- port
| | | `----------------------- host_end
| | `---------------------------------- host_start
| `--------------------------------------- username_end
`--------------------------------------------- protocol_end
Returns
A newly constructed url_components struct.
See also
https://github.com/servo/rust-url

Definition at line 50 of file url-inl.h.

References ada_really_inline, get_protocol(), get_search(), has_credentials(), ada::url_base::has_opaque_path, and ada::url_components::protocol_end.

◆ get_hash()

std::string ada::url::get_hash ( ) const
nodiscardnoexcept

Returns the URL's fragment prefixed with '#' (e.g., "#section"). Returns empty string if no fragment is set.

Returns
A newly allocated string with the hash.
See also
https://url.spec.whatwg.org/#dom-url-hash

Definition at line 678 of file url.cpp.

◆ get_host()

std::string ada::url::get_host ( ) const
nodiscardnoexcept

Returns the URL's host and port (e.g., "example.com:8080"). If no port is set, returns just the host. Returns empty string if no host.

Returns
A newly allocated string with host:port.
See also
https://url.spec.whatwg.org/#dom-url-host

Definition at line 641 of file url.cpp.

References get_port().

Referenced by get_origin().

◆ get_hostname()

std::string ada::url::get_hostname ( ) const
nodiscardnoexcept

Returns the URL's hostname (without port). Returns empty string if no host is set.

Returns
A newly allocated string with the hostname.
See also
https://url.spec.whatwg.org/#dom-url-hostname

Definition at line 655 of file url.cpp.

Referenced by ada::parser::parse_url_impl().

◆ get_href()

ada_really_inline std::string ada::url::get_href ( ) const
nodiscardnoexcept

Returns the full serialized URL (the href).

Returns
The complete URL string (allocates a new string).
See also
https://url.spec.whatwg.org/#dom-url-href

Definition at line 188 of file url-inl.h.

References ada_really_inline, get_password(), get_port(), get_protocol(), has_credentials(), and ada::url_base::has_opaque_path.

Referenced by ada::parser::parse_url_impl().

◆ get_origin()

std::string ada::url::get_origin ( ) const
nodiscardoverridevirtualnoexcept

Returns the URL's origin as a string (scheme + host + port for special URLs).

Returns
A newly allocated string containing the serialized origin.
See also
https://url.spec.whatwg.org/#concept-url-origin

Implements ada::url_base.

Definition at line 607 of file url.cpp.

References ada::scheme::FILE, get_host(), get_protocol(), ada::scheme::HTTP, ada::scheme::HTTPS, ada::url_base::is_special(), and ada::parse().

◆ get_password()

const std::string & ada::url::get_password ( ) const
nodiscardnoexcept

Returns the URL's password component.

Returns
A constant reference to the password string.
See also
https://url.spec.whatwg.org/#dom-url-password

Definition at line 670 of file url.cpp.

Referenced by get_href().

◆ get_pathname()

std::string_view ada::url::get_pathname ( ) const
nodiscardconstexprnoexcept

Returns the URL's path component.

Returns
A string_view pointing to the path.
See also
https://url.spec.whatwg.org/#dom-url-pathname

Definition at line 46 of file url-inl.h.

Referenced by ada::parser::parse_url_impl().

◆ get_pathname_length()

size_t ada::url::get_pathname_length ( ) const
nodiscardnoexcept

Returns the byte length of the pathname without creating a string.

Returns
Size of the pathname in bytes.
See also
https://url.spec.whatwg.org/#dom-url-pathname

Definition at line 42 of file url-inl.h.

◆ get_port()

std::string ada::url::get_port ( ) const
nodiscardnoexcept

Returns the URL's port as a string (e.g., "8080"). Returns empty string if no port is set.

Returns
A newly allocated string with the port.
See also
https://url.spec.whatwg.org/#dom-url-port

Definition at line 674 of file url.cpp.

Referenced by get_host(), and get_href().

◆ get_protocol()

std::string ada::url::get_protocol ( ) const
nodiscardnoexcept

Returns the URL's scheme followed by a colon (e.g., "https:").

Returns
A newly allocated string with the protocol.
See also
https://url.spec.whatwg.org/#dom-url-protocol

Definition at line 633 of file url.cpp.

References ada::url_base::is_special(), and ada::scheme::details::is_special_list.

Referenced by get_components(), get_href(), get_origin(), ada::parser::parse_url_impl(), and to_string().

◆ get_search()

std::string ada::url::get_search ( ) const
nodiscardnoexcept

Returns the URL's query string prefixed with '?' (e.g., "?foo=bar"). Returns empty string if no query is set.

Returns
A newly allocated string with the search/query.
See also
https://url.spec.whatwg.org/#dom-url-search

Definition at line 659 of file url.cpp.

Referenced by get_components().

◆ get_username()

const std::string & ada::url::get_username ( ) const
nodiscardnoexcept

Returns the URL's username component.

Returns
A constant reference to the username string.
See also
https://url.spec.whatwg.org/#dom-url-username

Definition at line 666 of file url.cpp.

◆ has_credentials()

ada_really_inline bool ada::url::has_credentials ( ) const
nodiscardnoexcept

Checks if the URL has credentials (non-empty username or password).

Returns
true if username or password is non-empty, false otherwise.

Definition at line 19 of file url-inl.h.

References ada_really_inline.

Referenced by get_components(), get_href(), and to_string().

◆ has_empty_hostname()

bool ada::url::has_empty_hostname ( ) const
inlinenodiscardnoexcept

Checks if the URL has an empty hostname (host is set but empty string).

Returns
true if host exists but is empty, false otherwise.

Definition at line 29 of file url-inl.h.

◆ has_hash()

bool ada::url::has_hash ( ) const
nodiscardconstexproverridevirtualnoexcept

Checks if the URL has a fragment/hash component.

Returns
true if hash is present, false otherwise.

Implements ada::url_base.

Definition at line 160 of file url-inl.h.

◆ has_hostname()

bool ada::url::has_hostname ( ) const
inlinenodiscardnoexcept

Checks if the URL has a hostname (including empty hostnames).

Returns
true if host is present, false otherwise.

Definition at line 35 of file url-inl.h.

◆ has_port()

ada_really_inline bool ada::url::has_port ( ) const
inlinenodiscardnoexcept

Checks if the URL has a non-default port explicitly specified.

Returns
true if a port is present, false otherwise.

Definition at line 22 of file url-inl.h.

References ada_really_inline.

◆ has_search()

bool ada::url::has_search ( ) const
nodiscardconstexproverridevirtualnoexcept

Checks if the URL has a query/search component.

Returns
true if query is present, false otherwise.

Implements ada::url_base.

Definition at line 164 of file url-inl.h.

Referenced by to_string().

◆ has_valid_domain()

bool ada::url::has_valid_domain ( ) const
nodiscardoverridevirtualnoexcept

Validates whether the hostname is a valid domain according to RFC 1034. Checks that the domain and its labels have valid lengths (max 255 octets total, max 63 octets per label).

Returns
true if the domain is valid, false otherwise.

Implements ada::url_base.

Definition at line 600 of file url.cpp.

◆ operator=() [1/2]

url & ada::url::operator= ( const url & u)
default

References url().

◆ operator=() [2/2]

url & ada::url::operator= ( url && u)
defaultnoexcept

References url().

◆ set_hash()

void ada::url::set_hash ( std::string_view input)

Sets the URL's fragment/hash (the part after '#').

Parameters
inputThe new hash value (with or without leading '#').
See also
https://url.spec.whatwg.org/#dom-url-hash

Definition at line 864 of file url.cpp.

References ada::character_sets::FRAGMENT_PERCENT_ENCODE.

◆ set_host()

bool ada::url::set_host ( std::string_view input)

Sets the URL's host (hostname and optionally port).

Parameters
inputThe new host value (e.g., "example.com:8080").
Returns
true on success, false if parsing fails.
See also
https://url.spec.whatwg.org/#dom-url-host

Definition at line 799 of file url.cpp.

◆ set_hostname()

bool ada::url::set_hostname ( std::string_view input)

Sets the URL's hostname (without port).

Parameters
inputThe new hostname value.
Returns
true on success, false if parsing fails.
See also
https://url.spec.whatwg.org/#dom-url-hostname

Definition at line 803 of file url.cpp.

◆ set_href()

bool ada::url::set_href ( std::string_view input)

Replaces the entire URL by parsing a new href string.

Parameters
inputThe new URL string to parse.
Returns
true on success, false if parsing fails.
See also
https://url.spec.whatwg.org/#dom-url-href

Definition at line 929 of file url.cpp.

References ada::parse().

◆ set_password()

bool ada::url::set_password ( std::string_view input)

Sets the URL's password, percent-encoding special characters.

Parameters
inputThe new password value.
Returns
true on success, false if the URL cannot have credentials.
See also
https://url.spec.whatwg.org/#dom-url-password

Definition at line 816 of file url.cpp.

References ada::character_sets::USERINFO_PERCENT_ENCODE.

◆ set_pathname()

bool ada::url::set_pathname ( std::string_view input)

Sets the URL's pathname.

Parameters
inputThe new path value.
Returns
true on success, false if the URL has an opaque path.
See also
https://url.spec.whatwg.org/#dom-url-pathname

Definition at line 896 of file url.cpp.

References ada::url_base::has_opaque_path.

◆ set_port()

bool ada::url::set_port ( std::string_view input)

Sets the URL's port from a string (e.g., "8080").

Parameters
inputThe port string. Empty string removes the port.
Returns
true on success, false if the URL cannot have a port.
See also
https://url.spec.whatwg.org/#dom-url-port

Definition at line 825 of file url.cpp.

References ada::url_base::is_valid.

◆ set_protocol()

bool ada::url::set_protocol ( std::string_view input)

Sets the URL's protocol/scheme.

Parameters
inputThe new protocol (with or without trailing ':').
Returns
true on success, false if the scheme is invalid.
See also
https://url.spec.whatwg.org/#dom-url-protocol

Definition at line 905 of file url.cpp.

References ada::checkers::is_alpha().

◆ set_search()

void ada::url::set_search ( std::string_view input)

Sets the URL's query string (the part after '?').

Parameters
inputThe new query value (with or without leading '?').
See also
https://url.spec.whatwg.org/#dom-url-search

Definition at line 878 of file url.cpp.

References ada::url_base::is_special(), ada::character_sets::QUERY_PERCENT_ENCODE, and ada::character_sets::SPECIAL_QUERY_PERCENT_ENCODE.

◆ set_username()

bool ada::url::set_username ( std::string_view input)

Sets the URL's username, percent-encoding special characters.

Parameters
inputThe new username value.
Returns
true on success, false if the URL cannot have credentials.
See also
https://url.spec.whatwg.org/#dom-url-username

Definition at line 807 of file url.cpp.

References ada::character_sets::USERINFO_PERCENT_ENCODE.

◆ to_string()

std::string ada::url::to_string ( ) const
nodiscardoverridevirtual

Returns a JSON string representation of this URL for debugging.

Returns
A JSON-formatted string with all URL components.

Implements ada::url_base.

Definition at line 550 of file url.cpp.

References get_protocol(), has_credentials(), ada::url_base::has_opaque_path, has_search(), and ada::url_base::is_valid.

Referenced by ada::operator<<().

Friends And Related Symbol Documentation

◆ ada::helpers::strip_trailing_spaces_from_opaque_path

void ada::helpers::strip_trailing_spaces_from_opaque_path ( ada::url & url)
friend

◆ ada::parser::parse_url [1/2]

ada::url ada::parser::parse_url ( std::string_view ,
const ada::url *  )
friend

◆ ada::parser::parse_url [2/2]

ada::url_aggregator ada::parser::parse_url ( std::string_view ,
const ada::url_aggregator *  )
friend

◆ ada::parser::parse_url_impl [1/2]

◆ ada::parser::parse_url_impl [2/2]


The documentation for this struct was generated from the following files: