24 std::numeric_limits<uint32_t>::max()};
37 std::array<uint8_t, 256>
result{};
38 for (
size_t i = 0; i <
result.size(); ++i) {
39 const auto c =
static_cast<uint8_t
>(i);
41 static_cast<uint8_t
>((c >=
'a' && c <=
'z') || (c >=
'0' && c <=
'9') ||
42 c ==
'-' || c ==
'.' || c ==
'_' || c ==
'~');
55 uint8_t hi)
noexcept {
56 constexpr uint64_t k_ones = 0x0101010101010101ull;
57 constexpr uint64_t k_high = 0x8080808080808080ull;
58 const uint64_t ge_lo = (w | k_high) - (k_ones * lo);
59 const uint64_t le_hi = ((k_ones * hi) | k_high) - w;
60 return ge_lo & le_hi & k_high;
64 const uint8_t* input)
noexcept {
66 std::memcpy(&w, input, 8);
67 constexpr uint64_t k_high = 0x8080808080808080ull;
68 if ((w & k_high) != 0) {
81#if defined(_MSC_VER) || \
82 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
83constexpr bool k_can_parse_little_endian =
true;
89 const auto* bytes =
reinterpret_cast<const uint8_t*
>(input.data());
90 const size_t length = input.size();
95 size_t authority_start;
99 std::memcpy(&first8, bytes, 8);
100 if (first8 == 0x2f2f3a7370747468ull) {
102 }
else if ((first8 & 0x00ffffffffffffffull) ==
103 0x002f2f3a70747468ull) {
108 }
else if (bytes[0] ==
'h' && bytes[1] ==
't' && bytes[2] ==
't' &&
109 bytes[3] ==
'p' && bytes[4] ==
':' && bytes[5] ==
'/' &&
117 std::memcpy(&first4, bytes, 4);
118 if (first4 != 0x68747470u) {
121 if (length >= 8 && bytes[4] ==
's' && bytes[5] ==
':' && bytes[6] ==
'/' &&
124 }
else if (bytes[4] ==
':' && bytes[5] ==
'/' && bytes[6] ==
'/') {
131 if (authority_start >= length) {
135 size_t cursor = authority_start;
139 while (cursor < length) {
140 const uint8_t c = bytes[cursor];
141 if (c ==
'/' || c ==
'?' || c ==
'#') {
150 const size_t host_length = cursor - authority_start;
151 if (host_length == 0) {
153 if (bytes[authority_start] ==
'/' || bytes[authority_start] ==
'\\') {
158 if (host_length > 253 || bytes[cursor - 1] ==
'.') {
162 const std::string_view host(input.data() + authority_start, host_length);
166 if (host.find(
'x') != std::string_view::npos &&
167 host.find(
"xn-") != std::string_view::npos) {
189 std::string_view input)
noexcept {
190 const uint8_t* b =
reinterpret_cast<const uint8_t*
>(input.data());
191 size_t len = input.size();
197 while (len > 0 && b[0] <= 0x20) {
201 while (len > 0 && b[len - 1] <= 0x20) {
204 if (len == 0)
return false;
213 if (len >= 7 && (b[0] | 0x20) ==
'h' && (b[1] | 0x20) ==
't' &&
214 (b[2] | 0x20) ==
't' && (b[3] | 0x20) ==
'p') {
215 if (b[4] ==
':' && b[5] ==
'/' && b[6] ==
'/') {
217 goto skip_extra_slashes;
219 if (len >= 8 && (b[4] | 0x20) ==
's' && b[5] ==
':' && b[6] ==
'/' &&
222 goto skip_extra_slashes;
234 size_t colon_pos = 0;
235 for (
size_t i = 1;; ++i) {
236 if (i >= 7 || i >= len)
return std::nullopt;
237 const char c =
static_cast<char>(b[i]);
243 if (c ==
'\t' || c ==
'\n' || c ==
'\r')
return std::nullopt;
244 if (!unicode::is_alnum_plus(c))
return false;
250 scheme_buf[0] =
static_cast<char>(b[0] | 0x20);
251 for (
size_t i = 1; i < colon_pos; ++i)
252 scheme_buf[i] =
static_cast<char>(b[i] | 0x20);
266 if (pos + 2 > len || b[pos] !=
'/' || b[pos + 1] !=
'/') {
276 while (pos < len && (b[pos] ==
'/' || b[pos] ==
'\\')) {
283 if (pos < len && b[pos] ==
'[')
return std::nullopt;
293 const size_t auth_start = pos;
294 size_t auth_end = pos;
295 size_t port_colon = SIZE_MAX;
296 bool all_dec_dots =
true;
298 for (; auth_end < len; ++auth_end) {
299 const uint8_t c = b[auth_end];
302 if (c >= 0x80)
return std::nullopt;
305 if (c ==
'/' || c ==
'?' || c ==
'#' || c ==
'\\')
break;
309 if (port_colon == SIZE_MAX) port_colon = auth_end;
314 if (c ==
'@' || c ==
'%')
return std::nullopt;
319 if (c ==
'\t' || c ==
'\n' || c ==
'\r')
return std::nullopt;
324 if (port_colon != SIZE_MAX)
continue;
333 if (c <= 0x20 || c == 0x7F || c ==
'<' || c ==
'>' || c ==
'[' ||
334 c ==
']' || c ==
'^' || c ==
'|') {
339 if (c !=
'.' && (c <
'0' || c >
'9')) all_dec_dots =
false;
345 if ((c | 0x20) ==
'x' && auth_end + 4 <= len &&
346 (b[auth_end + 1] | 0x20) ==
'n' && b[auth_end + 2] ==
'-' &&
347 b[auth_end + 3] ==
'-') {
352 const size_t host_end = (port_colon != SIZE_MAX) ? port_colon : auth_end;
355 if (auth_start == host_end)
return false;
358 const char* host_ptr =
reinterpret_cast<const char*
>(b + auth_start);
359 const size_t host_len = host_end - auth_start;
380 if (port_colon != SIZE_MAX) {
381 const uint8_t* pp = b + port_colon + 1;
382 size_t pl = auth_end - port_colon - 1;
386 while (pl > 0 && *pp ==
'0') {
390 if (pl > 5)
return false;
392 for (
size_t i = 0; i < pl; ++i) {
393 if (pp[i] <
'0' || pp[i] >
'9')
return false;
394 pv = pv * 10 + (pp[i] -
'0');
396 if (pv > 65535)
return false;
407template <
class result_type>
409 std::string_view input,
const result_type* base_url) {
418 const url* base_url =
nullptr);
420 std::string_view input,
const url_aggregator* base_url =
nullptr);
427 if (input.size() > max_length) {
432 std::string tmp_buffer;
433 std::string_view internal_input;
434 if (unicode::has_tabs_or_newline(input)) {
436 helpers::remove_ascii_tab_or_newline(tmp_buffer);
437 internal_input = tmp_buffer;
439 internal_input = input;
442 if (internal_input.empty()) {
444 }
else if ((internal_input[0] ==
'/') || (internal_input[0] ==
'\\')) {
445 helpers::parse_prepared_path(internal_input.substr(1),
450 std::string
result =
"file://" + path;
451 if (
result.size() > max_length) {
457bool can_parse(std::string_view input,
const std::string_view* base_input) {
467 if (base_input ==
nullptr) {
468 auto r = try_can_parse_clean_http(input);
469 if (!r.has_value()) {
470 r = try_can_parse_absolute_fast(input);
479 if (input.size() <=
static_cast<size_t>(max_length) / 5) {
482 if (input.size() > max_length) {
492 if (input.size() > max_length) {
495 if (base_input !=
nullptr && base_input->size() > max_length) {
501 const size_t combined =
502 input.size() + (base_input ==
nullptr ? 0 : base_input->size());
503 const bool size_safe = combined <= static_cast<size_t>(max_length) / 5;
509 if (base_input !=
nullptr) {
511 *base_input,
nullptr);
515 base_ptr = &base_agg;
523 if (base_input ==
nullptr) {
Definitions for URL specific checkers used within Ada.
Declarations for URL specific checkers used within Ada.
Cross-platform compiler macros and common definitions.
#define ada_really_inline
constexpr std::array< uint8_t, 256 > clean_http_host_byte
std::optional< bool > try_can_parse_clean_http(std::string_view input) noexcept
constexpr bool k_can_parse_little_endian
ada_really_inline uint64_t swar_in_range(uint64_t w, uint8_t lo, uint8_t hi) noexcept
ada_really_inline bool eight_clean_http_host_bytes(const uint8_t *input) noexcept
std::optional< bool > try_can_parse_absolute_fast(std::string_view input) noexcept
constexpr bool last_label_may_be_a_number(std::string_view view) noexcept
constexpr uint64_t ipv4_fast_fail
ada_really_inline uint64_t try_parse_ipv4_fast(std::string_view input) noexcept
constexpr bool is_alpha(char x) noexcept
result_type parse_url_impl(std::string_view user_input, const result_type *base_url=nullptr)
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
type
Enumeration of URL scheme types.
bool can_parse(std::string_view input, const std::string_view *base_input=nullptr)
template ada::result< url > parse< url >(std::string_view input, const url *base_url)
void set_max_input_length(uint32_t length)
static std::atomic< uint32_t > max_input_length_
std::string href_from_file(std::string_view path)
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
encoding_type
Character encoding types for URL processing.
ada_warn_unused std::string_view to_string(encoding_type type)
uint32_t get_max_input_length()
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Low-level URL parsing functions.
URL scheme type definitions and utilities.
Memory-efficient URL representation using a single buffer.
Definitions for unicode operations.
Declaration for the ada::url class.
Declaration for the ada::url_aggregator class.