Ada 3.2.2
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#if ADA_INCLUDE_URL_PATTERN
9#include "ada/url_pattern.h"
11#endif // ADA_INCLUDE_URL_PATTERN
12#include "ada/parser.h"
13
14#include <string>
15#include <string_view>
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.
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 =
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 =
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 std::string_view port = processed_init->port.value();
99 if (std::to_string(scheme::get_special_port(*processed_init->protocol)) ==
100 port) {
101 processed_init->port->clear();
102 }
103 }
104
105 // Let urlPattern be a new URL pattern.
106 url_pattern<regex_provider> url_pattern_{};
107
108 // Set urlPattern's protocol component to the result of compiling a component
109 // given processedInit["protocol"], canonicalize a protocol, and default
110 // options.
111 auto protocol_component = url_pattern_component<regex_provider>::compile(
112 processed_init->protocol.value(),
115 if (!protocol_component) {
116 ada_log("url_pattern_component::compile failed for protocol ",
117 processed_init->protocol.value());
118 return tl::unexpected(protocol_component.error());
119 }
120 url_pattern_.protocol_component = std::move(*protocol_component);
121
122 // Set urlPattern's username component to the result of compiling a component
123 // given processedInit["username"], canonicalize a username, and default
124 // options.
125 auto username_component = url_pattern_component<regex_provider>::compile(
126 processed_init->username.value(),
129 if (!username_component) {
130 ada_log("url_pattern_component::compile failed for username ",
131 processed_init->username.value());
132 return tl::unexpected(username_component.error());
133 }
134 url_pattern_.username_component = std::move(*username_component);
135
136 // Set urlPattern's password component to the result of compiling a component
137 // given processedInit["password"], canonicalize a password, and default
138 // options.
139 auto password_component = url_pattern_component<regex_provider>::compile(
140 processed_init->password.value(),
143 if (!password_component) {
144 ada_log("url_pattern_component::compile failed for password ",
145 processed_init->password.value());
146 return tl::unexpected(password_component.error());
147 }
148 url_pattern_.password_component = std::move(*password_component);
149
150 // TODO: Optimization opportunity. The following if statement can be
151 // simplified.
152 // If the result running hostname pattern is an IPv6 address given
153 // processedInit["hostname"] is true, then set urlPattern's hostname component
154 // to the result of compiling a component given processedInit["hostname"],
155 // canonicalize an IPv6 hostname, and hostname options.
156 if (url_pattern_helpers::is_ipv6_address(processed_init->hostname.value())) {
157 ada_log("processed_init->hostname is ipv6 address");
158 // then set urlPattern's hostname component to the result of compiling a
159 // component given processedInit["hostname"], canonicalize an IPv6 hostname,
160 // and hostname options.
161 auto hostname_component = url_pattern_component<regex_provider>::compile(
162 processed_init->hostname.value(),
165 if (!hostname_component) {
166 ada_log("url_pattern_component::compile failed for ipv6 hostname ",
167 processed_init->hostname.value());
168 return tl::unexpected(hostname_component.error());
169 }
170 url_pattern_.hostname_component = std::move(*hostname_component);
171 } else {
172 // Otherwise, set urlPattern's hostname component to the result of compiling
173 // a component given processedInit["hostname"], canonicalize a hostname, and
174 // hostname options.
175 auto hostname_component = url_pattern_component<regex_provider>::compile(
176 processed_init->hostname.value(),
179 if (!hostname_component) {
180 ada_log("url_pattern_component::compile failed for hostname ",
181 processed_init->hostname.value());
182 return tl::unexpected(hostname_component.error());
183 }
184 url_pattern_.hostname_component = std::move(*hostname_component);
185 }
186
187 // Set urlPattern's port component to the result of compiling a component
188 // given processedInit["port"], canonicalize a port, and default options.
190 processed_init->port.value(), url_pattern_helpers::canonicalize_port,
192 if (!port_component) {
193 ada_log("url_pattern_component::compile failed for port ",
194 processed_init->port.value());
195 return tl::unexpected(port_component.error());
196 }
197 url_pattern_.port_component = std::move(*port_component);
198
199 // Let compileOptions be a copy of the default options with the ignore case
200 // property set to options["ignoreCase"].
202 if (options) {
203 compile_options.ignore_case = options->ignore_case;
204 }
205
206 // TODO: Optimization opportunity: Simplify this if statement.
207 // If the result of running protocol component matches a special scheme given
208 // urlPattern's protocol component is true, then:
210 regex_provider>(url_pattern_.protocol_component)) {
211 // Let pathCompileOptions be copy of the pathname options with the ignore
212 // case property set to options["ignoreCase"].
213 auto path_compile_options = url_pattern_compile_component_options::PATHNAME;
214 if (options) {
215 path_compile_options.ignore_case = options->ignore_case;
216 }
217
218 // Set urlPattern's pathname component to the result of compiling a
219 // component given processedInit["pathname"], canonicalize a pathname, and
220 // pathCompileOptions.
221 auto pathname_component = url_pattern_component<regex_provider>::compile(
222 processed_init->pathname.value(),
223 url_pattern_helpers::canonicalize_pathname, path_compile_options);
224 if (!pathname_component) {
225 ada_log("url_pattern_component::compile failed for pathname ",
226 processed_init->pathname.value());
227 return tl::unexpected(pathname_component.error());
228 }
229 url_pattern_.pathname_component = std::move(*pathname_component);
230 } else {
231 // Otherwise set urlPattern's pathname component to the result of compiling
232 // a component given processedInit["pathname"], canonicalize an opaque
233 // pathname, and compileOptions.
234 auto pathname_component = url_pattern_component<regex_provider>::compile(
235 processed_init->pathname.value(),
237 if (!pathname_component) {
238 ada_log("url_pattern_component::compile failed for opaque pathname ",
239 processed_init->pathname.value());
240 return tl::unexpected(pathname_component.error());
241 }
242 url_pattern_.pathname_component = std::move(*pathname_component);
243 }
244
245 // Set urlPattern's search component to the result of compiling a component
246 // given processedInit["search"], canonicalize a search, and compileOptions.
248 processed_init->search.value(), url_pattern_helpers::canonicalize_search,
249 compile_options);
250 if (!search_component) {
251 ada_log("url_pattern_component::compile failed for search ",
252 processed_init->search.value());
253 return tl::unexpected(search_component.error());
254 }
255 url_pattern_.search_component = std::move(*search_component);
256
257 // Set urlPattern's hash component to the result of compiling a component
258 // given processedInit["hash"], canonicalize a hash, and compileOptions.
260 processed_init->hash.value(), url_pattern_helpers::canonicalize_hash,
261 compile_options);
262 if (!hash_component) {
263 ada_log("url_pattern_component::compile failed for hash ",
264 processed_init->hash.value());
265 return tl::unexpected(hash_component.error());
266 }
267 url_pattern_.hash_component = std::move(*hash_component);
268
269 // Return urlPattern.
270 return url_pattern_;
271}
272#endif // ADA_INCLUDE_URL_PATTERN
273
274} // namespace ada::parser
275
276#endif // ADA_PARSER_INL_H
static tl::expected< url_pattern_component, errors > compile(std::string_view input, F &encoding_callback, url_pattern_compile_component_options &options)
#define ADA_ASSERT_TRUE(COND)
Includes the definitions for supported parsers.
Definition parser-inl.h:18
constexpr uint16_t get_special_port(std::string_view scheme) noexcept
Definition scheme-inl.h:57
tl::expected< std::string, errors > canonicalize_opaque_pathname(std::string_view input)
tl::expected< std::string, errors > canonicalize_pathname(std::string_view input)
bool protocol_component_matches_special_scheme(url_pattern_component< regex_provider > &component)
tl::expected< std::string, errors > canonicalize_password(std::string_view input)
tl::expected< std::string, errors > canonicalize_protocol(std::string_view input)
tl::expected< std::string, errors > canonicalize_hostname(std::string_view input)
tl::expected< std::string, errors > canonicalize_hash(std::string_view input)
tl::expected< std::string, errors > canonicalize_port(std::string_view input)
bool is_ipv6_address(std::string_view input) noexcept
tl::expected< std::string, errors > canonicalize_search(std::string_view input)
tl::expected< std::string, errors > canonicalize_ipv6_hostname(std::string_view input)
tl::expected< std::string, errors > canonicalize_username(std::string_view input)
errors
Definition errors.h:10
@ type_error
Definition errors.h:10
Definitions for the parser.
static url_pattern_compile_component_options HOSTNAME
static url_pattern_compile_component_options PATHNAME
static url_pattern_compile_component_options DEFAULT
static tl::expected< url_pattern_init, errors > parse(std::string_view input)
std::optional< std::string > protocol
std::optional< std::string > base_url
static tl::expected< url_pattern_init, errors > process(const url_pattern_init &init, process_type type, std::optional< std::string_view > protocol=std::nullopt, std::optional< std::string_view > username=std::nullopt, std::optional< std::string_view > password=std::nullopt, std::optional< std::string_view > hostname=std::nullopt, std::optional< std::string_view > port=std::nullopt, std::optional< std::string_view > pathname=std::nullopt, std::optional< std::string_view > search=std::nullopt, std::optional< std::string_view > hash=std::nullopt)
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9
Declaration for the URLPattern implementation.
Declaration for the URLPattern helpers.