Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern.cpp
Go to the documentation of this file.
1#if ADA_INCLUDE_URL_PATTERN
2
4
5#include <algorithm>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <utility>
10
11namespace ada {
12
13tl::expected<url_pattern_init, errors> url_pattern_init::process(
14 const url_pattern_init& init, url_pattern_init::process_type type,
15 std::optional<std::string_view> protocol,
16 std::optional<std::string_view> username,
17 std::optional<std::string_view> password,
18 std::optional<std::string_view> hostname,
19 std::optional<std::string_view> port,
20 std::optional<std::string_view> pathname,
21 std::optional<std::string_view> search,
22 std::optional<std::string_view> hash) {
23 // Let result be the result of creating a new URLPatternInit.
24 auto result = url_pattern_init{};
25
26 // If protocol is not null, set result["protocol"] to protocol.
27 if (protocol.has_value()) result.protocol = *protocol;
28
29 // If username is not null, set result["username"] to username.
30 if (username.has_value()) result.username = *username;
31
32 // If password is not null, set result["password"] to password.
33 if (password.has_value()) result.password = *password;
34
35 // If hostname is not null, set result["hostname"] to hostname.
36 if (hostname.has_value()) result.hostname = *hostname;
37
38 // If port is not null, set result["port"] to port.
39 if (port.has_value()) result.port = *port;
40
41 // If pathname is not null, set result["pathname"] to pathname.
42 if (pathname.has_value()) result.pathname = *pathname;
43
44 // If search is not null, set result["search"] to search.
45 if (search.has_value()) result.search = *search;
46
47 // If hash is not null, set result["hash"] to hash.
48 if (hash.has_value()) result.hash = *hash;
49
50 // Let baseURL be null.
51 std::optional<url_aggregator> base_url{};
52
53 // If init["baseURL"] exists:
54 if (init.base_url.has_value()) {
55 // Set baseURL to the result of parsing init["baseURL"].
56 auto parsing_result = ada::parse<url_aggregator>(*init.base_url);
57 // If baseURL is failure, then throw a TypeError.
58 if (!parsing_result) {
59 return tl::unexpected(errors::type_error);
60 }
61 base_url = std::move(*parsing_result);
62
63 // If init["protocol"] does not exist, then set result["protocol"] to the
64 // result of processing a base URL string given baseURL's scheme and type.
65 if (!init.protocol.has_value()) {
66 ADA_ASSERT_TRUE(base_url.has_value());
67 std::string_view base_url_protocol = base_url->get_protocol();
68 if (base_url_protocol.ends_with(":")) base_url_protocol.remove_suffix(1);
69 result.protocol =
70 url_pattern_helpers::process_base_url_string(base_url_protocol, type);
71 }
72
73 // If type is not "pattern" and init contains none of "protocol",
74 // "hostname", "port" and "username", then set result["username"] to the
75 // result of processing a base URL string given baseURL's username and type.
76 if (type != process_type::pattern && !init.protocol && !init.hostname &&
77 !init.port && !init.username) {
78 result.username = url_pattern_helpers::process_base_url_string(
79 base_url->get_username(), type);
80 }
81
82 // TODO: Optimization opportunity: Merge this with the previous check.
83 // If type is not "pattern" and init contains none of "protocol",
84 // "hostname", "port", "username" and "password", then set
85 // result["password"] to the result of processing a base URL string given
86 // baseURL's password and type.
87 if (type != process_type::pattern && !init.protocol && !init.hostname &&
88 !init.port && !init.username && !init.password) {
89 result.password = url_pattern_helpers::process_base_url_string(
90 base_url->get_password(), type);
91 }
92
93 // If init contains neither "protocol" nor "hostname", then:
94 if (!init.protocol && !init.hostname) {
95 // Let baseHost be baseURL's host.
96 // If baseHost is null, then set baseHost to the empty string.
97 auto base_host = base_url->get_hostname();
98 // Set result["hostname"] to the result of processing a base URL string
99 // given baseHost and type.
100 result.hostname =
101 url_pattern_helpers::process_base_url_string(base_host, type);
102 }
103
104 // If init contains none of "protocol", "hostname", and "port", then:
105 if (!init.protocol && !init.hostname && !init.port) {
106 // If baseURL's port is null, then set result["port"] to the empty string.
107 // Otherwise, set result["port"] to baseURL's port, serialized.
108 result.port = base_url->get_port();
109 }
110
111 // If init contains none of "protocol", "hostname", "port", and "pathname",
112 // then set result["pathname"] to the result of processing a base URL string
113 // given the result of URL path serializing baseURL and type.
114 if (!init.protocol && !init.hostname && !init.port && !init.pathname) {
115 result.pathname = url_pattern_helpers::process_base_url_string(
116 base_url->get_pathname(), type);
117 }
118
119 // If init contains none of "protocol", "hostname", "port", "pathname", and
120 // "search", then:
121 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
122 !init.search) {
123 // Let baseQuery be baseURL's query. get_search() carries the leading "?"
124 // delimiter, but the spec inherits the query itself, so drop it.
125 std::string_view base_query = base_url->get_search();
126 if (base_query.starts_with("?")) base_query.remove_prefix(1);
127 // Set result["search"] to the result of processing a base URL string
128 // given baseQuery and type.
129 result.search =
130 url_pattern_helpers::process_base_url_string(base_query, type);
131 }
132
133 // If init contains none of "protocol", "hostname", "port", "pathname",
134 // "search", and "hash", then:
135 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
136 !init.search && !init.hash) {
137 // Let baseFragment be baseURL's fragment. get_hash() carries the leading
138 // "#" delimiter, but the spec inherits the fragment itself, so drop it.
139 std::string_view base_fragment = base_url->get_hash();
140 if (base_fragment.starts_with("#")) base_fragment.remove_prefix(1);
141 // Set result["hash"] to the result of processing a base URL string given
142 // baseFragment and type.
143 result.hash =
144 url_pattern_helpers::process_base_url_string(base_fragment, type);
145 }
146 }
147
148 // If init["protocol"] exists, then set result["protocol"] to the result of
149 // process protocol for init given init["protocol"] and type.
150 if (init.protocol) {
151 auto process_result = process_protocol(*init.protocol, type);
152 if (!process_result) {
153 return tl::unexpected(process_result.error());
154 }
155 result.protocol = std::move(*process_result);
156 }
157
158 // If init["username"] exists, then set result["username"] to the result of
159 // process username for init given init["username"] and type.
160 if (init.username.has_value()) {
161 auto process_result = process_username(*init.username, type);
162 if (!process_result) {
163 return tl::unexpected(process_result.error());
164 }
165 result.username = std::move(*process_result);
166 }
167
168 // If init["password"] exists, then set result["password"] to the result of
169 // process password for init given init["password"] and type.
170 if (init.password.has_value()) {
171 auto process_result = process_password(*init.password, type);
172 if (!process_result) {
173 return tl::unexpected(process_result.error());
174 }
175 result.password = std::move(*process_result);
176 }
177
178 // If init["hostname"] exists, then set result["hostname"] to the result of
179 // process hostname for init given init["hostname"] and type.
180 if (init.hostname.has_value()) {
181 auto process_result = process_hostname(*init.hostname, type);
182 if (!process_result) {
183 return tl::unexpected(process_result.error());
184 }
185 result.hostname = std::move(*process_result);
186 }
187
188 // If init["port"] exists, then set result["port"] to the result of process
189 // port for init given init["port"], result["protocol"], and type.
190 if (init.port) {
191 auto process_result =
192 process_port(*init.port, result.protocol.value_or("fake"), type);
193 if (!process_result) {
194 return tl::unexpected(process_result.error());
195 }
196 result.port = std::move(*process_result);
197 }
198
199 // If init["pathname"] exists:
200 if (init.pathname.has_value()) {
201 // Set result["pathname"] to init["pathname"].
202 result.pathname = init.pathname;
203
204 // If the following are all true:
205 // - baseURL is not null;
206 // - baseURL has an opaque path; and
207 // - the result of running is an absolute pathname given result["pathname"]
208 // and type is false,
209 if (base_url && !base_url->has_opaque_path &&
210 !url_pattern_helpers::is_absolute_pathname(*result.pathname, type)) {
211 // Let baseURLPath be the result of running process a base URL string
212 // given the result of URL path serializing baseURL and type.
213 // TODO: Optimization opportunity: Avoid returning a string if no slash
214 // exist.
215 std::string base_url_path = url_pattern_helpers::process_base_url_string(
216 base_url->get_pathname(), type);
217
218 // Let slash index be the index of the last U+002F (/) code point found in
219 // baseURLPath, interpreted as a sequence of code points, or null if there
220 // are no instances of the code point.
221 auto slash_index = base_url_path.find_last_of('/');
222
223 // If slash index is not null:
224 if (slash_index != std::string::npos) {
225 // Let new pathname be the code point substring from 0 to slash index +
226 // 1 within baseURLPath.
227 base_url_path.resize(slash_index + 1);
228 // Append result["pathname"] to the end of new pathname.
229 ADA_ASSERT_TRUE(result.pathname.has_value());
230 base_url_path.append(std::move(*result.pathname));
231 // Set result["pathname"] to new pathname.
232 result.pathname = std::move(base_url_path);
233 }
234 }
235
236 // Set result["pathname"] to the result of process pathname for init given
237 // result["pathname"], result["protocol"], and type.
238 auto pathname_processing_result =
239 process_pathname(*result.pathname, result.protocol.value_or(""), type);
240 if (!pathname_processing_result) {
241 return tl::unexpected(pathname_processing_result.error());
242 }
243 result.pathname = std::move(*pathname_processing_result);
244 }
245
246 // If init["search"] exists then set result["search"] to the result of process
247 // search for init given init["search"] and type.
248 if (init.search) {
249 auto process_result = process_search(*init.search, type);
250 if (!process_result) {
251 return tl::unexpected(process_result.error());
252 }
253 result.search = std::move(*process_result);
254 }
255
256 // If init["hash"] exists then set result["hash"] to the result of process
257 // hash for init given init["hash"] and type.
258 if (init.hash) {
259 auto process_result = process_hash(*init.hash, type);
260 if (!process_result) {
261 return tl::unexpected(process_result.error());
262 }
263 result.hash = std::move(*process_result);
264 }
265 // Return result.
266 return result;
267}
268
269tl::expected<std::string, errors> url_pattern_init::process_protocol(
270 std::string_view value, process_type type) {
271 ada_log("process_protocol=", value, " [", type, "]");
272 // Let strippedValue be the given value with a single trailing U+003A (:)
273 // removed, if any.
274 if (value.ends_with(":")) {
275 value.remove_suffix(1);
276 }
277 // If type is "pattern" then return strippedValue.
278 if (type == process_type::pattern) {
279 return std::string(value);
280 }
281 // Return the result of running canonicalize a protocol given strippedValue.
282 return url_pattern_helpers::canonicalize_protocol(value);
283}
284
285tl::expected<std::string, errors> url_pattern_init::process_username(
286 std::string_view value, process_type type) {
287 // If type is "pattern" then return value.
288 if (type == process_type::pattern) {
289 return std::string(value);
290 }
291 // Return the result of running canonicalize a username given value.
292 return url_pattern_helpers::canonicalize_username(value);
293}
294
295tl::expected<std::string, errors> url_pattern_init::process_password(
296 std::string_view value, process_type type) {
297 // If type is "pattern" then return value.
298 if (type == process_type::pattern) {
299 return std::string(value);
300 }
301 // Return the result of running canonicalize a password given value.
302 return url_pattern_helpers::canonicalize_password(value);
303}
304
305tl::expected<std::string, errors> url_pattern_init::process_hostname(
306 std::string_view value, process_type type) {
307 ada_log("process_hostname value=", value, " type=", type);
308 // If type is "pattern" then return value.
309 if (type == process_type::pattern) {
310 return std::string(value);
311 }
312 // Return the result of running canonicalize a hostname given value.
313 return url_pattern_helpers::canonicalize_hostname(value);
314}
315
316tl::expected<std::string, errors> url_pattern_init::process_port(
317 std::string_view port, std::string_view protocol, process_type type) {
318 // If type is "pattern" then return portValue.
319 if (type == process_type::pattern) {
320 return std::string(port);
321 }
322 // Return the result of running canonicalize a port given portValue and
323 // protocolValue.
324 return url_pattern_helpers::canonicalize_port_with_protocol(port, protocol);
325}
326
327tl::expected<std::string, errors> url_pattern_init::process_pathname(
328 std::string_view value, std::string_view protocol, process_type type) {
329 // If type is "pattern" then return pathnameValue.
330 if (type == process_type::pattern) {
331 return std::string(value);
332 }
333
334 // If protocolValue is a special scheme or the empty string, then return the
335 // result of running canonicalize a pathname given pathnameValue.
336 if (protocol.empty() || scheme::is_special(protocol)) {
337 return url_pattern_helpers::canonicalize_pathname(value);
338 }
339
340 // Return the result of running canonicalize an opaque pathname given
341 // pathnameValue.
342 return url_pattern_helpers::canonicalize_opaque_pathname(value);
343}
344
345tl::expected<std::string, errors> url_pattern_init::process_search(
346 std::string_view value, process_type type) {
347 // Let strippedValue be the given value with a single leading U+003F (?)
348 // removed, if any.
349 if (value.starts_with("?")) {
350 value.remove_prefix(1);
351 }
352 // We cannot assert that the value is no longer starting with a single
353 // question mark because technically it can start. The question is whether or
354 // not we should remove the first question mark. Ref:
355 // https://github.com/ada-url/ada/pull/992 The spec is not clear on this.
356
357 // If type is "pattern" then return strippedValue.
358 if (type == process_type::pattern) {
359 return std::string(value);
360 }
361 // Return the result of running canonicalize a search given strippedValue.
362 return url_pattern_helpers::canonicalize_search(value);
363}
364
365tl::expected<std::string, errors> url_pattern_init::process_hash(
366 std::string_view value, process_type type) {
367 // Let strippedValue be the given value with a single leading U+0023 (#)
368 // removed, if any.
369 if (value.starts_with("#")) {
370 value.remove_prefix(1);
371 }
372 ADA_ASSERT_TRUE(!value.starts_with("#"));
373 // If type is "pattern" then return strippedValue.
374 if (type == process_type::pattern) {
375 return std::string(value);
376 }
377 // Return the result of running canonicalize a hash given strippedValue.
378 return url_pattern_helpers::canonicalize_hash(value);
379}
380
381} // namespace ada
382
383#endif // ADA_INCLUDE_URL_PATTERN
#define ADA_ASSERT_TRUE(COND)
@ type_error
Definition errors.h:18
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
tl::expected< result_type, ada::errors > result
Declaration for the URLPattern inline functions.