Response.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/Forward.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Bindings/RequestPrototype.h>
  12. #include <LibWeb/Fetch/Body.h>
  13. #include <LibWeb/Fetch/BodyInit.h>
  14. #include <LibWeb/Fetch/Headers.h>
  15. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  16. #include <LibWeb/Forward.h>
  17. namespace Web::Fetch {
  18. // https://fetch.spec.whatwg.org/#responseinit
  19. struct ResponseInit {
  20. u16 status;
  21. String status_text;
  22. Optional<HeadersInit> headers;
  23. };
  24. // https://fetch.spec.whatwg.org/#response
  25. class Response final
  26. : public Bindings::PlatformObject
  27. , public BodyMixin {
  28. WEB_PLATFORM_OBJECT(Response, Bindings::PlatformObject);
  29. JS_DECLARE_ALLOCATOR(Response);
  30. public:
  31. [[nodiscard]] static JS::NonnullGCPtr<Response> create(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>, Headers::Guard);
  32. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> construct_impl(JS::Realm&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
  33. virtual ~Response() override;
  34. // ^BodyMixin
  35. virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
  36. virtual JS::GCPtr<Infrastructure::Body> body_impl() override;
  37. virtual JS::GCPtr<Infrastructure::Body const> body_impl() const override;
  38. virtual Bindings::PlatformObject& as_platform_object() override { return *this; }
  39. virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; }
  40. [[nodiscard]] JS::NonnullGCPtr<Infrastructure::Response> response() const { return m_response; }
  41. // JS API functions
  42. [[nodiscard]] static JS::NonnullGCPtr<Response> error(JS::VM&);
  43. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, String const& url, u16 status);
  44. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
  45. [[nodiscard]] Bindings::ResponseType type() const;
  46. [[nodiscard]] WebIDL::ExceptionOr<String> url() const;
  47. [[nodiscard]] bool redirected() const;
  48. [[nodiscard]] u16 status() const;
  49. [[nodiscard]] bool ok() const;
  50. [[nodiscard]] WebIDL::ExceptionOr<String> status_text() const;
  51. [[nodiscard]] JS::NonnullGCPtr<Headers> headers() const;
  52. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone() const;
  53. // Pull in json() from the BodyMixin, which gets lost due to the static json() above
  54. using BodyMixin::json;
  55. private:
  56. Response(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>);
  57. virtual void initialize(JS::Realm&) override;
  58. virtual void visit_edges(Cell::Visitor&) override;
  59. WebIDL::ExceptionOr<void> initialize_response(ResponseInit const&, Optional<Infrastructure::BodyWithType> const&);
  60. // https://fetch.spec.whatwg.org/#concept-response-response
  61. // A Response object has an associated response (a response).
  62. JS::NonnullGCPtr<Infrastructure::Response> m_response;
  63. // https://fetch.spec.whatwg.org/#response-headers
  64. // A Response object also has an associated headers (null or a Headers object), initially null.
  65. JS::GCPtr<Headers> m_headers;
  66. };
  67. }