Headers.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2022, 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 <LibWeb/MimeSniff/MimeType.h>
  17. namespace Web::Fetch::Infrastructure {
  18. // https://fetch.spec.whatwg.org/#concept-header
  19. // A header is a tuple that consists of a name (a header name) and value (a header value).
  20. struct Header {
  21. ByteBuffer name;
  22. ByteBuffer value;
  23. static ErrorOr<Header> from_string_pair(StringView, StringView);
  24. };
  25. // https://fetch.spec.whatwg.org/#concept-header-list
  26. // A header list is a list of zero or more headers. It is initially the empty list.
  27. class HeaderList final
  28. : public JS::Cell
  29. , Vector<Header> {
  30. JS_CELL(HeaderList, JS::Cell);
  31. public:
  32. using Vector::begin;
  33. using Vector::clear;
  34. using Vector::end;
  35. [[nodiscard]] static JS::NonnullGCPtr<HeaderList> create(JS::VM&);
  36. [[nodiscard]] bool contains(ReadonlyBytes) const;
  37. [[nodiscard]] ErrorOr<Optional<ByteBuffer>> get(ReadonlyBytes) const;
  38. [[nodiscard]] ErrorOr<Optional<Vector<String>>> get_decode_and_split(ReadonlyBytes) const;
  39. [[nodiscard]] ErrorOr<void> append(Header);
  40. void delete_(ReadonlyBytes name);
  41. [[nodiscard]] ErrorOr<void> set(Header);
  42. [[nodiscard]] ErrorOr<void> combine(Header);
  43. [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
  44. [[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
  45. };
  46. struct RangeHeaderValue {
  47. Optional<u64> start;
  48. Optional<u64> end;
  49. };
  50. [[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);
  51. [[nodiscard]] bool is_header_name(ReadonlyBytes);
  52. [[nodiscard]] bool is_header_value(ReadonlyBytes);
  53. [[nodiscard]] ErrorOr<ByteBuffer> normalize_header_value(ReadonlyBytes);
  54. [[nodiscard]] bool is_cors_safelisted_request_header(Header const&);
  55. [[nodiscard]] bool is_cors_unsafe_request_header_byte(u8);
  56. [[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> get_cors_unsafe_header_names(HeaderList const&);
  57. [[nodiscard]] bool is_cors_non_wildcard_request_header_name(ReadonlyBytes);
  58. [[nodiscard]] bool is_privileged_no_cors_request_header_name(ReadonlyBytes);
  59. [[nodiscard]] bool is_cors_safelisted_response_header_name(ReadonlyBytes, Span<ReadonlyBytes>);
  60. [[nodiscard]] bool is_no_cors_safelisted_request_header_name(ReadonlyBytes);
  61. [[nodiscard]] bool is_no_cors_safelisted_request_header(Header const&);
  62. [[nodiscard]] bool is_forbidden_header_name(ReadonlyBytes);
  63. [[nodiscard]] bool is_forbidden_response_header_name(ReadonlyBytes);
  64. [[nodiscard]] bool is_request_body_header_name(ReadonlyBytes);
  65. [[nodiscard]] Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes);
  66. }