Headers.h 3.8 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/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 ErrorOr<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. , 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]] ErrorOr<Optional<ByteBuffer>> get(ReadonlyBytes) const;
  41. [[nodiscard]] ErrorOr<Optional<Vector<String>>> get_decode_and_split(ReadonlyBytes) const;
  42. [[nodiscard]] ErrorOr<void> append(Header);
  43. void delete_(ReadonlyBytes name);
  44. [[nodiscard]] ErrorOr<void> set(Header);
  45. [[nodiscard]] ErrorOr<void> combine(Header);
  46. [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
  47. struct ExtractLengthFailure {
  48. };
  49. using ExtractLengthResult = Variant<u64, ExtractLengthFailure, Empty>;
  50. [[nodiscard]] ErrorOr<ExtractLengthResult> extract_length() const;
  51. [[nodiscard]] ErrorOr<Optional<MimeSniff::MimeType>> extract_mime_type() const;
  52. ErrorOr<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]] ErrorOr<Optional<Vector<String>>> get_decode_and_split_header_value(ReadonlyBytes);
  62. [[nodiscard]] ErrorOr<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]] ErrorOr<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]] ErrorOr<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]] ErrorOr<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]] ErrorOr<Optional<Vector<ByteBuffer>>> extract_header_values(Header const&);
  78. [[nodiscard]] ErrorOr<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. }