Ada 2.9.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 <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]] 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]] inline bool has_hash() const noexcept override;
288 [[nodiscard]] inline 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 inline void update_base_search(std::string_view input,
307 const uint8_t query_percent_encode_set[]);
308 inline void update_base_search(std::optional<std::string> input);
309 inline void update_base_pathname(std::string_view input);
310 inline void update_base_username(std::string_view input);
311 inline void update_base_password(std::string_view input);
312 inline void update_base_port(std::optional<uint16_t> input);
313
319 template <bool override_hostname = false>
320 bool set_host_or_hostname(std::string_view input);
321
326 [[nodiscard]] bool parse_ipv4(std::string_view input);
327
332 [[nodiscard]] bool parse_ipv6(std::string_view input);
333
338 [[nodiscard]] bool parse_opaque_host(std::string_view input);
339
349 std::string non_special_scheme{};
350
355 [[nodiscard]] inline bool cannot_have_credentials_or_port() const;
356
357 ada_really_inline size_t parse_port(
358 std::string_view view, bool check_trailing_content) noexcept override;
359
360 ada_really_inline size_t parse_port(std::string_view view) noexcept override {
361 return this->parse_port(view, false);
362 }
363
368 inline void copy_scheme(const ada::url &u);
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 inline void clear_pathname() override;
384 inline void clear_search() override;
385 inline 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 inline void copy_scheme(ada::url &&u) noexcept;
411
412}; // struct url
413
414inline std::ostream &operator<<(std::ostream &out, const ada::url &u);
415} // namespace ada
416
417#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:84
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)
std::string get_search() const noexcept
bool set_hostname(std::string_view input)
bool set_host(std::string_view input)
ada_really_inline ada::url_components get_components() const noexcept
Definition url-inl.h:46
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)
std::string_view get_pathname() const noexcept
url & operator=(url &&u) noexcept=default
bool has_hash() const noexcept override
Definition url-inl.h:155
url & operator=(const url &u)=default
void set_search(std::string_view input)
ada_really_inline size_t get_pathname_length() const noexcept
Definition url-inl.h:42
bool set_href(std::string_view input)
ada_really_inline std::string get_href() const noexcept
Definition url-inl.h:183
bool has_hostname() const noexcept
Definition url-inl.h:35
bool set_username(std::string_view input)
url(const url &u)=default
~url() override=default
url()=default
std::string get_host() const noexcept
std::string get_hash() const noexcept
bool set_pathname(std::string_view input)
std::string get_origin() const noexcept override
friend void ada::helpers::strip_trailing_spaces_from_opaque_path(ada::url &url) noexcept
std::string get_hostname() const noexcept
friend ada::url ada::parser::parse_url_impl(std::string_view, const ada::url *)
const std::string & get_password() const noexcept
bool set_protocol(std::string_view input)
std::string get_port() const noexcept
const std::string & get_username() const noexcept
bool set_port(std::string_view input)
std::string to_string() const override
Definition url.cpp:536
std::string get_protocol() const noexcept
bool has_valid_domain() const noexcept override
Definition url.cpp:585
bool has_search() const noexcept override
Definition url-inl.h:159
Definitions for all unicode specific functions.
Declaration for the basic URL definitions.
Declaration for the URL Components.