Ada 3.2.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#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 helpers::trim_c0_whitespace(port);
100 if (std::to_string(scheme::get_special_port(*processed_init->protocol)) ==
101 port) {
102 processed_init->port->clear();
103 }
104 }
105
106 // Let urlPattern be a new URL pattern.
107 url_pattern<regex_provider> url_pattern_{};
108
109 // Set urlPattern's protocol component to the result of compiling a component
110 // given processedInit["protocol"], canonicalize a protocol, and default
111 // options.
112 auto protocol_component = url_pattern_component<regex_provider>::compile(
113 processed_init->protocol.value(),
116 if (!protocol_component) {
117 ada_log("url_pattern_component::compile failed for protocol ",
118 processed_init->protocol.value());
119 return tl::unexpected(protocol_component.error());
120 }
121 url_pattern_.protocol_component = std::move(*protocol_component);
122
123 // Set urlPattern's username component to the result of compiling a component
124 // given processedInit["username"], canonicalize a username, and default
125 // options.
126 auto username_component = url_pattern_component<regex_provider>::compile(
127 processed_init->username.value(),
130 if (!username_component) {
131 ada_log("url_pattern_component::compile failed for username ",
132 processed_init->username.value());
133 return tl::unexpected(username_component.error());
134 }
135 url_pattern_.username_component = std::move(*username_component);
136
137 // Set urlPattern's password component to the result of compiling a component
138 // given processedInit["password"], canonicalize a password, and default
139 // options.
140 auto password_component = url_pattern_component<regex_provider>::compile(
141 processed_init->password.value(),
144 if (!password_component) {
145 ada_log("url_pattern_component::compile failed for password ",
146 processed_init->password.value());
147 return tl::unexpected(password_component.error());
148 }
149 url_pattern_.password_component = std::move(*password_component);
150
151 // TODO: Optimization opportunity. The following if statement can be
152 // simplified.
153 // If the result running hostname pattern is an IPv6 address given
154 // processedInit["hostname"] is true, then set urlPattern's hostname component
155 // to the result of compiling a component given processedInit["hostname"],
156 // canonicalize an IPv6 hostname, and hostname options.
157 if (url_pattern_helpers::is_ipv6_address(processed_init->hostname.value())) {
158 ada_log("processed_init->hostname is ipv6 address");
159 // then set urlPattern's hostname component to the result of compiling a
160 // component given processedInit["hostname"], canonicalize an IPv6 hostname,
161 // and hostname options.
162 auto hostname_component = url_pattern_component<regex_provider>::compile(
163 processed_init->hostname.value(),
166 if (!hostname_component) {
167 ada_log("url_pattern_component::compile failed for ipv6 hostname ",
168 processed_init->hostname.value());
169 return tl::unexpected(hostname_component.error());
170 }
171 url_pattern_.hostname_component = std::move(*hostname_component);
172 } else {
173 // Otherwise, set urlPattern's hostname component to the result of compiling
174 // a component given processedInit["hostname"], canonicalize a hostname, and
175 // hostname options.
176 auto hostname_component = url_pattern_component<regex_provider>::compile(
177 processed_init->hostname.value(),
180 if (!hostname_component) {
181 ada_log("url_pattern_component::compile failed for hostname ",
182 processed_init->hostname.value());
183 return tl::unexpected(hostname_component.error());
184 }
185 url_pattern_.hostname_component = std::move(*hostname_component);
186 }
187
188 // Set urlPattern's port component to the result of compiling a component
189 // given processedInit["port"], canonicalize a port, and default options.
191 processed_init->port.value(), url_pattern_helpers::canonicalize_port,
193 if (!port_component) {
194 ada_log("url_pattern_component::compile failed for port ",
195 processed_init->port.value());
196 return tl::unexpected(port_component.error());
197 }
198 url_pattern_.port_component = std::move(*port_component);
199
200 // Let compileOptions be a copy of the default options with the ignore case
201 // property set to options["ignoreCase"].
203 if (options) {
204 compile_options.ignore_case = options->ignore_case;
205 }
206
207 // TODO: Optimization opportunity: Simplify this if statement.
208 // If the result of running protocol component matches a special scheme given
209 // urlPattern's protocol component is true, then:
211 regex_provider>(url_pattern_.protocol_component)) {
212 // Let pathCompileOptions be copy of the pathname options with the ignore
213 // case property set to options["ignoreCase"].
214 auto path_compile_options = url_pattern_compile_component_options::PATHNAME;
215 if (options) {
216 path_compile_options.ignore_case = options->ignore_case;
217 }
218
219 // Set urlPattern's pathname component to the result of compiling a
220 // component given processedInit["pathname"], canonicalize a pathname, and
221 // pathCompileOptions.
222 auto pathname_component = url_pattern_component<regex_provider>::compile(
223 processed_init->pathname.value(),
224 url_pattern_helpers::canonicalize_pathname, path_compile_options);
225 if (!pathname_component) {
226 ada_log("url_pattern_component::compile failed for pathname ",
227 processed_init->pathname.value());
228 return tl::unexpected(pathname_component.error());
229 }
230 url_pattern_.pathname_component = std::move(*pathname_component);
231 } else {
232 // Otherwise set urlPattern's pathname component to the result of compiling
233 // a component given processedInit["pathname"], canonicalize an opaque
234 // pathname, and compileOptions.
235 auto pathname_component = url_pattern_component<regex_provider>::compile(
236 processed_init->pathname.value(),
238 if (!pathname_component) {
239 ada_log("url_pattern_component::compile failed for opaque pathname ",
240 processed_init->pathname.value());
241 return tl::unexpected(pathname_component.error());
242 }
243 url_pattern_.pathname_component = std::move(*pathname_component);
244 }
245
246 // Set urlPattern's search component to the result of compiling a component
247 // given processedInit["search"], canonicalize a search, and compileOptions.
249 processed_init->search.value(), url_pattern_helpers::canonicalize_search,
250 compile_options);
251 if (!search_component) {
252 ada_log("url_pattern_component::compile failed for search ",
253 processed_init->search.value());
254 return tl::unexpected(search_component.error());
255 }
256 url_pattern_.search_component = std::move(*search_component);
257
258 // Set urlPattern's hash component to the result of compiling a component
259 // given processedInit["hash"], canonicalize a hash, and compileOptions.
261 processed_init->hash.value(), url_pattern_helpers::canonicalize_hash,
262 compile_options);
263 if (!hash_component) {
264 ada_log("url_pattern_component::compile failed for hash ",
265 processed_init->hash.value());
266 return tl::unexpected(hash_component.error());
267 }
268 url_pattern_.hash_component = std::move(*hash_component);
269
270 // Return urlPattern.
271 return url_pattern_;
272}
273#endif // ADA_INCLUDE_URL_PATTERN
274
275} // namespace ada::parser
276
277#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.