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

Memory-efficient URL representation using a single buffer. More...

#include <url_aggregator.h>

Inheritance diagram for ada::url_aggregator:
ada::url_base

Public Member Functions

 url_aggregator ()=default
 
 url_aggregator (const url_aggregator &u)=default
 
 url_aggregator (url_aggregator &&u) noexcept=default
 
url_aggregatoroperator= (url_aggregator &&u) noexcept=default
 
url_aggregatoroperator= (const url_aggregator &u)=default
 
 ~url_aggregator () override=default
 
bool set_href (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_username (std::string_view input)
 
bool set_password (std::string_view input)
 
bool set_port (std::string_view input)
 
bool set_pathname (std::string_view input)
 
void set_search (std::string_view input)
 
void set_hash (std::string_view input)
 
bool has_valid_domain () const noexcept override
 
std::string get_origin () const override
 
constexpr std::string_view get_href () const noexcept ada_lifetime_bound
 
std::string_view get_username () const ada_lifetime_bound
 
std::string_view get_password () const ada_lifetime_bound
 
std::string_view get_port () const ada_lifetime_bound
 
std::string_view get_hash () const ada_lifetime_bound
 
std::string_view get_host () const ada_lifetime_bound
 
std::string_view get_hostname () const ada_lifetime_bound
 
constexpr std::string_view get_pathname () const ada_lifetime_bound
 
ada_really_inline uint32_t get_pathname_length () const noexcept
 
std::string_view get_search () const ada_lifetime_bound
 
std::string_view get_protocol () const ada_lifetime_bound
 
ada_really_inline constexpr bool has_credentials () const noexcept
 
ada_really_inline const url_componentsget_components () const noexcept
 
std::string to_string () const override
 
std::string to_diagram () const
 
constexpr bool validate () const noexcept
 
constexpr bool has_empty_hostname () const noexcept
 
constexpr bool has_hostname () const noexcept
 
constexpr bool has_non_empty_username () const noexcept
 
constexpr bool has_non_empty_password () const noexcept
 
constexpr bool has_port () const noexcept
 
constexpr bool has_password () const noexcept
 
constexpr bool has_hash () const noexcept override
 
constexpr bool has_search () const noexcept override
 
void clear_port ()
 
void clear_hash ()
 
void clear_search () override
 
- Public Member Functions inherited from ada::url_base
virtual ~url_base ()=default
 
ada_really_inline constexpr bool is_special () const noexcept
 

Friends

void helpers::strip_trailing_spaces_from_opaque_path (url_aggregator &url)
 
url_aggregator parser::parse_url (std::string_view, const url_aggregator *)
 
url_aggregator parser::parse_url_impl (std::string_view, const url_aggregator *)
 
url_aggregator parser::parse_url_impl (std::string_view, const 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

Memory-efficient URL representation using a single buffer.

The url_aggregator stores the entire normalized URL in a single string buffer and tracks component boundaries using offsets. This design minimizes memory allocations and is ideal for read-mostly access patterns.

Getter methods return std::string_view pointing into the internal buffer. These views are lightweight (no allocation) but become invalid if the url_aggregator is modified or destroyed.

Warning
Views returned by getters (e.g., get_pathname()) are invalidated when any setter is called. Do not use a getter's result as input to a setter on the same object without copying first.
Note
This is the default URL type returned by ada::parse().
See also
url For an alternative using separate std::string instances

Definition at line 46 of file url_aggregator.h.

Constructor & Destructor Documentation

◆ url_aggregator() [1/3]

ada::url_aggregator::url_aggregator ( )
default

◆ url_aggregator() [2/3]

ada::url_aggregator::url_aggregator ( const url_aggregator u)
default

◆ url_aggregator() [3/3]

ada::url_aggregator::url_aggregator ( url_aggregator &&  u)
defaultnoexcept

◆ ~url_aggregator()

ada::url_aggregator::~url_aggregator ( )
overridedefault

Member Function Documentation

◆ clear_hash()

void ada::url_aggregator::clear_hash ( )
inline

◆ clear_port()

◆ clear_search()

void ada::url_aggregator::clear_search ( )
inlineoverridevirtual

◆ get_components()

ada_really_inline const ada::url_components & ada::url_aggregator::get_components ( ) const
noexcept

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
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Returns
A constant reference to the url_components struct.
See also
https://github.com/servo/rust-url

Definition at line 732 of file url_aggregator-inl.h.

Referenced by exercise_aggregator_predicates().

◆ get_hash()

std::string_view ada::url_aggregator::get_hash ( ) const

Returns the URL's fragment prefixed with '#' (e.g., "#section"). Does not allocate memory. Returns empty view if no fragment is set. The returned view becomes invalid if this url_aggregator is modified.

Returns
A string_view of the hash.
See also
https://url.spec.whatwg.org/#dom-url-hash

Definition at line 744 of file url_aggregator.cpp.

References ada::url_components::hash_start, ada::url_components::omitted, and ada::parse().

Referenced by clear_hash(), and exercise_aggregator_predicates().

◆ get_host()

std::string_view ada::url_aggregator::get_host ( ) const

Returns the URL's host and port (e.g., "example.com:8080"). Does not allocate memory. Returns empty view if no host is set. The returned view becomes invalid if this url_aggregator is modified.

Returns
A string_view of host:port.
See also
https://url.spec.whatwg.org/#dom-url-host

Definition at line 758 of file url_aggregator.cpp.

References ada::url_components::host_end, ada::url_components::host_start, ada::parse(), and ada::url_components::pathname_start.

Referenced by exercise_aggregator_predicates(), and get_origin().

◆ get_hostname()

std::string_view ada::url_aggregator::get_hostname ( ) const

Returns the URL's hostname (without port). Does not allocate memory. Returns empty view if no host is set. The returned view becomes invalid if this url_aggregator is modified.

Returns
A string_view of the hostname.
See also
https://url.spec.whatwg.org/#dom-url-hostname

Definition at line 777 of file url_aggregator.cpp.

References ada::url_components::host_end, ada::url_components::host_start, and ada::parse().

Referenced by exercise_aggregator_predicates(), and has_valid_domain().

◆ get_href()

constexpr std::string_view ada::url_aggregator::get_href ( ) const
constexprnoexcept

Returns the full serialized URL (the href) as a string_view. Does not allocate memory. The returned view becomes invalid if this url_aggregator is modified or destroyed.

Returns
A string_view into the internal buffer.
See also
https://url.spec.whatwg.org/#dom-url-href

Definition at line 849 of file url_aggregator-inl.h.

References ada::parse().

Referenced by exercise_aggregator_predicates(), and LLVMFuzzerTestOneInput().

◆ get_origin()

std::string ada::url_aggregator::get_origin ( ) const
overridevirtual

Returns the URL's origin (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 687 of file url_aggregator.cpp.

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

Referenced by exercise_aggregator_predicates().

◆ get_password()

std::string_view ada::url_aggregator::get_password ( ) const

Returns the URL's password component. Does not allocate memory. The returned view becomes invalid if this url_aggregator is modified or destroyed.

Returns
A string_view of the password.
See also
https://url.spec.whatwg.org/#dom-url-password

Definition at line 724 of file url_aggregator.cpp.

References has_non_empty_password(), ada::url_components::host_start, ada::parse(), and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates().

◆ get_pathname()

constexpr std::string_view ada::url_aggregator::get_pathname ( ) const
constexpr

Returns the URL's path component. Does not allocate memory. The returned view becomes invalid if this url_aggregator is modified or destroyed.

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

Definition at line 1094 of file url_aggregator-inl.h.

References ada::url_components::hash_start, ada::url_components::omitted, ada::parse(), ada::url_components::pathname_start, and ada::url_components::search_start.

Referenced by exercise_aggregator_predicates(), get_origin(), and set_pathname().

◆ get_pathname_length()

ada_really_inline uint32_t ada::url_aggregator::get_pathname_length ( ) const
noexcept

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 154 of file url_aggregator-inl.h.

References ada::url_components::hash_start, ada::url_components::omitted, ada::parse(), ada::url_components::pathname_start, and ada::url_components::search_start.

Referenced by exercise_aggregator_predicates().

◆ get_port()

std::string_view ada::url_aggregator::get_port ( ) const

Returns the URL's port as a string (e.g., "8080"). Does not allocate memory. Returns empty view if no port is set. The returned view becomes invalid if this url_aggregator is modified.

Returns
A string_view of the port.
See also
https://url.spec.whatwg.org/#dom-url-port

Definition at line 734 of file url_aggregator.cpp.

References ada::url_components::host_end, ada::url_components::omitted, ada::parse(), ada::url_components::pathname_start, and ada::url_components::port.

Referenced by exercise_aggregator_predicates().

◆ get_protocol()

std::string_view ada::url_aggregator::get_protocol ( ) const

Returns the URL's scheme followed by a colon (e.g., "https:"). Does not allocate memory. The returned view becomes invalid if this url_aggregator is modified or destroyed.

Returns
A string_view of the protocol.
See also
https://url.spec.whatwg.org/#dom-url-protocol

Definition at line 810 of file url_aggregator.cpp.

References ada::parse(), and ada::url_components::protocol_end.

Referenced by exercise_aggregator_predicates(), and get_origin().

◆ get_search()

std::string_view ada::url_aggregator::get_search ( ) const

Returns the URL's query string prefixed with '?' (e.g., "?foo=bar"). Does not allocate memory. Returns empty view if no query is set. The returned view becomes invalid if this url_aggregator is modified.

Returns
A string_view of the search/query.
See also
https://url.spec.whatwg.org/#dom-url-search

Definition at line 792 of file url_aggregator.cpp.

References ada::url_components::hash_start, ada::url_components::omitted, ada::parse(), and ada::url_components::search_start.

Referenced by clear_search(), and exercise_aggregator_predicates().

◆ get_username()

std::string_view ada::url_aggregator::get_username ( ) const

Returns the URL's username component. Does not allocate memory. The returned view becomes invalid if this url_aggregator is modified or destroyed.

Returns
A string_view of the username.
See also
https://url.spec.whatwg.org/#dom-url-username

Definition at line 714 of file url_aggregator.cpp.

References has_non_empty_username(), ada::parse(), ada::url_components::protocol_end, and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates().

◆ has_credentials()

constexpr bool ada::url_aggregator::has_credentials ( ) const
constexprnoexcept

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 720 of file url_aggregator-inl.h.

References has_non_empty_password(), has_non_empty_username(), and ada::parse().

Referenced by exercise_aggregator_predicates().

◆ has_empty_hostname()

constexpr bool ada::url_aggregator::has_empty_hostname ( ) const
constexprnoexcept

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 793 of file url_aggregator-inl.h.

References has_hostname(), ada::url_components::host_end, ada::url_components::host_start, and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates().

◆ has_hash()

constexpr bool ada::url_aggregator::has_hash ( ) const
constexproverridevirtualnoexcept

Checks if the URL has a fragment/hash component.

Returns
true if hash is present, false otherwise.

Implements ada::url_base.

Definition at line 710 of file url_aggregator-inl.h.

References ada::url_components::hash_start, ada::url_components::omitted, and ada::parse().

Referenced by exercise_aggregator_predicates().

◆ has_hostname()

constexpr bool ada::url_aggregator::has_hostname ( ) const
constexprnoexcept

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

Returns
true if host is present, false otherwise.

Definition at line 806 of file url_aggregator-inl.h.

Referenced by exercise_aggregator_predicates(), has_empty_hostname(), and has_port().

◆ has_non_empty_password()

constexpr bool ada::url_aggregator::has_non_empty_password ( ) const
constexprnoexcept

Checks if the URL has a non-empty password.

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

Definition at line 781 of file url_aggregator-inl.h.

References ada::url_components::host_start, ada::parse(), and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates(), get_password(), and has_credentials().

◆ has_non_empty_username()

constexpr bool ada::url_aggregator::has_non_empty_username ( ) const
constexprnoexcept

Checks if the URL has a non-empty username.

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

Definition at line 776 of file url_aggregator-inl.h.

References ada::parse(), ada::url_components::protocol_end, and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates(), get_username(), and has_credentials().

◆ has_password()

constexpr bool ada::url_aggregator::has_password ( ) const
constexprnoexcept

Checks if the URL has a password component (may be empty).

Returns
true if password is present, false otherwise.

Definition at line 786 of file url_aggregator-inl.h.

References ada::url_components::host_start, ada::parse(), and ada::url_components::username_end.

Referenced by exercise_aggregator_predicates().

◆ has_port()

constexpr bool ada::url_aggregator::has_port ( ) const
constexprnoexcept

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

Returns
true if a port is present, false otherwise.

Definition at line 810 of file url_aggregator-inl.h.

References has_hostname(), ada::url_components::host_end, ada::parse(), and ada::url_components::pathname_start.

Referenced by exercise_aggregator_predicates().

◆ has_search()

constexpr bool ada::url_aggregator::has_search ( ) const
constexproverridevirtualnoexcept

Checks if the URL has a query/search component.

Returns
true if query is present, false otherwise.

Implements ada::url_base.

Definition at line 715 of file url_aggregator-inl.h.

References ada::url_components::omitted, ada::parse(), and ada::url_components::search_start.

Referenced by exercise_aggregator_predicates().

◆ has_valid_domain()

bool ada::url_aggregator::has_valid_domain ( ) const
overridevirtualnoexcept

Validates whether the hostname is a valid domain according to RFC 1034.

Returns
true if the domain is valid, false otherwise.

Implements ada::url_base.

Definition at line 909 of file url_aggregator.cpp.

References get_hostname(), ada::url_components::host_end, and ada::url_components::host_start.

Referenced by exercise_aggregator_predicates().

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ set_hash()

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

◆ set_host()

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

Definition at line 673 of file url_aggregator.cpp.

References ADA_ASSERT_TRUE, ada::parse(), and validate().

◆ set_hostname()

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

Definition at line 680 of file url_aggregator.cpp.

References ADA_ASSERT_TRUE, ada::parse(), and validate().

◆ set_href()

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

The setter functions follow the steps defined in the URL Standard.

The url_aggregator has a single buffer that contains the entire normalized URL. The various components are represented as offsets into that buffer. When you call get_pathname(), for example, you get a std::string_view that points into that buffer. If the url_aggregator is modified, the buffer may be reallocated, and the std::string_view you obtained earlier may become invalid. In particular, this implies that you cannot modify the URL using a setter function with a std::string_view that points into the url_aggregator E.g., the following is incorrect: url->set_hostname(url->get_pathname()). You must first copy the pathname to a separate string. std::string pathname(url->get_pathname()); url->set_hostname(pathname);

The caller is responsible for ensuring that the url_aggregator is not modified while any std::string_view obtained from it is in use.

Definition at line 426 of file url_aggregator.cpp.

References ADA_ASSERT_TRUE, ada::parse(), and ada::parse< url_aggregator >().

◆ set_password()

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

◆ set_pathname()

◆ set_port()

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

◆ set_protocol()

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

◆ set_search()

◆ set_username()

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

◆ to_diagram()

std::string ada::url_aggregator::to_diagram ( ) const

◆ to_string()

std::string ada::url_aggregator::to_string ( ) const
overridevirtual

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 816 of file url_aggregator.cpp.

References ada::url_components::omitted, and ada::parse().

Referenced by exercise_aggregator_predicates().

◆ validate()

constexpr bool ada::url_aggregator::validate ( ) const
constexprnoexcept

Validates internal consistency of component offsets (for debugging).

Returns
true if offsets are consistent, false if corrupted.

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

Definition at line 924 of file url_aggregator-inl.h.

References ada::url_components::check_offset_consistency(), ada::url_base::has_opaque_path, ada::url_components::hash_start, ada::url_components::host_end, ada::url_components::host_start, ada::url_base::is_valid, ada::url_components::omitted, ada::parse(), ada::url_components::pathname_start, ada::url_components::protocol_end, ada::url_components::search_start, to_diagram(), and ada::url_components::username_end.

Referenced by clear_hash(), clear_port(), clear_search(), exercise_aggregator_predicates(), set_hash(), set_host(), set_hostname(), set_password(), set_pathname(), set_port(), set_protocol(), set_search(), and set_username().

Friends And Related Symbol Documentation

◆ helpers::strip_trailing_spaces_from_opaque_path

void helpers::strip_trailing_spaces_from_opaque_path ( url_aggregator url)
friend

Referenced by set_hash(), and set_search().

◆ parser::parse_url

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

◆ parser::parse_url_impl [1/2]

url_aggregator parser::parse_url_impl ( std::string_view  ,
const url_aggregator  
)
friend

◆ parser::parse_url_impl [2/2]

url_aggregator parser::parse_url_impl ( std::string_view  ,
const url_aggregator  
)
friend

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