Responses.cpp 12 KB

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