Responses.cpp 11 KB

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