Headers.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public:
  22. enum class Guard {
  23. Immutable,
  24. Request,
  25. RequestNoCORS,
  26. Response,
  27. None,
  28. };
  29. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init);
  30. virtual ~Headers() override;
  31. [[nodiscard]] JS::NonnullGCPtr<Infrastructure::HeaderList> header_list() const { return m_header_list; }
  32. void set_header_list(JS::NonnullGCPtr<Infrastructure::HeaderList> header_list) { m_header_list = header_list; }
  33. [[nodiscard]] Guard guard() const { return m_guard; }
  34. void set_guard(Guard guard) { m_guard = guard; }
  35. WebIDL::ExceptionOr<void> fill(HeadersInit const&);
  36. WebIDL::ExceptionOr<void> append(Infrastructure::Header);
  37. // JS API functions
  38. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  39. WebIDL::ExceptionOr<void> delete_(String const& name);
  40. WebIDL::ExceptionOr<Optional<String>> get(String const& name);
  41. WebIDL::ExceptionOr<Vector<String>> get_set_cookie();
  42. WebIDL::ExceptionOr<bool> has(String const& name);
  43. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  44. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
  45. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  46. private:
  47. friend class HeadersIterator;
  48. Headers(JS::Realm&, JS::NonnullGCPtr<Infrastructure::HeaderList>);
  49. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  50. virtual void visit_edges(JS::Cell::Visitor&) override;
  51. WebIDL::ExceptionOr<bool> validate(Infrastructure::Header const&) const;
  52. void remove_privileged_no_cors_request_headers();
  53. // https://fetch.spec.whatwg.org/#concept-headers-header-list
  54. // A Headers object has an associated header list (a header list), which is initially empty.
  55. JS::NonnullGCPtr<Infrastructure::HeaderList> m_header_list;
  56. // https://fetch.spec.whatwg.org/#concept-headers-guard
  57. // 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".
  58. Guard m_guard { Guard::None };
  59. };
  60. }