1#if ADA_INCLUDE_URL_PATTERN
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) {
24 auto result = url_pattern_init{};
27 if (protocol.has_value())
result.protocol = *protocol;
30 if (username.has_value())
result.username = *username;
33 if (password.has_value())
result.password = *password;
36 if (hostname.has_value())
result.hostname = *hostname;
39 if (port.has_value())
result.port = *port;
42 if (pathname.has_value())
result.pathname = *pathname;
45 if (search.has_value())
result.search = *search;
48 if (hash.has_value())
result.hash = *hash;
51 std::optional<url_aggregator> base_url{};
54 if (init.base_url.has_value()) {
58 if (!parsing_result) {
61 base_url = std::move(*parsing_result);
65 if (!init.protocol.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);
70 url_pattern_helpers::process_base_url_string(base_url_protocol, 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);
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);
94 if (!init.protocol && !init.hostname) {
97 auto base_host = base_url->get_hostname();
101 url_pattern_helpers::process_base_url_string(base_host, type);
105 if (!init.protocol && !init.hostname && !init.port) {
108 result.port = base_url->get_port();
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);
121 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
125 std::string_view base_query = base_url->get_search();
126 if (base_query.starts_with(
"?")) base_query.remove_prefix(1);
130 url_pattern_helpers::process_base_url_string(base_query, type);
135 if (!init.protocol && !init.hostname && !init.port && !init.pathname &&
136 !init.search && !init.hash) {
139 std::string_view base_fragment = base_url->get_hash();
140 if (base_fragment.starts_with(
"#")) base_fragment.remove_prefix(1);
144 url_pattern_helpers::process_base_url_string(base_fragment, type);
151 auto process_result = process_protocol(*init.protocol, type);
152 if (!process_result) {
153 return tl::unexpected(process_result.error());
155 result.protocol = std::move(*process_result);
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());
165 result.username = std::move(*process_result);
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());
175 result.password = std::move(*process_result);
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());
185 result.hostname = std::move(*process_result);
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());
196 result.port = std::move(*process_result);
200 if (init.pathname.has_value()) {
202 result.pathname = init.pathname;
209 if (base_url && !base_url->has_opaque_path &&
210 !url_pattern_helpers::is_absolute_pathname(*
result.pathname, type)) {
215 std::string base_url_path = url_pattern_helpers::process_base_url_string(
216 base_url->get_pathname(), type);
221 auto slash_index = base_url_path.find_last_of(
'/');
224 if (slash_index != std::string::npos) {
227 base_url_path.resize(slash_index + 1);
230 base_url_path.append(std::move(*
result.pathname));
232 result.pathname = std::move(base_url_path);
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());
243 result.pathname = std::move(*pathname_processing_result);
249 auto process_result = process_search(*init.search, type);
250 if (!process_result) {
251 return tl::unexpected(process_result.error());
253 result.search = std::move(*process_result);
259 auto process_result = process_hash(*init.hash, type);
260 if (!process_result) {
261 return tl::unexpected(process_result.error());
263 result.hash = std::move(*process_result);
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,
"]");
274 if (value.ends_with(
":")) {
275 value.remove_suffix(1);
278 if (type == process_type::pattern) {
279 return std::string(value);
282 return url_pattern_helpers::canonicalize_protocol(value);
285tl::expected<std::string, errors> url_pattern_init::process_username(
286 std::string_view value, process_type type) {
288 if (type == process_type::pattern) {
289 return std::string(value);
292 return url_pattern_helpers::canonicalize_username(value);
295tl::expected<std::string, errors> url_pattern_init::process_password(
296 std::string_view value, process_type type) {
298 if (type == process_type::pattern) {
299 return std::string(value);
302 return url_pattern_helpers::canonicalize_password(value);
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);
309 if (type == process_type::pattern) {
310 return std::string(value);
313 return url_pattern_helpers::canonicalize_hostname(value);
316tl::expected<std::string, errors> url_pattern_init::process_port(
317 std::string_view port, std::string_view protocol, process_type type) {
319 if (type == process_type::pattern) {
320 return std::string(port);
324 return url_pattern_helpers::canonicalize_port_with_protocol(port, protocol);
327tl::expected<std::string, errors> url_pattern_init::process_pathname(
328 std::string_view value, std::string_view protocol, process_type type) {
330 if (type == process_type::pattern) {
331 return std::string(value);
336 if (protocol.empty() || scheme::is_special(protocol)) {
337 return url_pattern_helpers::canonicalize_pathname(value);
342 return url_pattern_helpers::canonicalize_opaque_pathname(value);
345tl::expected<std::string, errors> url_pattern_init::process_search(
346 std::string_view value, process_type type) {
349 if (value.starts_with(
"?")) {
350 value.remove_prefix(1);
358 if (type == process_type::pattern) {
359 return std::string(value);
362 return url_pattern_helpers::canonicalize_search(value);
365tl::expected<std::string, errors> url_pattern_init::process_hash(
366 std::string_view value, process_type type) {
369 if (value.starts_with(
"#")) {
370 value.remove_prefix(1);
374 if (type == process_type::pattern) {
375 return std::string(value);
378 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.