Requests.cpp 8.6 KB

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