Ada 4.1.1
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
url & operator= (url &&u) noexcept=default
url & operator= (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
size_t get_href_size () const noexcept
std::string get_origin () const override
std::string get_protocol () const
std::string get_host () const
std::string get_hostname () const
constexpr std::string_view get_pathname () const noexcept
ada_really_inline size_t get_pathname_length () const noexcept
std::string get_search () const
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
std::string get_hash () const
ada_really_inline bool has_credentials () const noexcept
ada_really_inline ada::url_components get_components () const
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)
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 *)
ada::url_aggregator ada::parser::parse_url_impl (std::string_view, const ada::url_aggregator *)
template<class result_type>
bool ada::parser::try_parse_simple_absolute (std::string_view, result_type &)
template<class result_type>
bool ada::parser::finish_simple_absolute_with_port (std::string_view, result_type &, ada::scheme::type, uint32_t, size_t, size_t, size_t, bool)
template<class result_type>
bool ada::parser::try_parse_simple_relative (std::string_view, const result_type &, result_type &)

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 64 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
nodiscard

Returns the URL component offsets for efficient serialization.

The components represent byte offsets into the serialized URL:

https://user:pass@example.com: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 55 of file url-inl.h.

References ada_really_inline, get_protocol(), get_search(), has_credentials(), ada::url_base::has_opaque_path, ada::url_components::hash_start, ada::url_components::host_end, ada::url_components::host_start, ada::url_components::pathname_start, ada::url_components::port, ada::url_components::protocol_end, ada::url_components::search_start, and ada::url_components::username_end.

Referenced by exercise_url_predicates().

◆ get_hash()

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

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 609 of file url.cpp.

Referenced by check_components_agree(), and exercise_url_predicates().

◆ get_host()

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

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 573 of file url.cpp.

References get_port().

Referenced by check_components_agree(), exercise_url_predicates(), and get_origin().

◆ get_hostname()

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

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 587 of file url.cpp.

Referenced by check_components_agree(), exercise_url_predicates(), and ada::parser::parse_url_impl().

◆ get_href()

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

◆ get_href_size()

size_t ada::url::get_href_size ( ) const
inlinenodiscardnoexcept

Returns the byte length of the serialized URL without allocating a string.

Returns
Size of the href in bytes.

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

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

Referenced by check_href_size(), check_href_size_invariant(), get_href(), ada::parser::parse_url_impl(), set_hash(), set_password(), set_pathname(), set_port(), set_protocol(), set_search(), and set_username().

◆ get_origin()

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

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 539 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().

Referenced by exercise_url_predicates().

◆ 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 601 of file url.cpp.

Referenced by check_components_agree(), and exercise_url_predicates().

◆ 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 51 of file url-inl.h.

Referenced by check_components_agree(), exercise_url_predicates(), and 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 47 of file url-inl.h.

Referenced by exercise_url_predicates().

◆ get_port()

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

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 605 of file url.cpp.

Referenced by check_components_agree(), exercise_url_predicates(), and get_host().

◆ get_protocol()

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

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 565 of file url.cpp.

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

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

◆ get_search()

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

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 591 of file url.cpp.

Referenced by check_components_agree(), exercise_url_predicates(), and 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 597 of file url.cpp.

Referenced by check_components_agree(), and exercise_url_predicates().

◆ 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 25 of file url-inl.h.

References ada_really_inline.

Referenced by exercise_url_predicates(), get_components(), get_href(), get_href_size(), 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 34 of file url-inl.h.

Referenced by exercise_url_predicates().

◆ 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 168 of file url-inl.h.

Referenced by exercise_url_predicates().

◆ 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 40 of file url-inl.h.

Referenced by exercise_url_predicates().

◆ 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 28 of file url-inl.h.

References ada_really_inline.

Referenced by exercise_url_predicates().

◆ 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 172 of file url-inl.h.

Referenced by exercise_url_predicates(), and 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 532 of file url.cpp.

Referenced by exercise_url_predicates().

◆ 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 826 of file url.cpp.

References ada::character_sets::FRAGMENT_PERCENT_ENCODE, get_href_size(), and ada::get_max_input_length().

◆ 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 741 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 745 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 916 of file url.cpp.

References ada::get_max_input_length(), and 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 766 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), set_password(), and ada::character_sets::USERINFO_PERCENT_ENCODE.

Referenced by set_password().

◆ 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 866 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), ada::url_base::has_opaque_path, and set_pathname().

Referenced by set_pathname().

◆ 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 783 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), and 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 883 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), and 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 844 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), 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 749 of file url.cpp.

References get_href_size(), ada::get_max_input_length(), set_username(), and ada::character_sets::USERINFO_PERCENT_ENCODE.

Referenced by set_username().

◆ 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 482 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 exercise_url_predicates(), and ada::operator<<().

◆ ada::helpers::strip_trailing_spaces_from_opaque_path

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

◆ ada::parser::finish_simple_absolute_with_port

template<class result_type>
bool ada::parser::finish_simple_absolute_with_port ( std::string_view ,
result_type & ,
ada::scheme::type ,
uint32_t ,
size_t ,
size_t ,
size_t ,
bool  )
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/3]

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

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

◆ ada::parser::try_parse_simple_absolute

template<class result_type>
bool ada::parser::try_parse_simple_absolute ( std::string_view ,
result_type &  )
friend

◆ ada::parser::try_parse_simple_relative

template<class result_type>
bool ada::parser::try_parse_simple_relative ( std::string_view ,
const result_type & ,
result_type &  )
friend

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