Requests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  10. namespace Web::Fetch::Infrastructure {
  11. Request::Request(JS::NonnullGCPtr<HeaderList> header_list)
  12. : m_header_list(header_list)
  13. {
  14. }
  15. void Request::visit_edges(JS::Cell::Visitor& visitor)
  16. {
  17. Base::visit_edges(visitor);
  18. visitor.visit(m_header_list);
  19. }
  20. JS::NonnullGCPtr<Request> Request::create(JS::VM& vm)
  21. {
  22. return { *vm.heap().allocate_without_realm<Request>(HeaderList::create(vm)) };
  23. }
  24. // https://fetch.spec.whatwg.org/#concept-request-url
  25. AK::URL& Request::url()
  26. {
  27. // A request has an associated URL (a URL).
  28. // NOTE: Implementations are encouraged to make this a pointer to the first URL in request’s URL list. It is provided as a distinct field solely for the convenience of other standards hooking into Fetch.
  29. VERIFY(!m_url_list.is_empty());
  30. return m_url_list.first();
  31. }
  32. // https://fetch.spec.whatwg.org/#concept-request-url
  33. AK::URL const& Request::url() const
  34. {
  35. return const_cast<Request&>(*this).url();
  36. }
  37. // https://fetch.spec.whatwg.org/#concept-request-current-url
  38. AK::URL& Request::current_url()
  39. {
  40. // A request has an associated current URL. It is a pointer to the last URL in request’s URL list.
  41. VERIFY(!m_url_list.is_empty());
  42. return m_url_list.last();
  43. }
  44. // https://fetch.spec.whatwg.org/#concept-request-current-url
  45. AK::URL const& Request::current_url() const
  46. {
  47. return const_cast<Request&>(*this).current_url();
  48. }
  49. void Request::set_url(AK::URL url)
  50. {
  51. // Sometimes setting the URL and URL list are done as two distinct steps in the spec,
  52. // but since we know the URL is always the URL list's first item and doesn't change later
  53. // on, we can combine them.
  54. if (!m_url_list.is_empty())
  55. m_url_list.clear();
  56. m_url_list.append(move(url));
  57. }
  58. // https://fetch.spec.whatwg.org/#request-destination-script-like
  59. bool Request::destination_is_script_like() const
  60. {
  61. // A request’s destination is script-like if it is "audioworklet", "paintworklet", "script", "serviceworker", "sharedworker", or "worker".
  62. static constexpr Array script_like_destinations = {
  63. Destination::AudioWorklet,
  64. Destination::PaintWorklet,
  65. Destination::Script,
  66. Destination::ServiceWorker,
  67. Destination::SharedWorker,
  68. Destination::Worker,
  69. };
  70. return any_of(script_like_destinations, [this](auto destination) {
  71. return m_destination == destination;
  72. });
  73. }
  74. // https://fetch.spec.whatwg.org/#subresource-request
  75. bool Request::is_subresource_request() const
  76. {
  77. // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string.
  78. static constexpr Array subresource_request_destinations = {
  79. Destination::Audio,
  80. Destination::AudioWorklet,
  81. Destination::Font,
  82. Destination::Image,
  83. Destination::Manifest,
  84. Destination::PaintWorklet,
  85. Destination::Script,
  86. Destination::Style,
  87. Destination::Track,
  88. Destination::Video,
  89. Destination::XSLT,
  90. };
  91. return any_of(subresource_request_destinations, [this](auto destination) {
  92. return m_destination == destination;
  93. }) || !m_destination.has_value();
  94. }
  95. // https://fetch.spec.whatwg.org/#non-subresource-request
  96. bool Request::is_non_subresource_request() const
  97. {
  98. // A non-subresource request is a request whose destination is "document", "embed", "frame", "iframe", "object", "report", "serviceworker", "sharedworker", or "worker".
  99. static constexpr Array non_subresource_request_destinations = {
  100. Destination::Document,
  101. Destination::Embed,
  102. Destination::Frame,
  103. Destination::IFrame,
  104. Destination::Object,
  105. Destination::Report,
  106. Destination::ServiceWorker,
  107. Destination::SharedWorker,
  108. Destination::Worker,
  109. };
  110. return any_of(non_subresource_request_destinations, [this](auto destination) {
  111. return m_destination == destination;
  112. });
  113. }
  114. // https://fetch.spec.whatwg.org/#navigation-request
  115. bool Request::is_navigation_request() const
  116. {
  117. // A navigation request is a request whose destination is "document", "embed", "frame", "iframe", or "object".
  118. static constexpr Array navigation_request_destinations = {
  119. Destination::Document,
  120. Destination::Embed,
  121. Destination::Frame,
  122. Destination::IFrame,
  123. Destination::Object,
  124. };
  125. return any_of(navigation_request_destinations, [this](auto destination) {
  126. return m_destination == destination;
  127. });
  128. }
  129. // https://fetch.spec.whatwg.org/#concept-request-tainted-origin
  130. bool Request::has_redirect_tainted_origin() const
  131. {
  132. // A request request has a redirect-tainted origin if these steps return true:
  133. // 1. Let lastURL be null.
  134. Optional<AK::URL const&> last_url;
  135. // 2. For each url in request’s URL list:
  136. for (auto const& url : m_url_list) {
  137. // 1. If lastURL is null, then set lastURL to url and continue.
  138. if (!last_url.has_value()) {
  139. last_url = url;
  140. continue;
  141. }
  142. // 2. If url’s origin is not same origin with lastURL’s origin and request’s origin is not same origin with lastURL’s origin, then return true.
  143. // FIXME: Actually use the given origins once we have https://url.spec.whatwg.org/#concept-url-origin.
  144. if (!HTML::Origin().is_same_origin(HTML::Origin()) && HTML::Origin().is_same_origin(HTML::Origin()))
  145. return true;
  146. // 3. Set lastURL to url.
  147. last_url = url;
  148. }
  149. // 3. Return false.
  150. return false;
  151. }
  152. // https://fetch.spec.whatwg.org/#serializing-a-request-origin
  153. String Request::serialize_origin() const
  154. {
  155. // 1. If request has a redirect-tainted origin, then return "null".
  156. if (has_redirect_tainted_origin())
  157. return "null"sv;
  158. // 2. Return request’s origin, serialized.
  159. return m_origin.get<HTML::Origin>().serialize();
  160. }
  161. // https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin
  162. ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
  163. {
  164. // Byte-serializing a request origin, given a request request, is to return the result of serializing a request origin with request, isomorphic encoded.
  165. return ByteBuffer::copy(serialize_origin().bytes());
  166. }
  167. // https://fetch.spec.whatwg.org/#concept-request-clone
  168. WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::clone(JS::VM& vm) const
  169. {
  170. // To clone a request request, run these steps:
  171. // 1. Let newRequest be a copy of request, except for its body.
  172. auto new_request = Infrastructure::Request::create(vm);
  173. new_request->set_method(m_method);
  174. new_request->set_local_urls_only(m_local_urls_only);
  175. for (auto const& header : *m_header_list)
  176. MUST(new_request->header_list()->append(header));
  177. new_request->set_unsafe_request(m_unsafe_request);
  178. new_request->set_client(m_client);
  179. new_request->set_reserved_client(m_reserved_client);
  180. new_request->set_replaces_client_id(m_replaces_client_id);
  181. new_request->set_window(m_window);
  182. new_request->set_keepalive(m_keepalive);
  183. new_request->set_initiator_type(m_initiator_type);
  184. new_request->set_service_workers_mode(m_service_workers_mode);
  185. new_request->set_initiator(m_initiator);
  186. new_request->set_destination(m_destination);
  187. new_request->set_priority(m_priority);
  188. new_request->set_origin(m_origin);
  189. new_request->set_policy_container(m_policy_container);
  190. new_request->set_referrer(m_referrer);
  191. new_request->set_referrer_policy(m_referrer_policy);
  192. new_request->set_mode(m_mode);
  193. new_request->set_use_cors_preflight(m_use_cors_preflight);
  194. new_request->set_credentials_mode(m_credentials_mode);
  195. new_request->set_use_url_credentials(m_use_url_credentials);
  196. new_request->set_cache_mode(m_cache_mode);
  197. new_request->set_redirect_mode(m_redirect_mode);
  198. new_request->set_integrity_metadata(m_integrity_metadata);
  199. new_request->set_cryptographic_nonce_metadata(m_cryptographic_nonce_metadata);
  200. new_request->set_parser_metadata(m_parser_metadata);
  201. new_request->set_reload_navigation(m_reload_navigation);
  202. new_request->set_history_navigation(m_history_navigation);
  203. new_request->set_user_activation(m_user_activation);
  204. new_request->set_render_blocking(m_render_blocking);
  205. new_request->set_url_list(m_url_list);
  206. new_request->set_redirect_count(m_redirect_count);
  207. new_request->set_response_tainting(m_response_tainting);
  208. new_request->set_prevent_no_cache_cache_control_header_modification(m_prevent_no_cache_cache_control_header_modification);
  209. new_request->set_done(m_done);
  210. new_request->set_timing_allow_failed(m_timing_allow_failed);
  211. // 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
  212. if (auto const* body = m_body.get_pointer<Body>())
  213. new_request->set_body(TRY(body->clone()));
  214. // 3. Return newRequest.
  215. return new_request;
  216. }
  217. // https://fetch.spec.whatwg.org/#concept-request-add-range-header
  218. ErrorOr<void> Request::add_range_header(u64 first, Optional<u64> const& last)
  219. {
  220. // To add a range header to a request request, with an integer first, and an optional integer last, run these steps:
  221. // 1. Assert: last is not given, or first is less than or equal to last.
  222. VERIFY(!last.has_value() || first <= last.value());
  223. // 2. Let rangeValue be `bytes=`.
  224. auto range_value = MUST(ByteBuffer::copy("bytes"sv.bytes()));
  225. // 3. Serialize and isomorphic encode first, and append the result to rangeValue.
  226. TRY(range_value.try_append(String::number(first).bytes()));
  227. // 4. Append 0x2D (-) to rangeValue.
  228. TRY(range_value.try_append('-'));
  229. // 5. If last is given, then serialize and isomorphic encode it, and append the result to rangeValue.
  230. if (last.has_value())
  231. TRY(range_value.try_append(String::number(*last).bytes()));
  232. // 6. Append (`Range`, rangeValue) to request’s header list.
  233. auto header = Header {
  234. .name = MUST(ByteBuffer::copy("Range"sv.bytes())),
  235. .value = move(range_value),
  236. };
  237. TRY(m_header_list->append(move(header)));
  238. return {};
  239. }
  240. // https://fetch.spec.whatwg.org/#cross-origin-embedder-policy-allows-credentials
  241. bool Request::cross_origin_embedder_policy_allows_credentials() const
  242. {
  243. // 1. If request’s mode is not "no-cors", then return true.
  244. if (m_mode != Mode::NoCORS)
  245. return true;
  246. // 2. If request’s client is null, then return true.
  247. if (m_client == nullptr)
  248. return true;
  249. // FIXME: 3. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
  250. // 4. If request’s origin is same origin with request’s current URL’s origin and request does not have a redirect-tainted origin, then return true.
  251. // FIXME: Actually use the given origins once we have https://url.spec.whatwg.org/#concept-url-origin.
  252. if (HTML::Origin().is_same_origin(HTML::Origin()) && !has_redirect_tainted_origin())
  253. return true;
  254. // 5. Return false.
  255. return false;
  256. }
  257. }