Ada 3.1.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_H
6#define ADA_URL_H
7
8#include <algorithm>
9#include <optional>
10#include <ostream>
11#include <string>
12#include <string_view>
13
14#include "ada/checkers.h"
15#include "ada/common_defs.h"
16#include "ada/url_base.h"
17#include "ada/url_components.h"
18#include "ada/helpers.h"
19
20namespace ada {
21
22struct url_aggregator;
23
24// namespace parser {
25// template <typename result_type>
26// result_type parse_url(std::string_view user_input,
27// const result_type* base_url = nullptr);
28// template <typename result_type, bool store_values>
29// result_type parse_url_impl(std::string_view user_input,
30// const result_type* base_url = nullptr);
31// }
32
45struct url : url_base {
46 url() = default;
47 url(const url &u) = default;
48 url(url &&u) noexcept = default;
49 url &operator=(url &&u) noexcept = default;
50 url &operator=(const url &u) = default;
51 ~url() override = default;
52
58 std::string username{};
59
65 std::string password{};
66
71 std::optional<std::string> host{};
72
78 std::optional<uint16_t> port{};
79
85 std::string path{};
86
91 std::optional<std::string> query{};
92
99 std::optional<std::string> hash{};
100
102 [[nodiscard]] inline bool has_empty_hostname() const noexcept;
104 [[nodiscard]] inline bool has_port() const noexcept;
106 [[nodiscard]] inline bool has_hostname() const noexcept;
107 [[nodiscard]] bool has_valid_domain() const noexcept override;
108
112 [[nodiscard]] std::string to_string() const override;
113
118 [[nodiscard]] ada_really_inline std::string get_href() const noexcept;
119
126 [[nodiscard]] std::string get_origin() const noexcept override;
127
134 [[nodiscard]] std::string get_protocol() const noexcept;
135
143 [[nodiscard]] std::string get_host() const noexcept;
144
151 [[nodiscard]] std::string get_hostname() const noexcept;
152
159 [[nodiscard]] constexpr std::string_view get_pathname() const noexcept;
160
167 [[nodiscard]] ada_really_inline size_t get_pathname_length() const noexcept;
168
174 [[nodiscard]] std::string get_search() const noexcept;
175
181 [[nodiscard]] const std::string &get_username() const noexcept;
182
187 bool set_username(std::string_view input);
188
193 bool set_password(std::string_view input);
194
199 bool set_port(std::string_view input);
200
205 void set_hash(std::string_view input);
206
211 void set_search(std::string_view input);
212
217 bool set_pathname(std::string_view input);
218
223 bool set_host(std::string_view input);
224
229 bool set_hostname(std::string_view input);
230
235 bool set_protocol(std::string_view input);
236
240 bool set_href(std::string_view input);
241
247 [[nodiscard]] const std::string &get_password() const noexcept;
248
254 [[nodiscard]] std::string get_port() const noexcept;
255
261 [[nodiscard]] std::string get_hash() const noexcept;
262
267 [[nodiscard]] ada_really_inline bool has_credentials() const noexcept;
268
290 [[nodiscard]] ada_really_inline ada::url_components get_components()
291 const noexcept;
293 [[nodiscard]] constexpr bool has_hash() const noexcept override;
295 [[nodiscard]] constexpr bool has_search() const noexcept override;
296
297 private:
298 friend ada::url ada::parser::parse_url<ada::url>(std::string_view,
299 const ada::url *);
301 std::string_view, const ada::url_aggregator *);
303 ada::url &url) noexcept;
304
305 friend ada::url ada::parser::parse_url_impl<ada::url, true>(std::string_view,
306 const ada::url *);
308 ada::url_aggregator, true>(std::string_view, const ada::url_aggregator *);
309
310 inline void update_unencoded_base_hash(std::string_view input);
311 inline void update_base_hostname(std::string_view input);
312 inline void update_base_search(std::string_view input,
313 const uint8_t query_percent_encode_set[]);
314 inline void update_base_search(std::optional<std::string> &&input);
315 inline void update_base_pathname(std::string_view input);
316 inline void update_base_username(std::string_view input);
317 inline void update_base_password(std::string_view input);
318 inline void update_base_port(std::optional<uint16_t> input);
319
325 template <bool override_hostname = false>
326 bool set_host_or_hostname(std::string_view input);
327
332 [[nodiscard]] bool parse_ipv4(std::string_view input);
333
338 [[nodiscard]] bool parse_ipv6(std::string_view input);
339
344 [[nodiscard]] bool parse_opaque_host(std::string_view input);
345
355 std::string non_special_scheme{};
356
361 [[nodiscard]] inline bool cannot_have_credentials_or_port() const;
362
363 ada_really_inline size_t parse_port(
364 std::string_view view, bool check_trailing_content) noexcept override;
365
366 ada_really_inline size_t parse_port(std::string_view view) noexcept override {
367 return this->parse_port(view, false);
368 }
369
378 [[nodiscard]] ada_really_inline bool parse_host(std::string_view input);
379
380 template <bool has_state_override = false>
381 [[nodiscard]] ada_really_inline bool parse_scheme(std::string_view input);
382
383 constexpr void clear_pathname() override;
384 constexpr void clear_search() override;
385 constexpr void set_protocol_as_file();
386
397 ada_really_inline void parse_path(std::string_view input);
398
404 inline void set_scheme(std::string &&new_scheme) noexcept;
405
410 constexpr void copy_scheme(ada::url &&u) noexcept;
411
416 constexpr void copy_scheme(const ada::url &u);
417
418}; // struct url
419
420inline std::ostream &operator<<(std::ostream &out, const ada::url &u);
421} // namespace ada
422
423#endif // ADA_URL_H
Declarations for URL specific checkers used within Ada.
Common definitions for cross-platform compiler support.
#define ada_really_inline
Definition common_defs.h:81
Definitions for helper functions used within Ada.
Includes the definitions for helper functions.
Includes the definitions for supported parsers.
Definition parser-inl.h:16
Definition ada_idna.h:13
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:38
Lightweight URL struct.
Base class of URL implementations.
Definition url_base.h:44
Generic URL struct reliant on std::string instantiation.
Definition url.h:45
void set_hash(std::string_view input)
Definition url.cpp:802
std::string get_search() const noexcept
Definition url.cpp:641
bool set_hostname(std::string_view input)
Definition url.cpp:753
bool set_host(std::string_view input)
Definition url.cpp:749
ada_really_inline ada::url_components get_components() const noexcept
Definition url-inl.h:50
url(url &&u) noexcept=default
bool has_empty_hostname() const noexcept
Definition url-inl.h:29
bool has_port() const noexcept
Definition url-inl.h:22
ada_really_inline bool has_credentials() const noexcept
Definition url-inl.h:19
friend ada::url ada::parser::parse_url(std::string_view, const ada::url *)
bool set_password(std::string_view input)
Definition url.cpp:766
url & operator=(url &&u) noexcept=default
url & operator=(const url &u)=default
void set_search(std::string_view input)
Definition url.cpp:816
ada_really_inline size_t get_pathname_length() const noexcept
Definition url-inl.h:42
bool set_href(std::string_view input)
Definition url.cpp:867
ada_really_inline std::string get_href() const noexcept
Definition url-inl.h:187
bool has_hostname() const noexcept
Definition url-inl.h:35
bool set_username(std::string_view input)
Definition url.cpp:757
constexpr std::string_view get_pathname() const noexcept
Definition url-inl.h:46
url(const url &u)=default
~url() override=default
url()=default
std::string get_host() const noexcept
Definition url.cpp:623
std::string get_hash() const noexcept
Definition url.cpp:660
bool set_pathname(std::string_view input)
Definition url.cpp:834
std::string get_origin() const noexcept override
Definition url.cpp:589
friend void ada::helpers::strip_trailing_spaces_from_opaque_path(ada::url &url) noexcept
std::string get_hostname() const noexcept
Definition url.cpp:637
friend ada::url ada::parser::parse_url_impl(std::string_view, const ada::url *)
const std::string & get_password() const noexcept
Definition url.cpp:652
bool set_protocol(std::string_view input)
Definition url.cpp:843
std::string get_port() const noexcept
Definition url.cpp:656
const std::string & get_username() const noexcept
Definition url.cpp:648
bool set_port(std::string_view input)
Definition url.cpp:775
constexpr bool has_search() const noexcept override
Definition url-inl.h:163
std::string to_string() const override
Definition url.cpp:533
std::string get_protocol() const noexcept
Definition url.cpp:615
constexpr bool has_hash() const noexcept override
Definition url-inl.h:159
bool has_valid_domain() const noexcept override
Definition url.cpp:582
Declaration for the basic URL definitions.
Declaration for the URL Components.