Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
parser-inl.h
Go to the documentation of this file.
1
4#ifndef ADA_PARSER_INL_H
5#define ADA_PARSER_INL_H
6
7#include "ada/expected.h"
8#include "ada/url_pattern.h"
10#include "ada/parser.h"
11
12#include <cstdint>
13#include <string>
14#include <string_view>
15#include <utility>
16#include <variant>
17
18namespace ada::parser {
19#if ADA_INCLUDE_URL_PATTERN
20template <url_pattern_regex::regex_concept regex_provider>
21tl::expected<url_pattern<regex_provider>, errors> parse_url_pattern_impl(
22 std::variant<std::string_view, url_pattern_init>&& input,
23 const std::string_view* base_url, const url_pattern_options* options) {
24 // Let init be null.
25 url_pattern_init init;
26
27 // If input is a scalar value string then:
28 if (std::holds_alternative<std::string_view>(input)) {
29 // Set init to the result of running parse a constructor string given input.
30 auto parse_result =
31 url_pattern_helpers::constructor_string_parser<regex_provider>::parse(
32 std::get<std::string_view>(input));
33 if (!parse_result) {
34 ada_log("constructor_string_parser::parse failed");
35 return tl::unexpected(parse_result.error());
36 }
37 init = std::move(*parse_result);
38 // If baseURL is null and init["protocol"] does not exist, then throw a
39 // TypeError.
40 if (!base_url && !init.protocol) {
41 ada_log("base url is null and protocol is not set");
42 return tl::unexpected(errors::type_error);
43 }
44
45 // If baseURL is not null, set init["baseURL"] to baseURL.
46 if (base_url) {
47 init.base_url = std::string(*base_url);
48 }
49 } else {
50 // Assert: input is a URLPatternInit.
51 ADA_ASSERT_TRUE(std::holds_alternative<url_pattern_init>(input));
52 // If baseURL is not null, then throw a TypeError.
53 if (base_url) {
54 ada_log("base url is not null");
55 return tl::unexpected(errors::type_error);
56 }
57 // Optimization: Avoid copy by moving the input value.
58 // Set init to input.
59 init = std::move(std::get<url_pattern_init>(input));
60 }
61
62 // Let processedInit be the result of process a URLPatternInit given init,
63 // "pattern", null, null, null, null, null, null, null, and null.
64 auto processed_init =
65 url_pattern_init::process(init, url_pattern_init::process_type::pattern);
66 if (!processed_init) {
67 ada_log("url_pattern_init::process failed for init and 'pattern'");
68 return tl::unexpected(processed_init.error());
69 }
70
71 // For each componentName of "protocol", "username", "password", "hostname",
72 // "port", "pathname", "search", "hash" If processedInit[componentName] does
73 // not exist, then set processedInit[componentName] to "*".
74 ADA_ASSERT_TRUE(processed_init.has_value());
75 if (!processed_init->protocol) processed_init->protocol = "*";
76 if (!processed_init->username) processed_init->username = "*";
77 if (!processed_init->password) processed_init->password = "*";
78 if (!processed_init->hostname) processed_init->hostname = "*";
79 if (!processed_init->port) processed_init->port = "*";
80 if (!processed_init->pathname) processed_init->pathname = "*";
81 if (!processed_init->search) processed_init->search = "*";
82 if (!processed_init->hash) processed_init->hash = "*";
83
84 ada_log("-- processed_init->protocol: ", processed_init->protocol.value());
85 ada_log("-- processed_init->username: ", processed_init->username.value());
86 ada_log("-- processed_init->password: ", processed_init->password.value());
87 ada_log("-- processed_init->hostname: ", processed_init->hostname.value());
88 ada_log("-- processed_init->port: ", processed_init->port.value());
89 ada_log("-- processed_init->pathname: ", processed_init->pathname.value());
90 ada_log("-- processed_init->search: ", processed_init->search.value());
91 ada_log("-- processed_init->hash: ", processed_init->hash.value());
92
93 // If processedInit["protocol"] is a special scheme and processedInit["port"]
94 // is a string which represents its corresponding default port in radix-10
95 // using ASCII digits then set processedInit["port"] to the empty string.
96 // TODO: Optimization opportunity.
97 if (scheme::is_special(*processed_init->protocol)) {
98 // file is the only special scheme with no default port; get_special_port
99 // returns 0 as its sentinel, so a literal "0" port must not be mistaken for
100 // a default and dropped.
101 uint16_t default_port = scheme::get_special_port(*processed_init->protocol);
102 if (default_port != 0 &&
103 std::to_string(default_port) == processed_init->port.value()) {
104 processed_init->port->clear();
105 }
106 }
107
108 // Let urlPattern be a new URL pattern.
109 url_pattern<regex_provider> url_pattern_{};
110
111 // Set urlPattern's protocol component to the result of compiling a component
112 // given processedInit["protocol"], canonicalize a protocol, and default
113 // options.
114 auto protocol_component = url_pattern_component<regex_provider>::compile(
115 processed_init->protocol.value(),
116 url_pattern_helpers::canonicalize_protocol,
117 url_pattern_compile_component_options::DEFAULT);
118 if (!protocol_component) {
119 ada_log("url_pattern_component::compile failed for protocol ",
120 processed_init->protocol.value());
121 return tl::unexpected(protocol_component.error());
122 }
123 url_pattern_.protocol_component = std::move(*protocol_component);
124
125 // Set urlPattern's username component to the result of compiling a component
126 // given processedInit["username"], canonicalize a username, and default
127 // options.
128 auto username_component = url_pattern_component<regex_provider>::compile(
129 processed_init->username.value(),
130 url_pattern_helpers::canonicalize_username,
131 url_pattern_compile_component_options::DEFAULT);
132 if (!username_component) {
133 ada_log("url_pattern_component::compile failed for username ",
134 processed_init->username.value());
135 return tl::unexpected(username_component.error());
136 }
137 url_pattern_.username_component = std::move(*username_component);
138
139 // Set urlPattern's password component to the result of compiling a component
140 // given processedInit["password"], canonicalize a password, and default
141 // options.
142 auto password_component = url_pattern_component<regex_provider>::compile(
143 processed_init->password.value(),
144 url_pattern_helpers::canonicalize_password,
145 url_pattern_compile_component_options::DEFAULT);
146 if (!password_component) {
147 ada_log("url_pattern_component::compile failed for password ",
148 processed_init->password.value());
149 return tl::unexpected(password_component.error());
150 }
151 url_pattern_.password_component = std::move(*password_component);
152
153 // TODO: Optimization opportunity. The following if statement can be
154 // simplified.
155 // If the result running hostname pattern is an IPv6 address given
156 // processedInit["hostname"] is true, then set urlPattern's hostname component
157 // to the result of compiling a component given processedInit["hostname"],
158 // canonicalize an IPv6 hostname, and hostname options.
159 if (url_pattern_helpers::is_ipv6_address(processed_init->hostname.value())) {
160 ada_log("processed_init->hostname is ipv6 address");
161 // then set urlPattern's hostname component to the result of compiling a
162 // component given processedInit["hostname"], canonicalize an IPv6 hostname,
163 // and hostname options.
164 auto hostname_component = url_pattern_component<regex_provider>::compile(
165 processed_init->hostname.value(),
166 url_pattern_helpers::canonicalize_ipv6_hostname,
167 url_pattern_compile_component_options::DEFAULT);
168 if (!hostname_component) {
169 ada_log("url_pattern_component::compile failed for ipv6 hostname ",
170 processed_init->hostname.value());
171 return tl::unexpected(hostname_component.error());
172 }
173 url_pattern_.hostname_component = std::move(*hostname_component);
174 } else {
175 // Otherwise, set urlPattern's hostname component to the result of compiling
176 // a component given processedInit["hostname"], canonicalize a hostname, and
177 // hostname options.
178 auto hostname_component = url_pattern_component<regex_provider>::compile(
179 processed_init->hostname.value(),
180 url_pattern_helpers::canonicalize_hostname,
181 url_pattern_compile_component_options::HOSTNAME);
182 if (!hostname_component) {
183 ada_log("url_pattern_component::compile failed for hostname ",
184 processed_init->hostname.value());
185 return tl::unexpected(hostname_component.error());
186 }
187 url_pattern_.hostname_component = std::move(*hostname_component);
188 }
189
190 // Set urlPattern's port component to the result of compiling a component
191 // given processedInit["port"], canonicalize a port, and default options.
192 auto port_component = url_pattern_component<regex_provider>::compile(
193 processed_init->port.value(), url_pattern_helpers::canonicalize_port,
194 url_pattern_compile_component_options::DEFAULT);
195 if (!port_component) {
196 ada_log("url_pattern_component::compile failed for port ",
197 processed_init->port.value());
198 return tl::unexpected(port_component.error());
199 }
200 url_pattern_.port_component = std::move(*port_component);
201
202 // Let compileOptions be a copy of the default options with the ignore case
203 // property set to options["ignoreCase"].
204 auto compile_options = url_pattern_compile_component_options::DEFAULT;
205 if (options) {
206 compile_options.ignore_case = options->ignore_case;
207 // Recorded on the pattern so that url_pattern::ignore_case() reports the
208 // option it was created with.
209 url_pattern_.ignore_case_ = options->ignore_case;
210 }
211
212 // TODO: Optimization opportunity: Simplify this if statement.
213 // If the result of running protocol component matches a special scheme given
214 // urlPattern's protocol component is true, then:
215 if (url_pattern_helpers::protocol_component_matches_special_scheme<
216 regex_provider>(url_pattern_.protocol_component)) {
217 // Let pathCompileOptions be copy of the pathname options with the ignore
218 // case property set to options["ignoreCase"].
219 auto path_compile_options = url_pattern_compile_component_options::PATHNAME;
220 if (options) {
221 path_compile_options.ignore_case = options->ignore_case;
222 }
223
224 // Set urlPattern's pathname component to the result of compiling a
225 // component given processedInit["pathname"], canonicalize a pathname, and
226 // pathCompileOptions.
227 auto pathname_component = url_pattern_component<regex_provider>::compile(
228 processed_init->pathname.value(),
229 url_pattern_helpers::canonicalize_pathname, path_compile_options);
230 if (!pathname_component) {
231 ada_log("url_pattern_component::compile failed for pathname ",
232 processed_init->pathname.value());
233 return tl::unexpected(pathname_component.error());
234 }
235 url_pattern_.pathname_component = std::move(*pathname_component);
236 } else {
237 // Otherwise set urlPattern's pathname component to the result of compiling
238 // a component given processedInit["pathname"], canonicalize an opaque
239 // pathname, and compileOptions.
240 auto pathname_component = url_pattern_component<regex_provider>::compile(
241 processed_init->pathname.value(),
242 url_pattern_helpers::canonicalize_opaque_pathname, compile_options);
243 if (!pathname_component) {
244 ada_log("url_pattern_component::compile failed for opaque pathname ",
245 processed_init->pathname.value());
246 return tl::unexpected(pathname_component.error());
247 }
248 url_pattern_.pathname_component = std::move(*pathname_component);
249 }
250
251 // Set urlPattern's search component to the result of compiling a component
252 // given processedInit["search"], canonicalize a search, and compileOptions.
253 auto search_component = url_pattern_component<regex_provider>::compile(
254 processed_init->search.value(), url_pattern_helpers::canonicalize_search,
255 compile_options);
256 if (!search_component) {
257 ada_log("url_pattern_component::compile failed for search ",
258 processed_init->search.value());
259 return tl::unexpected(search_component.error());
260 }
261 url_pattern_.search_component = std::move(*search_component);
262
263 // Set urlPattern's hash component to the result of compiling a component
264 // given processedInit["hash"], canonicalize a hash, and compileOptions.
265 auto hash_component = url_pattern_component<regex_provider>::compile(
266 processed_init->hash.value(), url_pattern_helpers::canonicalize_hash,
267 compile_options);
268 if (!hash_component) {
269 ada_log("url_pattern_component::compile failed for hash ",
270 processed_init->hash.value());
271 return tl::unexpected(hash_component.error());
272 }
273 url_pattern_.hash_component = std::move(*hash_component);
274
275 // Return urlPattern.
276 return url_pattern_;
277}
278#endif // ADA_INCLUDE_URL_PATTERN
279
280} // namespace ada::parser
281
282#endif // ADA_PARSER_INL_H
#define ADA_ASSERT_TRUE(COND)
Internal URL parsing implementation.
Definition parser-inl.h:18
constexpr uint16_t get_special_port(std::string_view scheme) noexcept
Definition scheme-inl.h:94
errors
Error codes for URL parsing operations.
Definition errors.h:17
@ type_error
Definition errors.h:18
Low-level URL parsing functions.
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9
URLPattern API implementation.
Declaration for the URLPattern helpers.