Responses.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // FIXME: 2. Let location be the result of extracting header list values given `Location` and response’s header list.
  85. auto location_value = ByteBuffer {};
  86. // 3. If location is a header value, then set location to the result of parsing location with response’s URL.
  87. auto location = AK::URL { StringView { location_value } };
  88. if (!location.is_valid())
  89. return Error::from_string_view("Invalid 'Location' header URL"sv);
  90. // 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
  91. if (location.fragment().is_null())
  92. location.set_fragment(request_fragment.value_or({}));
  93. // 5. Return location.
  94. return location;
  95. }
  96. // https://fetch.spec.whatwg.org/#concept-response-clone
  97. WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::VM& vm) const
  98. {
  99. // To clone a response response, run these steps:
  100. auto& realm = *vm.current_realm();
  101. // 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.
  102. if (is<FilteredResponse>(*this)) {
  103. auto internal_response = TRY(static_cast<FilteredResponse const&>(*this).internal_response()->clone(vm));
  104. if (is<BasicFilteredResponse>(*this))
  105. return TRY_OR_RETURN_OOM(realm, BasicFilteredResponse::create(vm, internal_response));
  106. if (is<CORSFilteredResponse>(*this))
  107. return TRY_OR_RETURN_OOM(realm, CORSFilteredResponse::create(vm, internal_response));
  108. if (is<OpaqueFilteredResponse>(*this))
  109. return OpaqueFilteredResponse::create(vm, internal_response);
  110. if (is<OpaqueRedirectFilteredResponse>(*this))
  111. return OpaqueRedirectFilteredResponse::create(vm, internal_response);
  112. VERIFY_NOT_REACHED();
  113. }
  114. // 2. Let newResponse be a copy of response, except for its body.
  115. auto new_response = Infrastructure::Response::create(vm);
  116. new_response->set_type(m_type);
  117. new_response->set_aborted(m_aborted);
  118. new_response->set_url_list(m_url_list);
  119. new_response->set_status(m_status);
  120. new_response->set_status_message(m_status_message);
  121. for (auto const& header : *m_header_list)
  122. MUST(new_response->header_list()->append(header));
  123. new_response->set_cache_state(m_cache_state);
  124. new_response->set_cors_exposed_header_name_list(m_cors_exposed_header_name_list);
  125. new_response->set_range_requested(m_range_requested);
  126. new_response->set_request_includes_credentials(m_request_includes_credentials);
  127. new_response->set_timing_allow_passed(m_timing_allow_passed);
  128. new_response->set_body_info(m_body_info);
  129. // FIXME: service worker timing info
  130. // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
  131. if (m_body.has_value())
  132. new_response->set_body(TRY(m_body->clone()));
  133. // 4. Return newResponse.
  134. return new_response;
  135. }
  136. FilteredResponse::FilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  137. : Response(header_list)
  138. , m_internal_response(internal_response)
  139. {
  140. }
  141. FilteredResponse::~FilteredResponse()
  142. {
  143. }
  144. void FilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  145. {
  146. Base::visit_edges(visitor);
  147. visitor.visit(m_internal_response);
  148. }
  149. ErrorOr<JS::NonnullGCPtr<BasicFilteredResponse>> BasicFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  150. {
  151. // A basic filtered response is a filtered response whose type is "basic" and header list excludes
  152. // any headers in internal response’s header list whose name is a forbidden response-header name.
  153. auto header_list = HeaderList::create(vm);
  154. for (auto const& header : *internal_response->header_list()) {
  155. if (!is_forbidden_response_header_name(header.name))
  156. TRY(header_list->append(header));
  157. }
  158. return { *vm.heap().allocate_without_realm<BasicFilteredResponse>(internal_response, header_list) };
  159. }
  160. BasicFilteredResponse::BasicFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  161. : FilteredResponse(internal_response, header_list)
  162. , m_header_list(header_list)
  163. {
  164. }
  165. void BasicFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  166. {
  167. Base::visit_edges(visitor);
  168. visitor.visit(m_header_list);
  169. }
  170. ErrorOr<JS::NonnullGCPtr<CORSFilteredResponse>> CORSFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  171. {
  172. // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
  173. // any headers in internal response’s header list whose name is not a CORS-safelisted response-header
  174. // name, given internal response’s CORS-exposed header-name list.
  175. Vector<ReadonlyBytes> cors_exposed_header_name_list;
  176. for (auto const& header_name : internal_response->cors_exposed_header_name_list())
  177. cors_exposed_header_name_list.append(header_name.span());
  178. auto header_list = HeaderList::create(vm);
  179. for (auto const& header : *internal_response->header_list()) {
  180. if (is_cors_safelisted_response_header_name(header.name, cors_exposed_header_name_list))
  181. TRY(header_list->append(header));
  182. }
  183. return { *vm.heap().allocate_without_realm<CORSFilteredResponse>(internal_response, header_list) };
  184. }
  185. CORSFilteredResponse::CORSFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  186. : FilteredResponse(internal_response, header_list)
  187. , m_header_list(header_list)
  188. {
  189. }
  190. void CORSFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  191. {
  192. Base::visit_edges(visitor);
  193. visitor.visit(m_header_list);
  194. }
  195. JS::NonnullGCPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  196. {
  197. // An opaque filtered response is a filtered response whose type is "opaque", URL list is the empty list,
  198. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  199. return { *vm.heap().allocate_without_realm<OpaqueFilteredResponse>(internal_response, HeaderList::create(vm)) };
  200. }
  201. OpaqueFilteredResponse::OpaqueFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  202. : FilteredResponse(internal_response, header_list)
  203. , m_header_list(header_list)
  204. {
  205. }
  206. void OpaqueFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  207. {
  208. Base::visit_edges(visitor);
  209. visitor.visit(m_header_list);
  210. }
  211. JS::NonnullGCPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(JS::VM& vm, JS::NonnullGCPtr<Response> internal_response)
  212. {
  213. // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
  214. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
  215. return { *vm.heap().allocate_without_realm<OpaqueRedirectFilteredResponse>(internal_response, HeaderList::create(vm)) };
  216. }
  217. OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
  218. : FilteredResponse(internal_response, header_list)
  219. , m_header_list(header_list)
  220. {
  221. }
  222. void OpaqueRedirectFilteredResponse::visit_edges(JS::Cell::Visitor& visitor)
  223. {
  224. Base::visit_edges(visitor);
  225. visitor.visit(m_header_list);
  226. }
  227. }