Response.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public:
  30. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> create(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>, Headers::Guard);
  31. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> construct_impl(JS::Realm&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
  32. virtual ~Response() override;
  33. // ^BodyMixin
  34. virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
  35. virtual Optional<Infrastructure::Body&> body_impl() override;
  36. virtual Optional<Infrastructure::Body const&> body_impl() const override;
  37. virtual Bindings::PlatformObject& as_platform_object() override { return *this; }
  38. virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; }
  39. [[nodiscard]] JS::NonnullGCPtr<Infrastructure::Response> response() const { return m_response; }
  40. // JS API functions
  41. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> error(JS::VM&);
  42. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, String const& url, u16 status);
  43. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
  44. [[nodiscard]] Bindings::ResponseType type() const;
  45. [[nodiscard]] WebIDL::ExceptionOr<String> url() const;
  46. [[nodiscard]] bool redirected() const;
  47. [[nodiscard]] u16 status() const;
  48. [[nodiscard]] bool ok() const;
  49. [[nodiscard]] WebIDL::ExceptionOr<String> status_text() const;
  50. [[nodiscard]] JS::NonnullGCPtr<Headers> headers() const;
  51. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone() const;
  52. // Pull in json() from the BodyMixin, which gets lost due to the static json() above
  53. using BodyMixin::json;
  54. private:
  55. Response(JS::Realm&, JS::NonnullGCPtr<Infrastructure::Response>);
  56. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  57. virtual void visit_edges(Cell::Visitor&) override;
  58. WebIDL::ExceptionOr<void> initialize_response(ResponseInit const&, Optional<Infrastructure::BodyWithType> const&);
  59. // https://fetch.spec.whatwg.org/#concept-response-response
  60. // A Response object has an associated response (a response).
  61. JS::NonnullGCPtr<Infrastructure::Response> m_response;
  62. // https://fetch.spec.whatwg.org/#response-headers
  63. // A Response object also has an associated headers (null or a Headers object), initially null.
  64. JS::GCPtr<Headers> m_headers;
  65. };
  66. }