Responses.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/ByteBuffer.h>
  8. #include <AK/Error.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Optional.h>
  11. #include <AK/URL.h>
  12. #include <AK/Vector.h>
  13. #include <LibJS/Forward.h>
  14. #include <LibJS/Heap/Cell.h>
  15. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  16. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  17. #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
  18. namespace Web::Fetch::Infrastructure {
  19. // https://fetch.spec.whatwg.org/#concept-response
  20. class Response : public JS::Cell {
  21. JS_CELL(Response, JS::Cell);
  22. public:
  23. enum class CacheState {
  24. Local,
  25. Validated,
  26. };
  27. enum class Type {
  28. Basic,
  29. CORS,
  30. Default,
  31. Error,
  32. Opaque,
  33. OpaqueRedirect,
  34. };
  35. // https://fetch.spec.whatwg.org/#response-body-info
  36. struct BodyInfo {
  37. // https://fetch.spec.whatwg.org/#fetch-timing-info-encoded-body-size
  38. u64 encoded_size { 0 };
  39. // https://fetch.spec.whatwg.org/#fetch-timing-info-decoded-body-size
  40. u64 decoded_size { 0 };
  41. };
  42. [[nodiscard]] static JS::NonnullGCPtr<Response> create(JS::VM&);
  43. [[nodiscard]] static JS::NonnullGCPtr<Response> aborted_network_error(JS::VM&);
  44. [[nodiscard]] static JS::NonnullGCPtr<Response> network_error(JS::VM&, DeprecatedString message);
  45. [[nodiscard]] static JS::NonnullGCPtr<Response> appropriate_network_error(JS::VM&, FetchParams const&);
  46. virtual ~Response() = default;
  47. [[nodiscard]] virtual Type type() const { return m_type; }
  48. void set_type(Type type) { m_type = type; }
  49. [[nodiscard]] virtual bool aborted() const { return m_aborted; }
  50. void set_aborted(bool aborted) { m_aborted = aborted; }
  51. [[nodiscard]] virtual Vector<AK::URL> const& url_list() const { return m_url_list; }
  52. [[nodiscard]] virtual Vector<AK::URL>& url_list() { return m_url_list; }
  53. void set_url_list(Vector<AK::URL> url_list) { m_url_list = move(url_list); }
  54. [[nodiscard]] virtual Status status() const { return m_status; }
  55. void set_status(Status status) { m_status = status; }
  56. [[nodiscard]] virtual ReadonlyBytes status_message() const { return m_status_message; }
  57. void set_status_message(ByteBuffer status_message) { m_status_message = move(status_message); }
  58. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const { return m_header_list; }
  59. void set_header_list(JS::NonnullGCPtr<HeaderList> header_list) { m_header_list = header_list; }
  60. [[nodiscard]] virtual Optional<Body> const& body() const { return m_body; }
  61. [[nodiscard]] virtual Optional<Body>& body() { return m_body; }
  62. void set_body(Optional<Body> body) { m_body = move(body); }
  63. [[nodiscard]] virtual Optional<CacheState> const& cache_state() const { return m_cache_state; }
  64. void set_cache_state(Optional<CacheState> cache_state) { m_cache_state = move(cache_state); }
  65. [[nodiscard]] virtual Vector<ByteBuffer> const& cors_exposed_header_name_list() const { return m_cors_exposed_header_name_list; }
  66. void set_cors_exposed_header_name_list(Vector<ByteBuffer> cors_exposed_header_name_list) { m_cors_exposed_header_name_list = move(cors_exposed_header_name_list); }
  67. [[nodiscard]] virtual bool range_requested() const { return m_range_requested; }
  68. void set_range_requested(bool range_requested) { m_range_requested = range_requested; }
  69. [[nodiscard]] virtual bool request_includes_credentials() const { return m_request_includes_credentials; }
  70. void set_request_includes_credentials(bool request_includes_credentials) { m_request_includes_credentials = request_includes_credentials; }
  71. [[nodiscard]] virtual bool timing_allow_passed() const { return m_timing_allow_passed; }
  72. void set_timing_allow_passed(bool timing_allow_passed) { m_timing_allow_passed = timing_allow_passed; }
  73. [[nodiscard]] virtual BodyInfo const& body_info() const { return m_body_info; }
  74. void set_body_info(BodyInfo body_info) { m_body_info = body_info; }
  75. [[nodiscard]] bool has_cross_origin_redirects() const { return m_has_cross_origin_redirects; }
  76. void set_has_cross_origin_redirects(bool has_cross_origin_redirects) { m_has_cross_origin_redirects = has_cross_origin_redirects; }
  77. [[nodiscard]] bool is_aborted_network_error() const;
  78. [[nodiscard]] bool is_network_error() const;
  79. [[nodiscard]] Optional<AK::URL const&> url() const;
  80. [[nodiscard]] ErrorOr<Optional<AK::URL>> location_url(Optional<DeprecatedString> const& request_fragment) const;
  81. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone(JS::VM&) const;
  82. // Non-standard
  83. Optional<DeprecatedString> const& network_error_message() const { return m_network_error_message; }
  84. protected:
  85. explicit Response(JS::NonnullGCPtr<HeaderList>);
  86. virtual void visit_edges(JS::Cell::Visitor&) override;
  87. private:
  88. // https://fetch.spec.whatwg.org/#concept-response-type
  89. // A response has an associated type which is "basic", "cors", "default", "error", "opaque", or "opaqueredirect". Unless stated otherwise, it is "default".
  90. Type m_type { Type::Default };
  91. // https://fetch.spec.whatwg.org/#concept-response-aborted
  92. // A response can have an associated aborted flag, which is initially unset.
  93. bool m_aborted { false };
  94. // https://fetch.spec.whatwg.org/#concept-response-url-list
  95. // A response has an associated URL list (a list of zero or more URLs). Unless stated otherwise, it is the empty list.
  96. Vector<AK::URL> m_url_list;
  97. // https://fetch.spec.whatwg.org/#concept-response-status
  98. // A response has an associated status, which is a status. Unless stated otherwise it is 200.
  99. Status m_status { 200 };
  100. // https://fetch.spec.whatwg.org/#concept-response-status-message
  101. // A response has an associated status message. Unless stated otherwise it is the empty byte sequence.
  102. ByteBuffer m_status_message;
  103. // https://fetch.spec.whatwg.org/#concept-response-header-list
  104. // A response has an associated header list (a header list). Unless stated otherwise it is empty.
  105. JS::NonnullGCPtr<HeaderList> m_header_list;
  106. // https://fetch.spec.whatwg.org/#concept-response-body
  107. // A response has an associated body (null or a body). Unless stated otherwise it is null.
  108. Optional<Body> m_body;
  109. // https://fetch.spec.whatwg.org/#concept-response-cache-state
  110. // A response has an associated cache state (the empty string, "local", or "validated"). Unless stated otherwise, it is the empty string.
  111. Optional<CacheState> m_cache_state;
  112. // https://fetch.spec.whatwg.org/#concept-response-cors-exposed-header-name-list
  113. // A response has an associated CORS-exposed header-name list (a list of zero or more header names). The list is empty unless otherwise specified.
  114. Vector<ByteBuffer> m_cors_exposed_header_name_list;
  115. // https://fetch.spec.whatwg.org/#concept-response-range-requested-flag
  116. // A response has an associated range-requested flag, which is initially unset.
  117. bool m_range_requested { false };
  118. // https://fetch.spec.whatwg.org/#response-request-includes-credentials
  119. // A response has an associated request-includes-credentials (a boolean), which is initially true.
  120. bool m_request_includes_credentials { true };
  121. // https://fetch.spec.whatwg.org/#concept-response-timing-allow-passed
  122. // A response has an associated timing allow passed flag, which is initially unset.
  123. bool m_timing_allow_passed { false };
  124. // https://fetch.spec.whatwg.org/#concept-response-body-info
  125. // A response has an associated body info (a response body info). Unless stated otherwise, it is a new response body info.
  126. BodyInfo m_body_info;
  127. // https://fetch.spec.whatwg.org/#response-service-worker-timing-info
  128. // FIXME: A response has an associated service worker timing info (null or a service worker timing info), which is initially null.
  129. // https://fetch.spec.whatwg.org/#response-has-cross-origin-redirects
  130. // A response has an associated has-cross-origin-redirects (a boolean), which is initially false.
  131. bool m_has_cross_origin_redirects { false };
  132. // Non-standard
  133. Optional<DeprecatedString> m_network_error_message;
  134. };
  135. // https://fetch.spec.whatwg.org/#concept-filtered-response
  136. class FilteredResponse : public Response {
  137. JS_CELL(FilteredResponse, Response);
  138. public:
  139. FilteredResponse(JS::NonnullGCPtr<Response>, JS::NonnullGCPtr<HeaderList>);
  140. virtual ~FilteredResponse() = 0;
  141. [[nodiscard]] virtual Type type() const override { return m_internal_response->type(); }
  142. [[nodiscard]] virtual bool aborted() const override { return m_internal_response->aborted(); }
  143. [[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_internal_response->url_list(); }
  144. [[nodiscard]] virtual Vector<AK::URL>& url_list() override { return m_internal_response->url_list(); }
  145. [[nodiscard]] virtual Status status() const override { return m_internal_response->status(); }
  146. [[nodiscard]] virtual ReadonlyBytes status_message() const override { return m_internal_response->status_message(); }
  147. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const override { return m_internal_response->header_list(); }
  148. [[nodiscard]] virtual Optional<Body> const& body() const override { return m_internal_response->body(); }
  149. [[nodiscard]] virtual Optional<Body>& body() override { return m_internal_response->body(); }
  150. [[nodiscard]] virtual Optional<CacheState> const& cache_state() const override { return m_internal_response->cache_state(); }
  151. [[nodiscard]] virtual Vector<ByteBuffer> const& cors_exposed_header_name_list() const override { return m_internal_response->cors_exposed_header_name_list(); }
  152. [[nodiscard]] virtual bool range_requested() const override { return m_internal_response->range_requested(); }
  153. [[nodiscard]] virtual bool request_includes_credentials() const override { return m_internal_response->request_includes_credentials(); }
  154. [[nodiscard]] virtual bool timing_allow_passed() const override { return m_internal_response->timing_allow_passed(); }
  155. [[nodiscard]] virtual BodyInfo const& body_info() const override { return m_internal_response->body_info(); }
  156. [[nodiscard]] JS::NonnullGCPtr<Response> internal_response() const { return m_internal_response; }
  157. protected:
  158. virtual void visit_edges(JS::Cell::Visitor&) override;
  159. private:
  160. // https://fetch.spec.whatwg.org/#concept-internal-response
  161. JS::NonnullGCPtr<Response> m_internal_response;
  162. };
  163. // https://fetch.spec.whatwg.org/#concept-filtered-response-basic
  164. class BasicFilteredResponse final : public FilteredResponse {
  165. JS_CELL(OpaqueRedirectFilteredResponse, FilteredResponse);
  166. public:
  167. [[nodiscard]] static ErrorOr<JS::NonnullGCPtr<BasicFilteredResponse>> create(JS::VM&, JS::NonnullGCPtr<Response>);
  168. [[nodiscard]] virtual Type type() const override { return Type::Basic; }
  169. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const override { return m_header_list; }
  170. private:
  171. BasicFilteredResponse(JS::NonnullGCPtr<Response>, JS::NonnullGCPtr<HeaderList>);
  172. virtual void visit_edges(JS::Cell::Visitor&) override;
  173. JS::NonnullGCPtr<HeaderList> m_header_list;
  174. };
  175. // https://fetch.spec.whatwg.org/#concept-filtered-response-cors
  176. class CORSFilteredResponse final : public FilteredResponse {
  177. JS_CELL(CORSFilteredResponse, FilteredResponse);
  178. public:
  179. [[nodiscard]] static ErrorOr<JS::NonnullGCPtr<CORSFilteredResponse>> create(JS::VM&, JS::NonnullGCPtr<Response>);
  180. [[nodiscard]] virtual Type type() const override { return Type::CORS; }
  181. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const override { return m_header_list; }
  182. private:
  183. CORSFilteredResponse(JS::NonnullGCPtr<Response>, JS::NonnullGCPtr<HeaderList>);
  184. virtual void visit_edges(JS::Cell::Visitor&) override;
  185. JS::NonnullGCPtr<HeaderList> m_header_list;
  186. };
  187. // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
  188. class OpaqueFilteredResponse final : public FilteredResponse {
  189. JS_CELL(OpaqueFilteredResponse, FilteredResponse);
  190. public:
  191. [[nodiscard]] static JS::NonnullGCPtr<OpaqueFilteredResponse> create(JS::VM&, JS::NonnullGCPtr<Response>);
  192. [[nodiscard]] virtual Type type() const override { return Type::Opaque; }
  193. [[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_url_list; }
  194. [[nodiscard]] virtual Vector<AK::URL>& url_list() override { return m_url_list; }
  195. [[nodiscard]] virtual Status status() const override { return 0; }
  196. [[nodiscard]] virtual ReadonlyBytes status_message() const override { return {}; }
  197. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const override { return m_header_list; }
  198. [[nodiscard]] virtual Optional<Body> const& body() const override { return m_body; }
  199. [[nodiscard]] virtual Optional<Body>& body() override { return m_body; }
  200. private:
  201. OpaqueFilteredResponse(JS::NonnullGCPtr<Response>, JS::NonnullGCPtr<HeaderList>);
  202. virtual void visit_edges(JS::Cell::Visitor&) override;
  203. Vector<AK::URL> m_url_list;
  204. JS::NonnullGCPtr<HeaderList> m_header_list;
  205. Optional<Body> m_body;
  206. };
  207. // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect
  208. class OpaqueRedirectFilteredResponse final : public FilteredResponse {
  209. JS_CELL(OpaqueRedirectFilteredResponse, FilteredResponse);
  210. public:
  211. [[nodiscard]] static JS::NonnullGCPtr<OpaqueRedirectFilteredResponse> create(JS::VM&, JS::NonnullGCPtr<Response>);
  212. [[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; }
  213. [[nodiscard]] virtual Status status() const override { return 0; }
  214. [[nodiscard]] virtual ReadonlyBytes status_message() const override { return {}; }
  215. [[nodiscard]] virtual JS::NonnullGCPtr<HeaderList> header_list() const override { return m_header_list; }
  216. [[nodiscard]] virtual Optional<Body> const& body() const override { return m_body; }
  217. [[nodiscard]] virtual Optional<Body>& body() override { return m_body; }
  218. private:
  219. OpaqueRedirectFilteredResponse(JS::NonnullGCPtr<Response>, JS::NonnullGCPtr<HeaderList>);
  220. virtual void visit_edges(JS::Cell::Visitor&) override;
  221. JS::NonnullGCPtr<HeaderList> m_header_list;
  222. Optional<Body> m_body;
  223. };
  224. }