Ada 2.9.2
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 <charconv>
10#include <iostream>
11#include <optional>
12#include <string>
13#include <string_view>
14
15#include "ada/checkers.h"
16#include "ada/common_defs.h"
17#include "ada/log.h"
18#include "ada/scheme.h"
19#include "ada/serializers.h"
20#include "ada/unicode.h"
21#include "ada/url_base.h"
22#include "ada/url_components.h"
23
24namespace ada {
25
38struct url : url_base {
39 url() = default;
40 url(const url &u) = default;
41 url(url &&u) noexcept = default;
42 url &operator=(url &&u) noexcept = default;
43 url &operator=(const url &u) = default;
44 ~url() override = default;
45
51 std::string username{};
52
58 std::string password{};
59
64 std::optional<std::string> host{};
65
71 std::optional<uint16_t> port{};
72
78 std::string path{};
79
84 std::optional<std::string> query{};
85
92 std::optional<std::string> hash{};
93
95 [[nodiscard]] inline bool has_empty_hostname() const noexcept;
97 [[nodiscard]] inline bool has_port() const noexcept;
99 [[nodiscard]] inline bool has_hostname() const noexcept;
100 [[nodiscard]] bool has_valid_domain() const noexcept override;
101
105 [[nodiscard]] std::string to_string() const override;
106
111 [[nodiscard]] ada_really_inline std::string get_href() const noexcept;
112
119 [[nodiscard]] std::string get_origin() const noexcept override;
120
127 [[nodiscard]] std::string get_protocol() const noexcept;
128
136 [[nodiscard]] std::string get_host() const noexcept;
137
144 [[nodiscard]] std::string get_hostname() const noexcept;
145
152 [[nodiscard]] constexpr std::string_view get_pathname() const noexcept;
153
160 [[nodiscard]] ada_really_inline size_t get_pathname_length() const noexcept;
161
167 [[nodiscard]] std::string get_search() const noexcept;
168
174 [[nodiscard]] const std::string &get_username() const noexcept;
175
180 bool set_username(std::string_view input);
181
186 bool set_password(std::string_view input);
187
192 bool set_port(std::string_view input);
193
198 void set_hash(std::string_view input);
199
204 void set_search(std::string_view input);
205
210 bool set_pathname(std::string_view input);
211
216 bool set_host(std::string_view input);
217
222 bool set_hostname(std::string_view input);
223
228 bool set_protocol(std::string_view input);
229
233 bool set_href(std::string_view input);
234
240 [[nodiscard]] const std::string &get_password() const noexcept;
241
247 [[nodiscard]] std::string get_port() const noexcept;
248
254 [[nodiscard]] std::string get_hash() const noexcept;
255
260 [[nodiscard]] ada_really_inline bool has_credentials() const noexcept;
261
283 [[nodiscard]] ada_really_inline ada::url_components get_components()
284 const noexcept;
286 [[nodiscard]] constexpr bool has_hash() const noexcept override;
288 [[nodiscard]] constexpr bool has_search() const noexcept override;
289
290 private:
291 friend ada::url ada::parser::parse_url<ada::url>(std::string_view,
292 const ada::url *);
294 std::string_view, const ada::url_aggregator *);
296 ada::url &url) noexcept;
297
298 friend ada::url ada::parser::parse_url_impl<ada::url, true>(std::string_view,
299 const ada::url *);
301 ada::url_aggregator, true>(std::string_view, const ada::url_aggregator *);
302
303 inline void update_unencoded_base_hash(std::string_view input);
304 inline void update_base_hostname(std::string_view input);
305 inline void update_base_search(std::string_view input,
306 const uint8_t query_percent_encode_set[]);
307 inline void update_base_search(std::optional<std::string> &&input);
308 inline void update_base_pathname(std::string_view input);
309 inline void update_base_username(std::string_view input);
310 inline void update_base_password(std::string_view input);
311 inline void update_base_port(std::optional<uint16_t> input);
312
318 template <bool override_hostname = false>
319 bool set_host_or_hostname(std::string_view input);
320
325 [[nodiscard]] bool parse_ipv4(std::string_view input);
326
331 [[nodiscard]] bool parse_ipv6(std::string_view input);
332
337 [[nodiscard]] bool parse_opaque_host(std::string_view input);
338
348 std::string non_special_scheme{};
349
354 [[nodiscard]] inline bool cannot_have_credentials_or_port() const;
355
356 ada_really_inline size_t parse_port(
357 std::string_view view, bool check_trailing_content) noexcept override;
358
359 ada_really_inline size_t parse_port(std::string_view view) noexcept override {
360 return this->parse_port(view, false);
361 }
362
371 [[nodiscard]] ada_really_inline bool parse_host(std::string_view input);
372
373 template <bool has_state_override = false>
374 [[nodiscard]] ada_really_inline bool parse_scheme(std::string_view input);
375
376 constexpr void clear_pathname() override;
377 constexpr void clear_search() override;
378 constexpr void set_protocol_as_file();
379
390 ada_really_inline void parse_path(std::string_view input);
391
397 inline void set_scheme(std::string &&new_scheme) noexcept;
398
403 constexpr void copy_scheme(ada::url &&u) noexcept;
404
409 constexpr void copy_scheme(const ada::url &u);
410
411}; // struct url
412
413inline std::ostream &operator<<(std::ostream &out, const ada::url &u);
414} // namespace ada
415
416#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:77
Definition ada_idna.h:13
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:38
Declarations for the URL scheme.
Definitions for the URL serializers.
Lightweight URL struct.
Base class of URL implementations.
Definition url_base.h:44
Generic URL struct reliant on std::string instantiation.
Definition url.h:38
void set_hash(std::string_view input)
Definition url.cpp:805
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:819
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:870
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:837
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:846
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
Definitions for all unicode specific functions.
Declaration for the basic URL definitions.
Declaration for the URL Components.