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/Cell.h>
  16. #include <LibJS/Heap/GCPtr.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. public:
  33. using Vector::begin;
  34. using Vector::clear;
  35. using Vector::end;
  36. using Vector::is_empty;
  37. [[nodiscard]] static JS::NonnullGCPtr<HeaderList> create(JS::VM&);
  38. [[nodiscard]] bool contains(ReadonlyBytes) const;
  39. [[nodiscard]] ErrorOr<Optional<ByteBuffer>> get(ReadonlyBytes) const;
  40. [[nodiscard]] ErrorOr<Optional<Vector<String>>> get_decode_and_split(ReadonlyBytes) const;
  41. [[nodiscard]] ErrorOr<void> append(Header);
  42. void delete_(ReadonlyBytes name);
  43. [[nodiscard]] ErrorOr<void> set(Header);
  44. [[nodiscard]] ErrorOr<void> combine(Header);
  45. [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
  46. struct ExtractLengthFailure {
  47. };
  48. using ExtractLengthResult = Variant<u64, ExtractLengthFailure, Empty>;
  49. [[nodiscard]] ErrorOr<ExtractLengthResult> extract_length() const;
  50. [[nodiscard]] ErrorOr<Optional<MimeSniff::MimeType>> extract_mime_type() const;
  51. ErrorOr<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]] ErrorOr<Optional<Vector<String>>> get_decode_and_split_header_value(ReadonlyBytes);
  61. [[nodiscard]] ErrorOr<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]] ErrorOr<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]] ErrorOr<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]] ErrorOr<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]] ErrorOr<Optional<Vector<ByteBuffer>>> extract_header_values(Header const&);
  77. [[nodiscard]] ErrorOr<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. }