Headers.h 3.7 KB

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