Headers.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init);
  28. virtual ~Headers() override;
  29. WebIDL::ExceptionOr<void> append(Infrastructure::Header);
  30. WebIDL::ExceptionOr<void> append(String const& name, String const& value);
  31. WebIDL::ExceptionOr<void> delete_(String const& name);
  32. WebIDL::ExceptionOr<String> get(String const& name);
  33. WebIDL::ExceptionOr<bool> has(String const& name);
  34. WebIDL::ExceptionOr<void> set(String const& name, String const& value);
  35. using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
  36. JS::ThrowCompletionOr<void> for_each(ForEachCallback);
  37. private:
  38. friend class HeadersIterator;
  39. explicit Headers(HTML::Window&);
  40. WebIDL::ExceptionOr<void> fill(HeadersInit const&);
  41. void remove_privileged_no_cors_headers();
  42. // https://fetch.spec.whatwg.org/#concept-headers-header-list
  43. // A Headers object has an associated header list (a header list), which is initially empty.
  44. Infrastructure::HeaderList m_header_list;
  45. // https://fetch.spec.whatwg.org/#concept-headers-guard
  46. // 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".
  47. Guard m_guard { Guard::None };
  48. };
  49. }