Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url.h
Go to the documentation of this file.
1
11#ifndef ADA_URL_H
12#define ADA_URL_H
13
14#include <algorithm>
15#include <cstddef>
16#include <cstdint>
17#include <optional>
18#include <ostream>
19#include <string>
20#include <string_view>
21
22#include "ada/checkers.h"
23#include "ada/common_defs.h"
24#include "ada/url_base.h"
25#include "ada/url_components.h"
26#include "ada/helpers.h"
27
28namespace ada {
29
30struct url_aggregator;
31
32// namespace parser {
33// template <typename result_type>
34// result_type parse_url(std::string_view user_input,
35// const result_type* base_url = nullptr);
36// template <typename result_type, bool store_values>
37// result_type parse_url_impl(std::string_view user_input,
38// const result_type* base_url = nullptr);
39// }
40
64struct url : url_base {
65 url() = default;
66 url(const url& u) = default;
67 url(url&& u) noexcept = default;
68 url& operator=(url&& u) noexcept = default;
69 url& operator=(const url& u) = default;
70 ~url() override = default;
71
72 // Fields are ordered so that the most frequently accessed components
73 // tend to occupy earlier cache lines and remain close together in memory.
74 //
75 // Note: The exact object layout (including cache-line boundaries, byte
76 // offsets, and member sizes) is implementation- and platform-dependent.
77 // This ordering expresses an intent for better cache locality but does not
78 // guarantee any specific in-memory layout.
79
84 std::optional<std::string> host{};
85
91 std::string path{};
92
97 std::optional<std::string> query{};
98
105 std::optional<std::string> hash{};
106
112 std::optional<uint16_t> port{};
113
119 std::string username{};
120
126 std::string password{};
127
132 [[nodiscard]] inline bool has_empty_hostname() const noexcept;
133
138 [[nodiscard]] inline bool has_port() const noexcept;
139
144 [[nodiscard]] inline bool has_hostname() const noexcept;
145
152 [[nodiscard]] bool has_valid_domain() const noexcept override;
153
158 [[nodiscard]] std::string to_string() const override;
159
165 [[nodiscard]] ada_really_inline std::string get_href() const;
166
171 [[nodiscard]] size_t get_href_size() const noexcept;
172
179 [[nodiscard]] std::string get_origin() const override;
180
186 [[nodiscard]] std::string get_protocol() const;
187
194 [[nodiscard]] std::string get_host() const;
195
202 [[nodiscard]] std::string get_hostname() const;
203
209 [[nodiscard]] constexpr std::string_view get_pathname() const noexcept;
210
216 [[nodiscard]] ada_really_inline size_t get_pathname_length() const noexcept;
217
224 [[nodiscard]] std::string get_search() const;
225
231 [[nodiscard]] const std::string& get_username() const noexcept;
232
239 bool set_username(std::string_view input);
240
247 bool set_password(std::string_view input);
248
255 bool set_port(std::string_view input);
256
262 void set_hash(std::string_view input);
263
269 void set_search(std::string_view input);
270
277 bool set_pathname(std::string_view input);
278
285 bool set_host(std::string_view input);
286
293 bool set_hostname(std::string_view input);
294
301 bool set_protocol(std::string_view input);
302
309 bool set_href(std::string_view input);
310
316 [[nodiscard]] const std::string& get_password() const noexcept;
317
324 [[nodiscard]] std::string get_port() const;
325
332 [[nodiscard]] std::string get_hash() const;
333
338 [[nodiscard]] ada_really_inline bool has_credentials() const noexcept;
339
359 [[nodiscard]] ada_really_inline ada::url_components get_components() const;
360
365 [[nodiscard]] constexpr bool has_hash() const noexcept override;
366
371 [[nodiscard]] constexpr bool has_search() const noexcept override;
372
373 private:
374 friend ada::url ada::parser::parse_url<ada::url>(std::string_view,
375 const ada::url*);
377 std::string_view, const ada::url_aggregator*);
379 ada::url& url);
380
381 friend ada::url ada::parser::parse_url_impl<ada::url, true>(std::string_view,
382 const ada::url*);
384 ada::url_aggregator, true>(std::string_view, const ada::url_aggregator*);
386 ada::url_aggregator, false>(std::string_view, const ada::url_aggregator*);
387 template <class result_type>
388 friend bool ada::parser::try_parse_simple_absolute(std::string_view,
389 result_type&);
390 template <class result_type>
392 std::string_view, result_type&, ada::scheme::type, uint32_t, size_t,
393 size_t, size_t, bool);
394 template <class result_type>
395 friend bool ada::parser::try_parse_simple_relative(std::string_view,
396 const result_type&,
397 result_type&);
398
399 inline void update_unencoded_base_hash(std::string_view input);
400 inline void update_base_hostname(std::string_view input);
401 inline void update_base_search(std::string_view input,
402 const uint8_t query_percent_encode_set[]);
403 inline void update_base_search(std::optional<std::string>&& input);
404 inline void update_base_pathname(std::string_view input);
405 inline void update_base_username(std::string_view input);
406 inline void update_base_password(std::string_view input);
407 inline void update_base_port(std::optional<uint16_t> input);
408
413 [[nodiscard]] bool needs_rollback_snapshot(size_t input_len) const noexcept;
414
420 template <bool override_hostname = false>
421 bool set_host_or_hostname(std::string_view input);
422
427 [[nodiscard]] bool parse_ipv4(std::string_view input);
428
433 [[nodiscard]] bool parse_ipv6(std::string_view input);
434
439 [[nodiscard]] bool parse_opaque_host(std::string_view input);
440
450 std::string non_special_scheme{};
451
456 [[nodiscard]] inline bool cannot_have_credentials_or_port() const;
457
458 ada_really_inline size_t parse_port(
459 std::string_view view, bool check_trailing_content) noexcept override;
460
461 ada_really_inline size_t parse_port(std::string_view view) noexcept override {
462 return this->parse_port(view, false);
463 }
464
473 [[nodiscard]] ada_really_inline bool parse_host(std::string_view input);
474
475 template <bool has_state_override = false>
476 [[nodiscard]] ada_really_inline bool parse_scheme(std::string_view input);
477
478 constexpr void clear_pathname() override;
479 constexpr void clear_search() override;
480 constexpr void set_protocol_as_file();
481
492 ada_really_inline void parse_path(std::string_view input);
493
499 inline void set_scheme(std::string&& new_scheme) noexcept;
500
505 constexpr void copy_scheme(ada::url&& u);
506
511 constexpr void copy_scheme(const ada::url& u);
512
513}; // struct url
514
515inline std::ostream& operator<<(std::ostream& out, const ada::url& u);
516} // namespace ada
517
518#endif // ADA_URL_H
Declarations for URL specific checkers used within Ada.
Cross-platform compiler macros and common definitions.
#define ada_really_inline
Definition common_defs.h:85
Definitions for helper functions used within Ada.
Includes the definitions for helper functions.
Internal URL parsing implementation.
Definition parser-inl.h:18
URL scheme utilities and constants.
Definition scheme-inl.h:14
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:43
Memory-efficient URL representation using a single buffer.
Abstract base class for URL representations.
Definition url_base.h:51
Represents a parsed URL with individual string components.
Definition url.h:64
void set_hash(std::string_view input)
Definition url.cpp:826
bool set_hostname(std::string_view input)
Definition url.cpp:745
bool set_host(std::string_view input)
Definition url.cpp:741
url(url &&u) noexcept=default
bool has_empty_hostname() const noexcept
Definition url-inl.h:34
friend 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)
bool has_port() const noexcept
Definition url-inl.h:28
ada_really_inline bool has_credentials() const noexcept
Definition url-inl.h:25
friend ada::url ada::parser::parse_url(std::string_view, const ada::url *)
bool set_password(std::string_view input)
Definition url.cpp:766
friend bool ada::parser::try_parse_simple_relative(std::string_view, const result_type &, result_type &)
url & operator=(url &&u) noexcept=default
url & operator=(const url &u)=default
void set_search(std::string_view input)
Definition url.cpp:844
ada_really_inline size_t get_pathname_length() const noexcept
Definition url-inl.h:47
std::string get_search() const
Definition url.cpp:591
bool set_href(std::string_view input)
Definition url.cpp:916
std::string get_port() const
Definition url.cpp:605
std::string get_protocol() const
Definition url.cpp:565
bool has_hostname() const noexcept
Definition url-inl.h:40
std::string get_host() const
Definition url.cpp:573
bool set_username(std::string_view input)
Definition url.cpp:749
constexpr std::string_view get_pathname() const noexcept
Definition url-inl.h:51
url(const url &u)=default
~url() override=default
size_t get_href_size() const noexcept
Definition url-inl.h:283
url()=default
bool set_pathname(std::string_view input)
Definition url.cpp:866
std::string get_hostname() const
Definition url.cpp:587
ada_really_inline std::string get_href() const
Definition url-inl.h:196
friend ada::url ada::parser::parse_url_impl(std::string_view, const ada::url *)
std::string get_origin() const override
Definition url.cpp:539
const std::string & get_password() const noexcept
Definition url.cpp:601
friend bool ada::parser::try_parse_simple_absolute(std::string_view, result_type &)
ada_really_inline ada::url_components get_components() const
Definition url-inl.h:55
bool set_protocol(std::string_view input)
Definition url.cpp:883
const std::string & get_username() const noexcept
Definition url.cpp:597
friend void ada::helpers::strip_trailing_spaces_from_opaque_path(ada::url &url)
bool set_port(std::string_view input)
Definition url.cpp:783
constexpr bool has_search() const noexcept override
Definition url-inl.h:172
std::string to_string() const override
Definition url.cpp:482
std::string get_hash() const
Definition url.cpp:609
constexpr bool has_hash() const noexcept override
Definition url-inl.h:168
bool has_valid_domain() const noexcept override
Definition url.cpp:532
Base class and common definitions for URL types.
Declaration for the URL Components.