Request.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/URLParser.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/RequestPrototype.h>
  9. #include <LibWeb/DOM/AbortSignal.h>
  10. #include <LibWeb/Fetch/Enums.h>
  11. #include <LibWeb/Fetch/Headers.h>
  12. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  13. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  14. #include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
  15. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  16. #include <LibWeb/Fetch/Request.h>
  17. #include <LibWeb/HTML/Scripting/Environments.h>
  18. #include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
  19. namespace Web::Fetch {
  20. Request::Request(JS::Realm& realm, JS::NonnullGCPtr<Infrastructure::Request> request)
  21. : PlatformObject(realm)
  22. , m_request(request)
  23. {
  24. set_prototype(&Bindings::cached_web_prototype(realm, "Request"));
  25. }
  26. Request::~Request() = default;
  27. void Request::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_request);
  31. visitor.visit(m_headers);
  32. visitor.visit(m_signal);
  33. }
  34. // https://fetch.spec.whatwg.org/#concept-body-mime-type
  35. // https://fetch.spec.whatwg.org/#ref-for-concept-body-mime-type%E2%91%A0
  36. Optional<MimeSniff::MimeType> Request::mime_type_impl() const
  37. {
  38. // Objects including the Body interface mixin need to define an associated MIME type algorithm which takes no arguments and returns failure or a MIME type.
  39. // A Request object’s MIME type is to return the result of extracting a MIME type from its request’s header list.
  40. return m_request->header_list()->extract_mime_type();
  41. }
  42. // https://fetch.spec.whatwg.org/#concept-body-body
  43. // https://fetch.spec.whatwg.org/#ref-for-concept-body-body%E2%91%A7
  44. Optional<Infrastructure::Body const&> Request::body_impl() const
  45. {
  46. // Objects including the Body interface mixin have an associated body (null or a body).
  47. // A Request object’s body is its request’s body.
  48. return m_request->body().visit(
  49. [](Infrastructure::Body const& b) -> Optional<Infrastructure::Body const&> { return b; },
  50. [](Empty) -> Optional<Infrastructure::Body const&> { return {}; },
  51. // A byte sequence will be safely extracted into a body early on in fetch.
  52. [](ByteBuffer const&) -> Optional<Infrastructure::Body const&> { VERIFY_NOT_REACHED(); });
  53. }
  54. // https://fetch.spec.whatwg.org/#concept-body-body
  55. // https://fetch.spec.whatwg.org/#ref-for-concept-body-body%E2%91%A7
  56. Optional<Infrastructure::Body&> Request::body_impl()
  57. {
  58. // Objects including the Body interface mixin have an associated body (null or a body).
  59. // A Request object’s body is its request’s body.
  60. return m_request->body().visit(
  61. [](Infrastructure::Body& b) -> Optional<Infrastructure::Body&> { return b; },
  62. [](Empty) -> Optional<Infrastructure::Body&> { return {}; },
  63. // A byte sequence will be safely extracted into a body early on in fetch.
  64. [](ByteBuffer&) -> Optional<Infrastructure::Body&> { VERIFY_NOT_REACHED(); });
  65. }
  66. // https://fetch.spec.whatwg.org/#request-create
  67. JS::NonnullGCPtr<Request> Request::create(JS::Realm& realm, JS::NonnullGCPtr<Infrastructure::Request> request, Headers::Guard guard)
  68. {
  69. // 1. Let requestObject be a new Request object with realm.
  70. // 2. Set requestObject’s request to request.
  71. auto* request_object = realm.heap().allocate<Request>(realm, realm, request);
  72. // 3. Set requestObject’s headers to a new Headers object with realm, whose headers list is request’s headers list and guard is guard.
  73. request_object->m_headers = realm.heap().allocate<Headers>(realm, realm, request->header_list());
  74. request_object->m_headers->set_guard(guard);
  75. // 4. Set requestObject’s signal to a new AbortSignal object with realm.
  76. request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(realm, realm);
  77. // 5. Return requestObject.
  78. return JS::NonnullGCPtr { *request_object };
  79. }
  80. // https://fetch.spec.whatwg.org/#dom-request
  81. WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm& realm, RequestInfo const& input, RequestInit const& init)
  82. {
  83. auto& vm = realm.vm();
  84. // Referred to as 'this' in the spec.
  85. auto request_object = JS::NonnullGCPtr { *realm.heap().allocate<Request>(realm, realm, Infrastructure::Request::create(vm)) };
  86. // 1. Let request be null.
  87. JS::GCPtr<Infrastructure::Request> input_request;
  88. // 2. Let fallbackMode be null.
  89. Optional<Infrastructure::Request::Mode> fallback_mode;
  90. // 3. Let baseURL be this’s relevant settings object’s API base URL.
  91. auto base_url = HTML::relevant_settings_object(*request_object).api_base_url();
  92. // 4. Let signal be null.
  93. DOM::AbortSignal const* input_signal = nullptr;
  94. // 5. If input is a string, then:
  95. if (input.has<DeprecatedString>()) {
  96. // 1. Let parsedURL be the result of parsing input with baseURL.
  97. auto parsed_url = URLParser::parse(input.get<DeprecatedString>(), &base_url);
  98. // 2. If parsedURL is failure, then throw a TypeError.
  99. if (!parsed_url.is_valid())
  100. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Input URL is not valid"sv };
  101. // 3. If parsedURL includes credentials, then throw a TypeError.
  102. if (parsed_url.includes_credentials())
  103. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Input URL must not include credentials"sv };
  104. // 4. Set request to a new request whose URL is parsedURL.
  105. input_request = Infrastructure::Request::create(vm);
  106. input_request->set_url(move(parsed_url));
  107. // 5. Set fallbackMode to "cors".
  108. fallback_mode = Infrastructure::Request::Mode::CORS;
  109. }
  110. // 6. Otherwise:
  111. else {
  112. // 1. Assert: input is a Request object.
  113. VERIFY(input.has<JS::Handle<Request>>());
  114. // 2. Set request to input’s request.
  115. input_request = input.get<JS::Handle<Request>>()->request();
  116. // 3. Set signal to input’s signal.
  117. input_signal = input.get<JS::Handle<Request>>()->signal();
  118. }
  119. // 7. Let origin be this’s relevant settings object’s origin.
  120. auto const& origin = HTML::relevant_settings_object(*request_object).origin();
  121. // 8. Let window be "client".
  122. auto window = Infrastructure::Request::WindowType { Infrastructure::Request::Window::Client };
  123. // 9. If request’s window is an environment settings object and its origin is same origin with origin, then set window to request’s window.
  124. if (input_request->window().has<HTML::EnvironmentSettingsObject*>()) {
  125. auto* eso = input_request->window().get<HTML::EnvironmentSettingsObject*>();
  126. if (eso->origin().is_same_origin(origin))
  127. window = input_request->window();
  128. }
  129. // 10. If init["window"] exists and is non-null, then throw a TypeError.
  130. if (init.window.has_value() && !init.window->is_null())
  131. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "The 'window' property must be omitted or null"sv };
  132. // 11. If init["window"] exists, then set window to "no-window".
  133. if (init.window.has_value())
  134. window = Infrastructure::Request::Window::NoWindow;
  135. // 12. Set request to a new request with the following properties:
  136. // NOTE: This is done at the beginning as the 'this' value Request object
  137. // cannot exist with a null Infrastructure::Request.
  138. auto request = request_object->request();
  139. // URL
  140. // request’s URL.
  141. request->set_url(input_request->url());
  142. // method
  143. // request’s method.
  144. request->set_method(TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(input_request->method())));
  145. // header list
  146. // A copy of request’s header list.
  147. auto header_list_copy = Infrastructure::HeaderList::create(vm);
  148. for (auto& header : *input_request->header_list())
  149. TRY_OR_RETURN_OOM(realm, header_list_copy->append(header));
  150. request->set_header_list(header_list_copy);
  151. // unsafe-request flag
  152. // Set.
  153. request->set_unsafe_request(true);
  154. // client
  155. // This’s relevant settings object.
  156. request->set_client(&HTML::relevant_settings_object(*request_object));
  157. // window
  158. // window.
  159. request->set_window(window);
  160. // priority
  161. // request’s priority.
  162. request->set_priority(input_request->priority());
  163. // origin
  164. // request’s origin. The propagation of the origin is only significant for navigation requests being handled by a service worker. In this scenario a request can have an origin that is different from the current client.
  165. request->set_origin(input_request->origin());
  166. // referrer
  167. // request’s referrer.
  168. request->set_referrer(input_request->referrer());
  169. // referrer policy
  170. // request’s referrer policy.
  171. request->set_referrer_policy(input_request->referrer_policy());
  172. // mode
  173. // request’s mode.
  174. request->set_mode(input_request->mode());
  175. // credentials mode
  176. // request’s credentials mode.
  177. request->set_credentials_mode(input_request->credentials_mode());
  178. // cache mode
  179. // request’s cache mode.
  180. request->set_cache_mode(input_request->cache_mode());
  181. // redirect mode
  182. // request’s redirect mode.
  183. request->set_redirect_mode(input_request->redirect_mode());
  184. // integrity metadata
  185. // request’s integrity metadata.
  186. request->set_integrity_metadata(input_request->integrity_metadata());
  187. // keepalive
  188. // request’s keepalive.
  189. request->set_keepalive(input_request->keepalive());
  190. // reload-navigation flag
  191. // request’s reload-navigation flag.
  192. request->set_reload_navigation(input_request->reload_navigation());
  193. // history-navigation flag
  194. // request’s history-navigation flag.
  195. request->set_history_navigation(input_request->history_navigation());
  196. // URL list
  197. // A clone of request’s URL list.
  198. request->set_url_list(input_request->url_list());
  199. // initiator type
  200. // "fetch".
  201. request->set_initiator_type(Infrastructure::Request::InitiatorType::Fetch);
  202. // 13. If init is not empty, then:
  203. if (!init.is_empty()) {
  204. // 1. If request’s mode is "navigate", then set it to "same-origin".
  205. if (request->mode() == Infrastructure::Request::Mode::Navigate)
  206. request->set_mode(Infrastructure::Request::Mode::SameOrigin);
  207. // 2. Unset request’s reload-navigation flag.
  208. request->set_reload_navigation(false);
  209. // 3. Unset request’s history-navigation flag.
  210. request->set_history_navigation(false);
  211. // 4. Set request’s origin to "client".
  212. request->set_origin(Infrastructure::Request::Origin::Client);
  213. // 5. Set request’s referrer to "client".
  214. request->set_referrer(Infrastructure::Request::Referrer::Client);
  215. // 6. Set request’s referrer policy to the empty string.
  216. request->set_referrer_policy({});
  217. // 7. Set request’s URL to request’s current URL.
  218. request->set_url(request->current_url());
  219. // 8. Set request’s URL list to « request’s URL ».
  220. // NOTE: This is done implicitly by assigning the initial URL above.
  221. }
  222. // 14. If init["referrer"] exists, then:
  223. if (init.referrer.has_value()) {
  224. // 1. Let referrer be init["referrer"].
  225. auto const& referrer = *init.referrer;
  226. // 2. If referrer is the empty string, then set request’s referrer to "no-referrer".
  227. if (referrer.is_empty()) {
  228. request->set_referrer(Infrastructure::Request::Referrer::NoReferrer);
  229. }
  230. // 3. Otherwise:
  231. else {
  232. // 1. Let parsedReferrer be the result of parsing referrer with baseURL.
  233. auto parsed_referrer = URLParser::parse(referrer, &base_url);
  234. // 2. If parsedReferrer is failure, then throw a TypeError.
  235. if (!parsed_referrer.is_valid())
  236. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Referrer must be a valid URL"sv };
  237. // 3. If one of the following is true
  238. // - parsedReferrer’s scheme is "about" and path is the string "client"
  239. // - parsedReferrer’s origin is not same origin with origin
  240. // then set request’s referrer to "client".
  241. // FIXME: Actually use the given origin once we have https://url.spec.whatwg.org/#concept-url-origin.
  242. if ((parsed_referrer.scheme() == "about"sv && parsed_referrer.path() == "client"sv) || !HTML::Origin().is_same_origin(origin)) {
  243. request->set_referrer(Infrastructure::Request::Referrer::Client);
  244. }
  245. // 4. Otherwise, set request’s referrer to parsedReferrer.
  246. else {
  247. request->set_referrer(move(parsed_referrer));
  248. }
  249. }
  250. }
  251. // 15. If init["referrerPolicy"] exists, then set request’s referrer policy to it.
  252. if (init.referrer_policy.has_value())
  253. request->set_referrer_policy(from_bindings_enum(*init.referrer_policy));
  254. // 16. Let mode be init["mode"] if it exists, and fallbackMode otherwise.
  255. auto mode = init.mode.has_value()
  256. ? from_bindings_enum(*init.mode)
  257. : fallback_mode;
  258. // 17. If mode is "navigate", then throw a TypeError.
  259. if (mode == Infrastructure::Request::Mode::Navigate)
  260. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Mode must not be 'navigate"sv };
  261. // 18. If mode is non-null, set request’s mode to mode.
  262. if (mode.has_value())
  263. request->set_mode(*mode);
  264. // 19. If init["credentials"] exists, then set request’s credentials mode to it.
  265. if (init.credentials.has_value())
  266. request->set_credentials_mode(from_bindings_enum(*init.credentials));
  267. // 20. If init["cache"] exists, then set request’s cache mode to it.
  268. if (init.cache.has_value())
  269. request->set_cache_mode(from_bindings_enum(*init.cache));
  270. // 21. If request’s cache mode is "only-if-cached" and request’s mode is not "same-origin", then throw a TypeError.
  271. if (request->cache_mode() == Infrastructure::Request::CacheMode::OnlyIfCached && request->mode() != Infrastructure::Request::Mode::SameOrigin)
  272. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Mode must be 'same-origin' when cache mode is 'only-if-cached'"sv };
  273. // 22. If init["redirect"] exists, then set request’s redirect mode to it.
  274. if (init.redirect.has_value())
  275. request->set_redirect_mode(from_bindings_enum(*init.redirect));
  276. // 23. If init["integrity"] exists, then set request’s integrity metadata to it.
  277. if (init.integrity.has_value())
  278. request->set_integrity_metadata(*init.integrity);
  279. // 24. If init["keepalive"] exists, then set request’s keepalive to it.
  280. if (init.keepalive.has_value())
  281. request->set_keepalive(*init.keepalive);
  282. // 25. If init["method"] exists, then:
  283. if (init.method.has_value()) {
  284. // 1. Let method be init["method"].
  285. auto method = *init.method;
  286. // 2. If method is not a method or method is a forbidden method, then throw a TypeError.
  287. if (!Infrastructure::is_method(method.bytes()))
  288. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method has invalid value"sv };
  289. if (Infrastructure::is_forbidden_method(method.bytes()))
  290. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv };
  291. // 3. Normalize method.
  292. method = TRY_OR_RETURN_OOM(realm, Infrastructure::normalize_method(method.bytes()));
  293. // 4. Set request’s method to method.
  294. request->set_method(MUST(ByteBuffer::copy(method.bytes())));
  295. }
  296. // 26. If init["signal"] exists, then set signal to it.
  297. if (init.signal.has_value())
  298. input_signal = *init.signal;
  299. // 27. Set this’s request to request.
  300. // NOTE: This is done at the beginning as the 'this' value Request object
  301. // cannot exist with a null Infrastructure::Request.
  302. // 28. Set this’s signal to a new AbortSignal object with this’s relevant Realm.
  303. auto& this_relevant_realm = HTML::relevant_realm(*request_object);
  304. request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(this_relevant_realm, this_relevant_realm);
  305. // 29. If signal is not null, then make this’s signal follow signal.
  306. if (input_signal != nullptr)
  307. request_object->m_signal->follow(*input_signal);
  308. // 30. Set this’s headers to a new Headers object with this’s relevant Realm, whose header list is request’s header list and guard is "request".
  309. request_object->m_headers = realm.heap().allocate<Headers>(realm, realm, request->header_list());
  310. request_object->m_headers->set_guard(Headers::Guard::Request);
  311. // 31. If this’s request’s mode is "no-cors", then:
  312. if (request_object->request()->mode() == Infrastructure::Request::Mode::NoCORS) {
  313. // 1. If this’s request’s method is not a CORS-safelisted method, then throw a TypeError.
  314. if (!Infrastructure::is_cors_safelisted_method(request_object->request()->method()))
  315. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must be one of GET, HEAD, or POST"sv };
  316. // 2. Set this’s headers’s guard to "request-no-cors".
  317. request_object->headers()->set_guard(Headers::Guard::RequestNoCORS);
  318. }
  319. // 32. If init is not empty, then:
  320. if (!init.is_empty()) {
  321. // 1. Let headers be a copy of this’s headers and its associated header list.
  322. auto headers = Variant<HeadersInit, JS::NonnullGCPtr<Infrastructure::HeaderList>> { request_object->headers()->header_list() };
  323. // 2. If init["headers"] exists, then set headers to init["headers"].
  324. if (init.headers.has_value())
  325. headers = *init.headers;
  326. // 3. Empty this’s headers’s header list.
  327. request_object->headers()->header_list()->clear();
  328. // 4. If headers is a Headers object, then for each header of its header list, append header to this’s headers.
  329. if (auto* header_list = headers.get_pointer<JS::NonnullGCPtr<Infrastructure::HeaderList>>()) {
  330. for (auto& header : *header_list->ptr())
  331. TRY(request_object->headers()->append(DeprecatedString::copy(header.name), DeprecatedString::copy(header.value)));
  332. }
  333. // 5. Otherwise, fill this’s headers with headers.
  334. else {
  335. TRY(request_object->headers()->fill(headers.get<HeadersInit>()));
  336. }
  337. }
  338. // 33. Let inputBody be input’s request’s body if input is a Request object; otherwise null.
  339. Optional<Infrastructure::Request::BodyType const&> input_body;
  340. if (input.has<JS::Handle<Request>>())
  341. input_body = input.get<JS::Handle<Request>>()->request()->body();
  342. // 34. If either init["body"] exists and is non-null or inputBody is non-null, and request’s method is `GET` or `HEAD`, then throw a TypeError.
  343. if (((init.body.has_value() && (*init.body).has_value()) || (input_body.has_value() && !input_body.value().has<Empty>())) && StringView { request->method() }.is_one_of("GET"sv, "HEAD"sv))
  344. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be GET or HEAD when body is provided"sv };
  345. // 35. Let initBody be null.
  346. Optional<Infrastructure::Body> init_body;
  347. // 36. If init["body"] exists and is non-null, then:
  348. if (init.body.has_value() && (*init.body).has_value()) {
  349. // 1. Let bodyWithType be the result of extracting init["body"], with keepalive set to request’s keepalive.
  350. auto body_with_type = TRY(extract_body(realm, (*init.body).value(), request->keepalive()));
  351. // 2. Set initBody to bodyWithType’s body.
  352. init_body = move(body_with_type.body);
  353. // 3. Let type be bodyWithType’s type.
  354. auto const& type = body_with_type.type;
  355. // 4. If type is non-null and this’s headers’s header list does not contain `Content-Type`, then append (`Content-Type`, type) to this’s headers.
  356. if (type.has_value() && !request_object->headers()->header_list()->contains("Content-Type"sv.bytes()))
  357. TRY(request_object->headers()->append("Content-Type"sv, DeprecatedString::copy(type->span())));
  358. }
  359. // 37. Let inputOrInitBody be initBody if it is non-null; otherwise inputBody.
  360. Optional<Infrastructure::Request::BodyType> input_or_init_body = init_body.has_value()
  361. ? Infrastructure::Request::BodyType { init_body.value() }
  362. : input_body;
  363. // 38. If inputOrInitBody is non-null and inputOrInitBody’s source is null, then:
  364. // FIXME: The spec doesn't check if inputOrInitBody is a body before accessing source.
  365. if (input_or_init_body.has_value() && input_or_init_body->has<Infrastructure::Body>() && input_or_init_body->get<Infrastructure::Body>().source().has<Empty>()) {
  366. // 1. If initBody is non-null and init["duplex"] does not exist, then throw a TypeError.
  367. if (init_body.has_value() && !init.duplex.has_value())
  368. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Body without source requires 'duplex' value to be set"sv };
  369. // 2. If this’s request’s mode is neither "same-origin" nor "cors", then throw a TypeError.
  370. if (request_object->request()->mode() != Infrastructure::Request::Mode::SameOrigin && request_object->request()->mode() != Infrastructure::Request::Mode::CORS)
  371. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Request mode must be 'same-origin' or 'cors'"sv };
  372. // 3. Set this’s request’s use-CORS-preflight flag.
  373. request_object->request()->set_use_cors_preflight(true);
  374. }
  375. // 39. Let finalBody be inputOrInitBody.
  376. auto const& final_body = input_or_init_body;
  377. // 40. If initBody is null and inputBody is non-null, then:
  378. if (!init_body.has_value() && input_body.has_value()) {
  379. // 2. If input is unusable, then throw a TypeError.
  380. if (input.has<JS::Handle<Request>>() && input.get<JS::Handle<Request>>()->is_unusable())
  381. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Request is unusable"sv };
  382. // FIXME: 2. Set finalBody to the result of creating a proxy for inputBody.
  383. }
  384. // 41. Set this’s request’s body to finalBody.
  385. if (final_body.has_value())
  386. request_object->request()->set_body(*final_body);
  387. return JS::NonnullGCPtr { *request_object };
  388. }
  389. // https://fetch.spec.whatwg.org/#dom-request-method
  390. DeprecatedString Request::method() const
  391. {
  392. // The method getter steps are to return this’s request’s method.
  393. return DeprecatedString::copy(m_request->method());
  394. }
  395. // https://fetch.spec.whatwg.org/#dom-request-url
  396. DeprecatedString Request::url() const
  397. {
  398. // The url getter steps are to return this’s request’s URL, serialized.
  399. return m_request->url().serialize();
  400. }
  401. // https://fetch.spec.whatwg.org/#dom-request-headers
  402. JS::NonnullGCPtr<Headers> Request::headers() const
  403. {
  404. // The headers getter steps are to return this’s headers.
  405. return *m_headers;
  406. }
  407. // https://fetch.spec.whatwg.org/#dom-request-destination
  408. Bindings::RequestDestination Request::destination() const
  409. {
  410. // The destination getter are to return this’s request’s destination.
  411. return to_bindings_enum(m_request->destination());
  412. }
  413. // https://fetch.spec.whatwg.org/#dom-request-referrer
  414. DeprecatedString Request::referrer() const
  415. {
  416. return m_request->referrer().visit(
  417. [&](Infrastructure::Request::Referrer const& referrer) {
  418. switch (referrer) {
  419. // 1. If this’s request’s referrer is "no-referrer", then return the empty string.
  420. case Infrastructure::Request::Referrer::NoReferrer:
  421. return DeprecatedString::empty();
  422. // 2. If this’s request’s referrer is "client", then return "about:client".
  423. case Infrastructure::Request::Referrer::Client:
  424. return DeprecatedString { "about:client"sv };
  425. default:
  426. VERIFY_NOT_REACHED();
  427. }
  428. },
  429. [&](AK::URL const& url) {
  430. // 3. Return this’s request’s referrer, serialized.
  431. return url.serialize();
  432. });
  433. }
  434. // https://fetch.spec.whatwg.org/#dom-request-referrerpolicy
  435. Bindings::ReferrerPolicy Request::referrer_policy() const
  436. {
  437. // The referrerPolicy getter steps are to return this’s request’s referrer policy.
  438. return to_bindings_enum(m_request->referrer_policy());
  439. }
  440. // https://fetch.spec.whatwg.org/#dom-request-mode
  441. Bindings::RequestMode Request::mode() const
  442. {
  443. // The mode getter steps are to return this’s request’s mode.
  444. return to_bindings_enum(m_request->mode());
  445. }
  446. // https://fetch.spec.whatwg.org/#dom-request-credentials
  447. Bindings::RequestCredentials Request::credentials() const
  448. {
  449. // The credentials getter steps are to return this’s request’s credentials mode.
  450. return to_bindings_enum(m_request->credentials_mode());
  451. }
  452. // https://fetch.spec.whatwg.org/#dom-request-cache
  453. Bindings::RequestCache Request::cache() const
  454. {
  455. // The cache getter steps are to return this’s request’s cache mode.
  456. return to_bindings_enum(m_request->cache_mode());
  457. }
  458. // https://fetch.spec.whatwg.org/#dom-request-redirect
  459. Bindings::RequestRedirect Request::redirect() const
  460. {
  461. // The redirect getter steps are to return this’s request’s redirect mode.
  462. return to_bindings_enum(m_request->redirect_mode());
  463. }
  464. // https://fetch.spec.whatwg.org/#dom-request-integrity
  465. DeprecatedString Request::integrity() const
  466. {
  467. // The integrity getter steps are to return this’s request’s integrity metadata.
  468. return m_request->integrity_metadata();
  469. }
  470. // https://fetch.spec.whatwg.org/#dom-request-keepalive
  471. bool Request::keepalive() const
  472. {
  473. // The keepalive getter steps are to return this’s request’s keepalive.
  474. return m_request->keepalive();
  475. }
  476. // https://fetch.spec.whatwg.org/#dom-request-isreloadnavigation
  477. bool Request::is_reload_navigation() const
  478. {
  479. // The isReloadNavigation getter steps are to return true if this’s request’s reload-navigation flag is set; otherwise false.
  480. return m_request->reload_navigation();
  481. }
  482. // https://fetch.spec.whatwg.org/#dom-request-ishistorynavigation
  483. bool Request::is_history_navigation() const
  484. {
  485. // The isHistoryNavigation getter steps are to return true if this’s request’s history-navigation flag is set; otherwise false.
  486. return m_request->history_navigation();
  487. }
  488. // https://fetch.spec.whatwg.org/#dom-request-signal
  489. JS::NonnullGCPtr<DOM::AbortSignal> Request::signal() const
  490. {
  491. // The signal getter steps are to return this’s signal.
  492. return *m_signal;
  493. }
  494. // https://fetch.spec.whatwg.org/#dom-request-duplex
  495. Bindings::RequestDuplex Request::duplex() const
  496. {
  497. // The duplex getter steps are to return "half".
  498. return Bindings::RequestDuplex::Half;
  499. }
  500. // https://fetch.spec.whatwg.org/#dom-request-clone
  501. WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::clone() const
  502. {
  503. auto& vm = this->vm();
  504. // 1. If this is unusable, then throw a TypeError.
  505. if (is_unusable())
  506. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Request is unusable"sv };
  507. // 2. Let clonedRequest be the result of cloning this’s request.
  508. auto cloned_request = TRY(m_request->clone(vm));
  509. // 3. Let clonedRequestObject be the result of creating a Request object, given clonedRequest, this’s headers’s guard, and this’s relevant Realm.
  510. auto cloned_request_object = Request::create(HTML::relevant_realm(*this), cloned_request, m_headers->guard());
  511. // 4. Make clonedRequestObject’s signal follow this’s signal.
  512. cloned_request_object->m_signal->follow(*m_signal);
  513. // 5. Return clonedRequestObject.
  514. return cloned_request_object;
  515. }
  516. }