Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_INL_H
6#define ADA_URL_INL_H
7
8#include "ada/url.h"
10
11#include <charconv>
12#include <cstdint>
13#include <cstring>
14#include <optional>
15#include <ostream>
16#include <string>
17#include <string_view>
18#include <system_error>
19#include <utility>
20#if ADA_REGULAR_VISUAL_STUDIO
21#include <intrin.h>
22#endif // ADA_REGULAR_VISUAL_STUDIO
23
24namespace ada {
25[[nodiscard]] ada_really_inline bool url::has_credentials() const noexcept {
26 return !username.empty() || !password.empty();
27}
28[[nodiscard]] ada_really_inline bool url::has_port() const noexcept {
29 return port.has_value();
30}
31[[nodiscard]] inline bool url::cannot_have_credentials_or_port() const {
32 return !host.has_value() || host->empty() || type == ada::scheme::type::FILE;
33}
34[[nodiscard]] inline bool url::has_empty_hostname() const noexcept {
35 if (!host.has_value()) {
36 return false;
37 }
38 return host->empty();
39}
40[[nodiscard]] inline bool url::has_hostname() const noexcept {
41 return host.has_value();
42}
43inline std::ostream& operator<<(std::ostream& out, const ada::url& u) {
44 return out << u.to_string();
45}
46
47[[nodiscard]] size_t url::get_pathname_length() const noexcept {
48 return path.size();
49}
50
51[[nodiscard]] constexpr std::string_view url::get_pathname() const noexcept {
52 return path;
53}
54
56 const {
57 url_components out{};
58
59 // protocol ends with ':'. for example: "https:"
60 out.protocol_end = uint32_t(get_protocol().size());
61
62 // Trailing index is always the next character of the current one.
63 // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
64 size_t running_index = out.protocol_end;
65
66 if (host.has_value()) {
67 // 2 characters for "//" and 1 character for starting index
68 out.host_start = out.protocol_end + 2;
69
70 if (has_credentials()) {
71 out.username_end = uint32_t(out.host_start + username.size());
72
73 out.host_start += uint32_t(username.size());
74
75 if (!password.empty()) {
76 out.host_start += uint32_t(password.size() + 1);
77 }
78
79 // host_start is the '@' when there are credentials, exactly as
80 // url_aggregator reports it, so the host itself begins one byte later.
81 out.host_end = uint32_t(out.host_start + 1 + host->size());
82 } else {
83 out.username_end = out.host_start;
84
85 // Host does not start with "@" if it does not include credentials.
86 out.host_end = uint32_t(out.host_start + host->size());
87 }
88
89 // host_end is one past the host, so it is already the next index.
90 running_index = out.host_end;
91 } else {
92 // Update host start and end date to the same index, since it does not
93 // exist.
94 out.host_start = out.protocol_end;
95 out.host_end = out.host_start;
96
97 if (!has_opaque_path && path.starts_with("//")) {
98 // If url's host is null, url does not have an opaque path, url's path's
99 // size is greater than 1, and url's path[0] is the empty string, then
100 // append U+002F (/) followed by U+002E (.) to output.
101 running_index = out.protocol_end + 2;
102 } else {
103 running_index = out.protocol_end;
104 }
105 }
106
107 if (port.has_value()) {
108 out.port = *port;
109 running_index += helpers::fast_digit_count(*port) + 1; // Port omits ':'
110 }
111
112 out.pathname_start = uint32_t(running_index);
113
114 running_index += path.size();
115
116 if (query.has_value()) {
117 out.search_start = uint32_t(running_index);
118 running_index += get_search().size();
119 if (get_search().empty()) {
120 running_index++;
121 }
122 }
123
124 if (hash.has_value()) {
125 out.hash_start = uint32_t(running_index);
126 }
127
128 return out;
129}
130
131inline void url::update_base_hostname(std::string_view input) { host = input; }
132
133inline void url::update_unencoded_base_hash(std::string_view input) {
134 // We do the percent encoding
135 hash = unicode::percent_encode(input,
137}
138
139inline void url::update_base_search(std::string_view input,
140 const uint8_t query_percent_encode_set[]) {
141 query = ada::unicode::percent_encode(input, query_percent_encode_set);
142}
143
144inline void url::update_base_search(std::optional<std::string>&& input) {
145 query = std::move(input);
146}
147
148inline void url::update_base_pathname(const std::string_view input) {
149 path = input;
150}
151
152inline void url::update_base_username(const std::string_view input) {
153 username = input;
154}
155
156inline void url::update_base_password(const std::string_view input) {
157 password = input;
158}
159
160inline void url::update_base_port(std::optional<uint16_t> input) {
161 port = input;
162}
163
164constexpr void url::clear_pathname() { path.clear(); }
165
166constexpr void url::clear_search() { query = std::nullopt; }
167
168[[nodiscard]] constexpr bool url::has_hash() const noexcept {
169 return hash.has_value();
170}
171
172[[nodiscard]] constexpr bool url::has_search() const noexcept {
173 return query.has_value();
174}
175
176constexpr void url::set_protocol_as_file() { type = ada::scheme::type::FILE; }
177
178inline void url::set_scheme(std::string&& new_scheme) noexcept {
179 type = ada::scheme::get_scheme_type(new_scheme);
180 // We only move the 'scheme' if it is non-special.
181 if (!is_special()) {
182 non_special_scheme = std::move(new_scheme);
183 }
184}
185
186constexpr void url::copy_scheme(ada::url&& u) {
187 non_special_scheme = u.non_special_scheme;
188 type = u.type;
189}
190
191constexpr void url::copy_scheme(const ada::url& u) {
192 non_special_scheme = u.non_special_scheme;
193 type = u.type;
194}
195
196[[nodiscard]] ada_really_inline std::string url::get_href() const {
197 if (is_special() && host.has_value() && username.empty() &&
198 password.empty() && !port.has_value()) [[likely]] {
199 const std::string_view scheme = ada::scheme::details::is_special_list[type];
200 const size_t host_size = host->size();
201 const size_t path_size = path.size();
202 const size_t query_size = query.has_value() ? query->size() : 0;
203 const size_t hash_size = hash.has_value() ? hash->size() : 0;
204 const size_t total = scheme.size() + 3 + host_size + path_size +
205 (query.has_value() ? query_size + 1 : 0) +
206 (hash.has_value() ? hash_size + 1 : 0);
207 std::string output(total, '\0');
208 char* p = output.data();
209 std::memcpy(p, scheme.data(), scheme.size());
210 p += scheme.size();
211 p[0] = ':';
212 p[1] = '/';
213 p[2] = '/';
214 p += 3;
215 // NOLINTNEXTLINE(bugprone-not-null-terminated-result)
216 std::memcpy(p, host->data(), host_size);
217 p += host_size;
218 std::memcpy(p, path.data(), path_size);
219 p += path_size;
220 if (query.has_value()) {
221 *p++ = '?';
222 // NOLINTNEXTLINE(bugprone-not-null-terminated-result)
223 std::memcpy(p, query->data(), query_size);
224 p += query_size;
225 }
226 if (hash.has_value()) {
227 *p++ = '#';
228 // NOLINTNEXTLINE(bugprone-not-null-terminated-result)
229 std::memcpy(p, hash->data(), hash_size);
230 }
231 return output;
232 }
233
234 std::string output;
235 output.reserve(get_href_size());
236
237 if (is_special()) {
238 output.append(ada::scheme::details::is_special_list[type]);
239 output += ':';
240 } else {
241 output.append(non_special_scheme);
242 output += ':';
243 }
244
245 if (host.has_value()) {
246 output += '/';
247 output += '/';
248 if (has_credentials()) {
249 output.append(username);
250 if (!password.empty()) {
251 output += ':';
252 output.append(password);
253 }
254 output += '@';
255 }
256 output.append(*host);
257 if (port.has_value()) {
258 output += ':';
259 char port_buf[5];
260 auto [ptr, ec] = std::to_chars(port_buf, port_buf + 5, *port);
261 (void)ec;
262 output.append(port_buf, static_cast<size_t>(ptr - port_buf));
263 }
264 } else if (!has_opaque_path && path.starts_with("//")) {
265 // If url's host is null, url does not have an opaque path, url's path's
266 // size is greater than 1, and url's path[0] is the empty string, then
267 // append U+002F (/) followed by U+002E (.) to output.
268 output += '/';
269 output += '.';
270 }
271 output.append(path);
272 if (query.has_value()) {
273 output += '?';
274 output.append(*query);
275 }
276 if (hash.has_value()) {
277 output += '#';
278 output.append(*hash);
279 }
280 return output;
281}
282
283[[nodiscard]] inline size_t url::get_href_size() const noexcept {
284 size_t size = 0;
285 if (is_special()) {
286 size += ada::scheme::details::is_special_list[type].size() + 1;
287 } else {
288 size += non_special_scheme.size() + 1;
289 }
290 if (host.has_value()) {
291 size += host->size();
292 size += 2;
293 if (has_credentials()) {
294 size += username.size();
295 if (!password.empty()) {
296 size += 1 + password.size();
297 }
298 size += 1;
299 }
300 if (port.has_value()) {
301 size += 1;
302 uint16_t p = *port;
303 size += (p >= 10000) ? 5
304 : (p >= 1000) ? 4
305 : (p >= 100) ? 3
306 : (p >= 10) ? 2
307 : 1;
308 }
309 } else if (!has_opaque_path && path.starts_with("//")) {
310 size += 2;
311 }
312 size += path.size();
313 if (query.has_value()) {
314 size += 1 + query->size();
315 }
316 if (hash.has_value()) {
317 size += 1 + hash->size();
318 }
319 return size;
320}
321
322ada_really_inline size_t url::parse_port(std::string_view view,
323 bool check_trailing_content) noexcept {
324 ada_log("parse_port('", view, "') ", view.size());
325 if (!view.empty() && view[0] == '-') {
326 ada_log("parse_port: view[0] == '0' && view.size() > 1");
327 is_valid = false;
328 return 0;
329 }
330 uint16_t parsed_port{};
331 auto r = std::from_chars(view.data(), view.data() + view.size(), parsed_port);
332 if (r.ec == std::errc::result_out_of_range) {
333 ada_log("parse_port: r.ec == std::errc::result_out_of_range");
334 is_valid = false;
335 return 0;
336 }
337 ada_log("parse_port: ", parsed_port);
338 const auto consumed = size_t(r.ptr - view.data());
339 ada_log("parse_port: consumed ", consumed);
340 if (check_trailing_content) {
341 is_valid &=
342 (consumed == view.size() || view[consumed] == '/' ||
343 view[consumed] == '?' || (is_special() && view[consumed] == '\\'));
344 }
345 ada_log("parse_port: is_valid = ", is_valid);
346 if (is_valid) {
347 // scheme_default_port can return 0, and we should allow 0 as a base port.
348 auto default_port = scheme_default_port();
349 bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
350 (default_port != parsed_port);
351 port = (r.ec == std::errc() && is_port_valid) ? std::optional(parsed_port)
352 : std::nullopt;
353 }
354 return consumed;
355}
356
357} // namespace ada
358
359#endif // ADA_URL_H
#define ada_really_inline
Definition common_defs.h:85
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
constexpr std::string_view is_special_list[]
Definition scheme-inl.h:23
URL scheme utilities and constants.
Definition scheme-inl.h:14
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
Definition scheme-inl.h:111
std::ostream & operator<<(std::ostream &out, const ada::url &u)
Definition url-inl.h:43
ada_really_inline constexpr bool is_special() const noexcept
bool has_opaque_path
Definition url_base.h:64
Stores byte offsets for URL components within a buffer.
Represents a parsed URL with individual string components.
Definition url.h:64
bool has_empty_hostname() const noexcept
Definition url-inl.h:34
bool has_port() const noexcept
Definition url-inl.h:28
ada_really_inline bool has_credentials() const noexcept
Definition url-inl.h:25
ada_really_inline size_t get_pathname_length() const noexcept
Definition url-inl.h:47
std::string get_search() const
Definition url.cpp:591
std::string get_protocol() const
Definition url.cpp:565
bool has_hostname() const noexcept
Definition url-inl.h:40
constexpr std::string_view get_pathname() const noexcept
Definition url-inl.h:51
size_t get_href_size() const noexcept
Definition url-inl.h:283
ada_really_inline std::string get_href() const
Definition url-inl.h:196
ada_really_inline ada::url_components get_components() const
Definition url-inl.h:55
constexpr bool has_search() const noexcept override
Definition url-inl.h:172
std::string to_string() const override
Definition url.cpp:482
constexpr bool has_hash() const noexcept override
Definition url-inl.h:168
Declaration for the ada::url class.
Declaration for the URL Components.