Headers.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/HashMap.h>
  8. #include <AK/String.h>
  9. #include <AK/Variant.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Heap/GCPtr.h>
  13. #include <LibWeb/Bindings/PlatformObject.h>
  14. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::Fetch {
  17. using HeadersInit = Variant<Vector<Vector<String>>, OrderedHashMap<String, String>>;
  18. // https://fetch.spec.whatwg.org/#headers-class
  19. class Headers final : public Bindings::PlatformObject {
  20. WEB_PLATFORM_OBJECT(Headers, Bindings::PlatformObject);
  21. JS_DECLARE_ALLOCATOR(Headers);
  22. public:
  23. enum class Guard {
  24. Immutable,
  25. Request,
  26. RequestNoCORS,
  27. Response,
  28. None,
  29. };
  30. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init);
  31. virtual ~Headers() override;
  32. [[nodiscard]] JS::NonnullGCPtr<Infrastructure::HeaderList> header_list() const { return m_header_list; }
  33. void set_header_list(JS::NonnullGCPtr<Infrastructure::HeaderList> header_list) { m_header_list = header_list; }
  34. [[nodiscard]] Guard guard() const { return m_guard; }
  35. void set_guard(Guard guard) { m_guard = guard; }
  36. WebIDL::ExceptionOr<void> fill(HeadersInit const&);
  37. WebIDL::ExceptionOr<void> append(Infrastructure::Header);
  38. // JS API functions
  39. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  40. WebIDL::ExceptionOr<void> delete_(String const& name);
  41. WebIDL::ExceptionOr<Optional<String>> get(String const& name);
  42. WebIDL::ExceptionOr<Vector<String>> get_set_cookie();
  43. WebIDL::ExceptionOr<bool> has(String const& name);
  44. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  45. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
  46. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  47. private:
  48. friend class HeadersIterator;
  49. Headers(JS::Realm&, JS::NonnullGCPtr<Infrastructure::HeaderList>);
  50. virtual void initialize(JS::Realm&) override;
  51. virtual void visit_edges(JS::Cell::Visitor&) override;
  52. WebIDL::ExceptionOr<bool> validate(Infrastructure::Header const&) const;
  53. void remove_privileged_no_cors_request_headers();
  54. // https://fetch.spec.whatwg.org/#concept-headers-header-list
  55. // A Headers object has an associated header list (a header list), which is initially empty.
  56. JS::NonnullGCPtr<Infrastructure::HeaderList> m_header_list;
  57. // https://fetch.spec.whatwg.org/#concept-headers-guard
  58. // A Headers object also has an associated guard, which is a headers guard. A headers guard is "immutable", "request", "request-no-cors", "response" or "none".
  59. Guard m_guard { Guard::None };
  60. };
  61. }