Responses.h 15 KB

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