Headers.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Error.h>
  9. #include <AK/Forward.h>
  10. #include <AK/HashTable.h>
  11. #include <AK/Optional.h>
  12. #include <AK/String.h>
  13. #include <AK/Vector.h>
  14. #include <LibJS/Forward.h>
  15. #include <LibJS/Heap/Cell.h>
  16. #include <LibJS/Heap/GCPtr.h>
  17. #include <LibJS/Heap/Heap.h>
  18. #include <LibWeb/MimeSniff/MimeType.h>
  19. namespace Web::Fetch::Infrastructure {
  20. // https://fetch.spec.whatwg.org/#concept-header
  21. // A header is a tuple that consists of a name (a header name) and value (a header value).
  22. struct Header {
  23. ByteBuffer name;
  24. ByteBuffer value;
  25. [[nodiscard]] static Header copy(Header const&);
  26. [[nodiscard]] static Header from_string_pair(StringView, StringView);
  27. };
  28. // https://fetch.spec.whatwg.org/#concept-header-list
  29. // A header list is a list of zero or more headers. It is initially the empty list.
  30. class HeaderList final
  31. : public JS::Cell
  32. , public Vector<Header> {
  33. JS_CELL(HeaderList, JS::Cell);
  34. JS_DECLARE_ALLOCATOR(HeaderList);
  35. public:
  36. using Vector::begin;
  37. using Vector::clear;
  38. using Vector::end;
  39. using Vector::is_empty;
  40. [[nodiscard]] static JS::NonnullGCPtr<HeaderList> create(JS::VM&);
  41. [[nodiscard]] bool contains(ReadonlyBytes) const;
  42. [[nodiscard]] Optional<ByteBuffer> get(ReadonlyBytes) const;
  43. [[nodiscard]] Optional<Vector<String>> get_decode_and_split(ReadonlyBytes) const;
  44. void append(Header);
  45. void delete_(ReadonlyBytes name);
  46. void set(Header);
  47. void combine(Header);
  48. [[nodiscard]] Vector<Header> sort_and_combine() const;
  49. struct ExtractLengthFailure { };
  50. using ExtractLengthResult = Variant<u64, ExtractLengthFailure, Empty>;
  51. [[nodiscard]] ExtractLengthResult extract_length() const;
  52. [[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
  53. [[nodiscard]] Vector<ByteBuffer> unique_names() const;
  54. };
  55. struct RangeHeaderValue {
  56. Optional<u64> start;
  57. Optional<u64> end;
  58. };
  59. struct ExtractHeaderParseFailure {
  60. };
  61. [[nodiscard]] StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding);
  62. [[nodiscard]] Optional<Vector<String>> get_decode_and_split_header_value(ReadonlyBytes);
  63. [[nodiscard]] OrderedHashTable<ByteBuffer> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);
  64. [[nodiscard]] bool is_header_name(ReadonlyBytes);
  65. [[nodiscard]] bool is_header_value(ReadonlyBytes);
  66. [[nodiscard]] ByteBuffer normalize_header_value(ReadonlyBytes);
  67. [[nodiscard]] bool is_cors_safelisted_request_header(Header const&);
  68. [[nodiscard]] bool is_cors_unsafe_request_header_byte(u8);
  69. [[nodiscard]] OrderedHashTable<ByteBuffer> get_cors_unsafe_header_names(HeaderList const&);
  70. [[nodiscard]] bool is_cors_non_wildcard_request_header_name(ReadonlyBytes);
  71. [[nodiscard]] bool is_privileged_no_cors_request_header_name(ReadonlyBytes);
  72. [[nodiscard]] bool is_cors_safelisted_response_header_name(ReadonlyBytes, Span<ReadonlyBytes>);
  73. [[nodiscard]] bool is_no_cors_safelisted_request_header_name(ReadonlyBytes);
  74. [[nodiscard]] bool is_no_cors_safelisted_request_header(Header const&);
  75. [[nodiscard]] bool is_forbidden_request_header(Header const&);
  76. [[nodiscard]] bool is_forbidden_response_header_name(ReadonlyBytes);
  77. [[nodiscard]] bool is_request_body_header_name(ReadonlyBytes);
  78. [[nodiscard]] Optional<Vector<ByteBuffer>> extract_header_values(Header const&);
  79. [[nodiscard]] Variant<Vector<ByteBuffer>, ExtractHeaderParseFailure, Empty> extract_header_list_values(ReadonlyBytes, HeaderList const&);
  80. [[nodiscard]] Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes);
  81. [[nodiscard]] ByteBuffer default_user_agent_value();
  82. }