Request.cpp 28 KB

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