Requests.cpp 11 KB

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