Ada 3.4.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_init.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_INIT_H
6#define ADA_URL_PATTERN_INIT_H
7
8#include "ada/expected.h"
9#include "ada/errors.h"
10
11#include <string_view>
12#include <string>
13#include <optional>
14#include <iostream>
15
16#if ADA_TESTING
17#include <iostream>
18#endif // ADA_TESTING
19
20#if ADA_INCLUDE_URL_PATTERN
21namespace ada {
22
23// Important: C++20 allows us to use concept rather than `using` or `typedef
24// and allows functions with second argument, which is optional (using either
25// std::nullopt or a parameter with default value)
26template <typename F>
27concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
28 { f(sv) } -> std::same_as<tl::expected<std::string, errors>>;
29};
30
31// A structure providing matching patterns for individual components
32// of a URL. When a URLPattern is created, or when a URLPattern is
33// used to match or test against a URL, the input can be given as
34// either a string or a URLPatternInit struct. If a string is given,
35// it will be parsed to create a URLPatternInit. The URLPatternInit
36// API is defined as part of the URLPattern specification.
37// All provided strings must be valid UTF-8.
38struct url_pattern_init {
39 enum class process_type : uint8_t {
40 url,
41 pattern,
42 };
43
44 friend std::ostream& operator<<(std::ostream& os, process_type type) {
45 switch (type) {
46 case process_type::url:
47 return os << "url";
48 case process_type::pattern:
49 return os << "pattern";
50 default:
51 return os << "unknown";
52 }
53 }
54
55 // All strings must be valid UTF-8.
56 // @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
57 static tl::expected<url_pattern_init, errors> process(
58 const url_pattern_init& init, process_type type,
59 std::optional<std::string_view> protocol = std::nullopt,
60 std::optional<std::string_view> username = std::nullopt,
61 std::optional<std::string_view> password = std::nullopt,
62 std::optional<std::string_view> hostname = std::nullopt,
63 std::optional<std::string_view> port = std::nullopt,
64 std::optional<std::string_view> pathname = std::nullopt,
65 std::optional<std::string_view> search = std::nullopt,
66 std::optional<std::string_view> hash = std::nullopt);
67
68 // @see https://urlpattern.spec.whatwg.org/#process-protocol-for-init
69 static tl::expected<std::string, errors> process_protocol(
70 std::string_view value, process_type type);
71
72 // @see https://urlpattern.spec.whatwg.org/#process-username-for-init
73 static tl::expected<std::string, errors> process_username(
74 std::string_view value, process_type type);
75
76 // @see https://urlpattern.spec.whatwg.org/#process-password-for-init
77 static tl::expected<std::string, errors> process_password(
78 std::string_view value, process_type type);
79
80 // @see https://urlpattern.spec.whatwg.org/#process-hostname-for-init
81 static tl::expected<std::string, errors> process_hostname(
82 std::string_view value, process_type type);
83
84 // @see https://urlpattern.spec.whatwg.org/#process-port-for-init
85 static tl::expected<std::string, errors> process_port(
86 std::string_view port, std::string_view protocol, process_type type);
87
88 // @see https://urlpattern.spec.whatwg.org/#process-pathname-for-init
89 static tl::expected<std::string, errors> process_pathname(
90 std::string_view value, std::string_view protocol, process_type type);
91
92 // @see https://urlpattern.spec.whatwg.org/#process-search-for-init
93 static tl::expected<std::string, errors> process_search(
94 std::string_view value, process_type type);
95
96 // @see https://urlpattern.spec.whatwg.org/#process-hash-for-init
97 static tl::expected<std::string, errors> process_hash(std::string_view value,
98 process_type type);
99
100#if ADA_TESTING
101 friend void PrintTo(const url_pattern_init& init, std::ostream* os) {
102 *os << "protocol: '" << init.protocol.value_or("undefined") << "', ";
103 *os << "username: '" << init.username.value_or("undefined") << "', ";
104 *os << "password: '" << init.password.value_or("undefined") << "', ";
105 *os << "hostname: '" << init.hostname.value_or("undefined") << "', ";
106 *os << "port: '" << init.port.value_or("undefined") << "', ";
107 *os << "pathname: '" << init.pathname.value_or("undefined") << "', ";
108 *os << "search: '" << init.search.value_or("undefined") << "', ";
109 *os << "hash: '" << init.hash.value_or("undefined") << "', ";
110 *os << "base_url: '" << init.base_url.value_or("undefined") << "', ";
111 }
112#endif // ADA_TESTING
113
114 bool operator==(const url_pattern_init&) const;
115 // If present, must be valid UTF-8.
116 std::optional<std::string> protocol{};
117 // If present, must be valid UTF-8.
118 std::optional<std::string> username{};
119 // If present, must be valid UTF-8.
120 std::optional<std::string> password{};
121 // If present, must be valid UTF-8.
122 std::optional<std::string> hostname{};
123 // If present, must be valid UTF-8.
124 std::optional<std::string> port{};
125 // If present, must be valid UTF-8.
126 std::optional<std::string> pathname{};
127 // If present, must be valid UTF-8.
128 std::optional<std::string> search{};
129 // If present, must be valid UTF-8.
130 std::optional<std::string> hash{};
131 // If present, must be valid UTF-8.
132 std::optional<std::string> base_url{};
133};
134} // namespace ada
135#endif // ADA_INCLUDE_URL_PATTERN
136#endif // ADA_URL_PATTERN_INIT_H
Error type definitions for URL parsing.
Definition ada_idna.h:13
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:38