Headers.h 2.7 KB

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