Response.h 2.8 KB

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