Headers.h 2.3 KB

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