Responses.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <AK/URLParser.h>
  9. #include <LibJS/Heap/Heap.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/VM.h>
  12. #include <LibWeb/Bindings/MainThreadVM.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. 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 = AK::URLParser::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. // Non-standard
  148. Optional<StringView> Response::network_error_message() const
  149. {
  150. if (!m_network_error_message.has_value())
  151. return {};
  152. return m_network_error_message->visit([](auto const& s) -> StringView { return s; });
  153. }
  154. FilteredResponse::FilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  155. : Response(header_list)
  156. , m_internal_response(internal_response)
  157. {
  158. }
  159. FilteredResponse::~FilteredResponse()
  160. {
  161. }
  162. void FilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  163. {
  164. Base::visit_edges(visitor);
  165. visitor.visit(m_internal_response);
  166. }
  167. ErrorOr<JS::NonnullGCPtr<BasicFilteredResponse>> BasicFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  168. {
  169. // A basic filtered response is a filtered response whose type is "basic" and header list excludes
  170. // any headers in internal response’s header list whose name is a forbidden response-header name.
  171. auto header_list = HeaderList::create(vm);
  172. for (auto const& header : *internal_response->header_list()) {
  173. if (!is_forbidden_response_header_name(header.name))
  174. TRY(header_list->append(header));
  175. }
  176. return vm.heap().allocate_without_realm<BasicFilteredResponse>(internal_response, header_list);
  177. }
  178. BasicFilteredResponse::BasicFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  179. : FilteredResponse(internal_response, header_list)
  180. , m_header_list(header_list)
  181. {
  182. }
  183. void BasicFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  184. {
  185. Base::visit_edges(visitor);
  186. visitor.visit(m_header_list);
  187. }
  188. ErrorOr<JS::NonnullGCPtr<CORSFilteredResponse>> CORSFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  189. {
  190. // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
  191. // any headers in internal response’s header list whose name is not a CORS-safelisted response-header
  192. // name, given internal response’s CORS-exposed header-name list.
  193. Vector<ReadonlyBytes> cors_exposed_header_name_list;
  194. for (auto const& header_name : internal_response->cors_exposed_header_name_list())
  195. cors_exposed_header_name_list.append(header_name.span());
  196. auto header_list = HeaderList::create(vm);
  197. for (auto const& header : *internal_response->header_list()) {
  198. if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
  199. TRY(header_list->append(header));
  200. }
  201. return vm.heap().allocate_without_realm<CORSFilteredResponse>(internal_response, header_list);
  202. }
  203. CORSFilteredResponse::CORSFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  204. : FilteredResponse(internal_response, header_list)
  205. , m_header_list(header_list)
  206. {
  207. }
  208. void CORSFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  209. {
  210. Base::visit_edges(visitor);
  211. visitor.visit(m_header_list);
  212. }
  213. JS::NonnullGCPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  214. {
  215. // An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list,
  216. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  217. return vm.heap().allocate_without_realm<OpaqueFilteredResponse>(internal_response, HeaderList::create(vm));
  218. }
  219. OpaqueFilteredResponse::OpaqueFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  220. : FilteredResponse(internal_response, header_list)
  221. , m_header_list(header_list)
  222. {
  223. }
  224. void OpaqueFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  225. {
  226. Base::visit_edges(visitor);
  227. visitor.visit(m_header_list);
  228. }
  229. JS::NonnullGCPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  230. {
  231. // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
  232. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  233. return vm.heap().allocate_without_realm<OpaqueRedirectFilteredResponse>(internal_response, HeaderList::create(vm));
  234. }
  235. OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  236. : FilteredResponse(internal_response, header_list)
  237. , m_header_list(header_list)
  238. {
  239. }
  240. void OpaqueRedirectFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  241. {
  242. Base::visit_edges(visitor);
  243. visitor.visit(m_header_list);
  244. }
  245. }