Responses.cpp 10 KB

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