Responses.cpp 11 KB

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