Requests.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (c) 2022-2023, 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/DOMURL/DOMURL.h>
  10. #include <LibWeb/Fetch/Fetching/PendingResponse.h>
  11. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  12. namespace Web::Fetch::Infrastructure {
  13. JS_DEFINE_ALLOCATOR(Request);
  14. Request::Request(JS::NonnullGCPtr<HeaderList> header_list)
  15. : m_header_list(header_list)
  16. {
  17. }
  18. void Request::visit_edges(JS::Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_header_list);
  22. visitor.visit(m_client);
  23. m_body.visit(
  24. [&](JS::NonnullGCPtr<Body>& body) { visitor.visit(body); },
  25. [](auto&) {});
  26. visitor.visit(m_reserved_client);
  27. m_window.visit(
  28. [&](JS::GCPtr<HTML::EnvironmentSettingsObject> const& value) { visitor.visit(value); },
  29. [](auto const&) {});
  30. visitor.visit(m_pending_responses);
  31. }
  32. JS::NonnullGCPtr<Request> Request::create(JS::VM& vm)
  33. {
  34. return vm.heap().allocate<Request>(HeaderList::create(vm));
  35. }
  36. // https://fetch.spec.whatwg.org/#concept-request-url
  37. URL::URL& Request::url()
  38. {
  39. // A request has an associated URL (a URL).
  40. // 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.
  41. VERIFY(!m_url_list.is_empty());
  42. return m_url_list.first();
  43. }
  44. // https://fetch.spec.whatwg.org/#concept-request-url
  45. URL::URL const& Request::url() const
  46. {
  47. return const_cast<Request&>(*this).url();
  48. }
  49. // https://fetch.spec.whatwg.org/#concept-request-current-url
  50. URL::URL& Request::current_url()
  51. {
  52. // A request has an associated current URL. It is a pointer to the last URL in request’s URL list.
  53. VERIFY(!m_url_list.is_empty());
  54. return m_url_list.last();
  55. }
  56. // https://fetch.spec.whatwg.org/#concept-request-current-url
  57. URL::URL const& Request::current_url() const
  58. {
  59. return const_cast<Request&>(*this).current_url();
  60. }
  61. void Request::set_url(URL::URL url)
  62. {
  63. // Sometimes setting the URL and URL list are done as two distinct steps in the spec,
  64. // but since we know the URL is always the URL list's first item and doesn't change later
  65. // on, we can combine them.
  66. if (!m_url_list.is_empty())
  67. m_url_list.clear();
  68. m_url_list.append(move(url));
  69. }
  70. // https://fetch.spec.whatwg.org/#request-destination-script-like
  71. bool Request::destination_is_script_like() const
  72. {
  73. // A request’s destination is script-like if it is "audioworklet", "paintworklet", "script", "serviceworker", "sharedworker", or "worker".
  74. static constexpr Array script_like_destinations = {
  75. Destination::AudioWorklet,
  76. Destination::PaintWorklet,
  77. Destination::Script,
  78. Destination::ServiceWorker,
  79. Destination::SharedWorker,
  80. Destination::Worker,
  81. };
  82. return any_of(script_like_destinations, [this](auto destination) {
  83. return m_destination == destination;
  84. });
  85. }
  86. // https://fetch.spec.whatwg.org/#subresource-request
  87. bool Request::is_subresource_request() const
  88. {
  89. // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "json", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string.
  90. static constexpr Array subresource_request_destinations = {
  91. Destination::Audio,
  92. Destination::AudioWorklet,
  93. Destination::Font,
  94. Destination::Image,
  95. Destination::JSON,
  96. Destination::Manifest,
  97. Destination::PaintWorklet,
  98. Destination::Script,
  99. Destination::Style,
  100. Destination::Track,
  101. Destination::Video,
  102. Destination::XSLT,
  103. };
  104. return any_of(subresource_request_destinations, [this](auto destination) {
  105. return m_destination == destination;
  106. }) || !m_destination.has_value();
  107. }
  108. // https://fetch.spec.whatwg.org/#non-subresource-request
  109. bool Request::is_non_subresource_request() const
  110. {
  111. // A non-subresource request is a request whose destination is "document", "embed", "frame", "iframe", "object", "report", "serviceworker", "sharedworker", or "worker".
  112. static constexpr Array non_subresource_request_destinations = {
  113. Destination::Document,
  114. Destination::Embed,
  115. Destination::Frame,
  116. Destination::IFrame,
  117. Destination::Object,
  118. Destination::Report,
  119. Destination::ServiceWorker,
  120. Destination::SharedWorker,
  121. Destination::Worker,
  122. };
  123. return any_of(non_subresource_request_destinations, [this](auto destination) {
  124. return m_destination == destination;
  125. });
  126. }
  127. // https://fetch.spec.whatwg.org/#navigation-request
  128. bool Request::is_navigation_request() const
  129. {
  130. // A navigation request is a request whose destination is "document", "embed", "frame", "iframe", or "object".
  131. static constexpr Array navigation_request_destinations = {
  132. Destination::Document,
  133. Destination::Embed,
  134. Destination::Frame,
  135. Destination::IFrame,
  136. Destination::Object,
  137. };
  138. return any_of(navigation_request_destinations, [this](auto destination) {
  139. return m_destination == destination;
  140. });
  141. }
  142. // https://fetch.spec.whatwg.org/#concept-request-tainted-origin
  143. bool Request::has_redirect_tainted_origin() const
  144. {
  145. // A request request has a redirect-tainted origin if these steps return true:
  146. // 1. Let lastURL be null.
  147. Optional<URL::URL const&> last_url;
  148. // 2. For each url of request’s URL list:
  149. for (auto const& url : m_url_list) {
  150. // 1. If lastURL is null, then set lastURL to url and continue.
  151. if (!last_url.has_value()) {
  152. last_url = url;
  153. continue;
  154. }
  155. // 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.
  156. auto const* request_origin = m_origin.get_pointer<URL::Origin>();
  157. if (!url.origin().is_same_origin(last_url->origin())
  158. && (request_origin == nullptr || !request_origin->is_same_origin(last_url->origin()))) {
  159. return true;
  160. }
  161. // 3. Set lastURL to url.
  162. last_url = url;
  163. }
  164. // 3. Return false.
  165. return false;
  166. }
  167. // https://fetch.spec.whatwg.org/#serializing-a-request-origin
  168. String Request::serialize_origin() const
  169. {
  170. // 1. If request has a redirect-tainted origin, then return "null".
  171. if (has_redirect_tainted_origin())
  172. return "null"_string;
  173. // 2. Return request’s origin, serialized.
  174. return MUST(String::from_byte_string(m_origin.get<URL::Origin>().serialize()));
  175. }
  176. // https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin
  177. ByteBuffer Request::byte_serialize_origin() const
  178. {
  179. // Byte-serializing a request origin, given a request request, is to return the result of serializing a request origin with request, isomorphic encoded.
  180. return MUST(ByteBuffer::copy(serialize_origin().bytes()));
  181. }
  182. // https://fetch.spec.whatwg.org/#concept-request-clone
  183. JS::NonnullGCPtr<Request> Request::clone(JS::Realm& realm) const
  184. {
  185. // To clone a request request, run these steps:
  186. auto& vm = realm.vm();
  187. // 1. Let newRequest be a copy of request, except for its body.
  188. auto new_request = Infrastructure::Request::create(vm);
  189. new_request->set_method(m_method);
  190. new_request->set_local_urls_only(m_local_urls_only);
  191. for (auto const& header : *m_header_list)
  192. new_request->header_list()->append(header);
  193. new_request->set_unsafe_request(m_unsafe_request);
  194. new_request->set_client(m_client);
  195. new_request->set_reserved_client(m_reserved_client);
  196. new_request->set_replaces_client_id(m_replaces_client_id);
  197. new_request->set_window(m_window);
  198. new_request->set_keepalive(m_keepalive);
  199. new_request->set_initiator_type(m_initiator_type);
  200. new_request->set_service_workers_mode(m_service_workers_mode);
  201. new_request->set_initiator(m_initiator);
  202. new_request->set_destination(m_destination);
  203. new_request->set_priority(m_priority);
  204. new_request->set_origin(m_origin);
  205. new_request->set_policy_container(m_policy_container);
  206. new_request->set_referrer(m_referrer);
  207. new_request->set_referrer_policy(m_referrer_policy);
  208. new_request->set_mode(m_mode);
  209. new_request->set_use_cors_preflight(m_use_cors_preflight);
  210. new_request->set_credentials_mode(m_credentials_mode);
  211. new_request->set_use_url_credentials(m_use_url_credentials);
  212. new_request->set_cache_mode(m_cache_mode);
  213. new_request->set_redirect_mode(m_redirect_mode);
  214. new_request->set_integrity_metadata(m_integrity_metadata);
  215. new_request->set_cryptographic_nonce_metadata(m_cryptographic_nonce_metadata);
  216. new_request->set_parser_metadata(m_parser_metadata);
  217. new_request->set_reload_navigation(m_reload_navigation);
  218. new_request->set_history_navigation(m_history_navigation);
  219. new_request->set_user_activation(m_user_activation);
  220. new_request->set_render_blocking(m_render_blocking);
  221. new_request->set_url_list(m_url_list);
  222. new_request->set_redirect_count(m_redirect_count);
  223. new_request->set_response_tainting(m_response_tainting);
  224. new_request->set_prevent_no_cache_cache_control_header_modification(m_prevent_no_cache_cache_control_header_modification);
  225. new_request->set_done(m_done);
  226. new_request->set_timing_allow_failed(m_timing_allow_failed);
  227. new_request->set_buffer_policy(m_buffer_policy);
  228. // 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
  229. if (auto const* body = m_body.get_pointer<JS::NonnullGCPtr<Body>>())
  230. new_request->set_body((*body)->clone(realm));
  231. // 3. Return newRequest.
  232. return new_request;
  233. }
  234. // https://fetch.spec.whatwg.org/#concept-request-add-range-header
  235. void Request::add_range_header(u64 first, Optional<u64> const& last)
  236. {
  237. // To add a range header to a request request, with an integer first, and an optional integer last, run these steps:
  238. // 1. Assert: last is not given, or first is less than or equal to last.
  239. VERIFY(!last.has_value() || first <= last.value());
  240. // 2. Let rangeValue be `bytes=`.
  241. auto range_value = MUST(ByteBuffer::copy("bytes"sv.bytes()));
  242. // 3. Serialize and isomorphic encode first, and append the result to rangeValue.
  243. range_value.append(String::number(first).bytes());
  244. // 4. Append 0x2D (-) to rangeValue.
  245. range_value.append('-');
  246. // 5. If last is given, then serialize and isomorphic encode it, and append the result to rangeValue.
  247. if (last.has_value())
  248. range_value.append(String::number(*last).bytes());
  249. // 6. Append (`Range`, rangeValue) to request’s header list.
  250. auto header = Header {
  251. .name = MUST(ByteBuffer::copy("Range"sv.bytes())),
  252. .value = move(range_value),
  253. };
  254. m_header_list->append(move(header));
  255. }
  256. // https://fetch.spec.whatwg.org/#append-a-request-origin-header
  257. void Request::add_origin_header()
  258. {
  259. // 1. Let serializedOrigin be the result of byte-serializing a request origin with request.
  260. auto serialized_origin = byte_serialize_origin();
  261. // 2. If request’s response tainting is "cors" or request’s mode is "websocket", then append (`Origin`, serializedOrigin) to request’s header list.
  262. if (m_response_tainting == ResponseTainting::CORS || m_mode == Mode::WebSocket) {
  263. auto header = Header {
  264. .name = MUST(ByteBuffer::copy("Origin"sv.bytes())),
  265. .value = move(serialized_origin),
  266. };
  267. m_header_list->append(move(header));
  268. }
  269. // 3. Otherwise, if request’s method is neither `GET` nor `HEAD`, then:
  270. else if (!StringView { m_method }.is_one_of("GET"sv, "HEAD"sv)) {
  271. // 1. If request’s mode is not "cors", then switch on request’s referrer policy:
  272. if (m_mode != Mode::CORS) {
  273. switch (m_referrer_policy) {
  274. // -> "no-referrer"
  275. case ReferrerPolicy::ReferrerPolicy::NoReferrer:
  276. // Set serializedOrigin to `null`.
  277. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  278. break;
  279. // -> "no-referrer-when-downgrade"
  280. // -> "strict-origin"
  281. // -> "strict-origin-when-cross-origin"
  282. case ReferrerPolicy::ReferrerPolicy::NoReferrerWhenDowngrade:
  283. case ReferrerPolicy::ReferrerPolicy::StrictOrigin:
  284. case ReferrerPolicy::ReferrerPolicy::StrictOriginWhenCrossOrigin:
  285. // If request’s origin is a tuple origin, its scheme is "https", and request’s current URL’s scheme is
  286. // not "https", then set serializedOrigin to `null`.
  287. if (m_origin.has<URL::Origin>() && m_origin.get<URL::Origin>().scheme() == "https"sv && current_url().scheme() != "https"sv)
  288. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  289. break;
  290. // -> "same-origin"
  291. case ReferrerPolicy::ReferrerPolicy::SameOrigin:
  292. // If request’s origin is not same origin with request’s current URL’s origin, then set serializedOrigin
  293. // to `null`.
  294. if (m_origin.has<URL::Origin>() && !m_origin.get<URL::Origin>().is_same_origin(current_url().origin()))
  295. serialized_origin = MUST(ByteBuffer::copy("null"sv.bytes()));
  296. break;
  297. // -> Otherwise
  298. default:
  299. // Do nothing.
  300. break;
  301. }
  302. }
  303. // 2. Append (`Origin`, serializedOrigin) to request’s header list.
  304. auto header = Header {
  305. .name = MUST(ByteBuffer::copy("Origin"sv.bytes())),
  306. .value = move(serialized_origin),
  307. };
  308. m_header_list->append(move(header));
  309. }
  310. }
  311. // https://fetch.spec.whatwg.org/#cross-origin-embedder-policy-allows-credentials
  312. bool Request::cross_origin_embedder_policy_allows_credentials() const
  313. {
  314. // 1. If request’s mode is not "no-cors", then return true.
  315. if (m_mode != Mode::NoCORS)
  316. return true;
  317. // 2. If request’s client is null, then return true.
  318. if (m_client == nullptr)
  319. return true;
  320. // 3. If request’s client’s policy container’s embedder policy’s value is not "credentialless", then return true.
  321. if (m_policy_container.has<HTML::PolicyContainer>() && m_policy_container.get<HTML::PolicyContainer>().embedder_policy.value != HTML::EmbedderPolicyValue::Credentialless)
  322. return true;
  323. // 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.
  324. // 5. Return false.
  325. auto const* request_origin = m_origin.get_pointer<URL::Origin>();
  326. if (request_origin == nullptr)
  327. return false;
  328. return request_origin->is_same_origin(current_url().origin()) && !has_redirect_tainted_origin();
  329. }
  330. StringView request_destination_to_string(Request::Destination destination)
  331. {
  332. switch (destination) {
  333. case Request::Destination::Audio:
  334. return "audio"sv;
  335. case Request::Destination::AudioWorklet:
  336. return "audioworklet"sv;
  337. case Request::Destination::Document:
  338. return "document"sv;
  339. case Request::Destination::Embed:
  340. return "embed"sv;
  341. case Request::Destination::Font:
  342. return "font"sv;
  343. case Request::Destination::Frame:
  344. return "frame"sv;
  345. case Request::Destination::IFrame:
  346. return "iframe"sv;
  347. case Request::Destination::Image:
  348. return "image"sv;
  349. case Request::Destination::JSON:
  350. return "json"sv;
  351. case Request::Destination::Manifest:
  352. return "manifest"sv;
  353. case Request::Destination::Object:
  354. return "object"sv;
  355. case Request::Destination::PaintWorklet:
  356. return "paintworklet"sv;
  357. case Request::Destination::Report:
  358. return "report"sv;
  359. case Request::Destination::Script:
  360. return "script"sv;
  361. case Request::Destination::ServiceWorker:
  362. return "serviceworker"sv;
  363. case Request::Destination::SharedWorker:
  364. return "sharedworker"sv;
  365. case Request::Destination::Style:
  366. return "style"sv;
  367. case Request::Destination::Track:
  368. return "track"sv;
  369. case Request::Destination::Video:
  370. return "video"sv;
  371. case Request::Destination::WebIdentity:
  372. return "webidentity"sv;
  373. case Request::Destination::Worker:
  374. return "worker"sv;
  375. case Request::Destination::XSLT:
  376. return "xslt"sv;
  377. }
  378. VERIFY_NOT_REACHED();
  379. }
  380. StringView request_mode_to_string(Request::Mode mode)
  381. {
  382. switch (mode) {
  383. case Request::Mode::SameOrigin:
  384. return "same-origin"sv;
  385. case Request::Mode::CORS:
  386. return "cors"sv;
  387. case Request::Mode::NoCORS:
  388. return "no-cors"sv;
  389. case Request::Mode::Navigate:
  390. return "navigate"sv;
  391. case Request::Mode::WebSocket:
  392. return "websocket"sv;
  393. }
  394. VERIFY_NOT_REACHED();
  395. }
  396. Optional<Request::Priority> request_priority_from_string(StringView string)
  397. {
  398. if (string.equals_ignoring_ascii_case("high"sv))
  399. return Request::Priority::High;
  400. if (string.equals_ignoring_ascii_case("low"sv))
  401. return Request::Priority::Low;
  402. if (string.equals_ignoring_ascii_case("auto"sv))
  403. return Request::Priority::Auto;
  404. return {};
  405. }
  406. }