Headers.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. };
  22. // https://fetch.spec.whatwg.org/#concept-header-list
  23. // A header list is a list of zero or more headers. It is initially the empty list.
  24. class HeaderList final
  25. : public RefCounted<HeaderList>
  26. , Vector<Header> {
  27. public:
  28. using Vector::begin;
  29. using Vector::end;
  30. [[nodiscard]] bool contains(ReadonlyBytes) const;
  31. [[nodiscard]] ErrorOr<Optional<ByteBuffer>> get(ReadonlyBytes) const;
  32. [[nodiscard]] ErrorOr<Optional<Vector<String>>> get_decode_and_split(ReadonlyBytes) const;
  33. [[nodiscard]] ErrorOr<void> append(Header);
  34. void delete_(ReadonlyBytes name);
  35. [[nodiscard]] ErrorOr<void> set(Header);
  36. [[nodiscard]] ErrorOr<void> combine(Header);
  37. [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
  38. [[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
  39. };
  40. [[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);
  41. [[nodiscard]] bool is_header_name(ReadonlyBytes);
  42. [[nodiscard]] bool is_header_value(ReadonlyBytes);
  43. [[nodiscard]] ErrorOr<ByteBuffer> normalize_header_value(ReadonlyBytes);
  44. [[nodiscard]] bool is_cors_safelisted_request_header(Header const&);
  45. [[nodiscard]] bool is_cors_unsafe_request_header_byte(u8);
  46. [[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> get_cors_unsafe_header_names(HeaderList const&);
  47. [[nodiscard]] bool is_cors_non_wildcard_request_header_name(ReadonlyBytes);
  48. [[nodiscard]] bool is_privileged_no_cors_request_header_name(ReadonlyBytes);
  49. [[nodiscard]] bool is_cors_safelisted_response_header_name(ReadonlyBytes, Span<ReadonlyBytes>);
  50. [[nodiscard]] bool is_no_cors_safelisted_request_header_name(ReadonlyBytes);
  51. [[nodiscard]] bool is_no_cors_safelisted_request_header(Header const&);
  52. [[nodiscard]] bool is_forbidden_header_name(ReadonlyBytes);
  53. [[nodiscard]] bool is_forbidden_response_header_name(ReadonlyBytes);
  54. [[nodiscard]] bool is_request_body_header_name(ReadonlyBytes);
  55. [[nodiscard]] bool is_simple_range_header_value(ReadonlyBytes);
  56. }