Responses.cpp 13 KB

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