Headers.h 3.6 KB

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