Responses.cpp 12 KB

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