Responses.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Heap/Heap.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/VM.h>
  11. #include <LibWeb/Bindings/MainThreadVM.h>
  12. #include <LibWeb/Fetch/Infrastructure/FetchParams.h>
  13. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  14. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  15. #include <LibWeb/URL/URL.h>
  16. namespace Web::Fetch::Infrastructure {
  17. Response::Response(JS::NonnullGCPtr<HeaderList> header_list)
  18. : m_header_list(header_list)
  19. {
  20. }
  21. void Response::visit_edges(JS::Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. visitor.visit(m_header_list);
  25. visitor.visit(m_body);
  26. }
  27. JS::NonnullGCPtr<Response> Response::create(JS::VM& vm)
  28. {
  29. return vm.heap().allocate_without_realm<Response>(HeaderList::create(vm));
  30. }
  31. // https://fetch.spec.whatwg.org/#ref-for-concept-network-error%E2%91%A3
  32. // A network error is a response whose status is always 0, status message is always
  33. // the empty byte sequence, header list is always empty, and body is always null.
  34. JS::NonnullGCPtr<Response> Response::aborted_network_error(JS::VM& vm)
  35. {
  36. auto response = network_error(vm, "Fetch has been aborted"sv);
  37. response->set_aborted(true);
  38. return response;
  39. }
  40. JS::NonnullGCPtr<Response> Response::network_error(JS::VM& vm, Variant<String, StringView> message)
  41. {
  42. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Creating network error response with message: {}", message.visit([](auto const& s) -> StringView { return s; }));
  43. auto response = Response::create(vm);
  44. response->set_status(0);
  45. response->set_type(Type::Error);
  46. VERIFY(!response->body());
  47. response->m_network_error_message = move(message);
  48. return response;
  49. }
  50. // https://fetch.spec.whatwg.org/#appropriate-network-error
  51. JS::NonnullGCPtr<Response> Response::appropriate_network_error(JS::VM& vm, FetchParams const& fetch_params)
  52. {
  53. // 1. Assert: fetchParams is canceled.
  54. VERIFY(fetch_params.is_canceled());
  55. // 2. Return an aborted network error if fetchParams is aborted; otherwise return a network error.
  56. return fetch_params.is_aborted()
  57. ? aborted_network_error(vm)
  58. : network_error(vm, "Fetch has been terminated"sv);
  59. }
  60. // https://fetch.spec.whatwg.org/#concept-aborted-network-error
  61. bool Response::is_aborted_network_error() const
  62. {
  63. // A response whose type is "error" and aborted flag is set is known as an aborted network error.
  64. // NOTE: We have to use the virtual getter here to not bypass filtered responses.
  65. return type() == Type::Error && aborted();
  66. }
  67. // https://fetch.spec.whatwg.org/#concept-network-error
  68. bool Response::is_network_error() const
  69. {
  70. // A response whose type is "error" is known as a network error.
  71. // NOTE: We have to use the virtual getter here to not bypass filtered responses.
  72. return type() == Type::Error;
  73. }
  74. // https://fetch.spec.whatwg.org/#concept-response-url
  75. Optional<AK::URL const&> Response::url() const
  76. {
  77. // A response has an associated URL. It is a pointer to the last URL in response’s URL list and null if response’s URL list is empty.
  78. // NOTE: We have to use the virtual getter here to not bypass filtered responses.
  79. if (url_list().is_empty())
  80. return {};
  81. return url_list().last();
  82. }
  83. // https://fetch.spec.whatwg.org/#concept-response-location-url
  84. ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& request_fragment) const
  85. {
  86. // The location URL of a response response, given null or an ASCII string requestFragment, is the value returned by the following steps. They return null, failure, or a URL.
  87. // 1. If response’s status is not a redirect status, then return null.
  88. // NOTE: We have to use the virtual getter here to not bypass filtered responses.
  89. if (!is_redirect_status(status()))
  90. return Optional<AK::URL> {};
  91. // 2. Let location be the result of extracting header list values given `Location` and response’s header list.
  92. auto location_values_or_failure = TRY(extract_header_list_values("Location"sv.bytes(), m_header_list));
  93. if (location_values_or_failure.has<Infrastructure::ExtractHeaderParseFailure>() || location_values_or_failure.has<Empty>())
  94. return Optional<AK::URL> {};
  95. auto const& location_values = location_values_or_failure.get<Vector<ByteBuffer>>();
  96. if (location_values.size() != 1)
  97. return Optional<AK::URL> {};
  98. // 3. If location is a header value, then set location to the result of parsing location with response’s URL.
  99. auto location = URL::parse(location_values.first(), url());
  100. if (!location.is_valid())
  101. return Error::from_string_view("Invalid 'Location' header URL"sv);
  102. // 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
  103. if (!location.fragment().has_value())
  104. location.set_fragment(request_fragment);
  105. // 5. Return location.
  106. return location;
  107. }
  108. // https://fetch.spec.whatwg.org/#concept-response-clone
  109. WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::Realm& realm) const
  110. {
  111. // To clone a response response, run these steps:
  112. auto& vm = realm.vm();
  113. // 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of response’s internal response.
  114. if (is<FilteredResponse>(*this)) {
  115. auto internal_response = TRY(static_cast<FilteredResponse const&>(*this).internal_response()->clone(realm));
  116. if (is<BasicFilteredResponse>(*this))
  117. return TRY_OR_THROW_OOM(vm, BasicFilteredResponse::create(vm, internal_response));
  118. if (is<CORSFilteredResponse>(*this))
  119. return TRY_OR_THROW_OOM(vm, CORSFilteredResponse::create(vm, internal_response));
  120. if (is<OpaqueFilteredResponse>(*this))
  121. return OpaqueFilteredResponse::create(vm, internal_response);
  122. if (is<OpaqueRedirectFilteredResponse>(*this))
  123. return OpaqueRedirectFilteredResponse::create(vm, internal_response);
  124. VERIFY_NOT_REACHED();
  125. }
  126. // 2. Let newResponse be a copy of response, except for its body.
  127. auto new_response = Infrastructure::Response::create(vm);
  128. new_response->set_type(m_type);
  129. new_response->set_aborted(m_aborted);
  130. new_response->set_url_list(m_url_list);
  131. new_response->set_status(m_status);
  132. new_response->set_status_message(m_status_message);
  133. for (auto const& header : *m_header_list)
  134. MUST(new_response->header_list()->append(header));
  135. new_response->set_cache_state(m_cache_state);
  136. new_response->set_cors_exposed_header_name_list(m_cors_exposed_header_name_list);
  137. new_response->set_range_requested(m_range_requested);
  138. new_response->set_request_includes_credentials(m_request_includes_credentials);
  139. new_response->set_timing_allow_passed(m_timing_allow_passed);
  140. new_response->set_body_info(m_body_info);
  141. // FIXME: service worker timing info
  142. // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
  143. if (m_body)
  144. new_response->set_body(m_body->clone(realm));
  145. // 4. Return newResponse.
  146. return new_response;
  147. }
  148. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#unsafe-response
  149. JS::NonnullGCPtr<Response> Response::unsafe_response()
  150. {
  151. // A response's unsafe response is its internal response if it has one, and the response itself otherwise.
  152. if (is<FilteredResponse>(this))
  153. return static_cast<FilteredResponse&>(*this).internal_response();
  154. return *this;
  155. }
  156. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-cross-origin
  157. bool Response::is_cors_cross_origin() const
  158. {
  159. // A response whose type is "opaque" or "opaqueredirect" is CORS-cross-origin.
  160. return type() == Type::Opaque || type() == Type::OpaqueRedirect;
  161. }
  162. // Non-standard
  163. Optional<StringView> Response::network_error_message() const
  164. {
  165. if (!m_network_error_message.has_value())
  166. return {};
  167. return m_network_error_message->visit([](auto const& s) -> StringView { return s; });
  168. }
  169. FilteredResponse::FilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  170. : Response(header_list)
  171. , m_internal_response(internal_response)
  172. {
  173. }
  174. FilteredResponse::~FilteredResponse()
  175. {
  176. }
  177. void FilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  178. {
  179. Base::visit_edges(visitor);
  180. visitor.visit(m_internal_response);
  181. }
  182. ErrorOr<JS::NonnullGCPtr<BasicFilteredResponse>> BasicFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  183. {
  184. // A basic filtered response is a filtered response whose type is "basic" and header list excludes
  185. // any headers in internal response’s header list whose name is a forbidden response-header name.
  186. auto header_list = HeaderList::create(vm);
  187. for (auto const& header : *internal_response->header_list()) {
  188. if (!is_forbidden_response_header_name(header.name))
  189. TRY(header_list->append(header));
  190. }
  191. return vm.heap().allocate_without_realm<BasicFilteredResponse>(internal_response, header_list);
  192. }
  193. BasicFilteredResponse::BasicFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  194. : FilteredResponse(internal_response, header_list)
  195. , m_header_list(header_list)
  196. {
  197. }
  198. void BasicFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  199. {
  200. Base::visit_edges(visitor);
  201. visitor.visit(m_header_list);
  202. }
  203. ErrorOr<JS::NonnullGCPtr<CORSFilteredResponse>> CORSFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  204. {
  205. // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
  206. // any headers in internal response’s header list whose name is not a CORS-safelisted response-header
  207. // name, given internal response’s CORS-exposed header-name list.
  208. Vector<ReadonlyBytes> cors_exposed_header_name_list;
  209. for (auto const& header_name : internal_response->cors_exposed_header_name_list())
  210. cors_exposed_header_name_list.append(header_name.span());
  211. auto header_list = HeaderList::create(vm);
  212. for (auto const& header : *internal_response->header_list()) {
  213. if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
  214. TRY(header_list->append(header));
  215. }
  216. return vm.heap().allocate_without_realm<CORSFilteredResponse>(internal_response, header_list);
  217. }
  218. CORSFilteredResponse::CORSFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  219. : FilteredResponse(internal_response, header_list)
  220. , m_header_list(header_list)
  221. {
  222. }
  223. void CORSFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  224. {
  225. Base::visit_edges(visitor);
  226. visitor.visit(m_header_list);
  227. }
  228. JS::NonnullGCPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  229. {
  230. // An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list,
  231. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  232. return vm.heap().allocate_without_realm<OpaqueFilteredResponse>(internal_response, HeaderList::create(vm));
  233. }
  234. OpaqueFilteredResponse::OpaqueFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  235. : FilteredResponse(internal_response, header_list)
  236. , m_header_list(header_list)
  237. {
  238. }
  239. void OpaqueFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  240. {
  241. Base::visit_edges(visitor);
  242. visitor.visit(m_header_list);
  243. visitor.visit(m_body);
  244. }
  245. JS::NonnullGCPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  246. {
  247. // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
  248. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  249. return vm.heap().allocate_without_realm<OpaqueRedirectFilteredResponse>(internal_response, HeaderList::create(vm));
  250. }
  251. OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  252. : FilteredResponse(internal_response, header_list)
  253. , m_header_list(header_list)
  254. {
  255. }
  256. void OpaqueRedirectFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  257. {
  258. Base::visit_edges(visitor);
  259. visitor.visit(m_header_list);
  260. visitor.visit(m_body);
  261. }
  262. }