Serenity Operating System
at master 830 lines 34 kB view raw
1/* 2 * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org> 3 * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> 4 * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#include <AK/CharacterTypes.h> 10#include <AK/Checked.h> 11#include <AK/GenericLexer.h> 12#include <AK/QuickSort.h> 13#include <AK/ScopeGuard.h> 14#include <AK/StringUtils.h> 15#include <LibJS/Heap/Heap.h> 16#include <LibJS/Runtime/VM.h> 17#include <LibRegex/Regex.h> 18#include <LibWeb/Fetch/Infrastructure/HTTP.h> 19#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h> 20#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h> 21#include <LibWeb/Infra/ByteSequences.h> 22#include <LibWeb/Loader/ResourceLoader.h> 23#include <LibWeb/MimeSniff/MimeType.h> 24 25namespace Web::Fetch::Infrastructure { 26 27template<typename T> 28requires(IsSameIgnoringCV<T, u8>) struct CaseInsensitiveBytesTraits : public Traits<Span<T>> { 29 static constexpr bool equals(Span<T> const& a, Span<T> const& b) 30 { 31 return StringView { a }.equals_ignoring_ascii_case(StringView { b }); 32 } 33 34 static constexpr unsigned hash(Span<T> const& span) 35 { 36 if (span.is_empty()) 37 return 0; 38 return AK::case_insensitive_string_hash(reinterpret_cast<char const*>(span.data()), span.size()); 39 } 40}; 41 42ErrorOr<Header> Header::from_string_pair(StringView name, StringView value) 43{ 44 return Header { 45 .name = TRY(ByteBuffer::copy(name.bytes())), 46 .value = TRY(ByteBuffer::copy(value.bytes())), 47 }; 48} 49 50JS::NonnullGCPtr<HeaderList> HeaderList::create(JS::VM& vm) 51{ 52 return vm.heap().allocate_without_realm<HeaderList>(); 53} 54 55// https://fetch.spec.whatwg.org/#header-list-contains 56bool HeaderList::contains(ReadonlyBytes name) const 57{ 58 // A header list list contains a header name name if list contains a header whose name is a byte-case-insensitive match for name. 59 return any_of(*this, [&](auto const& header) { 60 return StringView { header.name }.equals_ignoring_ascii_case(name); 61 }); 62} 63 64// https://fetch.spec.whatwg.org/#concept-header-list-get 65ErrorOr<Optional<ByteBuffer>> HeaderList::get(ReadonlyBytes name) const 66{ 67 // To get a header name name from a header list list, run these steps: 68 69 // 1. If list does not contain name, then return null. 70 if (!contains(name)) 71 return Optional<ByteBuffer> {}; 72 73 // 2. Return the values of all headers in list whose name is a byte-case-insensitive match for name, separated from each other by 0x2C 0x20, in order. 74 ByteBuffer buffer; 75 auto first = true; 76 for (auto const& header : *this) { 77 if (!StringView { header.name }.equals_ignoring_ascii_case(name)) 78 continue; 79 if (first) { 80 first = false; 81 } else { 82 TRY(buffer.try_append(0x2c)); 83 TRY(buffer.try_append(0x20)); 84 } 85 TRY(buffer.try_append(header.value)); 86 } 87 return buffer; 88} 89 90// https://fetch.spec.whatwg.org/#concept-header-list-get-decode-split 91ErrorOr<Optional<Vector<String>>> HeaderList::get_decode_and_split(ReadonlyBytes name) const 92{ 93 // To get, decode, and split a header name name from header list list, run these steps: 94 95 // 1. Let value be the result of getting name from list. 96 auto value = TRY(get(name)); 97 98 // 2. If value is null, then return null. 99 if (!value.has_value()) 100 return Optional<Vector<String>> {}; 101 102 // 3. Return the result of getting, decoding, and splitting value. 103 return get_decode_and_split_header_value(*value); 104} 105 106// https://fetch.spec.whatwg.org/#header-value-get-decode-and-split 107ErrorOr<Optional<Vector<String>>> get_decode_and_split_header_value(ReadonlyBytes value) 108{ 109 // To get, decode, and split a header value value, run these steps: 110 111 // 1. Let input be the result of isomorphic decoding value. 112 auto input = StringView { value }; 113 114 // 2. Let position be a position variable for input, initially pointing at the start of input. 115 auto lexer = GenericLexer { input }; 116 117 // 3. Let values be a list of strings, initially empty. 118 Vector<String> values; 119 120 // 4. Let temporaryValue be the empty string. 121 StringBuilder temporary_value_builder; 122 123 // 5. While position is not past the end of input: 124 while (!lexer.is_eof()) { 125 // 1. Append the result of collecting a sequence of code points that are not U+0022 (") or U+002C (,) from input, given position, to temporaryValue. 126 // NOTE: The result might be the empty string. 127 TRY(temporary_value_builder.try_append(lexer.consume_until(is_any_of("\","sv)))); 128 129 // 2. If position is not past the end of input, then: 130 if (!lexer.is_eof()) { 131 // 1. If the code point at position within input is U+0022 ("), then: 132 if (lexer.peek() == '"') { 133 // 1. Append the result of collecting an HTTP quoted string from input, given position, to temporaryValue. 134 TRY(temporary_value_builder.try_append(TRY(collect_an_http_quoted_string(lexer)))); 135 136 // 2. If position is not past the end of input, then continue. 137 if (!lexer.is_eof()) 138 continue; 139 } 140 // 2. Otherwise: 141 else { 142 // 1. Assert: the code point at position within input is U+002C (,). 143 VERIFY(lexer.peek() == ','); 144 145 // 2. Advance position by 1. 146 lexer.ignore(1); 147 } 148 } 149 150 // 3. Remove all HTTP tab or space from the start and end of temporaryValue. 151 auto temporary_value = TRY(String::from_utf8(temporary_value_builder.string_view().trim(HTTP_TAB_OR_SPACE, TrimMode::Both))); 152 153 // 4. Append temporaryValue to values. 154 TRY(values.try_append(move(temporary_value))); 155 156 // 5. Set temporaryValue to the empty string. 157 temporary_value_builder.clear(); 158 } 159 160 // 8. Return values. 161 return values; 162} 163 164// https://fetch.spec.whatwg.org/#concept-header-list-append 165ErrorOr<void> HeaderList::append(Header header) 166{ 167 // To append a header (name, value) to a header list list, run these steps: 168 // NOTE: Can't use structured bindings captured in the lambda due to https://github.com/llvm/llvm-project/issues/48582 169 auto& name = header.name; 170 171 // 1. If list contains name, then set name to the first such header’s name. 172 // NOTE: This reuses the casing of the name of the header already in list, if any. If there are multiple matched headers their names will all be identical. 173 if (contains(name)) { 174 auto matching_header = first_matching([&](auto const& existing_header) { 175 return StringView { existing_header.name }.equals_ignoring_ascii_case(name); 176 }); 177 name.overwrite(0, matching_header->name.data(), matching_header->name.size()); 178 } 179 180 // 2. Append (name, value) to list. 181 TRY(Vector<Header>::try_append(move(header))); 182 183 return {}; 184} 185 186// https://fetch.spec.whatwg.org/#concept-header-list-delete 187void HeaderList::delete_(ReadonlyBytes name) 188{ 189 // To delete a header name name from a header list list, remove all headers whose name is a byte-case-insensitive match for name from list. 190 remove_all_matching([&](auto const& header) { 191 return StringView { header.name }.equals_ignoring_ascii_case(name); 192 }); 193} 194 195// https://fetch.spec.whatwg.org/#concept-header-list-set 196ErrorOr<void> HeaderList::set(Header header) 197{ 198 // To set a header (name, value) in a header list list, run these steps: 199 // NOTE: Can't use structured bindings captured in the lambda due to https://github.com/llvm/llvm-project/issues/48582 200 auto const& name = header.name; 201 auto const& value = header.value; 202 203 // 1. If list contains name, then set the value of the first such header to value and remove the others. 204 if (contains(name)) { 205 auto matching_index = find_if([&](auto const& existing_header) { 206 return StringView { existing_header.name }.equals_ignoring_ascii_case(name); 207 }).index(); 208 auto& matching_header = at(matching_index); 209 matching_header.value = TRY(ByteBuffer::copy(value)); 210 size_t i = 0; 211 remove_all_matching([&](auto const& existing_header) { 212 ScopeGuard increment_i = [&]() { i++; }; 213 if (i <= matching_index) 214 return false; 215 return StringView { existing_header.name }.equals_ignoring_ascii_case(name); 216 }); 217 } 218 // 2. Otherwise, append header (name, value) to list. 219 else { 220 TRY(try_append(move(header))); 221 } 222 223 return {}; 224} 225 226// https://fetch.spec.whatwg.org/#concept-header-list-combine 227ErrorOr<void> HeaderList::combine(Header header) 228{ 229 // To combine a header (name, value) in a header list list, run these steps: 230 // NOTE: Can't use structured bindings captured in the lambda due to https://github.com/llvm/llvm-project/issues/48582 231 auto const& name = header.name; 232 auto const& value = header.value; 233 234 // 1. If list contains name, then set the value of the first such header to its value, followed by 0x2C 0x20, followed by value. 235 if (contains(name)) { 236 auto matching_header = first_matching([&](auto const& existing_header) { 237 return StringView { existing_header.name }.equals_ignoring_ascii_case(name); 238 }); 239 TRY(matching_header->value.try_append(0x2c)); 240 TRY(matching_header->value.try_append(0x20)); 241 TRY(matching_header->value.try_append(value)); 242 } 243 // 2. Otherwise, append (name, value) to list. 244 else { 245 TRY(try_append(move(header))); 246 } 247 248 return {}; 249} 250 251// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine 252ErrorOr<Vector<Header>> HeaderList::sort_and_combine() const 253{ 254 // To sort and combine a header list list, run these steps: 255 256 // 1. Let headers be an empty list of headers with the key being the name and value the value. 257 Vector<Header> headers; 258 259 // 2. Let names be the result of convert header names to a sorted-lowercase set with all the names of the headers in list. 260 Vector<ReadonlyBytes> names_list; 261 TRY(names_list.try_ensure_capacity(size())); 262 for (auto const& header : *this) 263 names_list.unchecked_append(header.name); 264 auto names = TRY(convert_header_names_to_a_sorted_lowercase_set(names_list)); 265 266 // 3. For each name of names: 267 for (auto& name : names) { 268 // 1. If name is `set-cookie`, then: 269 if (name == "set-cookie"sv.bytes()) { 270 // 1. Let values be a list of all values of headers in list whose name is a byte-case-insensitive match for name, in order. 271 // 2. For each value of values: 272 for (auto const& [header_name, value] : *this) { 273 if (StringView { header_name }.equals_ignoring_ascii_case(name)) { 274 // 1. Append (name, value) to headers. 275 auto header = TRY(Header::from_string_pair(name, value)); 276 TRY(headers.try_append(move(header))); 277 } 278 } 279 } 280 // 2. Otherwise: 281 else { 282 // 1. Let value be the result of getting name from list. 283 auto value = TRY(get(name)); 284 285 // 2. Assert: value is not null. 286 VERIFY(value.has_value()); 287 288 // 3. Append (name, value) to headers. 289 auto header = Header { 290 .name = move(name), 291 .value = value.release_value(), 292 }; 293 TRY(headers.try_append(move(header))); 294 } 295 } 296 297 // 4. Return headers. 298 return headers; 299} 300 301// https://fetch.spec.whatwg.org/#header-list-extract-a-length 302ErrorOr<HeaderList::ExtractLengthResult> HeaderList::extract_length() const 303{ 304 // 1. Let values be the result of getting, decoding, and splitting `Content-Length` from headers. 305 auto values = TRY(get_decode_and_split("Content-Length"sv.bytes())); 306 307 // 2. If values is null, then return null. 308 if (!values.has_value()) 309 return Empty {}; 310 311 // 3. Let candidateValue be null. 312 Optional<String> candidate_value; 313 314 // 4. For each value of values: 315 for (auto const& value : *values) { 316 // 1. If candidateValue is null, then set candidateValue to value. 317 if (!candidate_value.has_value()) { 318 candidate_value = value; 319 } 320 // 2. Otherwise, if value is not candidateValue, return failure. 321 else if (candidate_value.value() != value) { 322 return ExtractLengthFailure {}; 323 } 324 } 325 326 // 5. If candidateValue is the empty string or has a code point that is not an ASCII digit, then return null. 327 // NOTE: to_uint does this for us. 328 // 6. Return candidateValue, interpreted as decimal number. 329 // NOTE: The spec doesn't say anything about trimming here, so we don't trim. If it contains a space, step 5 will cause us to return null. 330 // FIXME: This will return an empty Optional if it cannot fit into a u64, is this correct? 331 auto conversion_result = AK::StringUtils::convert_to_uint<u64>(candidate_value.value(), TrimWhitespace::No); 332 if (!conversion_result.has_value()) 333 return Empty {}; 334 return ExtractLengthResult { conversion_result.release_value() }; 335} 336 337// https://fetch.spec.whatwg.org/#concept-header-extract-mime-type 338ErrorOr<Optional<MimeSniff::MimeType>> HeaderList::extract_mime_type() const 339{ 340 // 1. Let charset be null. 341 Optional<String> charset; 342 343 // 2. Let essence be null. 344 Optional<String> essence; 345 346 // 3. Let mimeType be null. 347 Optional<MimeSniff::MimeType> mime_type; 348 349 // 4. Let values be the result of getting, decoding, and splitting `Content-Type` from headers. 350 auto values_or_error = get_decode_and_split("Content-Type"sv.bytes()); 351 if (values_or_error.is_error()) 352 return OptionalNone {}; 353 auto values = values_or_error.release_value(); 354 355 // 5. If values is null, then return failure. 356 if (!values.has_value()) 357 return OptionalNone {}; 358 359 // 6. For each value of values: 360 for (auto const& value : *values) { 361 // 1. Let temporaryMimeType be the result of parsing value. 362 auto temporary_mime_type = TRY(MimeSniff::MimeType::parse(value)); 363 364 // 2. If temporaryMimeType is failure or its essence is "*/*", then continue. 365 if (!temporary_mime_type.has_value() || temporary_mime_type->essence() == "*/*"sv) 366 continue; 367 368 // 3. Set mimeType to temporaryMimeType. 369 mime_type = temporary_mime_type; 370 371 // 4. If mimeType’s essence is not essence, then: 372 if (!essence.has_value() || (mime_type->essence() != essence->bytes_as_string_view())) { 373 // 1. Set charset to null. 374 charset = {}; 375 376 // 2. If mimeType’s parameters["charset"] exists, then set charset to mimeType’s parameters["charset"]. 377 auto it = mime_type->parameters().find("charset"sv); 378 if (it != mime_type->parameters().end()) 379 charset = it->value; 380 381 // 3. Set essence to mimeType’s essence. 382 essence = mime_type->essence(); 383 } 384 // 5. Otherwise, if mimeType’s parameters["charset"] does not exist, and charset is non-null, set mimeType’s parameters["charset"] to charset. 385 else if (!mime_type->parameters().contains("charset"sv) && charset.has_value()) { 386 TRY(mime_type->set_parameter(TRY("charset"_string), charset.release_value())); 387 } 388 } 389 390 // 7. If mimeType is null, then return failure. 391 // 8. Return mimeType. 392 return mime_type; 393} 394 395// https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set 396ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes> header_names) 397{ 398 // To convert header names to a sorted-lowercase set, given a list of names headerNames, run these steps: 399 400 // 1. Let headerNamesSet be a new ordered set. 401 Vector<ByteBuffer> header_names_set; 402 HashTable<ReadonlyBytes, CaseInsensitiveBytesTraits<u8 const>> header_names_seen; 403 404 // 2. For each name of headerNames, append the result of byte-lowercasing name to headerNamesSet. 405 for (auto name : header_names) { 406 if (header_names_seen.contains(name)) 407 continue; 408 auto bytes = TRY(ByteBuffer::copy(name)); 409 Infra::byte_lowercase(bytes); 410 header_names_seen.set(name); 411 header_names_set.append(move(bytes)); 412 } 413 414 // 3. Return the result of sorting headerNamesSet in ascending order with byte less than. 415 quick_sort(header_names_set, [](auto const& a, auto const& b) { 416 return StringView { a } < StringView { b }; 417 }); 418 OrderedHashTable<ByteBuffer> ordered { header_names_set.size() }; 419 for (auto& name : header_names_set) { 420 auto result = ordered.set(move(name)); 421 VERIFY(result == AK::HashSetResult::InsertedNewEntry); 422 } 423 return ordered; 424} 425 426// https://fetch.spec.whatwg.org/#header-name 427bool is_header_name(ReadonlyBytes header_name) 428{ 429 // A header name is a byte sequence that matches the field-name token production. 430 Regex<ECMA262Parser> regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" }; 431 return regex.has_match(StringView { header_name }); 432} 433 434// https://fetch.spec.whatwg.org/#header-value 435bool is_header_value(ReadonlyBytes header_value) 436{ 437 // A header value is a byte sequence that matches the following conditions: 438 // - Has no leading or trailing HTTP tab or space bytes. 439 // - Contains no 0x00 (NUL) or HTTP newline bytes. 440 if (header_value.is_empty()) 441 return true; 442 auto first_byte = header_value[0]; 443 auto last_byte = header_value[header_value.size() - 1]; 444 if (HTTP_TAB_OR_SPACE_BYTES.span().contains_slow(first_byte) || HTTP_TAB_OR_SPACE_BYTES.span().contains_slow(last_byte)) 445 return false; 446 return !any_of(header_value, [](auto byte) { 447 return byte == 0x00 || HTTP_NEWLINE_BYTES.span().contains_slow(byte); 448 }); 449} 450 451// https://fetch.spec.whatwg.org/#concept-header-value-normalize 452ErrorOr<ByteBuffer> normalize_header_value(ReadonlyBytes potential_value) 453{ 454 // To normalize a byte sequence potentialValue, remove any leading and trailing HTTP whitespace bytes from potentialValue. 455 if (potential_value.is_empty()) 456 return ByteBuffer {}; 457 auto trimmed = StringView { potential_value }.trim(HTTP_WHITESPACE, TrimMode::Both); 458 return ByteBuffer::copy(trimmed.bytes()); 459} 460 461// https://fetch.spec.whatwg.org/#cors-safelisted-request-header 462bool is_cors_safelisted_request_header(Header const& header) 463{ 464 // To determine whether a header (name, value) is a CORS-safelisted request-header, run these steps: 465 466 auto const& value = header.value; 467 468 // 1. If value’s length is greater than 128, then return false. 469 if (value.size() > 128) 470 return false; 471 472 // 2. Byte-lowercase name and switch on the result: 473 auto name = StringView { header.name }; 474 475 // `accept` 476 if (name.equals_ignoring_ascii_case("accept"sv)) { 477 // If value contains a CORS-unsafe request-header byte, then return false. 478 if (any_of(value.span(), is_cors_unsafe_request_header_byte)) 479 return false; 480 } 481 // `accept-language` 482 // `content-language` 483 else if (name.is_one_of_ignoring_ascii_case("accept-language"sv, "content-language"sv)) { 484 // If value contains a byte that is not in the range 0x30 (0) to 0x39 (9), inclusive, is not in the range 0x41 (A) to 0x5A (Z), inclusive, is not in the range 0x61 (a) to 0x7A (z), inclusive, and is not 0x20 (SP), 0x2A (*), 0x2C (,), 0x2D (-), 0x2E (.), 0x3B (;), or 0x3D (=), then return false. 485 if (any_of(value.span(), [](auto byte) { 486 return !(is_ascii_digit(byte) || is_ascii_alpha(byte) || " *,-.;="sv.contains(static_cast<char>(byte))); 487 })) 488 return false; 489 } 490 // `content-type` 491 else if (name.equals_ignoring_ascii_case("content-type"sv)) { 492 // 1. If value contains a CORS-unsafe request-header byte, then return false. 493 if (any_of(value.span(), is_cors_unsafe_request_header_byte)) 494 return false; 495 496 // 2. Let mimeType be the result of parsing the result of isomorphic decoding value. 497 auto mime_type = MimeSniff::MimeType::parse(StringView { value }).release_value_but_fixme_should_propagate_errors(); 498 499 // 3. If mimeType is failure, then return false. 500 if (!mime_type.has_value()) 501 return false; 502 503 // 4. If mimeType’s essence is not "application/x-www-form-urlencoded", "multipart/form-data", or "text/plain", then return false. 504 if (!mime_type->essence().is_one_of("application/x-www-form-urlencoded"sv, "multipart/form-data"sv, "text/plain"sv)) 505 return false; 506 } 507 // `range` 508 else if (name.equals_ignoring_ascii_case("range"sv)) { 509 // 1. Let rangeValue be the result of parsing a single range header value given value. 510 auto range_value = parse_single_range_header_value(value); 511 512 // 2. If rangeValue is failure, then return false. 513 if (!range_value.has_value()) 514 return false; 515 516 // 3. If rangeValue[0] is null, then return false. 517 // NOTE: As web browsers have historically not emitted ranges such as `bytes=-500` this algorithm does not safelist them. 518 if (!range_value->start.has_value()) 519 return false; 520 } 521 // Otherwise 522 else { 523 // Return false. 524 return false; 525 } 526 527 // 3. Return true. 528 return true; 529} 530 531// https://fetch.spec.whatwg.org/#cors-unsafe-request-header-byte 532bool is_cors_unsafe_request_header_byte(u8 byte) 533{ 534 // A CORS-unsafe request-header byte is a byte byte for which one of the following is true: 535 // - byte is less than 0x20 and is not 0x09 HT 536 // - byte is 0x22 ("), 0x28 (left parenthesis), 0x29 (right parenthesis), 0x3A (:), 0x3C (<), 0x3E (>), 0x3F (?), 0x40 (@), 0x5B ([), 0x5C (\), 0x5D (]), 0x7B ({), 0x7D (}), or 0x7F DEL. 537 return (byte < 0x20 && byte != 0x09) 538 || (Array<u8, 14> { 0x22, 0x28, 0x29, 0x3A, 0x3C, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x7B, 0x7D, 0x7F }.span().contains_slow(byte)); 539} 540 541// https://fetch.spec.whatwg.org/#cors-unsafe-request-header-names 542ErrorOr<OrderedHashTable<ByteBuffer>> get_cors_unsafe_header_names(HeaderList const& headers) 543{ 544 // The CORS-unsafe request-header names, given a header list headers, are determined as follows: 545 546 // 1. Let unsafeNames be a new list. 547 Vector<ReadonlyBytes> unsafe_names; 548 549 // 2. Let potentiallyUnsafeNames be a new list. 550 Vector<ReadonlyBytes> potentially_unsafe_names; 551 552 // 3. Let safelistValueSize be 0. 553 Checked<size_t> safelist_value_size = 0; 554 555 // 4. For each header of headers: 556 for (auto const& header : headers) { 557 // 1. If header is not a CORS-safelisted request-header, then append header’s name to unsafeNames. 558 if (!is_cors_safelisted_request_header(header)) { 559 unsafe_names.append(header.name.span()); 560 } 561 // 2. Otherwise, append header’s name to potentiallyUnsafeNames and increase safelistValueSize by header’s value’s length. 562 else { 563 potentially_unsafe_names.append(header.name.span()); 564 safelist_value_size += header.value.size(); 565 } 566 } 567 568 // 5. If safelistValueSize is greater than 1024, then for each name of potentiallyUnsafeNames, append name to unsafeNames. 569 if (safelist_value_size.has_overflow() || safelist_value_size.value() > 1024) { 570 for (auto const& name : potentially_unsafe_names) 571 unsafe_names.append(name); 572 } 573 574 // 6. Return the result of convert header names to a sorted-lowercase set with unsafeNames. 575 return convert_header_names_to_a_sorted_lowercase_set(unsafe_names.span()); 576} 577 578// https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name 579bool is_cors_non_wildcard_request_header_name(ReadonlyBytes header_name) 580{ 581 // A CORS non-wildcard request-header name is a header name that is a byte-case-insensitive match for `Authorization`. 582 return StringView { header_name }.equals_ignoring_ascii_case("Authorization"sv); 583} 584 585// https://fetch.spec.whatwg.org/#privileged-no-cors-request-header-name 586bool is_privileged_no_cors_request_header_name(ReadonlyBytes header_name) 587{ 588 // A privileged no-CORS request-header name is a header name that is a byte-case-insensitive match for one of 589 // - `Range`. 590 return StringView { header_name }.equals_ignoring_ascii_case("Range"sv); 591} 592 593// https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name 594bool is_cors_safelisted_response_header_name(ReadonlyBytes header_name, Span<ReadonlyBytes> list) 595{ 596 // A CORS-safelisted response-header name, given a list of header names list, is a header name that is a byte-case-insensitive match for one of 597 // - `Cache-Control` 598 // - `Content-Language` 599 // - `Content-Length` 600 // - `Content-Type` 601 // - `Expires` 602 // - `Last-Modified` 603 // - `Pragma` 604 // - Any item in list that is not a forbidden response-header name. 605 return StringView { header_name }.is_one_of_ignoring_ascii_case( 606 "Cache-Control"sv, 607 "Content-Language"sv, 608 "Content-Length"sv, 609 "Content-Type"sv, 610 "Expires"sv, 611 "Last-Modified"sv, 612 "Pragma"sv) 613 || any_of(list, [&](auto list_header_name) { 614 return StringView { header_name }.equals_ignoring_ascii_case(list_header_name) 615 && !is_forbidden_response_header_name(list_header_name); 616 }); 617} 618 619// https://fetch.spec.whatwg.org/#no-cors-safelisted-request-header-name 620bool is_no_cors_safelisted_request_header_name(ReadonlyBytes header_name) 621{ 622 // A no-CORS-safelisted request-header name is a header name that is a byte-case-insensitive match for one of 623 // - `Accept` 624 // - `Accept-Language` 625 // - `Content-Language` 626 // - `Content-Type` 627 return StringView { header_name }.is_one_of_ignoring_ascii_case( 628 "Accept"sv, 629 "Accept-Language"sv, 630 "Content-Language"sv, 631 "Content-Type"sv); 632} 633 634// https://fetch.spec.whatwg.org/#no-cors-safelisted-request-header 635bool is_no_cors_safelisted_request_header(Header const& header) 636{ 637 // To determine whether a header (name, value) is a no-CORS-safelisted request-header, run these steps: 638 639 // 1. If name is not a no-CORS-safelisted request-header name, then return false. 640 if (!is_no_cors_safelisted_request_header_name(header.name)) 641 return false; 642 643 // 2. Return whether (name, value) is a CORS-safelisted request-header. 644 return is_cors_safelisted_request_header(header); 645} 646 647// https://fetch.spec.whatwg.org/#forbidden-header-name 648ErrorOr<bool> is_forbidden_request_header(Header const& header) 649{ 650 // A header (name, value) is forbidden request-header if these steps return true: 651 auto name = StringView { header.name }; 652 653 // 1. If name is a byte-case-insensitive match for one of: 654 // [...] 655 // then return true. 656 if (name.is_one_of_ignoring_ascii_case( 657 "Accept-Charset"sv, 658 "Accept-Encoding"sv, 659 "Access-Control-Request-Headers"sv, 660 "Access-Control-Request-Method"sv, 661 "Connection"sv, 662 "Content-Length"sv, 663 "Cookie"sv, 664 "Cookie2"sv, 665 "Date"sv, 666 "DNT"sv, 667 "Expect"sv, 668 "Host"sv, 669 "Keep-Alive"sv, 670 "Origin"sv, 671 "Referer"sv, 672 "TE"sv, 673 "Trailer"sv, 674 "Transfer-Encoding"sv, 675 "Upgrade"sv, 676 "Via"sv)) { 677 return true; 678 } 679 680 // 2. If name when byte-lowercased starts with `proxy-` or `sec-`, then return true. 681 if (name.starts_with("proxy-"sv, CaseSensitivity::CaseInsensitive) 682 || name.starts_with("sec-"sv, CaseSensitivity::CaseInsensitive)) { 683 return true; 684 } 685 686 // 3. If name is a byte-case-insensitive match for one of: 687 // - `X-HTTP-Method` 688 // - `X-HTTP-Method-Override` 689 // - `X-Method-Override` 690 // then: 691 if (name.is_one_of_ignoring_ascii_case( 692 "X-HTTP-Method"sv, 693 "X-HTTP-Method-Override"sv, 694 "X-Method"sv)) { 695 // 1. Let parsedValues be the result of getting, decoding, and splitting value. 696 auto parsed_values = TRY(get_decode_and_split_header_value(header.value)); 697 698 // 2. For each method of parsedValues: if the isomorphic encoding of method is a forbidden method, then return true. 699 if (parsed_values.has_value() && any_of(*parsed_values, [](auto method) { return is_forbidden_method(method.bytes()); })) 700 return true; 701 } 702 703 // 4. Return false. 704 return false; 705} 706 707// https://fetch.spec.whatwg.org/#forbidden-response-header-name 708bool is_forbidden_response_header_name(ReadonlyBytes header_name) 709{ 710 // A forbidden response-header name is a header name that is a byte-case-insensitive match for one of: 711 // - `Set-Cookie` 712 // - `Set-Cookie2` 713 return StringView { header_name }.is_one_of_ignoring_ascii_case( 714 "Set-Cookie"sv, 715 "Set-Cookie2"sv); 716} 717 718// https://fetch.spec.whatwg.org/#request-body-header-name 719bool is_request_body_header_name(ReadonlyBytes header_name) 720{ 721 // A request-body-header name is a header name that is a byte-case-insensitive match for one of: 722 // - `Content-Encoding` 723 // - `Content-Language` 724 // - `Content-Location` 725 // - `Content-Type` 726 return StringView { header_name }.is_one_of_ignoring_ascii_case( 727 "Content-Encoding"sv, 728 "Content-Language"sv, 729 "Content-Location"sv, 730 "Content-Type"sv); 731} 732 733// https://fetch.spec.whatwg.org/#extract-header-values 734ErrorOr<Optional<Vector<ByteBuffer>>> extract_header_values(Header const& header) 735{ 736 // FIXME: 1. If parsing header’s value, per the ABNF for header’s name, fails, then return failure. 737 // FIXME: 2. Return one or more values resulting from parsing header’s value, per the ABNF for header’s name. 738 // This always ignores the ABNF rules for now and returns the header value as a single list item. 739 return Vector { TRY(ByteBuffer::copy(header.value)) }; 740} 741 742// https://fetch.spec.whatwg.org/#extract-header-list-values 743ErrorOr<Variant<Vector<ByteBuffer>, ExtractHeaderParseFailure, Empty>> extract_header_list_values(ReadonlyBytes name, HeaderList const& list) 744{ 745 // 1. If list does not contain name, then return null. 746 if (!list.contains(name)) 747 return Empty {}; 748 749 // FIXME: 2. If the ABNF for name allows a single header and list contains more than one, then return failure. 750 // NOTE: If different error handling is needed, extract the desired header first. 751 752 // 3. Let values be an empty list. 753 auto values = Vector<ByteBuffer> {}; 754 755 // 4. For each header header list contains whose name is name: 756 for (auto const& header : list) { 757 if (!StringView { header.name }.equals_ignoring_ascii_case(name)) 758 continue; 759 760 // 1. Let extract be the result of extracting header values from header. 761 auto extract = TRY(extract_header_values(header)); 762 763 // 2. If extract is failure, then return failure. 764 if (!extract.has_value()) 765 return ExtractHeaderParseFailure {}; 766 767 // 3. Append each value in extract, in order, to values. 768 values.extend(extract.release_value()); 769 } 770 771 // 5. Return values. 772 return values; 773} 774 775// https://fetch.spec.whatwg.org/#simple-range-header-value 776Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes value) 777{ 778 // 1. Let data be the isomorphic decoding of value. 779 auto data = StringView { value }; 780 781 // 2. If data does not start with "bytes=", then return failure. 782 if (!data.starts_with("bytes="sv)) 783 return {}; 784 785 // 3. Let position be a position variable for data, initially pointing at the 6th code point of data. 786 auto lexer = GenericLexer { data }; 787 lexer.ignore(6); 788 789 // 4. Let rangeStart be the result of collecting a sequence of code points that are ASCII digits, from data given position. 790 auto range_start = lexer.consume_while(is_ascii_digit); 791 792 // 5. Let rangeStartValue be rangeStart, interpreted as decimal number, if rangeStart is not the empty string; otherwise null. 793 auto range_start_value = range_start.to_uint<u64>(); 794 795 // 6. If the code point at position within data is not U+002D (-), then return failure. 796 // 7. Advance position by 1. 797 if (!lexer.consume_specific('-')) 798 return {}; 799 800 // 8. Let rangeEnd be the result of collecting a sequence of code points that are ASCII digits, from data given position. 801 auto range_end = lexer.consume_while(is_ascii_digit); 802 803 // 9. Let rangeEndValue be rangeEnd, interpreted as decimal number, if rangeEnd is not the empty string; otherwise null. 804 auto range_end_value = range_end.to_uint<u64>(); 805 806 // 10. If position is not past the end of data, then return failure. 807 if (!lexer.is_eof()) 808 return {}; 809 810 // 11. If rangeEndValue and rangeStartValue are null, then return failure. 811 if (!range_end_value.has_value() && !range_start_value.has_value()) 812 return {}; 813 814 // 12. If rangeStartValue and rangeEndValue are numbers, and rangeStartValue is greater than rangeEndValue, then return failure. 815 if (range_start_value.has_value() && range_end_value.has_value() && *range_start_value > *range_end_value) 816 return {}; 817 818 // 13. Return (rangeStartValue, rangeEndValue). 819 // NOTE: The range end or start can be omitted, e.g., `bytes=0-` or `bytes=-500` are valid ranges. 820 return RangeHeaderValue { move(range_start_value), move(range_end_value) }; 821} 822 823// https://fetch.spec.whatwg.org/#default-user-agent-value 824ErrorOr<ByteBuffer> default_user_agent_value() 825{ 826 // A default `User-Agent` value is an implementation-defined header value for the `User-Agent` header. 827 return ByteBuffer::copy(default_user_agent.bytes()); 828} 829 830}