Ada 2.8.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 "ada/checkers.h"
9#include "ada/common_defs.h"
10#include "ada/log.h"
11#include "ada/scheme.h"
12#include "ada/serializers.h"
13#include "ada/unicode.h"
14#include "ada/url_base.h"
15#include "ada/url_components.h"
16
17#include <algorithm>
18#include <charconv>
19#include <iostream>
20#include <optional>
21#include <string>
22#include <string_view>
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 inline void update_unencoded_base_hash(std::string_view input);
299 inline void update_base_hostname(std::string_view input);
300 inline void update_base_search(std::string_view input);
301 inline void update_base_search(std::string_view input,
302 const uint8_t query_percent_encode_set[]);
303 inline void update_base_search(std::optional<std::string> input);
304 inline void update_base_pathname(std::string_view input);
305 inline void update_base_username(std::string_view input);
306 inline void update_base_password(std::string_view input);
307 inline void update_base_port(std::optional<uint16_t> input);
308
314 template <bool override_hostname = false>
315 bool set_host_or_hostname(std::string_view input);
316
321 [[nodiscard]] bool parse_ipv4(std::string_view input);
322
327 [[nodiscard]] bool parse_ipv6(std::string_view input);
328
333 [[nodiscard]] bool parse_opaque_host(std::string_view input);
334
344 std::string non_special_scheme{};
345
350 [[nodiscard]] inline bool cannot_have_credentials_or_port() const;
351
352 ada_really_inline size_t parse_port(
353 std::string_view view, bool check_trailing_content) noexcept override;
354
355 ada_really_inline size_t parse_port(std::string_view view) noexcept override {
356 return this->parse_port(view, false);
357 }
358
363 inline void copy_scheme(const ada::url &u);
364
373 [[nodiscard]] ada_really_inline bool parse_host(std::string_view input);
374
375 template <bool has_state_override = false>
376 [[nodiscard]] ada_really_inline bool parse_scheme(std::string_view input);
377
378 inline void clear_pathname() override;
379 inline void clear_search() override;
380 inline void set_protocol_as_file();
381
392 ada_really_inline void parse_path(std::string_view input);
393
399 inline void set_scheme(std::string &&new_scheme) noexcept;
400
405 inline void copy_scheme(ada::url &&u) noexcept;
406
407}; // struct url
408
409inline std::ostream &operator<<(std::ostream &out, const ada::url &u);
410} // namespace ada
411
412#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
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.