Responses.cpp 11 KB

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