Headers.h 2.6 KB

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