1#if ADA_INCLUDE_URL_PATTERN
11tl::expected<url_pattern_init, errors> url_pattern_init::process(
12 const url_pattern_init& init, url_pattern_init::process_type type,
13 std::optional<std::string_view> protocol,
14 std::optional<std::string_view> username,
15 std::optional<std::string_view> password,
16 std::optional<std::string_view> hostname,
17 std::optional<std::string_view> port,
18 std::optional<std::string_view> pathname,
19 std::optional<std::string_view> search,
20 std::optional<std::string_view> hash) {
22 auto result = url_pattern_init{};
25 if (protocol.has_value())
result.protocol = *protocol;
28 if (username.has_value())
result.username = *username;
31 if (password.has_value())
result.password = *password;
34 if (hostname.has_value())
result.hostname = *hostname;
37 if (port.has_value())
result.port = *port;
40 if (pathname.has_value())
result.pathname = *pathname;
43 if (search.has_value())
result.search = *search;
46 if (hash.has_value())
result.hash = *hash;
49 std::optional<url_aggregator> base_url{};
52 if (init.base_url.has_value()) {
56 if (!parsing_result) {
59 base_url = std::move(*parsing_result);
63 if (!init.protocol.has_value()) {
65 std::string_view base_url_protocol = base_url->get_protocol();
66 if (base_url_protocol.ends_with(
":")) base_url_protocol.remove_suffix(1);
68 url_pattern_helpers::process_base_url_string(base_url_protocol, type);
74 if (type != process_type::pattern && !init.protocol && !init.hostname &&
75 !init.port && !init.username) {
76 result.username = url_pattern_helpers::process_base_url_string(
77 base_url->get_username(), type);
85 if (type != process_type::pattern && !init.protocol && !init.hostname &&
86 !init.port && !init.username && !init.password) {
87 result.password = url_pattern_helpers::process_base_url_string(
88 base_url->get_password(), type);
92 if (!init.protocol && !init.hostname) {
95 auto base_host = base_url->get_hostname();
99 url_pattern_helpers::process_base_url_string(base_host, type);
103 if (!init.protocol && !init.hostname && !init.port) {
106 result.port = base_url->get_port();
112 if (!init.protocol && !init.hostname && !init.port && !init.pathname) {
113 result.pathname = url_pattern_helpers::process_base_url_string(
114 base_url->get_pathname(), type);
119 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
124 result.search = url_pattern_helpers::process_base_url_string(
125 base_url->get_search(), type);
130 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
131 !init.search && !init.hash) {
135 result.hash = url_pattern_helpers::process_base_url_string(
136 base_url->get_hash(), type);
143 auto process_result = process_protocol(*init.protocol, type);
144 if (!process_result) {
145 return tl::unexpected(process_result.error());
147 result.protocol = std::move(*process_result);
152 if (init.username.has_value()) {
153 auto process_result = process_username(*init.username, type);
154 if (!process_result) {
155 return tl::unexpected(process_result.error());
157 result.username = std::move(*process_result);
162 if (init.password.has_value()) {
163 auto process_result = process_password(*init.password, type);
164 if (!process_result) {
165 return tl::unexpected(process_result.error());
167 result.password = std::move(*process_result);
172 if (init.hostname.has_value()) {
173 auto process_result = process_hostname(*init.hostname, type);
174 if (!process_result) {
175 return tl::unexpected(process_result.error());
177 result.hostname = std::move(*process_result);
183 auto process_result =
184 process_port(*init.port,
result.protocol.value_or(
"fake"), type);
185 if (!process_result) {
186 return tl::unexpected(process_result.error());
188 result.port = std::move(*process_result);
192 if (init.pathname.has_value()) {
194 result.pathname = init.pathname;
201 if (base_url && !base_url->has_opaque_path &&
202 !url_pattern_helpers::is_absolute_pathname(*
result.pathname, type)) {
207 std::string base_url_path = url_pattern_helpers::process_base_url_string(
208 base_url->get_pathname(), type);
213 auto slash_index = base_url_path.find_last_of(
'/');
216 if (slash_index != std::string::npos) {
219 base_url_path.resize(slash_index + 1);
222 base_url_path.append(std::move(*
result.pathname));
224 result.pathname = std::move(base_url_path);
230 auto pathname_processing_result =
231 process_pathname(*
result.pathname,
result.protocol.value_or(
""), type);
232 if (!pathname_processing_result) {
233 return tl::unexpected(pathname_processing_result.error());
235 result.pathname = std::move(*pathname_processing_result);
241 auto process_result = process_search(*init.search, type);
242 if (!process_result) {
243 return tl::unexpected(process_result.error());
245 result.search = std::move(*process_result);
251 auto process_result = process_hash(*init.hash, type);
252 if (!process_result) {
253 return tl::unexpected(process_result.error());
255 result.hash = std::move(*process_result);
261tl::expected<std::string, errors> url_pattern_init::process_protocol(
262 std::string_view value, process_type type) {
263 ada_log(
"process_protocol=", value,
" [", type,
"]");
266 if (value.ends_with(
":")) {
267 value.remove_suffix(1);
270 if (type == process_type::pattern) {
271 return std::string(value);
274 return url_pattern_helpers::canonicalize_protocol(value);
277tl::expected<std::string, errors> url_pattern_init::process_username(
278 std::string_view value, process_type type) {
280 if (type == process_type::pattern) {
281 return std::string(value);
284 return url_pattern_helpers::canonicalize_username(value);
287tl::expected<std::string, errors> url_pattern_init::process_password(
288 std::string_view value, process_type type) {
290 if (type == process_type::pattern) {
291 return std::string(value);
294 return url_pattern_helpers::canonicalize_password(value);
297tl::expected<std::string, errors> url_pattern_init::process_hostname(
298 std::string_view value, process_type type) {
299 ada_log(
"process_hostname value=", value,
" type=", type);
301 if (type == process_type::pattern) {
302 return std::string(value);
305 return url_pattern_helpers::canonicalize_hostname(value);
308tl::expected<std::string, errors> url_pattern_init::process_port(
309 std::string_view port, std::string_view protocol, process_type type) {
311 if (type == process_type::pattern) {
312 return std::string(port);
316 return url_pattern_helpers::canonicalize_port_with_protocol(port, protocol);
319tl::expected<std::string, errors> url_pattern_init::process_pathname(
320 std::string_view value, std::string_view protocol, process_type type) {
322 if (type == process_type::pattern) {
323 return std::string(value);
328 if (protocol.empty() || scheme::is_special(protocol)) {
329 return url_pattern_helpers::canonicalize_pathname(value);
334 return url_pattern_helpers::canonicalize_opaque_pathname(value);
337tl::expected<std::string, errors> url_pattern_init::process_search(
338 std::string_view value, process_type type) {
341 if (value.starts_with(
"?")) {
342 value.remove_prefix(1);
350 if (type == process_type::pattern) {
351 return std::string(value);
354 return url_pattern_helpers::canonicalize_search(value);
357tl::expected<std::string, errors> url_pattern_init::process_hash(
358 std::string_view value, process_type type) {
361 if (value.starts_with(
"#")) {
362 value.remove_prefix(1);
366 if (type == process_type::pattern) {
367 return std::string(value);
370 return url_pattern_helpers::canonicalize_hash(value);
#define ADA_ASSERT_TRUE(COND)
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.