Request.cpp 28 KB

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