Responses.h 13 KB

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