Requests.cpp 11 KB

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