Requests.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 {
  9. // https://fetch.spec.whatwg.org/#concept-request-url
  10. AK::URL const& Request::url() const
  11. {
  12. // A request has an associated URL (a URL).
  13. // 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.
  14. VERIFY(!m_url_list.is_empty());
  15. return m_url_list.first();
  16. }
  17. // https://fetch.spec.whatwg.org/#concept-request-current-url
  18. AK::URL const& Request::current_url()
  19. {
  20. // A request has an associated current URL. It is a pointer to the last URL in request’s URL list.
  21. VERIFY(!m_url_list.is_empty());
  22. return m_url_list.last();
  23. }
  24. void Request::set_url(AK::URL url)
  25. {
  26. // Sometimes setting the URL and URL list are done as two distinct steps in the spec,
  27. // but since we know the URL is always the URL list's first item and doesn't change later
  28. // on, we can combine them.
  29. VERIFY(m_url_list.is_empty());
  30. m_url_list.append(move(url));
  31. }
  32. // https://fetch.spec.whatwg.org/#request-destination-script-like
  33. bool Request::destination_is_script_like() const
  34. {
  35. // A request’s destination is script-like if it is "audioworklet", "paintworklet", "script", "serviceworker", "sharedworker", or "worker".
  36. static constexpr Array script_like_destinations = {
  37. Destination::AudioWorklet,
  38. Destination::PaintWorklet,
  39. Destination::Script,
  40. Destination::ServiceWorker,
  41. Destination::SharedWorker,
  42. Destination::Worker,
  43. };
  44. return any_of(script_like_destinations, [this](auto destination) {
  45. return m_destination == destination;
  46. });
  47. }
  48. // https://fetch.spec.whatwg.org/#subresource-request
  49. bool Request::is_subresource_request() const
  50. {
  51. // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string.
  52. static constexpr Array subresource_request_destinations = {
  53. Destination::Audio,
  54. Destination::AudioWorklet,
  55. Destination::Font,
  56. Destination::Image,
  57. Destination::Manifest,
  58. Destination::PaintWorklet,
  59. Destination::Script,
  60. Destination::Style,
  61. Destination::Track,
  62. Destination::Video,
  63. Destination::XSLT,
  64. };
  65. return any_of(subresource_request_destinations, [this](auto destination) {
  66. return m_destination == destination;
  67. }) || !m_destination.has_value();
  68. }
  69. // https://fetch.spec.whatwg.org/#non-subresource-request
  70. bool Request::is_non_subresource_request() const
  71. {
  72. // A non-subresource request is a request whose destination is "document", "embed", "frame", "iframe", "object", "report", "serviceworker", "sharedworker", or "worker".
  73. static constexpr Array non_subresource_request_destinations = {
  74. Destination::Document,
  75. Destination::Embed,
  76. Destination::Frame,
  77. Destination::IFrame,
  78. Destination::Object,
  79. Destination::Report,
  80. Destination::ServiceWorker,
  81. Destination::SharedWorker,
  82. Destination::Worker,
  83. };
  84. return any_of(non_subresource_request_destinations, [this](auto destination) {
  85. return m_destination == destination;
  86. });
  87. }
  88. // https://fetch.spec.whatwg.org/#navigation-request
  89. bool Request::is_navigation_request() const
  90. {
  91. // A navigation request is a request whose destination is "document", "embed", "frame", "iframe", or "object".
  92. static constexpr Array navigation_request_destinations = {
  93. Destination::Document,
  94. Destination::Embed,
  95. Destination::Frame,
  96. Destination::IFrame,
  97. Destination::Object,
  98. };
  99. return any_of(navigation_request_destinations, [this](auto destination) {
  100. return m_destination == destination;
  101. });
  102. }
  103. // https://fetch.spec.whatwg.org/#concept-request-tainted-origin
  104. bool Request::has_redirect_tainted_origin() const
  105. {
  106. // A request request has a redirect-tainted origin if these steps return true:
  107. // 1. Let lastURL be null.
  108. Optional<AK::URL const&> last_url;
  109. // 2. For each url in request’s URL list:
  110. for (auto const& url : m_url_list) {
  111. // 1. If lastURL is null, then set lastURL to url and continue.
  112. if (!last_url.has_value()) {
  113. last_url = url;
  114. continue;
  115. }
  116. // 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.
  117. // FIXME: Actually use the given origins once we have https://url.spec.whatwg.org/#concept-url-origin.
  118. if (!HTML::Origin().is_same_origin(HTML::Origin()) && HTML::Origin().is_same_origin(HTML::Origin()))
  119. return true;
  120. // 3. Set lastURL to url.
  121. last_url = url;
  122. }
  123. // 3. Return false.
  124. return false;
  125. }
  126. // https://fetch.spec.whatwg.org/#serializing-a-request-origin
  127. String Request::serialize_origin() const
  128. {
  129. // 1. If request has a redirect-tainted origin, then return "null".
  130. if (has_redirect_tainted_origin())
  131. return "null"sv;
  132. // 2. Return request’s origin, serialized.
  133. return m_origin.get<HTML::Origin>().serialize();
  134. }
  135. // https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin
  136. ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
  137. {
  138. // Byte-serializing a request origin, given a request request, is to return the result of serializing a request origin with request, isomorphic encoded.
  139. return ByteBuffer::copy(serialize_origin().bytes());
  140. }
  141. // https://fetch.spec.whatwg.org/#concept-request-clone
  142. Request Request::clone() const
  143. {
  144. // To clone a request request, run these steps:
  145. // 1. Let newRequest be a copy of request, except for its body.
  146. BodyType body;
  147. swap(body, const_cast<BodyType&>(m_body));
  148. auto new_request = *this;
  149. swap(body, const_cast<BodyType&>(m_body));
  150. // FIXME: 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
  151. // 3. Return newRequest.
  152. return new_request;
  153. }
  154. // https://fetch.spec.whatwg.org/#concept-request-add-range-header
  155. ErrorOr<void> Request::add_range_reader(u64 first, Optional<u64> const& last)
  156. {
  157. // To add a range header to a request request, with an integer first, and an optional integer last, run these steps:
  158. // 1. Assert: last is not given, or first is less than or equal to last.
  159. VERIFY(!last.has_value() || first <= last.value());
  160. // 2. Let rangeValue be `bytes=`.
  161. auto range_value = TRY(ByteBuffer::copy("bytes"sv.bytes()));
  162. // 3. Serialize and isomorphic encode first, and append the result to rangeValue.
  163. TRY(range_value.try_append(String::number(first).bytes()));
  164. // 4. Append 0x2D (-) to rangeValue.
  165. TRY(range_value.try_append('-'));
  166. // 5. If last is given, then serialize and isomorphic encode it, and append the result to rangeValue.
  167. if (last.has_value())
  168. TRY(range_value.try_append(String::number(*last).bytes()));
  169. // 6. Append (`Range`, rangeValue) to request’s header list.
  170. auto header = Header {
  171. .name = TRY(ByteBuffer::copy("Range"sv.bytes())),
  172. .value = move(range_value),
  173. };
  174. TRY(m_header_list.append(move(header)));
  175. return {};
  176. }
  177. // https://fetch.spec.whatwg.org/#cross-origin-embedder-policy-allows-credentials
  178. bool Request::cross_origin_embedder_policy_allows_credentials() const
  179. {
  180. // 1. If request’s mode is not "no-cors", then return true.
  181. if (m_mode != Mode::NoCORS)
  182. return true;
  183. // 2. If request’s client is null, then return true.
  184. if (m_client == nullptr)
  185. return true;
  186. // FIXME: 3. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
  187. // 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.
  188. // FIXME: Actually use the given origins once we have https://url.spec.whatwg.org/#concept-url-origin.
  189. if (HTML::Origin().is_same_origin(HTML::Origin()) && !has_redirect_tainted_origin())
  190. return true;
  191. // 5. Return false.
  192. return false;
  193. }
  194. }