Fetching.cpp 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Base64.h>
  10. #include <AK/Debug.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <LibJS/Runtime/Completion.h>
  13. #include <LibWeb/Bindings/MainThreadVM.h>
  14. #include <LibWeb/Cookie/Cookie.h>
  15. #include <LibWeb/DOM/Document.h>
  16. #include <LibWeb/DOMURL/DOMURL.h>
  17. #include <LibWeb/Fetch/BodyInit.h>
  18. #include <LibWeb/Fetch/Fetching/Checks.h>
  19. #include <LibWeb/Fetch/Fetching/FetchedDataReceiver.h>
  20. #include <LibWeb/Fetch/Fetching/Fetching.h>
  21. #include <LibWeb/Fetch/Fetching/PendingResponse.h>
  22. #include <LibWeb/Fetch/Fetching/RefCountedFlag.h>
  23. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  24. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  25. #include <LibWeb/Fetch/Infrastructure/FetchParams.h>
  26. #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
  27. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  28. #include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
  29. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  30. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  31. #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
  32. #include <LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h>
  33. #include <LibWeb/Fetch/Infrastructure/NoSniffBlocking.h>
  34. #include <LibWeb/Fetch/Infrastructure/PortBlocking.h>
  35. #include <LibWeb/Fetch/Infrastructure/Task.h>
  36. #include <LibWeb/Fetch/Infrastructure/URL.h>
  37. #include <LibWeb/FileAPI/Blob.h>
  38. #include <LibWeb/FileAPI/BlobURLStore.h>
  39. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  40. #include <LibWeb/HTML/Scripting/Environments.h>
  41. #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
  42. #include <LibWeb/HTML/Window.h>
  43. #include <LibWeb/HTML/WorkerGlobalScope.h>
  44. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  45. #include <LibWeb/Loader/LoadRequest.h>
  46. #include <LibWeb/Loader/ResourceLoader.h>
  47. #include <LibWeb/Platform/EventLoopPlugin.h>
  48. #include <LibWeb/ReferrerPolicy/AbstractOperations.h>
  49. #include <LibWeb/SRI/SRI.h>
  50. #include <LibWeb/SecureContexts/AbstractOperations.h>
  51. #include <LibWeb/Streams/TransformStream.h>
  52. #include <LibWeb/Streams/TransformStreamDefaultController.h>
  53. #include <LibWeb/Streams/Transformer.h>
  54. #include <LibWeb/WebIDL/DOMException.h>
  55. namespace Web::Fetch::Fetching {
  56. #define TRY_OR_IGNORE(expression) \
  57. ({ \
  58. auto&& _temporary_result = (expression); \
  59. if (_temporary_result.is_error()) \
  60. return; \
  61. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  62. "Do not return a reference from a fallible expression"); \
  63. _temporary_result.release_value(); \
  64. })
  65. // https://fetch.spec.whatwg.org/#concept-fetch
  66. WebIDL::ExceptionOr<JS::NonnullGCPtr<Infrastructure::FetchController>> fetch(JS::Realm& realm, Infrastructure::Request& request, Infrastructure::FetchAlgorithms const& algorithms, UseParallelQueue use_parallel_queue)
  67. {
  68. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'fetch' with: request @ {}", &request);
  69. auto& vm = realm.vm();
  70. // 1. Assert: request’s mode is "navigate" or processEarlyHintsResponse is null.
  71. VERIFY(request.mode() == Infrastructure::Request::Mode::Navigate || !algorithms.process_early_hints_response());
  72. // 2. Let taskDestination be null.
  73. JS::GCPtr<JS::Object> task_destination;
  74. // 3. Let crossOriginIsolatedCapability be false.
  75. auto cross_origin_isolated_capability = HTML::CanUseCrossOriginIsolatedAPIs::No;
  76. // 4. If request’s client is non-null, then:
  77. if (request.client() != nullptr) {
  78. // 1. Set taskDestination to request’s client’s global object.
  79. task_destination = request.client()->global_object();
  80. // 2. Set crossOriginIsolatedCapability to request’s client’s cross-origin isolated capability.
  81. cross_origin_isolated_capability = request.client()->cross_origin_isolated_capability();
  82. }
  83. // FIXME: 5. If useParallelQueue is true, then set taskDestination to the result of starting a new parallel queue.
  84. (void)use_parallel_queue;
  85. // 6. Let timingInfo be a new fetch timing info whose start time and post-redirect start time are the coarsened
  86. // shared current time given crossOriginIsolatedCapability, and render-blocking is set to request’s
  87. // render-blocking.
  88. auto timing_info = Infrastructure::FetchTimingInfo::create(vm);
  89. auto now = HighResolutionTime::coarsened_shared_current_time(cross_origin_isolated_capability == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  90. timing_info->set_start_time(now);
  91. timing_info->set_post_redirect_start_time(now);
  92. timing_info->set_render_blocking(request.render_blocking());
  93. // 7. Let fetchParams be a new fetch params whose request is request, timing info is timingInfo, process request
  94. // body chunk length is processRequestBodyChunkLength, process request end-of-body is processRequestEndOfBody,
  95. // process early hints response is processEarlyHintsResponse, process response is processResponse, process
  96. // response consume body is processResponseConsumeBody, process response end-of-body is processResponseEndOfBody,
  97. // task destination is taskDestination, and cross-origin isolated capability is crossOriginIsolatedCapability.
  98. auto fetch_params = Infrastructure::FetchParams::create(vm, request, timing_info);
  99. fetch_params->set_algorithms(algorithms);
  100. if (task_destination)
  101. fetch_params->set_task_destination({ *task_destination });
  102. fetch_params->set_cross_origin_isolated_capability(cross_origin_isolated_capability);
  103. // 8. If request’s body is a byte sequence, then set request’s body to request’s body as a body.
  104. if (auto const* buffer = request.body().get_pointer<ByteBuffer>())
  105. request.set_body(TRY(Infrastructure::byte_sequence_as_body(realm, buffer->bytes())));
  106. // 9. If request’s window is "client", then set request’s window to request’s client, if request’s client’s global
  107. // object is a Window object; otherwise "no-window".
  108. auto const* window = request.window().get_pointer<Infrastructure::Request::Window>();
  109. if (window && *window == Infrastructure::Request::Window::Client) {
  110. if (is<HTML::Window>(request.client()->global_object())) {
  111. request.set_window(request.client());
  112. } else {
  113. request.set_window(Infrastructure::Request::Window::NoWindow);
  114. }
  115. }
  116. // 10. If request’s origin is "client", then set request’s origin to request’s client’s origin.
  117. auto const* origin = request.origin().get_pointer<Infrastructure::Request::Origin>();
  118. if (origin && *origin == Infrastructure::Request::Origin::Client)
  119. request.set_origin(request.client()->origin());
  120. // 12. If request’s policy container is "client", then:
  121. auto const* policy_container = request.policy_container().get_pointer<Infrastructure::Request::PolicyContainer>();
  122. if (policy_container) {
  123. VERIFY(*policy_container == Infrastructure::Request::PolicyContainer::Client);
  124. // 1. If request’s client is non-null, then set request’s policy container to a clone of request’s client’s
  125. // policy container.
  126. if (request.client() != nullptr)
  127. request.set_policy_container(request.client()->policy_container());
  128. // 2. Otherwise, set request’s policy container to a new policy container.
  129. else
  130. request.set_policy_container(HTML::PolicyContainer {});
  131. }
  132. // 13. If request’s header list does not contain `Accept`, then:
  133. if (!request.header_list()->contains("Accept"sv.bytes())) {
  134. // 1. Let value be `*/*`.
  135. auto value = "*/*"sv;
  136. // 2. A user agent should set value to the first matching statement, if any, switching on request’s
  137. // destination:
  138. if (request.destination().has_value()) {
  139. switch (*request.destination()) {
  140. // -> "document"
  141. // -> "frame"
  142. // -> "iframe"
  143. case Infrastructure::Request::Destination::Document:
  144. case Infrastructure::Request::Destination::Frame:
  145. case Infrastructure::Request::Destination::IFrame:
  146. // `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`
  147. value = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"sv;
  148. break;
  149. // -> "image"
  150. case Infrastructure::Request::Destination::Image:
  151. // `image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5`
  152. value = "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"sv;
  153. break;
  154. // -> "json"
  155. case Infrastructure::Request::Destination::JSON:
  156. // `application/json,*/*;q=0.5`
  157. value = "application/json,*/*;q=0.5"sv;
  158. break;
  159. // -> "style"
  160. case Infrastructure::Request::Destination::Style:
  161. // `text/css,*/*;q=0.1`
  162. value = "text/css,*/*;q=0.1"sv;
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. // 3. Append (`Accept`, value) to request’s header list.
  169. auto header = Infrastructure::Header::from_string_pair("Accept"sv, value.bytes());
  170. request.header_list()->append(move(header));
  171. }
  172. // 14. If request’s header list does not contain `Accept-Language`, then user agents should append
  173. // (`Accept-Language, an appropriate header value) to request’s header list.
  174. if (!request.header_list()->contains("Accept-Language"sv.bytes())) {
  175. auto header = Infrastructure::Header::from_string_pair("Accept-Language"sv, "*"sv);
  176. request.header_list()->append(move(header));
  177. }
  178. // 15. If request’s priority is null, then use request’s initiator, destination, and render-blocking appropriately
  179. // in setting request’s priority to a user-agent-defined object.
  180. // NOTE: The user-agent-defined object could encompass stream weight and dependency for HTTP/2, and equivalent
  181. // information used to prioritize dispatch and processing of HTTP/1 fetches.
  182. // 16. If request is a subresource request, then:
  183. if (request.is_subresource_request()) {
  184. // FIXME: 1. Let record be a new fetch record whose request is request and controller is fetchParams’s controller.
  185. // FIXME: 2. Append record to request’s client’s fetch group list of fetch records.
  186. }
  187. // 17. Run main fetch given fetchParams.
  188. (void)TRY(main_fetch(realm, fetch_params));
  189. // 18. Return fetchParams’s controller.
  190. return fetch_params->controller();
  191. }
  192. // https://fetch.spec.whatwg.org/#concept-main-fetch
  193. WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> main_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Recursive recursive)
  194. {
  195. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' with: fetch_params @ {}", &fetch_params);
  196. auto& vm = realm.vm();
  197. // 1. Let request be fetchParams’s request.
  198. auto request = fetch_params.request();
  199. // 2. Let response be null.
  200. JS::GCPtr<Infrastructure::Response> response;
  201. // 3. If request’s local-URLs-only flag is set and request’s current URL is not local, then set response to a
  202. // network error.
  203. if (request->local_urls_only() && !Infrastructure::is_local_url(request->current_url()))
  204. response = Infrastructure::Response::network_error(vm, "Request with 'local-URLs-only' flag must have a local URL"sv);
  205. // FIXME: 4. Run report Content Security Policy violations for request.
  206. // FIXME: 5. Upgrade request to a potentially trustworthy URL, if appropriate.
  207. // FIXME: 6. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate.
  208. // 7. If should request be blocked due to a bad port, should fetching request be blocked as mixed content, or
  209. // should request be blocked by Content Security Policy returns blocked, then set response to a network error.
  210. if (Infrastructure::block_bad_port(request) == Infrastructure::RequestOrResponseBlocking::Blocked
  211. || false // FIXME: "should fetching request be blocked as mixed content"
  212. || false // FIXME: "should request be blocked by Content Security Policy returns blocked"
  213. ) {
  214. response = Infrastructure::Response::network_error(vm, "Request was blocked"sv);
  215. }
  216. // 8. If request’s referrer policy is the empty string, then set request’s referrer policy to request’s policy
  217. // container’s referrer policy.
  218. if (request->referrer_policy() == ReferrerPolicy::ReferrerPolicy::EmptyString) {
  219. VERIFY(request->policy_container().has<HTML::PolicyContainer>());
  220. request->set_referrer_policy(request->policy_container().get<HTML::PolicyContainer>().referrer_policy);
  221. }
  222. // 9. If request’s referrer is not "no-referrer", then set request’s referrer to the result of invoking determine
  223. // request’s referrer.
  224. // NOTE: As stated in Referrer Policy, user agents can provide the end user with options to override request’s
  225. // referrer to "no-referrer" or have it expose less sensitive information.
  226. auto const* referrer = request->referrer().get_pointer<Infrastructure::Request::Referrer>();
  227. if (!referrer || *referrer != Infrastructure::Request::Referrer::NoReferrer) {
  228. auto determined_referrer = ReferrerPolicy::determine_requests_referrer(request);
  229. if (determined_referrer.has_value())
  230. request->set_referrer(*determined_referrer);
  231. else
  232. request->set_referrer(Infrastructure::Request::Referrer::NoReferrer);
  233. }
  234. // 10. Set request’s current URL’s scheme to "https" if all of the following conditions are true:
  235. if (
  236. // - request’s current URL’s scheme is "http"
  237. request->current_url().scheme() == "http"sv
  238. // - request’s current URL’s host is a domain
  239. && DOMURL::host_is_domain(request->current_url().host())
  240. // FIXME: - Matching request’s current URL’s host per Known HSTS Host Domain Name Matching results in either a
  241. // superdomain match with an asserted includeSubDomains directive or a congruent match (with or without an
  242. // asserted includeSubDomains directive) [HSTS]; or DNS resolution for the request finds a matching HTTPS RR
  243. // per section 9.5 of [SVCB].
  244. && false
  245. ) {
  246. request->current_url().set_scheme("https"_string);
  247. }
  248. JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>>()> get_response = [&realm, &vm, &fetch_params, request]() -> WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> {
  249. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' get_response() function");
  250. // -> fetchParams’s preloaded response candidate is not null
  251. if (!fetch_params.preloaded_response_candidate().has<Empty>()) {
  252. // 1. Wait until fetchParams’s preloaded response candidate is not "pending".
  253. HTML::main_thread_event_loop().spin_until([&] {
  254. return !fetch_params.preloaded_response_candidate().has<Infrastructure::FetchParams::PreloadedResponseCandidatePendingTag>();
  255. });
  256. // 2. Assert: fetchParams’s preloaded response candidate is a response.
  257. VERIFY(fetch_params.preloaded_response_candidate().has<JS::NonnullGCPtr<Infrastructure::Response>>());
  258. // 3. Return fetchParams’s preloaded response candidate.
  259. return PendingResponse::create(vm, request, fetch_params.preloaded_response_candidate().get<JS::NonnullGCPtr<Infrastructure::Response>>());
  260. }
  261. // -> request’s current URL’s origin is same origin with request’s origin, and request’s response tainting
  262. // is "basic"
  263. // -> request’s current URL’s scheme is "data"
  264. // -> request’s mode is "navigate" or "websocket"
  265. else if (
  266. (request->origin().has<HTML::Origin>() && DOMURL::url_origin(request->current_url()).is_same_origin(request->origin().get<HTML::Origin>()) && request->response_tainting() == Infrastructure::Request::ResponseTainting::Basic)
  267. || request->current_url().scheme() == "data"sv
  268. || (request->mode() == Infrastructure::Request::Mode::Navigate || request->mode() == Infrastructure::Request::Mode::WebSocket)) {
  269. // 1. Set request’s response tainting to "basic".
  270. request->set_response_tainting(Infrastructure::Request::ResponseTainting::Basic);
  271. // 2. Return the result of running scheme fetch given fetchParams.
  272. return scheme_fetch(realm, fetch_params);
  273. // NOTE: HTML assigns any documents and workers created from URLs whose scheme is "data" a unique
  274. // opaque origin. Service workers can only be created from URLs whose scheme is an HTTP(S) scheme.
  275. }
  276. // -> request’s mode is "same-origin"
  277. else if (request->mode() == Infrastructure::Request::Mode::SameOrigin) {
  278. // Return a network error.
  279. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'same-origin' mode must have same URL and request origin"sv));
  280. }
  281. // -> request’s mode is "no-cors"
  282. else if (request->mode() == Infrastructure::Request::Mode::NoCORS) {
  283. // 1. If request’s redirect mode is not "follow", then return a network error.
  284. if (request->redirect_mode() != Infrastructure::Request::RedirectMode::Follow)
  285. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'no-cors' mode must have redirect mode set to 'follow'"sv));
  286. // 2. Set request’s response tainting to "opaque".
  287. request->set_response_tainting(Infrastructure::Request::ResponseTainting::Opaque);
  288. // 3. Return the result of running scheme fetch given fetchParams.
  289. return scheme_fetch(realm, fetch_params);
  290. }
  291. // -> request’s current URL’s scheme is not an HTTP(S) scheme
  292. else if (!Infrastructure::is_http_or_https_scheme(request->current_url().scheme())) {
  293. // NOTE: At this point all other request modes have been handled. Ensure we're not lying in the error message :^)
  294. VERIFY(request->mode() == Infrastructure::Request::Mode::CORS);
  295. // Return a network error.
  296. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' mode must have URL with HTTP or HTTPS scheme"sv));
  297. }
  298. // -> request’s use-CORS-preflight flag is set
  299. // -> request’s unsafe-request flag is set and either request’s method is not a CORS-safelisted method or
  300. // CORS-unsafe request-header names with request’s header list is not empty
  301. else if (
  302. request->use_cors_preflight()
  303. || (request->unsafe_request()
  304. && (!Infrastructure::is_cors_safelisted_method(request->method())
  305. || !Infrastructure::get_cors_unsafe_header_names(request->header_list()).is_empty()))) {
  306. // 1. Set request’s response tainting to "cors".
  307. request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  308. auto returned_pending_response = PendingResponse::create(vm, request);
  309. // 2. Let corsWithPreflightResponse be the result of running HTTP fetch given fetchParams and true.
  310. auto cors_with_preflight_response = TRY(http_fetch(realm, fetch_params, MakeCORSPreflight::Yes));
  311. cors_with_preflight_response->when_loaded([returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> cors_with_preflight_response) {
  312. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' cors_with_preflight_response load callback");
  313. // 3. If corsWithPreflightResponse is a network error, then clear cache entries using request.
  314. if (cors_with_preflight_response->is_network_error()) {
  315. // FIXME: Clear cache entries
  316. }
  317. // 4. Return corsWithPreflightResponse.
  318. returned_pending_response->resolve(cors_with_preflight_response);
  319. });
  320. return returned_pending_response;
  321. }
  322. // -> Otherwise
  323. else {
  324. // 1. Set request’s response tainting to "cors".
  325. request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  326. // 2. Return the result of running HTTP fetch given fetchParams.
  327. return http_fetch(realm, fetch_params);
  328. }
  329. };
  330. if (recursive == Recursive::Yes) {
  331. // 12. If response is null, then set response to the result of running the steps corresponding to the first
  332. // matching statement:
  333. auto pending_response = !response
  334. ? TRY(get_response())
  335. : PendingResponse::create(vm, request, *response);
  336. // 13. If recursive is true, then return response.
  337. return pending_response;
  338. }
  339. // 11. If recursive is false, then run the remaining steps in parallel.
  340. Platform::EventLoopPlugin::the().deferred_invoke([&realm, &vm, &fetch_params, request, response, get_response = move(get_response)] {
  341. // 12. If response is null, then set response to the result of running the steps corresponding to the first
  342. // matching statement:
  343. auto pending_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  344. if (!response) {
  345. auto pending_response_or_error = get_response();
  346. if (pending_response_or_error.is_error())
  347. return;
  348. pending_response = pending_response_or_error.release_value();
  349. }
  350. pending_response->when_loaded([&realm, &vm, &fetch_params, request, response, response_was_null = !response](JS::NonnullGCPtr<Infrastructure::Response> resolved_response) mutable {
  351. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' pending_response load callback");
  352. if (response_was_null)
  353. response = resolved_response;
  354. // 14. If response is not a network error and response is not a filtered response, then:
  355. if (!response->is_network_error() && !is<Infrastructure::FilteredResponse>(*response)) {
  356. // 1. If request’s response tainting is "cors", then:
  357. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS) {
  358. // 1. Let headerNames be the result of extracting header list values given
  359. // `Access-Control-Expose-Headers` and response’s header list.
  360. auto header_names_or_failure = Infrastructure::extract_header_list_values("Access-Control-Expose-Headers"sv.bytes(), response->header_list());
  361. auto header_names = header_names_or_failure.has<Vector<ByteBuffer>>() ? header_names_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
  362. // 2. If request’s credentials mode is not "include" and headerNames contains `*`, then set
  363. // response’s CORS-exposed header-name list to all unique header names in response’s header
  364. // list.
  365. if (request->credentials_mode() != Infrastructure::Request::CredentialsMode::Include && header_names.contains_slow("*"sv.bytes())) {
  366. auto unique_header_names = response->header_list()->unique_names();
  367. response->set_cors_exposed_header_name_list(move(unique_header_names));
  368. }
  369. // 3. Otherwise, if headerNames is not null or failure, then set response’s CORS-exposed
  370. // header-name list to headerNames.
  371. else if (!header_names.is_empty()) {
  372. response->set_cors_exposed_header_name_list(move(header_names));
  373. }
  374. }
  375. // 2. Set response to the following filtered response with response as its internal response, depending
  376. // on request’s response tainting:
  377. response = [&]() -> JS::NonnullGCPtr<Infrastructure::Response> {
  378. switch (request->response_tainting()) {
  379. // -> "basic"
  380. case Infrastructure::Request::ResponseTainting::Basic:
  381. // basic filtered response
  382. return Infrastructure::BasicFilteredResponse::create(vm, *response);
  383. // -> "cors"
  384. case Infrastructure::Request::ResponseTainting::CORS:
  385. // CORS filtered response
  386. return Infrastructure::CORSFilteredResponse::create(vm, *response);
  387. // -> "opaque"
  388. case Infrastructure::Request::ResponseTainting::Opaque:
  389. // opaque filtered response
  390. return Infrastructure::OpaqueFilteredResponse::create(vm, *response);
  391. default:
  392. VERIFY_NOT_REACHED();
  393. }
  394. }();
  395. }
  396. // 15. Let internalResponse be response, if response is a network error, and response’s internal response
  397. // otherwise.
  398. auto internal_response = response->is_network_error()
  399. ? JS::NonnullGCPtr { *response }
  400. : static_cast<Infrastructure::FilteredResponse&>(*response).internal_response();
  401. // 16. If internalResponse’s URL list is empty, then set it to a clone of request’s URL list.
  402. // NOTE: A response’s URL list can be empty (for example, when the response represents an about URL).
  403. if (internal_response->url_list().is_empty())
  404. internal_response->set_url_list(request->url_list());
  405. // 17. If request has a redirect-tainted origin, then set internalResponse’s has-cross-origin-redirects to true.
  406. if (request->has_redirect_tainted_origin())
  407. internal_response->set_has_cross_origin_redirects(true);
  408. // 18. If request’s timing allow failed flag is unset, then set internalResponse’s timing allow passed flag.
  409. if (!request->timing_allow_failed())
  410. internal_response->set_timing_allow_passed(true);
  411. // 19. If response is not a network error and any of the following returns blocked
  412. if (!response->is_network_error() && (
  413. // FIXME: - should internalResponse to request be blocked as mixed content
  414. false
  415. // FIXME: - should internalResponse to request be blocked by Content Security Policy
  416. || false
  417. // - should internalResponse to request be blocked due to its MIME type
  418. || Infrastructure::should_response_to_request_be_blocked_due_to_its_mime_type(internal_response, request) == Infrastructure::RequestOrResponseBlocking::Blocked
  419. // - should internalResponse to request be blocked due to nosniff
  420. || Infrastructure::should_response_to_request_be_blocked_due_to_nosniff(internal_response, request) == Infrastructure::RequestOrResponseBlocking::Blocked)) {
  421. // then set response and internalResponse to a network error.
  422. response = internal_response = Infrastructure::Response::network_error(vm, "Response was blocked"_string);
  423. }
  424. // 20. If response’s type is "opaque", internalResponse’s status is 206, internalResponse’s range-requested
  425. // flag is set, and request’s header list does not contain `Range`, then set response and
  426. // internalResponse to a network error.
  427. // NOTE: Traditionally, APIs accept a ranged response even if a range was not requested. This prevents a
  428. // partial response from an earlier ranged request being provided to an API that did not make a range
  429. // request.
  430. if (response->type() == Infrastructure::Response::Type::Opaque
  431. && internal_response->status() == 206
  432. && internal_response->range_requested()
  433. && !request->header_list()->contains("Range"sv.bytes())) {
  434. response = internal_response = Infrastructure::Response::network_error(vm, "Response has status 206 and 'range-requested' flag set, but request has no 'Range' header"_string);
  435. }
  436. // 21. If response is not a network error and either request’s method is `HEAD` or `CONNECT`, or
  437. // internalResponse’s status is a null body status, set internalResponse’s body to null and disregard
  438. // any enqueuing toward it (if any).
  439. // NOTE: This standardizes the error handling for servers that violate HTTP.
  440. if (!response->is_network_error() && (StringView { request->method() }.is_one_of("HEAD"sv, "CONNECT"sv) || Infrastructure::is_null_body_status(internal_response->status())))
  441. internal_response->set_body({});
  442. // 22. If request’s integrity metadata is not the empty string, then:
  443. if (!request->integrity_metadata().is_empty()) {
  444. // 1. Let processBodyError be this step: run fetch response handover given fetchParams and a network
  445. // error.
  446. auto process_body_error = JS::create_heap_function(vm.heap(), [&realm, &vm, &fetch_params](JS::Value) {
  447. fetch_response_handover(realm, fetch_params, Infrastructure::Response::network_error(vm, "Response body could not be processed"sv));
  448. });
  449. // 2. If response’s body is null, then run processBodyError and abort these steps.
  450. if (!response->body()) {
  451. process_body_error->function()({});
  452. return;
  453. }
  454. // 3. Let processBody given bytes be these steps:
  455. auto process_body = JS::create_heap_function(vm.heap(), [&realm, request, response, &fetch_params, process_body_error = move(process_body_error)](ByteBuffer bytes) {
  456. // 1. If bytes do not match request’s integrity metadata, then run processBodyError and abort these steps.
  457. if (!TRY_OR_IGNORE(SRI::do_bytes_match_metadata_list(bytes, request->integrity_metadata()))) {
  458. process_body_error->function()({});
  459. return;
  460. }
  461. // 2. Set response’s body to bytes as a body.
  462. response->set_body(TRY_OR_IGNORE(Infrastructure::byte_sequence_as_body(realm, bytes)));
  463. // 3. Run fetch response handover given fetchParams and response.
  464. fetch_response_handover(realm, fetch_params, *response);
  465. });
  466. // 4. Fully read response’s body given processBody and processBodyError.
  467. response->body()->fully_read(realm, process_body, process_body_error, fetch_params.task_destination());
  468. }
  469. // 23. Otherwise, run fetch response handover given fetchParams and response.
  470. else {
  471. fetch_response_handover(realm, fetch_params, *response);
  472. }
  473. });
  474. });
  475. return JS::GCPtr<PendingResponse> {};
  476. }
  477. // https://fetch.spec.whatwg.org/#fetch-finale
  478. void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Infrastructure::Response& response)
  479. {
  480. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'fetch response handover' with: fetch_params @ {}, response @ {}", &fetch_params, &response);
  481. auto& vm = realm.vm();
  482. // 1. Let timingInfo be fetchParams’s timing info.
  483. auto timing_info = fetch_params.timing_info();
  484. // 2. If response is not a network error and fetchParams’s request’s client is a secure context, then set
  485. // timingInfo’s server-timing headers to the result of getting, decoding, and splitting `Server-Timing` from
  486. // response’s header list.
  487. // The user agent may decide to expose `Server-Timing` headers to non-secure contexts requests as well.
  488. auto client = fetch_params.request()->client();
  489. if (!response.is_network_error() && client != nullptr && HTML::is_secure_context(*client)) {
  490. auto server_timing_headers = response.header_list()->get_decode_and_split("Server-Timing"sv.bytes());
  491. if (server_timing_headers.has_value())
  492. timing_info->set_server_timing_headers(server_timing_headers.release_value());
  493. }
  494. // 3. Let processResponseEndOfBody be the following steps:
  495. auto process_response_end_of_body = [&vm, &response, &fetch_params, timing_info] {
  496. // 1. Let unsafeEndTime be the unsafe shared current time.
  497. auto unsafe_end_time = HighResolutionTime::unsafe_shared_current_time();
  498. // 2. If fetchParams’s request’s destination is "document", then set fetchParams’s controller’s full timing
  499. // info to fetchParams’s timing info.
  500. if (fetch_params.request()->destination() == Infrastructure::Request::Destination::Document)
  501. fetch_params.controller()->set_full_timing_info(fetch_params.timing_info());
  502. // 3. Set fetchParams’s controller’s report timing steps to the following steps given a global object global:
  503. fetch_params.controller()->set_report_timing_steps([&vm, &response, &fetch_params, timing_info, unsafe_end_time](JS::Object const& global) mutable {
  504. // 1. If fetchParams’s request’s URL’s scheme is not an HTTP(S) scheme, then return.
  505. if (!Infrastructure::is_http_or_https_scheme(fetch_params.request()->url().scheme()))
  506. return;
  507. // 2. Set timingInfo’s end time to the relative high resolution time given unsafeEndTime and global.
  508. timing_info->set_end_time(HighResolutionTime::relative_high_resolution_time(unsafe_end_time, global));
  509. // 3. Let cacheState be response’s cache state.
  510. auto cache_state = response.cache_state();
  511. // 4. Let bodyInfo be response’s body info.
  512. auto body_info = response.body_info();
  513. // 5. If response’s timing allow passed flag is not set, then set timingInfo to the result of creating an
  514. // opaque timing info for timingInfo, set bodyInfo to a new response body info, and set cacheState to
  515. // the empty string.
  516. // NOTE: This covers the case of response being a network error.
  517. if (!response.timing_allow_passed()) {
  518. timing_info = Infrastructure::create_opaque_timing_info(vm, timing_info);
  519. body_info = Infrastructure::Response::BodyInfo {};
  520. cache_state = {};
  521. }
  522. // 6. Let responseStatus be 0.
  523. auto response_status = 0;
  524. // 7. If fetchParams’s request’s mode is not "navigate" or response’s has-cross-origin-redirects is false:
  525. if (fetch_params.request()->mode() != Infrastructure::Request::Mode::Navigate || !response.has_cross_origin_redirects()) {
  526. // 1. Set responseStatus to response’s status.
  527. response_status = response.status();
  528. // 2. Let mimeType be the result of extracting a MIME type from response’s header list.
  529. auto mime_type = response.header_list()->extract_mime_type();
  530. // 3. If mimeType is non-null, then set bodyInfo’s content type to the result of minimizing a supported MIME type given mimeType.
  531. if (mime_type.has_value())
  532. body_info.content_type = MimeSniff::minimise_a_supported_mime_type(mime_type.value());
  533. }
  534. // FIXME: 8. If fetchParams’s request’s initiator type is not null, then mark resource timing given timingInfo,
  535. // request’s URL, request’s initiator type, global, cacheState, bodyInfo, and responseStatus.
  536. (void)timing_info;
  537. (void)global;
  538. (void)cache_state;
  539. (void)body_info;
  540. (void)response_status;
  541. });
  542. // 4. Let processResponseEndOfBodyTask be the following steps:
  543. auto process_response_end_of_body_task = JS::create_heap_function(vm.heap(), [&fetch_params, &response] {
  544. // 1. Set fetchParams’s request’s done flag.
  545. fetch_params.request()->set_done(true);
  546. // 2. If fetchParams’s process response end-of-body is non-null, then run fetchParams’s process response
  547. // end-of-body given response.
  548. if (fetch_params.algorithms()->process_response_end_of_body())
  549. (fetch_params.algorithms()->process_response_end_of_body())(response);
  550. // 3. If fetchParams’s request’s initiator type is non-null and fetchParams’s request’s client’s global
  551. // object is fetchParams’s task destination, then run fetchParams’s controller’s report timing steps
  552. // given fetchParams’s request’s client’s global object.
  553. auto client = fetch_params.request()->client();
  554. auto const* task_destination_global_object = fetch_params.task_destination().get_pointer<JS::NonnullGCPtr<JS::Object>>();
  555. if (client != nullptr && task_destination_global_object != nullptr) {
  556. if (fetch_params.request()->initiator_type().has_value() && &client->global_object() == task_destination_global_object->ptr())
  557. fetch_params.controller()->report_timing(client->global_object());
  558. }
  559. });
  560. // FIXME: Handle 'parallel queue' task destination
  561. auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
  562. // 5. Queue a fetch task to run processResponseEndOfBodyTask with fetchParams’s task destination.
  563. Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, move(process_response_end_of_body_task));
  564. };
  565. // FIXME: Handle 'parallel queue' task destination
  566. auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
  567. // 4. If fetchParams’s process response is non-null, then queue a fetch task to run fetchParams’s process response
  568. // given response, with fetchParams’s task destination.
  569. if (fetch_params.algorithms()->process_response()) {
  570. Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, JS::create_heap_function(vm.heap(), [&fetch_params, &response]() {
  571. fetch_params.algorithms()->process_response()(response);
  572. }));
  573. }
  574. // 5. Let internalResponse be response, if response is a network error; otherwise response’s internal response.
  575. auto internal_response = response.is_network_error() ? JS::NonnullGCPtr { response } : response.unsafe_response();
  576. // 6. If internalResponse’s body is null, then run processResponseEndOfBody.
  577. if (!internal_response->body()) {
  578. process_response_end_of_body();
  579. }
  580. // 7. Otherwise:
  581. else {
  582. HTML::TemporaryExecutionContext const execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  583. // 1. Let transformStream be a new TransformStream.
  584. auto transform_stream = realm.heap().allocate<Streams::TransformStream>(realm, realm);
  585. // 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.
  586. auto identity_transform_algorithm = JS::create_heap_function(realm.heap(), [&realm, transform_stream](JS::Value chunk) -> JS::NonnullGCPtr<WebIDL::Promise> {
  587. MUST(Streams::transform_stream_default_controller_enqueue(*transform_stream->controller(), chunk));
  588. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  589. });
  590. // 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm set
  591. // to processResponseEndOfBody.
  592. auto flush_algorithm = JS::create_heap_function(realm.heap(), [&realm, process_response_end_of_body]() -> JS::NonnullGCPtr<WebIDL::Promise> {
  593. process_response_end_of_body();
  594. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  595. });
  596. Streams::transform_stream_set_up(transform_stream, identity_transform_algorithm, flush_algorithm);
  597. // 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.
  598. auto promise = Streams::readable_stream_pipe_to(internal_response->body()->stream(), transform_stream->writable(), false, false, false, {});
  599. WebIDL::mark_promise_as_handled(*promise);
  600. internal_response->body()->set_stream(transform_stream->readable());
  601. }
  602. // 8. If fetchParams’s process response consume body is non-null, then:
  603. if (fetch_params.algorithms()->process_response_consume_body()) {
  604. // 1. Let processBody given nullOrBytes be this step: run fetchParams’s process response consume body given
  605. // response and nullOrBytes.
  606. auto process_body = JS::create_heap_function(vm.heap(), [&fetch_params, &response](ByteBuffer null_or_bytes) {
  607. (fetch_params.algorithms()->process_response_consume_body())(response, null_or_bytes);
  608. });
  609. // 2. Let processBodyError be this step: run fetchParams’s process response consume body given response and
  610. // failure.
  611. auto process_body_error = JS::create_heap_function(vm.heap(), [&fetch_params, &response](JS::Value) {
  612. (fetch_params.algorithms()->process_response_consume_body())(response, Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag {});
  613. });
  614. // 3. If internalResponse's body is null, then queue a fetch task to run processBody given null, with
  615. // fetchParams’s task destination.
  616. if (!internal_response->body()) {
  617. Infrastructure::queue_fetch_task(fetch_params.controller(), task_destination, JS::create_heap_function(vm.heap(), [process_body = move(process_body)]() {
  618. process_body->function()({});
  619. }));
  620. }
  621. // 4. Otherwise, fully read internalResponse body given processBody, processBodyError, and fetchParams’s task
  622. // destination.
  623. else {
  624. internal_response->body()->fully_read(realm, process_body, process_body_error, fetch_params.task_destination());
  625. }
  626. }
  627. }
  628. // https://fetch.spec.whatwg.org/#concept-scheme-fetch
  629. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> scheme_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params)
  630. {
  631. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'scheme fetch' with: fetch_params @ {}", &fetch_params);
  632. auto& vm = realm.vm();
  633. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  634. if (fetch_params.is_canceled())
  635. return PendingResponse::create(vm, fetch_params.request(), Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  636. // 2. Let request be fetchParams’s request.
  637. auto request = fetch_params.request();
  638. // 3. Switch on request’s current URL’s scheme and run the associated steps:
  639. // -> "about"
  640. if (request->current_url().scheme() == "about"sv) {
  641. // If request’s current URL’s path is the string "blank", then return a new response whose status message is
  642. // `OK`, header list is « (`Content-Type`, `text/html;charset=utf-8`) », and body is the empty byte sequence as
  643. // a body.
  644. // NOTE: URLs such as "about:config" are handled during navigation and result in a network error in the context
  645. // of fetching.
  646. if (request->current_url().serialize_path() == "blank"sv) {
  647. auto response = Infrastructure::Response::create(vm);
  648. response->set_status_message(MUST(ByteBuffer::copy("OK"sv.bytes())));
  649. auto header = Infrastructure::Header::from_string_pair("Content-Type"sv, "text/html;charset=utf-8"sv);
  650. response->header_list()->append(move(header));
  651. response->set_body(MUST(Infrastructure::byte_sequence_as_body(realm, ""sv.bytes())));
  652. return PendingResponse::create(vm, request, response);
  653. }
  654. // FIXME: This is actually wrong, see note above.
  655. return TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, fetch_params));
  656. }
  657. // -> "blob"
  658. else if (request->current_url().scheme() == "blob"sv) {
  659. // 1. Let blobURLEntry be request’s current URL’s blob URL entry.
  660. auto const& blob_url_entry = request->current_url().blob_url_entry();
  661. // 2. If request’s method is not `GET`, blobURLEntry is null, or blobURLEntry’s object is not a Blob object,
  662. // then return a network error. [FILEAPI]
  663. if (request->method() != "GET"sv.bytes() || !blob_url_entry.has_value()) {
  664. // FIXME: Handle "blobURLEntry’s object is not a Blob object". It could be a MediaSource object, but we
  665. // have not yet implemented the Media Source Extensions spec.
  666. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has an invalid 'blob:' URL"sv));
  667. }
  668. // 3. Let blob be blobURLEntry’s object.
  669. auto const blob = FileAPI::Blob::create(realm, blob_url_entry.value().byte_buffer, blob_url_entry.value().type);
  670. // 4. Let response be a new response.
  671. auto response = Infrastructure::Response::create(vm);
  672. // 5. Let fullLength be blob’s size.
  673. auto full_length = blob->size();
  674. // 6. Let serializedFullLength be fullLength, serialized and isomorphic encoded.
  675. auto serialized_full_length = TRY_OR_THROW_OOM(vm, String::number(full_length));
  676. // 7. Let type be blob’s type.
  677. auto const& type = blob->type();
  678. // 8. If request’s header list does not contain `Range`:
  679. if (!request->header_list()->contains("Range"sv.bytes())) {
  680. // 1. Let bodyWithType be the result of safely extracting blob.
  681. auto body_with_type = TRY(safely_extract_body(realm, blob->bytes()));
  682. // 2. Set response’s status message to `OK`.
  683. response->set_status_message(MUST(ByteBuffer::copy("OK"sv.bytes())));
  684. // 3. Set response’s body to bodyWithType’s body.
  685. response->set_body(move(body_with_type.body));
  686. // 4. Set response’s header list to « (`Content-Length`, serializedFullLength), (`Content-Type`, type) ».
  687. auto content_length_header = Infrastructure::Header::from_string_pair("Content-Length"sv, serialized_full_length);
  688. response->header_list()->append(move(content_length_header));
  689. auto content_type_header = Infrastructure::Header::from_string_pair("Content-Type"sv, type);
  690. response->header_list()->append(move(content_type_header));
  691. }
  692. // FIXME: 9. Otherwise:
  693. else {
  694. // 1. Set response’s range-requested flag.
  695. // 2. Let rangeHeader be the result of getting `Range` from request’s header list.
  696. // 3. Let rangeValue be the result of parsing a single range header value given rangeHeader and true.
  697. // 4. If rangeValue is failure, then return a network error.
  698. // 5. Let (rangeStart, rangeEnd) be rangeValue.
  699. // 6. If rangeStart is null:
  700. // 1. Set rangeStart to fullLength − rangeEnd.
  701. // 2. Set rangeEnd to rangeStart + rangeEnd − 1.
  702. // 7. Otherwise:
  703. // 1. If rangeStart is greater than or equal to fullLength, then return a network error.
  704. // 2. If rangeEnd is null or rangeEnd is greater than or equal to fullLength, then set rangeEnd to fullLength − 1.
  705. // 8. Let slicedBlob be the result of invoking slice blob given blob, rangeStart, rangeEnd + 1, and type.
  706. // 9. Let slicedBodyWithType be the result of safely extracting slicedBlob.
  707. // 10. Set response’s body to slicedBodyWithType’s body.
  708. // 11. Let serializedSlicedLength be slicedBlob’s size, serialized and isomorphic encoded.
  709. // 12. Let contentRange be `bytes `.
  710. // 13. Append rangeStart, serialized and isomorphic encoded, to contentRange.
  711. // 14. Append 0x2D (-) to contentRange.
  712. // 15. Append rangeEnd, serialized and isomorphic encoded to contentRange.
  713. // 16. Append 0x2F (/) to contentRange.
  714. // 17. Append serializedFullLength to contentRange.
  715. // 18. Set response’s status to 206.
  716. // 19. Set response’s status message to `Partial Content`.
  717. // 20. Set response’s header list to « (`Content-Length`, serializedSlicedLength), (`Content-Type`, type), (`Content-Range`, contentRange) ».
  718. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has a 'blob:' URL with a Content-Range header, which is currently unsupported"sv));
  719. }
  720. // 10. Return response.
  721. return PendingResponse::create(vm, request, response);
  722. }
  723. // -> "data"
  724. else if (request->current_url().scheme() == "data"sv) {
  725. // 1. Let dataURLStruct be the result of running the data: URL processor on request’s current URL.
  726. auto data_url_struct = Infrastructure::process_data_url(request->current_url());
  727. // 2. If dataURLStruct is failure, then return a network error.
  728. if (data_url_struct.is_error())
  729. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Failed to process 'data:' URL"sv));
  730. // 3. Let mimeType be dataURLStruct’s MIME type, serialized.
  731. auto const& mime_type = MUST(data_url_struct.value().mime_type.serialized());
  732. // 4. Return a new response whose status message is `OK`, header list is « (`Content-Type`, mimeType) », and
  733. // body is dataURLStruct’s body as a body.
  734. auto response = Infrastructure::Response::create(vm);
  735. response->set_status_message(MUST(ByteBuffer::copy("OK"sv.bytes())));
  736. auto header = Infrastructure::Header::from_string_pair("Content-Type"sv, mime_type);
  737. response->header_list()->append(move(header));
  738. response->set_body(TRY(Infrastructure::byte_sequence_as_body(realm, data_url_struct.value().body)));
  739. return PendingResponse::create(vm, request, response);
  740. }
  741. // -> "file"
  742. else if (request->current_url().scheme() == "file"sv) {
  743. // For now, unfortunate as it is, file: URLs are left as an exercise for the reader.
  744. // When in doubt, return a network error.
  745. return TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, fetch_params));
  746. }
  747. // -> HTTP(S) scheme
  748. else if (Infrastructure::is_http_or_https_scheme(request->current_url().scheme())) {
  749. // Return the result of running HTTP fetch given fetchParams.
  750. return http_fetch(realm, fetch_params);
  751. }
  752. // AD-HOC: "resource"
  753. else if (request->current_url().scheme() == "resource"sv) {
  754. return TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, fetch_params));
  755. }
  756. // 4. Return a network error.
  757. auto message = request->current_url().scheme() == "about"sv
  758. ? "Request has invalid 'about:' URL, only 'about:blank' can be fetched"_string
  759. : "Request URL has invalid scheme, must be one of 'about', 'blob', 'data', 'file', 'http', or 'https'"_string;
  760. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, move(message)));
  761. }
  762. // https://fetch.spec.whatwg.org/#concept-http-fetch
  763. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, MakeCORSPreflight make_cors_preflight)
  764. {
  765. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' with: fetch_params @ {}, make_cors_preflight = {}",
  766. &fetch_params, make_cors_preflight == MakeCORSPreflight::Yes ? "Yes"sv : "No"sv);
  767. auto& vm = realm.vm();
  768. // 1. Let request be fetchParams’s request.
  769. auto request = fetch_params.request();
  770. // 2. Let response and internalResponse be null.
  771. JS::GCPtr<Infrastructure::Response> response;
  772. JS::GCPtr<Infrastructure::Response> internal_response;
  773. // 3. If request’s service-workers mode is "all", then:
  774. if (request->service_workers_mode() == Infrastructure::Request::ServiceWorkersMode::All) {
  775. // 1. Let requestForServiceWorker be a clone of request.
  776. auto request_for_service_worker = request->clone(realm);
  777. // 2. If requestForServiceWorker’s body is non-null, then:
  778. if (!request_for_service_worker->body().has<Empty>()) {
  779. // FIXME: 1. Let transformStream be a new TransformStream.
  780. // FIXME: 2. Let transformAlgorithm given chunk be these steps:
  781. // FIXME: 3. Set up transformStream with transformAlgorithm set to transformAlgorithm.
  782. // FIXME: 4. Set requestForServiceWorker’s body’s stream to the result of requestForServiceWorker’s body’s stream
  783. // piped through transformStream.
  784. }
  785. // 3. Let serviceWorkerStartTime be the coarsened shared current time given fetchParams’s cross-origin isolated
  786. // capability.
  787. auto service_worker_start_time = HighResolutionTime::coarsened_shared_current_time(fetch_params.cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  788. // FIXME: 4. Set response to the result of invoking handle fetch for requestForServiceWorker, with fetchParams’s
  789. // controller and fetchParams’s cross-origin isolated capability.
  790. // 5. If response is non-null, then:
  791. if (response) {
  792. // 1. Set fetchParams’s timing info’s final service worker start time to serviceWorkerStartTime.
  793. fetch_params.timing_info()->set_final_service_worker_start_time(service_worker_start_time);
  794. // 2. If request’s body is non-null, then cancel request’s body with undefined.
  795. if (!request->body().has<Empty>()) {
  796. // FIXME: Implement cancelling streams
  797. }
  798. // 3. Set internalResponse to response, if response is not a filtered response; otherwise to response’s
  799. // internal response.
  800. internal_response = !is<Infrastructure::FilteredResponse>(*response)
  801. ? JS::NonnullGCPtr { *response }
  802. : static_cast<Infrastructure::FilteredResponse const&>(*response).internal_response();
  803. // 4. If one of the following is true
  804. if (
  805. // - response’s type is "error"
  806. response->type() == Infrastructure::Response::Type::Error
  807. // - request’s mode is "same-origin" and response’s type is "cors"
  808. || (request->mode() == Infrastructure::Request::Mode::SameOrigin && response->type() == Infrastructure::Response::Type::CORS)
  809. // - request’s mode is not "no-cors" and response’s type is "opaque"
  810. || (request->mode() != Infrastructure::Request::Mode::NoCORS && response->type() == Infrastructure::Response::Type::Opaque)
  811. // - request’s redirect mode is not "manual" and response’s type is "opaqueredirect"
  812. || (request->redirect_mode() != Infrastructure::Request::RedirectMode::Manual && response->type() == Infrastructure::Response::Type::OpaqueRedirect)
  813. // - request’s redirect mode is not "follow" and response’s URL list has more than one item.
  814. || (request->redirect_mode() != Infrastructure::Request::RedirectMode::Follow && response->url_list().size() > 1)) {
  815. // then return a network error.
  816. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Invalid request/response state combination"sv));
  817. }
  818. }
  819. }
  820. JS::GCPtr<PendingResponse> pending_actual_response;
  821. auto returned_pending_response = PendingResponse::create(vm, request);
  822. // 4. If response is null, then:
  823. if (!response) {
  824. // 1. If makeCORSPreflight is true and one of these conditions is true:
  825. // NOTE: This step checks the CORS-preflight cache and if there is no suitable entry it performs a
  826. // CORS-preflight fetch which, if successful, populates the cache. The purpose of the CORS-preflight
  827. // fetch is to ensure the fetched resource is familiar with the CORS protocol. The cache is there to
  828. // minimize the number of CORS-preflight fetches.
  829. JS::GCPtr<PendingResponse> pending_preflight_response;
  830. if (make_cors_preflight == MakeCORSPreflight::Yes && (
  831. // - There is no method cache entry match for request’s method using request, and either request’s
  832. // method is not a CORS-safelisted method or request’s use-CORS-preflight flag is set.
  833. // FIXME: We currently have no cache, so there will always be no method cache entry.
  834. (!Infrastructure::is_cors_safelisted_method(request->method()) || request->use_cors_preflight())
  835. // - There is at least one item in the CORS-unsafe request-header names with request’s header list for
  836. // which there is no header-name cache entry match using request.
  837. // FIXME: We currently have no cache, so there will always be no header-name cache entry.
  838. || !Infrastructure::get_cors_unsafe_header_names(request->header_list()).is_empty())) {
  839. // 1. Let preflightResponse be the result of running CORS-preflight fetch given request.
  840. pending_preflight_response = TRY(cors_preflight_fetch(realm, request));
  841. // NOTE: Step 2 is performed in pending_preflight_response's load callback below.
  842. }
  843. auto fetch_main_content = [request = JS::make_handle(request), realm = JS::make_handle(realm), fetch_params = JS::make_handle(fetch_params)]() -> WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> {
  844. // 2. If request’s redirect mode is "follow", then set request’s service-workers mode to "none".
  845. // NOTE: Redirects coming from the network (as opposed to from a service worker) are not to be exposed to a
  846. // service worker.
  847. if (request->redirect_mode() == Infrastructure::Request::RedirectMode::Follow)
  848. request->set_service_workers_mode(Infrastructure::Request::ServiceWorkersMode::None);
  849. // 3. Set response and internalResponse to the result of running HTTP-network-or-cache fetch given fetchParams.
  850. return http_network_or_cache_fetch(*realm, *fetch_params);
  851. };
  852. if (pending_preflight_response) {
  853. pending_actual_response = PendingResponse::create(vm, request);
  854. pending_preflight_response->when_loaded([returned_pending_response, pending_actual_response, fetch_main_content = move(fetch_main_content)](JS::NonnullGCPtr<Infrastructure::Response> preflight_response) {
  855. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_preflight_response load callback");
  856. // 2. If preflightResponse is a network error, then return preflightResponse.
  857. if (preflight_response->is_network_error()) {
  858. returned_pending_response->resolve(preflight_response);
  859. return;
  860. }
  861. auto pending_main_content_response = TRY_OR_IGNORE(fetch_main_content());
  862. pending_main_content_response->when_loaded([pending_actual_response](JS::NonnullGCPtr<Infrastructure::Response> main_content_response) {
  863. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_main_content_response load callback");
  864. pending_actual_response->resolve(main_content_response);
  865. });
  866. });
  867. } else {
  868. pending_actual_response = TRY(fetch_main_content());
  869. }
  870. } else {
  871. pending_actual_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  872. }
  873. pending_actual_response->when_loaded([&realm, &vm, &fetch_params, request, response, internal_response, returned_pending_response, response_was_null = !response](JS::NonnullGCPtr<Infrastructure::Response> resolved_actual_response) mutable {
  874. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_actual_response load callback");
  875. if (response_was_null) {
  876. response = internal_response = resolved_actual_response;
  877. // 4. If request’s response tainting is "cors" and a CORS check for request and response returns failure,
  878. // then return a network error.
  879. // NOTE: As the CORS check is not to be applied to responses whose status is 304 or 407, or responses from
  880. // a service worker for that matter, it is applied here.
  881. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS
  882. && !cors_check(request, *response)) {
  883. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "Request with 'cors' response tainting failed CORS check"_string));
  884. return;
  885. }
  886. // 5. If the TAO check for request and response returns failure, then set request’s timing allow failed flag.
  887. if (!tao_check(request, *response))
  888. request->set_timing_allow_failed(true);
  889. }
  890. // 5. If either request’s response tainting or response’s type is "opaque", and the cross-origin resource
  891. // policy check with request’s origin, request’s client, request’s destination, and internalResponse returns
  892. // blocked, then return a network error.
  893. // NOTE: The cross-origin resource policy check runs for responses coming from the network and responses coming
  894. // from the service worker. This is different from the CORS check, as request’s client and the service
  895. // worker can have different embedder policies.
  896. if ((request->response_tainting() == Infrastructure::Request::ResponseTainting::Opaque || response->type() == Infrastructure::Response::Type::Opaque)
  897. && false // FIXME: "and the cross-origin resource policy check with request’s origin, request’s client, request’s destination, and actualResponse returns blocked"
  898. ) {
  899. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "Response was blocked by cross-origin resource policy check"_string));
  900. return;
  901. }
  902. JS::GCPtr<PendingResponse> inner_pending_response;
  903. // 6. If internalResponse’s status is a redirect status:
  904. if (Infrastructure::is_redirect_status(internal_response->status())) {
  905. // FIXME: 1. If internalResponse’s status is not 303, request’s body is non-null, and the connection uses HTTP/2,
  906. // then user agents may, and are even encouraged to, transmit an RST_STREAM frame.
  907. // NOTE: 303 is excluded as certain communities ascribe special status to it.
  908. // 2. Switch on request’s redirect mode:
  909. switch (request->redirect_mode()) {
  910. // -> "error"
  911. case Infrastructure::Request::RedirectMode::Error:
  912. // 1. Set response to a network error.
  913. response = Infrastructure::Response::network_error(vm, "Request with 'error' redirect mode received redirect response"_string);
  914. break;
  915. // -> "manual"
  916. case Infrastructure::Request::RedirectMode::Manual:
  917. // 1. If request’s mode is "navigate", then set fetchParams’s controller’s next manual redirect steps
  918. // to run HTTP-redirect fetch given fetchParams and response.
  919. if (request->mode() == Infrastructure::Request::Mode::Navigate) {
  920. fetch_params.controller()->set_next_manual_redirect_steps([&realm, &fetch_params, response] {
  921. (void)http_redirect_fetch(realm, fetch_params, *response);
  922. });
  923. }
  924. // 2. Otherwise, set response to an opaque-redirect filtered response whose internal response is
  925. // internalResponse.
  926. else {
  927. response = Infrastructure::OpaqueRedirectFilteredResponse::create(vm, *internal_response);
  928. }
  929. break;
  930. // -> "follow"
  931. case Infrastructure::Request::RedirectMode::Follow:
  932. // 1. Set response to the result of running HTTP-redirect fetch given fetchParams and response.
  933. inner_pending_response = TRY_OR_IGNORE(http_redirect_fetch(realm, fetch_params, *response));
  934. break;
  935. default:
  936. VERIFY_NOT_REACHED();
  937. }
  938. }
  939. if (inner_pending_response) {
  940. inner_pending_response->when_loaded([returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> response) {
  941. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' inner_pending_response load callback");
  942. returned_pending_response->resolve(response);
  943. });
  944. } else {
  945. returned_pending_response->resolve(*response);
  946. }
  947. });
  948. // 7. Return response.
  949. // NOTE: Typically internalResponse’s body’s stream is still being enqueued to after returning.
  950. return returned_pending_response;
  951. }
  952. // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch
  953. WebIDL::ExceptionOr<JS::GCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Infrastructure::Response& response)
  954. {
  955. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-redirect fetch' with: fetch_params @ {}, response = {}", &fetch_params, &response);
  956. auto& vm = realm.vm();
  957. // 1. Let request be fetchParams’s request.
  958. auto request = fetch_params.request();
  959. // 2. Let actualResponse be response, if response is not a filtered response, and response’s internal response
  960. // otherwise.
  961. auto actual_response = !is<Infrastructure::FilteredResponse>(response)
  962. ? JS::NonnullGCPtr { response }
  963. : static_cast<Infrastructure::FilteredResponse const&>(response).internal_response();
  964. // 3. Let locationURL be actualResponse’s location URL given request’s current URL’s fragment.
  965. auto location_url_or_error = actual_response->location_url(request->current_url().fragment());
  966. // 4. If locationURL is null, then return response.
  967. if (!location_url_or_error.is_error() && !location_url_or_error.value().has_value())
  968. return PendingResponse::create(vm, request, response);
  969. // 5. If locationURL is failure, then return a network error.
  970. if (location_url_or_error.is_error())
  971. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request redirect URL is invalid"sv));
  972. auto location_url = location_url_or_error.release_value().release_value();
  973. // 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network error.
  974. if (!Infrastructure::is_http_or_https_scheme(location_url.scheme()))
  975. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request redirect URL must have HTTP or HTTPS scheme"sv));
  976. // 7. If request’s redirect count is 20, then return a network error.
  977. if (request->redirect_count() == 20)
  978. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has reached maximum redirect count of 20"sv));
  979. // 8. Increase request’s redirect count by 1.
  980. request->set_redirect_count(request->redirect_count() + 1);
  981. // 8. If request’s mode is "cors", locationURL includes credentials, and request’s origin is not same origin with
  982. // locationURL’s origin, then return a network error.
  983. if (request->mode() == Infrastructure::Request::Mode::CORS
  984. && location_url.includes_credentials()
  985. && request->origin().has<HTML::Origin>()
  986. && !request->origin().get<HTML::Origin>().is_same_origin(DOMURL::url_origin(location_url))) {
  987. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' mode and different URL and request origin must not include credentials in redirect URL"sv));
  988. }
  989. // 10. If request’s response tainting is "cors" and locationURL includes credentials, then return a network error.
  990. // NOTE: This catches a cross-origin resource redirecting to a same-origin URL.
  991. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS && location_url.includes_credentials())
  992. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' response tainting must not include credentials in redirect URL"sv));
  993. // 11. If actualResponse’s status is not 303, request’s body is non-null, and request’s body’s source is null, then
  994. // return a network error.
  995. if (actual_response->status() != 303
  996. && !request->body().has<Empty>()
  997. && request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source().has<Empty>()) {
  998. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has body but no body source"sv));
  999. }
  1000. // 12. If one of the following is true
  1001. if (
  1002. // - actualResponse’s status is 301 or 302 and request’s method is `POST`
  1003. ((actual_response->status() == 301 || actual_response->status() == 302) && request->method() == "POST"sv.bytes())
  1004. // - actualResponse’s status is 303 and request’s method is not `GET` or `HEAD`
  1005. || (actual_response->status() == 303 && !(request->method() == "GET"sv.bytes() || request->method() == "HEAD"sv.bytes()))
  1006. // then:
  1007. ) {
  1008. // 1. Set request’s method to `GET` and request’s body to null.
  1009. request->set_method(MUST(ByteBuffer::copy("GET"sv.bytes())));
  1010. request->set_body({});
  1011. static constexpr Array request_body_header_names {
  1012. "Content-Encoding"sv,
  1013. "Content-Language"sv,
  1014. "Content-Location"sv,
  1015. "Content-Type"sv
  1016. };
  1017. // 2. For each headerName of request-body-header name, delete headerName from request’s header list.
  1018. for (auto header_name : request_body_header_names.span())
  1019. request->header_list()->delete_(header_name.bytes());
  1020. }
  1021. // 13. If request’s current URL’s origin is not same origin with locationURL’s origin, then for each headerName of
  1022. // CORS non-wildcard request-header name, delete headerName from request’s header list.
  1023. // NOTE: I.e., the moment another origin is seen after the initial request, the `Authorization` header is removed.
  1024. if (!DOMURL::url_origin(request->current_url()).is_same_origin(DOMURL::url_origin(location_url))) {
  1025. static constexpr Array cors_non_wildcard_request_header_names {
  1026. "Authorization"sv
  1027. };
  1028. for (auto header_name : cors_non_wildcard_request_header_names)
  1029. request->header_list()->delete_(header_name.bytes());
  1030. }
  1031. // 14. If request’s body is non-null, then set request’s body to the body of the result of safely extracting
  1032. // request’s body’s source.
  1033. // NOTE: request’s body’s source’s nullity has already been checked.
  1034. if (!request->body().has<Empty>()) {
  1035. auto const& source = request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source();
  1036. // NOTE: BodyInitOrReadableBytes is a superset of Body::SourceType
  1037. auto converted_source = source.has<ByteBuffer>()
  1038. ? BodyInitOrReadableBytes { source.get<ByteBuffer>() }
  1039. : BodyInitOrReadableBytes { source.get<JS::Handle<FileAPI::Blob>>() };
  1040. auto [body, _] = TRY(safely_extract_body(realm, converted_source));
  1041. request->set_body(move(body));
  1042. }
  1043. // 15. Let timingInfo be fetchParams’s timing info.
  1044. auto timing_info = fetch_params.timing_info();
  1045. // 16. Set timingInfo’s redirect end time and post-redirect start time to the coarsened shared current time given
  1046. // fetchParams’s cross-origin isolated capability.
  1047. auto now = HighResolutionTime::coarsened_shared_current_time(fetch_params.cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  1048. timing_info->set_redirect_end_time(now);
  1049. timing_info->set_post_redirect_start_time(now);
  1050. // 17. If timingInfo’s redirect start time is 0, then set timingInfo’s redirect start time to timingInfo’s start
  1051. // time.
  1052. if (timing_info->redirect_start_time() == 0)
  1053. timing_info->set_redirect_start_time(timing_info->start_time());
  1054. // 18. Append locationURL to request’s URL list.
  1055. request->url_list().append(location_url);
  1056. // FIXME: 19. Invoke set request’s referrer policy on redirect on request and actualResponse.
  1057. // 20. Let recursive be true.
  1058. auto recursive = Recursive::Yes;
  1059. // 21. If request’s redirect mode is "manual", then:
  1060. if (request->redirect_mode() == Infrastructure::Request::RedirectMode::Manual) {
  1061. // 1. Assert: request’s mode is "navigate".
  1062. VERIFY(request->mode() == Infrastructure::Request::Mode::Navigate);
  1063. // 2. Set recursive to false.
  1064. recursive = Recursive::No;
  1065. }
  1066. // 22. Return the result of running main fetch given fetchParams and recursive.
  1067. return main_fetch(realm, fetch_params, recursive);
  1068. }
  1069. // https://fetch.spec.whatwg.org/#network-partition-key
  1070. struct NetworkPartitionKey {
  1071. HTML::Origin top_level_origin;
  1072. // FIXME: See https://github.com/whatwg/fetch/issues/1035
  1073. // This is the document origin in other browsers
  1074. void* second_key = nullptr;
  1075. bool operator==(NetworkPartitionKey const&) const = default;
  1076. };
  1077. }
  1078. // FIXME: Take this with us to the eventual header these structs end up in to avoid closing and re-opening the namespace.
  1079. template<>
  1080. class AK::Traits<Web::Fetch::Fetching::NetworkPartitionKey> : public DefaultTraits<Web::Fetch::Fetching::NetworkPartitionKey> {
  1081. public:
  1082. static unsigned hash(Web::Fetch::Fetching::NetworkPartitionKey const& partition_key)
  1083. {
  1084. return ::AK::Traits<Web::HTML::Origin>::hash(partition_key.top_level_origin);
  1085. }
  1086. };
  1087. namespace Web::Fetch::Fetching {
  1088. struct CachedResponse {
  1089. Vector<Infrastructure::Header> headers;
  1090. ByteBuffer body;
  1091. ByteBuffer method;
  1092. URL::URL url;
  1093. UnixDateTime current_age;
  1094. };
  1095. class CachePartition {
  1096. public:
  1097. // FIXME: Copy the headers... less
  1098. Optional<CachedResponse> select_response(URL::URL const& url, ReadonlyBytes method, Vector<Infrastructure::Header> const& headers) const
  1099. {
  1100. auto it = m_cache.find(url);
  1101. if (it == m_cache.end())
  1102. return {};
  1103. auto const& cached_response = it->value;
  1104. // FIXME: Validate headers and method
  1105. (void)method;
  1106. (void)headers;
  1107. return cached_response;
  1108. }
  1109. private:
  1110. HashMap<URL::URL, CachedResponse> m_cache;
  1111. };
  1112. class HTTPCache {
  1113. public:
  1114. CachePartition& get(NetworkPartitionKey const& key)
  1115. {
  1116. return *m_cache.ensure(key, [] {
  1117. return make<CachePartition>();
  1118. });
  1119. }
  1120. static HTTPCache& the()
  1121. {
  1122. static HTTPCache s_cache;
  1123. return s_cache;
  1124. }
  1125. private:
  1126. HashMap<NetworkPartitionKey, NonnullOwnPtr<CachePartition>> m_cache;
  1127. };
  1128. // https://fetch.spec.whatwg.org/#determine-the-network-partition-key
  1129. static NetworkPartitionKey determine_the_network_partition_key(HTML::Environment const& environment)
  1130. {
  1131. // 1. Let topLevelOrigin be environment’s top-level origin.
  1132. auto top_level_origin = environment.top_level_origin;
  1133. // FIXME: 2. If topLevelOrigin is null, then set topLevelOrigin to environment’s top-level creation URL’s origin
  1134. // This field is supposed to be nullable
  1135. // 3. Assert: topLevelOrigin is an origin.
  1136. // FIXME: 4. Let topLevelSite be the result of obtaining a site, given topLevelOrigin.
  1137. // 5. Let secondKey be null or an implementation-defined value.
  1138. void* second_key = nullptr;
  1139. // 6. Return (topLevelSite, secondKey).
  1140. return { top_level_origin, second_key };
  1141. }
  1142. // https://fetch.spec.whatwg.org/#request-determine-the-network-partition-key
  1143. static Optional<NetworkPartitionKey> determine_the_network_partition_key(Infrastructure::Request const& request)
  1144. {
  1145. // 1. If request’s reserved client is non-null, then return the result of determining the network partition key given request’s reserved client.
  1146. if (auto reserved_client = request.reserved_client())
  1147. return determine_the_network_partition_key(*reserved_client);
  1148. // 2. If request’s client is non-null, then return the result of determining the network partition key given request’s client.
  1149. if (auto client = request.client())
  1150. return determine_the_network_partition_key(*client);
  1151. return {};
  1152. }
  1153. // https://fetch.spec.whatwg.org/#determine-the-http-cache-partition
  1154. static Optional<CachePartition> determine_the_http_cache_partition(Infrastructure::Request const& request)
  1155. {
  1156. // 1. Let key be the result of determining the network partition key given request.
  1157. auto key = determine_the_network_partition_key(request);
  1158. // 2. If key is null, then return null.
  1159. if (!key.has_value())
  1160. return OptionalNone {};
  1161. // 3. Return the unique HTTP cache associated with key. [HTTP-CACHING]
  1162. return HTTPCache::the().get(key.value());
  1163. }
  1164. // https://fetch.spec.whatwg.org/#concept-http-network-or-cache-fetch
  1165. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, IsAuthenticationFetch is_authentication_fetch, IsNewConnectionFetch is_new_connection_fetch)
  1166. {
  1167. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-network-or-cache fetch' with: fetch_params @ {}, is_authentication_fetch = {}, is_new_connection_fetch = {}",
  1168. &fetch_params, is_authentication_fetch == IsAuthenticationFetch::Yes ? "Yes"sv : "No"sv, is_new_connection_fetch == IsNewConnectionFetch::Yes ? "Yes"sv : "No"sv);
  1169. auto& vm = realm.vm();
  1170. // 1. Let request be fetchParams’s request.
  1171. auto request = fetch_params.request();
  1172. // 2. Let httpFetchParams be null.
  1173. JS::GCPtr<Infrastructure::FetchParams const> http_fetch_params;
  1174. // 3. Let httpRequest be null.
  1175. JS::GCPtr<Infrastructure::Request> http_request;
  1176. // 4. Let response be null.
  1177. JS::GCPtr<Infrastructure::Response> response;
  1178. // 5. Let storedResponse be null.
  1179. JS::GCPtr<Infrastructure::Response> stored_response;
  1180. // 6. Let httpCache be null.
  1181. // (Typeless until we actually implement it, needed for checks below)
  1182. Optional<CachePartition> http_cache;
  1183. // 7. Let the revalidatingFlag be unset.
  1184. auto revalidating_flag = RefCountedFlag::create(false);
  1185. auto include_credentials = IncludeCredentials::No;
  1186. // 8. Run these steps, but abort when fetchParams is canceled:
  1187. // NOTE: There's an 'if aborted' check after this anyway, so not doing this is fine and only incurs a small delay.
  1188. // For now, support for aborting fetch requests is limited anyway as ResourceLoader doesn't support it.
  1189. auto aborted = false;
  1190. {
  1191. ScopeGuard set_aborted = [&] {
  1192. if (fetch_params.is_canceled())
  1193. aborted = true;
  1194. };
  1195. // 1. If request’s window is "no-window" and request’s redirect mode is "error", then set httpFetchParams to
  1196. // fetchParams and httpRequest to request.
  1197. if (request->window().has<Infrastructure::Request::Window>()
  1198. && request->window().get<Infrastructure::Request::Window>() == Infrastructure::Request::Window::NoWindow
  1199. && request->redirect_mode() == Infrastructure::Request::RedirectMode::Error) {
  1200. http_fetch_params = fetch_params;
  1201. http_request = request;
  1202. }
  1203. // 2. Otherwise:
  1204. else {
  1205. // 1. Set httpRequest to a clone of request.
  1206. // NOTE: Implementations are encouraged to avoid teeing request’s body’s stream when request’s body’s
  1207. // source is null as only a single body is needed in that case. E.g., when request’s body’s source
  1208. // is null, redirects and authentication will end up failing the fetch.
  1209. http_request = request->clone(realm);
  1210. // 2. Set httpFetchParams to a copy of fetchParams.
  1211. // 3. Set httpFetchParams’s request to httpRequest.
  1212. auto new_http_fetch_params = Infrastructure::FetchParams::create(vm, *http_request, fetch_params.timing_info());
  1213. new_http_fetch_params->set_algorithms(fetch_params.algorithms());
  1214. new_http_fetch_params->set_task_destination(fetch_params.task_destination());
  1215. new_http_fetch_params->set_cross_origin_isolated_capability(fetch_params.cross_origin_isolated_capability());
  1216. new_http_fetch_params->set_preloaded_response_candidate(fetch_params.preloaded_response_candidate());
  1217. http_fetch_params = new_http_fetch_params;
  1218. }
  1219. // 3. Let includeCredentials be true if one of
  1220. if (
  1221. // - request’s credentials mode is "include"
  1222. request->credentials_mode() == Infrastructure::Request::CredentialsMode::Include
  1223. // - request’s credentials mode is "same-origin" and request’s response tainting is "basic"
  1224. || (request->credentials_mode() == Infrastructure::Request::CredentialsMode::SameOrigin
  1225. && request->response_tainting() == Infrastructure::Request::ResponseTainting::Basic)
  1226. // is true; otherwise false.
  1227. ) {
  1228. include_credentials = IncludeCredentials::Yes;
  1229. } else {
  1230. include_credentials = IncludeCredentials::No;
  1231. }
  1232. // 4. If Cross-Origin-Embedder-Policy allows credentials with request returns false, then set
  1233. // includeCredentials to false.
  1234. if (!request->cross_origin_embedder_policy_allows_credentials())
  1235. include_credentials = IncludeCredentials::No;
  1236. // 5. Let contentLength be httpRequest’s body’s length, if httpRequest’s body is non-null; otherwise null.
  1237. auto content_length = http_request->body().has<JS::NonnullGCPtr<Infrastructure::Body>>()
  1238. ? http_request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->length()
  1239. : Optional<u64> {};
  1240. // 6. Let contentLengthHeaderValue be null.
  1241. auto content_length_header_value = Optional<ByteBuffer> {};
  1242. // 7. If httpRequest’s body is null and httpRequest’s method is `POST` or `PUT`, then set
  1243. // contentLengthHeaderValue to `0`.
  1244. if (http_request->body().has<Empty>() && StringView { http_request->method() }.is_one_of("POST"sv, "PUT"sv))
  1245. content_length_header_value = MUST(ByteBuffer::copy("0"sv.bytes()));
  1246. // 8. If contentLength is non-null, then set contentLengthHeaderValue to contentLength, serialized and
  1247. // isomorphic encoded.
  1248. if (content_length.has_value())
  1249. content_length_header_value = MUST(ByteBuffer::copy(TRY_OR_THROW_OOM(vm, String::number(*content_length)).bytes()));
  1250. // 9. If contentLengthHeaderValue is non-null, then append (`Content-Length`, contentLengthHeaderValue) to
  1251. // httpRequest’s header list.
  1252. if (content_length_header_value.has_value()) {
  1253. auto header = Infrastructure::Header {
  1254. .name = MUST(ByteBuffer::copy("Content-Length"sv.bytes())),
  1255. .value = content_length_header_value.release_value(),
  1256. };
  1257. http_request->header_list()->append(move(header));
  1258. }
  1259. // FIXME: 10. If contentLength is non-null and httpRequest’s keepalive is true, then:
  1260. if (content_length.has_value() && http_request->keepalive()) {
  1261. // FIXME: 1-5., requires 'fetch records' and 'fetch group' concepts.
  1262. // NOTE: The above limit ensures that requests that are allowed to outlive the environment settings object
  1263. // and contain a body, have a bounded size and are not allowed to stay alive indefinitely.
  1264. }
  1265. // 11. If httpRequest’s referrer is a URL, then:
  1266. if (http_request->referrer().has<URL::URL>()) {
  1267. // 1. Let referrerValue be httpRequest’s referrer, serialized and isomorphic encoded.
  1268. auto referrer_string = http_request->referrer().get<URL::URL>().serialize();
  1269. auto referrer_value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(referrer_string.bytes()));
  1270. // 2. Append (`Referer`, referrerValue) to httpRequest’s header list.
  1271. auto header = Infrastructure::Header {
  1272. .name = MUST(ByteBuffer::copy("Referer"sv.bytes())),
  1273. .value = move(referrer_value),
  1274. };
  1275. http_request->header_list()->append(move(header));
  1276. }
  1277. // 12. Append a request `Origin` header for httpRequest.
  1278. http_request->add_origin_header();
  1279. // 13. Append the Fetch metadata headers for httpRequest.
  1280. append_fetch_metadata_headers_for_request(*http_request);
  1281. // 14. FIXME If httpRequest’s initiator is "prefetch", then set a structured field value
  1282. // given (`Sec-Purpose`, the token prefetch) in httpRequest’s header list.
  1283. // 15. If httpRequest’s header list does not contain `User-Agent`, then user agents should append
  1284. // (`User-Agent`, default `User-Agent` value) to httpRequest’s header list.
  1285. if (!http_request->header_list()->contains("User-Agent"sv.bytes())) {
  1286. auto header = Infrastructure::Header {
  1287. .name = MUST(ByteBuffer::copy("User-Agent"sv.bytes())),
  1288. .value = Infrastructure::default_user_agent_value(),
  1289. };
  1290. http_request->header_list()->append(move(header));
  1291. }
  1292. // 16. If httpRequest’s cache mode is "default" and httpRequest’s header list contains `If-Modified-Since`,
  1293. // `If-None-Match`, `If-Unmodified-Since`, `If-Match`, or `If-Range`, then set httpRequest’s cache mode to
  1294. // "no-store".
  1295. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::Default
  1296. && (http_request->header_list()->contains("If-Modified-Since"sv.bytes())
  1297. || http_request->header_list()->contains("If-None-Match"sv.bytes())
  1298. || http_request->header_list()->contains("If-Unmodified-Since"sv.bytes())
  1299. || http_request->header_list()->contains("If-Match"sv.bytes())
  1300. || http_request->header_list()->contains("If-Range"sv.bytes()))) {
  1301. http_request->set_cache_mode(Infrastructure::Request::CacheMode::NoStore);
  1302. }
  1303. // 17. If httpRequest’s cache mode is "no-cache", httpRequest’s prevent no-cache cache-control header
  1304. // modification flag is unset, and httpRequest’s header list does not contain `Cache-Control`, then append
  1305. // (`Cache-Control`, `max-age=0`) to httpRequest’s header list.
  1306. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::NoCache
  1307. && !http_request->prevent_no_cache_cache_control_header_modification()
  1308. && !http_request->header_list()->contains("Cache-Control"sv.bytes())) {
  1309. auto header = Infrastructure::Header::from_string_pair("Cache-Control"sv, "max-age=0"sv);
  1310. http_request->header_list()->append(move(header));
  1311. }
  1312. // 18. If httpRequest’s cache mode is "no-store" or "reload", then:
  1313. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::NoStore
  1314. || http_request->cache_mode() == Infrastructure::Request::CacheMode::Reload) {
  1315. // 1. If httpRequest’s header list does not contain `Pragma`, then append (`Pragma`, `no-cache`) to
  1316. // httpRequest’s header list.
  1317. if (!http_request->header_list()->contains("Pragma"sv.bytes())) {
  1318. auto header = Infrastructure::Header::from_string_pair("Pragma"sv, "no-cache"sv);
  1319. http_request->header_list()->append(move(header));
  1320. }
  1321. // 2. If httpRequest’s header list does not contain `Cache-Control`, then append
  1322. // (`Cache-Control`, `no-cache`) to httpRequest’s header list.
  1323. if (!http_request->header_list()->contains("Cache-Control"sv.bytes())) {
  1324. auto header = Infrastructure::Header::from_string_pair("Cache-Control"sv, "no-cache"sv);
  1325. http_request->header_list()->append(move(header));
  1326. }
  1327. }
  1328. // 19. If httpRequest’s header list contains `Range`, then append (`Accept-Encoding`, `identity`) to
  1329. // httpRequest’s header list.
  1330. // NOTE: This avoids a failure when handling content codings with a part of an encoded response.
  1331. // Additionally, many servers mistakenly ignore `Range` headers if a non-identity encoding is accepted.
  1332. if (http_request->header_list()->contains("Range"sv.bytes())) {
  1333. auto header = Infrastructure::Header::from_string_pair("Accept-Encoding"sv, "identity"sv);
  1334. http_request->header_list()->append(move(header));
  1335. }
  1336. // 20. Modify httpRequest’s header list per HTTP. Do not append a given header if httpRequest’s header list
  1337. // contains that header’s name.
  1338. // NOTE: It would be great if we could make this more normative somehow. At this point headers such as
  1339. // `Accept-Encoding`, `Connection`, `DNT`, and `Host`, are to be appended if necessary.
  1340. // `Accept`, `Accept-Charset`, and `Accept-Language` must not be included at this point.
  1341. // NOTE: `Accept` and `Accept-Language` are already included (unless fetch() is used, which does not include
  1342. // the latter by default), and `Accept-Charset` is a waste of bytes. See HTTP header layer division for
  1343. // more details.
  1344. // 21. If includeCredentials is true, then:
  1345. if (include_credentials == IncludeCredentials::Yes) {
  1346. // 1. If the user agent is not configured to block cookies for httpRequest (see section 7 of [COOKIES]),
  1347. // then:
  1348. if (true) {
  1349. // 1. Let cookies be the result of running the "cookie-string" algorithm (see section 5.4 of [COOKIES])
  1350. // with the user agent’s cookie store and httpRequest’s current URL.
  1351. auto cookies = ([&] {
  1352. // FIXME: Getting to the page client reliably is way too complicated, and going via the document won't work in workers.
  1353. auto document = Bindings::host_defined_environment_settings_object(realm).responsible_document();
  1354. if (!document)
  1355. return String {};
  1356. return document->page().client().page_did_request_cookie(http_request->current_url(), Cookie::Source::Http);
  1357. })();
  1358. // 2. If cookies is not the empty string, then append (`Cookie`, cookies) to httpRequest’s header list.
  1359. if (!cookies.is_empty()) {
  1360. auto header = Infrastructure::Header::from_string_pair("Cookie"sv, cookies);
  1361. http_request->header_list()->append(move(header));
  1362. }
  1363. }
  1364. // 2. If httpRequest’s header list does not contain `Authorization`, then:
  1365. if (!http_request->header_list()->contains("Authorization"sv.bytes())) {
  1366. // 1. Let authorizationValue be null.
  1367. auto authorization_value = Optional<String> {};
  1368. // 2. If there’s an authentication entry for httpRequest and either httpRequest’s use-URL-credentials
  1369. // flag is unset or httpRequest’s current URL does not include credentials, then set
  1370. // authorizationValue to authentication entry.
  1371. if (false // FIXME: "If there’s an authentication entry for httpRequest"
  1372. && (!http_request->use_url_credentials() || !http_request->current_url().includes_credentials())) {
  1373. // FIXME: "set authorizationValue to authentication entry."
  1374. }
  1375. // 3. Otherwise, if httpRequest’s current URL does include credentials and isAuthenticationFetch is
  1376. // true, set authorizationValue to httpRequest’s current URL, converted to an `Authorization` value.
  1377. else if (http_request->current_url().includes_credentials() && is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1378. auto const& url = http_request->current_url();
  1379. auto payload = MUST(String::formatted("{}:{}", MUST(url.username()), MUST(url.password())));
  1380. authorization_value = TRY_OR_THROW_OOM(vm, encode_base64(payload.bytes()));
  1381. }
  1382. // 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to
  1383. // httpRequest’s header list.
  1384. if (authorization_value.has_value()) {
  1385. auto header = Infrastructure::Header::from_string_pair("Authorization"sv, *authorization_value);
  1386. http_request->header_list()->append(move(header));
  1387. }
  1388. }
  1389. }
  1390. // FIXME: 22. If there’s a proxy-authentication entry, use it as appropriate.
  1391. // NOTE: This intentionally does not depend on httpRequest’s credentials mode.
  1392. // 23. Set httpCache to the result of determining the HTTP cache partition, given httpRequest.
  1393. http_cache = determine_the_http_cache_partition(*http_request);
  1394. // 24. If httpCache is null, then set httpRequest’s cache mode to "no-store".
  1395. if (!http_cache.has_value())
  1396. http_request->set_cache_mode(Infrastructure::Request::CacheMode::NoStore);
  1397. // 25. If httpRequest’s cache mode is neither "no-store" nor "reload", then:
  1398. if (http_request->cache_mode() != Infrastructure::Request::CacheMode::NoStore
  1399. && http_request->cache_mode() != Infrastructure::Request::CacheMode::Reload) {
  1400. // 1. Set storedResponse to the result of selecting a response from the httpCache, possibly needing
  1401. // validation, as per the "Constructing Responses from Caches" chapter of HTTP Caching [HTTP-CACHING],
  1402. // if any.
  1403. // NOTE: As mandated by HTTP, this still takes the `Vary` header into account.
  1404. auto raw_response = http_cache->select_response(http_request->url(), http_request->method(), *http_request->header_list());
  1405. // 2. If storedResponse is non-null, then:
  1406. if (raw_response.has_value()) {
  1407. // FIXME: Set more properties from the cached response
  1408. auto [body, _] = TRY(extract_body(realm, ReadonlyBytes(raw_response->body)));
  1409. stored_response = Infrastructure::Response::create(vm);
  1410. stored_response->set_body(body);
  1411. // 1. If cache mode is "default", storedResponse is a stale-while-revalidate response,
  1412. // and httpRequest’s client is non-null, then:
  1413. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::Default
  1414. && stored_response->is_stale_while_revalidate()
  1415. && http_request->client() != nullptr) {
  1416. // 1. Set response to storedResponse.
  1417. response = stored_response;
  1418. // 2. Set response’s cache state to "local".
  1419. response->set_cache_state(Infrastructure::Response::CacheState::Local);
  1420. // 3. Let revalidateRequest be a clone of request.
  1421. auto revalidate_request = request->clone(realm);
  1422. // 4. Set revalidateRequest’s cache mode set to "no-cache".
  1423. revalidate_request->set_cache_mode(Infrastructure::Request::CacheMode::NoCache);
  1424. // 5. Set revalidateRequest’s prevent no-cache cache-control header modification flag.
  1425. revalidate_request->set_prevent_no_cache_cache_control_header_modification(true);
  1426. // 6. Set revalidateRequest’s service-workers mode set to "none".
  1427. revalidate_request->set_service_workers_mode(Infrastructure::Request::ServiceWorkersMode::None);
  1428. // 7. In parallel, run main fetch given a new fetch params whose request is revalidateRequest.
  1429. Platform::EventLoopPlugin::the().deferred_invoke([&vm, &realm, revalidate_request, fetch_params = JS::NonnullGCPtr(fetch_params)] {
  1430. (void)main_fetch(realm, Infrastructure::FetchParams::create(vm, revalidate_request, fetch_params->timing_info()));
  1431. });
  1432. }
  1433. // 2. Otherwise:
  1434. else {
  1435. // 1. If storedResponse is a stale response, then set the revalidatingFlag.
  1436. if (stored_response->is_stale())
  1437. revalidating_flag->set_value(true);
  1438. // 2. If the revalidatingFlag is set and httpRequest’s cache mode is neither "force-cache" nor "only-if-cached", then:
  1439. if (revalidating_flag->value()
  1440. && http_request->cache_mode() != Infrastructure::Request::CacheMode::ForceCache
  1441. && http_request->cache_mode() != Infrastructure::Request::CacheMode::OnlyIfCached) {
  1442. // 1. If storedResponse’s header list contains `ETag`, then append (`If-None-Match`, `ETag`'s value) to httpRequest’s header list.
  1443. if (auto etag = stored_response->header_list()->get("ETag"sv.bytes()); etag.has_value()) {
  1444. stored_response->header_list()->append(Infrastructure::Header::from_string_pair("If-None-Match"sv, *etag));
  1445. }
  1446. // 2. If storedResponse’s header list contains `Last-Modified`, then append (`If-Modified-Since`, `Last-Modified`'s value) to httpRequest’s header list.
  1447. if (auto last_modified = stored_response->header_list()->get("Last-Modified"sv.bytes()); last_modified.has_value()) {
  1448. stored_response->header_list()->append(Infrastructure::Header::from_string_pair("If-Modified-Since"sv, *last_modified));
  1449. }
  1450. }
  1451. // 3. Otherwise, set response to storedResponse and set response’s cache state to "local".
  1452. else {
  1453. response = stored_response;
  1454. response->set_cache_state(Infrastructure::Response::CacheState::Local);
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. // 9. If aborted, then return the appropriate network error for fetchParams.
  1461. if (aborted)
  1462. return PendingResponse::create(vm, request, Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1463. JS::GCPtr<PendingResponse> pending_forward_response;
  1464. // 10. If response is null, then:
  1465. if (!response) {
  1466. // 1. If httpRequest’s cache mode is "only-if-cached", then return a network error.
  1467. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::OnlyIfCached)
  1468. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'only-if-cached' cache mode doesn't have a cached response"sv));
  1469. // 2. Let forwardResponse be the result of running HTTP-network fetch given httpFetchParams, includeCredentials,
  1470. // and isNewConnectionFetch.
  1471. pending_forward_response = TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, *http_fetch_params, include_credentials, is_new_connection_fetch));
  1472. } else {
  1473. pending_forward_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  1474. }
  1475. auto returned_pending_response = PendingResponse::create(vm, request);
  1476. pending_forward_response->when_loaded([&realm, &vm, &fetch_params, request, response, stored_response, http_request, returned_pending_response, is_authentication_fetch, is_new_connection_fetch, revalidating_flag, include_credentials, response_was_null = !response](JS::NonnullGCPtr<Infrastructure::Response> resolved_forward_response) mutable {
  1477. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-network-or-cache fetch' pending_forward_response load callback");
  1478. if (response_was_null) {
  1479. auto forward_response = resolved_forward_response;
  1480. // NOTE: TRACE is omitted as it is a forbidden method in Fetch.
  1481. auto method_is_unsafe = StringView { http_request->method() }.is_one_of("GET"sv, "HEAD"sv, "OPTIONS"sv);
  1482. // 3. If httpRequest’s method is unsafe and forwardResponse’s status is in the range 200 to 399, inclusive,
  1483. // invalidate appropriate stored responses in httpCache, as per the "Invalidation" chapter of HTTP
  1484. // Caching, and set storedResponse to null.
  1485. if (method_is_unsafe && forward_response->status() >= 200 && forward_response->status() <= 399) {
  1486. // FIXME: "invalidate appropriate stored responses in httpCache, as per the "Invalidation" chapter of HTTP Caching"
  1487. stored_response = nullptr;
  1488. }
  1489. // 4. If the revalidatingFlag is set and forwardResponse’s status is 304, then:
  1490. if (revalidating_flag->value() && forward_response->status() == 304) {
  1491. // FIXME: 1. Update storedResponse’s header list using forwardResponse’s header list, as per the "Freshening
  1492. // Stored Responses upon Validation" chapter of HTTP Caching.
  1493. // NOTE: This updates the stored response in cache as well.
  1494. // 2. Set response to storedResponse.
  1495. response = stored_response;
  1496. // 3. Set response’s cache state to "validated".
  1497. if (response)
  1498. response->set_cache_state(Infrastructure::Response::CacheState::Validated);
  1499. }
  1500. // 5. If response is null, then:
  1501. if (!response) {
  1502. // 1. Set response to forwardResponse.
  1503. response = forward_response;
  1504. // FIXME: 2. Store httpRequest and forwardResponse in httpCache, as per the "Storing Responses in Caches"
  1505. // chapter of HTTP Caching.
  1506. // NOTE: If forwardResponse is a network error, this effectively caches the network error, which is
  1507. // sometimes known as "negative caching".
  1508. // NOTE: The associated body info is stored in the cache alongside the response.
  1509. }
  1510. }
  1511. // 11. Set response’s URL list to a clone of httpRequest’s URL list.
  1512. response->set_url_list(http_request->url_list());
  1513. // 12. If httpRequest’s header list contains `Range`, then set response’s range-requested flag.
  1514. if (http_request->header_list()->contains("Range"sv.bytes()))
  1515. response->set_range_requested(true);
  1516. // 13. Set response’s request-includes-credentials to includeCredentials.
  1517. response->set_request_includes_credentials(include_credentials == IncludeCredentials::Yes);
  1518. auto inner_pending_response = PendingResponse::create(vm, request, *response);
  1519. // 14. If response’s status is 401, httpRequest’s response tainting is not "cors", includeCredentials is true,
  1520. // and request’s window is an environment settings object, then:
  1521. if (response->status() == 401
  1522. && http_request->response_tainting() != Infrastructure::Request::ResponseTainting::CORS
  1523. && include_credentials == IncludeCredentials::Yes
  1524. && request->window().has<JS::GCPtr<HTML::EnvironmentSettingsObject>>()) {
  1525. // 1. Needs testing: multiple `WWW-Authenticate` headers, missing, parsing issues.
  1526. // (Red box in the spec, no-op)
  1527. // 2. If request’s body is non-null, then:
  1528. if (!request->body().has<Empty>()) {
  1529. // 1. If request’s body’s source is null, then return a network error.
  1530. if (request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source().has<Empty>()) {
  1531. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "Request has body but no body source"_string));
  1532. return;
  1533. }
  1534. // 2. Set request’s body to the body of the result of safely extracting request’s body’s source.
  1535. auto const& source = request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source();
  1536. // NOTE: BodyInitOrReadableBytes is a superset of Body::SourceType
  1537. auto converted_source = source.has<ByteBuffer>()
  1538. ? BodyInitOrReadableBytes { source.get<ByteBuffer>() }
  1539. : BodyInitOrReadableBytes { source.get<JS::Handle<FileAPI::Blob>>() };
  1540. auto [body, _] = TRY_OR_IGNORE(safely_extract_body(realm, converted_source));
  1541. request->set_body(move(body));
  1542. }
  1543. // 3. If request’s use-URL-credentials flag is unset or isAuthenticationFetch is true, then:
  1544. if (!request->use_url_credentials() || is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1545. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1546. if (fetch_params.is_canceled()) {
  1547. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1548. return;
  1549. }
  1550. // FIXME: 2. Let username and password be the result of prompting the end user for a username and password,
  1551. // respectively, in request’s window.
  1552. dbgln("Fetch: Username/password prompt is not implemented, using empty strings. This request will probably fail.");
  1553. auto username = ByteString::empty();
  1554. auto password = ByteString::empty();
  1555. // 3. Set the username given request’s current URL and username.
  1556. MUST(request->current_url().set_username(username));
  1557. // 4. Set the password given request’s current URL and password.
  1558. MUST(request->current_url().set_password(password));
  1559. }
  1560. // 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true.
  1561. inner_pending_response = TRY_OR_IGNORE(http_network_or_cache_fetch(realm, fetch_params, IsAuthenticationFetch::Yes));
  1562. }
  1563. inner_pending_response->when_loaded([&realm, &vm, &fetch_params, request, returned_pending_response, is_authentication_fetch, is_new_connection_fetch](JS::NonnullGCPtr<Infrastructure::Response> response) {
  1564. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP network-or-cache fetch' inner_pending_response load callback");
  1565. // 15. If response’s status is 407, then:
  1566. if (response->status() == 407) {
  1567. // 1. If request’s window is "no-window", then return a network error.
  1568. if (request->window().has<Infrastructure::Request::Window>()
  1569. && request->window().get<Infrastructure::Request::Window>() == Infrastructure::Request::Window::NoWindow) {
  1570. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "Request requires proxy authentication but has 'no-window' set"_string));
  1571. return;
  1572. }
  1573. // 2. Needs testing: multiple `Proxy-Authenticate` headers, missing, parsing issues.
  1574. // (Red box in the spec, no-op)
  1575. // 3. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1576. if (fetch_params.is_canceled()) {
  1577. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1578. return;
  1579. }
  1580. // FIXME: 4. Prompt the end user as appropriate in request’s window and store the result as a
  1581. // proxy-authentication entry.
  1582. // NOTE: Remaining details surrounding proxy authentication are defined by HTTP.
  1583. // FIXME: 5. Set response to the result of running HTTP-network-or-cache fetch given fetchParams.
  1584. // (Doing this without step 4 would potentially lead to an infinite request cycle.)
  1585. }
  1586. auto inner_pending_response = PendingResponse::create(vm, request, *response);
  1587. // 16. If all of the following are true
  1588. if (
  1589. // - response’s status is 421
  1590. response->status() == 421
  1591. // - isNewConnectionFetch is false
  1592. && is_new_connection_fetch == IsNewConnectionFetch::No
  1593. // - request’s body is null, or request’s body is non-null and request’s body’s source is non-null
  1594. && (request->body().has<Empty>() || !request->body().get<JS::NonnullGCPtr<Infrastructure::Body>>()->source().has<Empty>())
  1595. // then:
  1596. ) {
  1597. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1598. if (fetch_params.is_canceled()) {
  1599. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1600. return;
  1601. }
  1602. // 2. Set response to the result of running HTTP-network-or-cache fetch given fetchParams,
  1603. // isAuthenticationFetch, and true.
  1604. inner_pending_response = TRY_OR_IGNORE(http_network_or_cache_fetch(realm, fetch_params, is_authentication_fetch, IsNewConnectionFetch::Yes));
  1605. }
  1606. inner_pending_response->when_loaded([returned_pending_response, is_authentication_fetch](JS::NonnullGCPtr<Infrastructure::Response> response) {
  1607. // 17. If isAuthenticationFetch is true, then create an authentication entry for request and the given
  1608. // realm.
  1609. if (is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1610. // FIXME: "create an authentication entry for request and the given realm"
  1611. }
  1612. returned_pending_response->resolve(response);
  1613. });
  1614. });
  1615. });
  1616. // 18. Return response.
  1617. // NOTE: Typically response’s body’s stream is still being enqueued to after returning.
  1618. return returned_pending_response;
  1619. }
  1620. #if defined(WEB_FETCH_DEBUG)
  1621. static void log_load_request(auto const& load_request)
  1622. {
  1623. dbgln("Fetch: Invoking ResourceLoader");
  1624. dbgln("> {} {} HTTP/1.1", load_request.method(), load_request.url());
  1625. for (auto const& [name, value] : load_request.headers())
  1626. dbgln("> {}: {}", name, value);
  1627. dbgln(">");
  1628. for (auto line : StringView { load_request.body() }.split_view('\n', SplitBehavior::KeepEmpty))
  1629. dbgln("> {}", line);
  1630. }
  1631. static void log_response(auto const& status_code, auto const& headers, auto const& data)
  1632. {
  1633. dbgln("< HTTP/1.1 {}", status_code.value_or(0));
  1634. for (auto const& [name, value] : headers)
  1635. dbgln("< {}: {}", name, value);
  1636. dbgln("<");
  1637. for (auto line : StringView { data }.split_view('\n', SplitBehavior::KeepEmpty))
  1638. dbgln("< {}", line);
  1639. }
  1640. #endif
  1641. // https://fetch.spec.whatwg.org/#concept-http-network-fetch
  1642. // Drop-in replacement for 'HTTP-network fetch', but obviously non-standard :^)
  1643. // It also handles file:// URLs since those can also go through ResourceLoader.
  1644. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> nonstandard_resource_loader_file_or_http_network_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, IncludeCredentials include_credentials, IsNewConnectionFetch is_new_connection_fetch)
  1645. {
  1646. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'non-standard HTTP-network fetch' with: fetch_params @ {}", &fetch_params);
  1647. auto& vm = realm.vm();
  1648. (void)include_credentials;
  1649. (void)is_new_connection_fetch;
  1650. auto request = fetch_params.request();
  1651. auto& page = Bindings::host_defined_page(realm);
  1652. // NOTE: Using LoadRequest::create_for_url_on_page here will unconditionally add cookies as long as there's a page available.
  1653. // However, it is up to http_network_or_cache_fetch to determine if cookies should be added to the request.
  1654. LoadRequest load_request;
  1655. load_request.set_url(request->current_url());
  1656. load_request.set_page(page);
  1657. load_request.set_method(ByteString::copy(request->method()));
  1658. for (auto const& header : *request->header_list())
  1659. load_request.set_header(ByteString::copy(header.name), ByteString::copy(header.value));
  1660. if (auto const* body = request->body().get_pointer<JS::NonnullGCPtr<Infrastructure::Body>>()) {
  1661. TRY((*body)->source().visit(
  1662. [&](ByteBuffer const& byte_buffer) -> WebIDL::ExceptionOr<void> {
  1663. load_request.set_body(TRY_OR_THROW_OOM(vm, ByteBuffer::copy(byte_buffer)));
  1664. return {};
  1665. },
  1666. [&](JS::Handle<FileAPI::Blob> const& blob_handle) -> WebIDL::ExceptionOr<void> {
  1667. load_request.set_body(TRY_OR_THROW_OOM(vm, ByteBuffer::copy(blob_handle->bytes())));
  1668. return {};
  1669. },
  1670. [](Empty) -> WebIDL::ExceptionOr<void> {
  1671. return {};
  1672. }));
  1673. }
  1674. auto pending_response = PendingResponse::create(vm, request);
  1675. if constexpr (WEB_FETCH_DEBUG) {
  1676. dbgln("Fetch: Invoking ResourceLoader");
  1677. log_load_request(load_request);
  1678. }
  1679. // FIXME: This check should be removed and all HTTP requests should go through the `ResourceLoader::load_unbuffered`
  1680. // path. The buffer option should then be supplied to the steps below that allow us to buffer data up to a
  1681. // user-agent-defined limit (or not). However, we will need to fully use stream operations throughout the
  1682. // fetch process to enable this (e.g. Body::fully_read must use streams for this to work).
  1683. if (request->buffer_policy() == Infrastructure::Request::BufferPolicy::DoNotBufferResponse) {
  1684. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  1685. // 12. Let stream be a new ReadableStream.
  1686. auto stream = realm.heap().allocate<Streams::ReadableStream>(realm, realm);
  1687. auto fetched_data_receiver = realm.heap().allocate<FetchedDataReceiver>(realm, fetch_params, stream);
  1688. // 10. Let pullAlgorithm be the followings steps:
  1689. auto pull_algorithm = JS::create_heap_function(realm.heap(), [&realm, fetched_data_receiver]() {
  1690. // 1. Let promise be a new promise.
  1691. auto promise = WebIDL::create_promise(realm);
  1692. // 2. Run the following steps in parallel:
  1693. // NOTE: This is handled by FetchedDataReceiver.
  1694. fetched_data_receiver->set_pending_promise(promise);
  1695. // 3. Return promise.
  1696. return promise;
  1697. });
  1698. // 11. Let cancelAlgorithm be an algorithm that aborts fetchParams’s controller with reason, given reason.
  1699. auto cancel_algorithm = JS::create_heap_function(realm.heap(), [&realm, &fetch_params](JS::Value reason) {
  1700. fetch_params.controller()->abort(realm, reason);
  1701. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  1702. });
  1703. // 13. Set up stream with byte reading support with pullAlgorithm set to pullAlgorithm, cancelAlgorithm set to cancelAlgorithm.
  1704. Streams::set_up_readable_stream_controller_with_byte_reading_support(stream, pull_algorithm, cancel_algorithm);
  1705. auto on_headers_received = [&vm, request, pending_response, stream](auto const& response_headers, Optional<u32> status_code) {
  1706. if (pending_response->is_resolved()) {
  1707. // RequestServer will send us the response headers twice, the second time being for HTTP trailers. This
  1708. // fetch algorithm is not interested in trailers, so just drop them here.
  1709. return;
  1710. }
  1711. auto response = Infrastructure::Response::create(vm);
  1712. response->set_status(status_code.value_or(200));
  1713. // FIXME: Set response status message
  1714. if constexpr (WEB_FETCH_DEBUG) {
  1715. dbgln("Fetch: ResourceLoader load for '{}' {}: (status {})",
  1716. request->url(),
  1717. Infrastructure::is_ok_status(response->status()) ? "complete"sv : "failed"sv,
  1718. response->status());
  1719. log_response(status_code, response_headers, ReadonlyBytes {});
  1720. }
  1721. for (auto const& [name, value] : response_headers) {
  1722. auto header = Infrastructure::Header::from_string_pair(name, value);
  1723. response->header_list()->append(move(header));
  1724. }
  1725. // 14. Set response’s body to a new body whose stream is stream.
  1726. response->set_body(Infrastructure::Body::create(vm, stream));
  1727. // 17. Return response.
  1728. // NOTE: Typically response’s body’s stream is still being enqueued to after returning.
  1729. pending_response->resolve(response);
  1730. };
  1731. // 16. Run these steps in parallel:
  1732. // FIXME: 1. Run these steps, but abort when fetchParams is canceled:
  1733. auto on_data_received = [fetched_data_receiver](auto bytes) {
  1734. // 1. If one or more bytes have been transmitted from response’s message body, then:
  1735. if (!bytes.is_empty()) {
  1736. // 1. Let bytes be the transmitted bytes.
  1737. // FIXME: 2. Let codings be the result of extracting header list values given `Content-Encoding` and response’s header list.
  1738. // FIXME: 3. Increase response’s body info’s encoded size by bytes’s length.
  1739. // FIXME: 4. Set bytes to the result of handling content codings given codings and bytes.
  1740. // FIXME: 5. Increase response’s body info’s decoded size by bytes’s length.
  1741. // FIXME: 6. If bytes is failure, then terminate fetchParams’s controller.
  1742. // 7. Append bytes to buffer.
  1743. fetched_data_receiver->on_data_received(bytes);
  1744. // FIXME: 8. If the size of buffer is larger than an upper limit chosen by the user agent, ask the user agent
  1745. // to suspend the ongoing fetch.
  1746. }
  1747. };
  1748. auto on_complete = [&vm, &realm, pending_response, stream](auto success, auto error_message) {
  1749. HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
  1750. // 16.1.1.2. Otherwise, if the bytes transmission for response’s message body is done normally and stream is readable,
  1751. // then close stream, and abort these in-parallel steps.
  1752. if (success) {
  1753. if (stream->is_readable())
  1754. stream->close();
  1755. }
  1756. // 16.1.2.2. Otherwise, if stream is readable, error stream with a TypeError.
  1757. else {
  1758. auto error = MUST(String::formatted("Load failed: {}", error_message));
  1759. if (stream->is_readable())
  1760. stream->error(JS::TypeError::create(realm, error));
  1761. if (!pending_response->is_resolved())
  1762. pending_response->resolve(Infrastructure::Response::network_error(vm, error));
  1763. }
  1764. };
  1765. ResourceLoader::the().load_unbuffered(load_request, move(on_headers_received), move(on_data_received), move(on_complete));
  1766. } else {
  1767. auto on_load_success = [&realm, &vm, request, pending_response](auto data, auto& response_headers, auto status_code) {
  1768. dbgln_if(WEB_FETCH_DEBUG, "Fetch: ResourceLoader load for '{}' complete", request->url());
  1769. if constexpr (WEB_FETCH_DEBUG)
  1770. log_response(status_code, response_headers, data);
  1771. auto [body, _] = TRY_OR_IGNORE(extract_body(realm, data));
  1772. auto response = Infrastructure::Response::create(vm);
  1773. response->set_status(status_code.value_or(200));
  1774. response->set_body(move(body));
  1775. for (auto const& [name, value] : response_headers) {
  1776. auto header = Infrastructure::Header::from_string_pair(name, value);
  1777. response->header_list()->append(move(header));
  1778. }
  1779. // FIXME: Set response status message
  1780. pending_response->resolve(response);
  1781. };
  1782. auto on_load_error = [&realm, &vm, request, pending_response](auto& error, auto status_code, auto data, auto& response_headers) {
  1783. dbgln_if(WEB_FETCH_DEBUG, "Fetch: ResourceLoader load for '{}' failed: {} (status {})", request->url(), error, status_code.value_or(0));
  1784. if constexpr (WEB_FETCH_DEBUG)
  1785. log_response(status_code, response_headers, data);
  1786. auto response = Infrastructure::Response::create(vm);
  1787. // FIXME: This is ugly, ResourceLoader should tell us.
  1788. if (status_code.value_or(0) == 0) {
  1789. response = Infrastructure::Response::network_error(vm, "HTTP request failed"_string);
  1790. } else {
  1791. response->set_type(Infrastructure::Response::Type::Error);
  1792. response->set_status(status_code.value_or(400));
  1793. auto [body, _] = TRY_OR_IGNORE(extract_body(realm, data));
  1794. response->set_body(move(body));
  1795. for (auto const& [name, value] : response_headers) {
  1796. auto header = Infrastructure::Header::from_string_pair(name, value);
  1797. response->header_list()->append(move(header));
  1798. }
  1799. // FIXME: Set response status message
  1800. }
  1801. pending_response->resolve(response);
  1802. };
  1803. ResourceLoader::the().load(load_request, move(on_load_success), move(on_load_error));
  1804. }
  1805. return pending_response;
  1806. }
  1807. // https://fetch.spec.whatwg.org/#cors-preflight-fetch-0
  1808. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> cors_preflight_fetch(JS::Realm& realm, Infrastructure::Request& request)
  1809. {
  1810. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' with request @ {}", &request);
  1811. auto& vm = realm.vm();
  1812. // 1. Let preflight be a new request whose method is `OPTIONS`, URL list is a clone of request’s URL list, initiator is
  1813. // request’s initiator, destination is request’s destination, origin is request’s origin, referrer is request’s referrer,
  1814. // referrer policy is request’s referrer policy, mode is "cors", and response tainting is "cors".
  1815. auto preflight = Fetch::Infrastructure::Request::create(vm);
  1816. preflight->set_method(TRY_OR_THROW_OOM(vm, ByteBuffer::copy("OPTIONS"sv.bytes())));
  1817. preflight->set_url_list(request.url_list());
  1818. preflight->set_initiator(request.initiator());
  1819. preflight->set_destination(request.destination());
  1820. preflight->set_origin(request.origin());
  1821. preflight->set_referrer(request.referrer());
  1822. preflight->set_referrer_policy(request.referrer_policy());
  1823. preflight->set_mode(Infrastructure::Request::Mode::CORS);
  1824. preflight->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  1825. // 2. Append (`Accept`, `*/*`) to preflight’s header list.
  1826. auto temp_header = Infrastructure::Header::from_string_pair("Accept"sv, "*/*"sv);
  1827. preflight->header_list()->append(move(temp_header));
  1828. // 3. Append (`Access-Control-Request-Method`, request’s method) to preflight’s header list.
  1829. temp_header = Infrastructure::Header::from_string_pair("Access-Control-Request-Method"sv, request.method());
  1830. preflight->header_list()->append(move(temp_header));
  1831. // 4. Let headers be the CORS-unsafe request-header names with request’s header list.
  1832. auto headers = Infrastructure::get_cors_unsafe_header_names(request.header_list());
  1833. // 5. If headers is not empty, then:
  1834. if (!headers.is_empty()) {
  1835. // 1. Let value be the items in headers separated from each other by `,`.
  1836. // NOTE: This intentionally does not use combine, as 0x20 following 0x2C is not the way this was implemented,
  1837. // for better or worse.
  1838. ByteBuffer value;
  1839. bool first = true;
  1840. for (auto const& header : headers) {
  1841. if (!first)
  1842. TRY_OR_THROW_OOM(vm, value.try_append(','));
  1843. TRY_OR_THROW_OOM(vm, value.try_append(header));
  1844. first = false;
  1845. }
  1846. // 2. Append (`Access-Control-Request-Headers`, value) to preflight’s header list.
  1847. temp_header = Infrastructure::Header {
  1848. .name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("Access-Control-Request-Headers"sv.bytes())),
  1849. .value = move(value),
  1850. };
  1851. preflight->header_list()->append(move(temp_header));
  1852. }
  1853. // 6. Let response be the result of running HTTP-network-or-cache fetch given a new fetch params whose request is preflight.
  1854. // FIXME: The spec doesn't say anything about timing_info here, but FetchParams requires a non-null FetchTimingInfo object.
  1855. auto timing_info = Infrastructure::FetchTimingInfo::create(vm);
  1856. auto fetch_params = Infrastructure::FetchParams::create(vm, preflight, timing_info);
  1857. auto returned_pending_response = PendingResponse::create(vm, request);
  1858. auto preflight_response = TRY(http_network_or_cache_fetch(realm, fetch_params));
  1859. preflight_response->when_loaded([&vm, &request, returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> response) {
  1860. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' preflight_response load callback");
  1861. // 7. If a CORS check for request and response returns success and response’s status is an ok status, then:
  1862. // NOTE: The CORS check is done on request rather than preflight to ensure the correct credentials mode is used.
  1863. if (cors_check(request, response) && Infrastructure::is_ok_status(response->status())) {
  1864. // 1. Let methods be the result of extracting header list values given `Access-Control-Allow-Methods` and response’s header list.
  1865. auto methods_or_failure = Infrastructure::extract_header_list_values("Access-Control-Allow-Methods"sv.bytes(), response->header_list());
  1866. // 2. Let headerNames be the result of extracting header list values given `Access-Control-Allow-Headers` and
  1867. // response’s header list.
  1868. auto header_names_or_failure = Infrastructure::extract_header_list_values("Access-Control-Allow-Headers"sv.bytes(), response->header_list());
  1869. // 3. If either methods or headerNames is failure, return a network error.
  1870. if (methods_or_failure.has<Infrastructure::ExtractHeaderParseFailure>()) {
  1871. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Methods in the CORS-preflight response is syntactically invalid"_string));
  1872. return;
  1873. }
  1874. if (header_names_or_failure.has<Infrastructure::ExtractHeaderParseFailure>()) {
  1875. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "The Access-Control-Allow-Headers in the CORS-preflight response is syntactically invalid"_string));
  1876. return;
  1877. }
  1878. // NOTE: We treat "methods_or_failure" being `Empty` as empty Vector here.
  1879. auto methods = methods_or_failure.has<Vector<ByteBuffer>>() ? methods_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
  1880. // NOTE: We treat "header_names_or_failure" being `Empty` as empty Vector here.
  1881. auto header_names = header_names_or_failure.has<Vector<ByteBuffer>>() ? header_names_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
  1882. // 4. If methods is null and request’s use-CORS-preflight flag is set, then set methods to a new list containing request’s method.
  1883. // NOTE: This ensures that a CORS-preflight fetch that happened due to request’s use-CORS-preflight flag being set is cached.
  1884. if (methods.is_empty() && request.use_cors_preflight())
  1885. methods = Vector { TRY_OR_IGNORE(ByteBuffer::copy(request.method())) };
  1886. // 5. If request’s method is not in methods, request’s method is not a CORS-safelisted method, and request’s credentials mode
  1887. // is "include" or methods does not contain `*`, then return a network error.
  1888. if (!methods.contains_slow(request.method()) && !Infrastructure::is_cors_safelisted_method(request.method())) {
  1889. if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) {
  1890. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("Non-CORS-safelisted method '{}' not found in the CORS-preflight response's Access-Control-Allow-Methods header (the header may be missing). '*' is not allowed as the main request includes credentials."sv, StringView { request.method() }))));
  1891. return;
  1892. }
  1893. if (!methods.contains_slow("*"sv.bytes())) {
  1894. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("Non-CORS-safelisted method '{}' not found in the CORS-preflight response's Access-Control-Allow-Methods header and there was no '*' entry. The header may be missing."sv, StringView { request.method() }))));
  1895. return;
  1896. }
  1897. }
  1898. // 6. If one of request’s header list’s names is a CORS non-wildcard request-header name and is not a byte-case-insensitive match
  1899. // for an item in headerNames, then return a network error.
  1900. for (auto const& header : *request.header_list()) {
  1901. if (Infrastructure::is_cors_non_wildcard_request_header_name(header.name)) {
  1902. bool is_in_header_names = false;
  1903. for (auto const& allowed_header_name : header_names) {
  1904. if (StringView { allowed_header_name }.equals_ignoring_ascii_case(header.name)) {
  1905. is_in_header_names = true;
  1906. break;
  1907. }
  1908. }
  1909. if (!is_in_header_names) {
  1910. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("Main request contains the header '{}' that is not specified in the CORS-preflight response's Access-Control-Allow-Headers header (the header may be missing). '*' does not capture this header."sv, StringView { header.name }))));
  1911. return;
  1912. }
  1913. }
  1914. }
  1915. // 7. For each unsafeName of the CORS-unsafe request-header names with request’s header list, if unsafeName is not a
  1916. // byte-case-insensitive match for an item in headerNames and request’s credentials mode is "include" or headerNames
  1917. // does not contain `*`, return a network error.
  1918. auto unsafe_names = Infrastructure::get_cors_unsafe_header_names(request.header_list());
  1919. for (auto const& unsafe_name : unsafe_names) {
  1920. bool is_in_header_names = false;
  1921. for (auto const& header_name : header_names) {
  1922. if (StringView { unsafe_name }.equals_ignoring_ascii_case(header_name)) {
  1923. is_in_header_names = true;
  1924. break;
  1925. }
  1926. }
  1927. if (!is_in_header_names) {
  1928. if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) {
  1929. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("CORS-unsafe request-header '{}' not found in the CORS-preflight response's Access-Control-Allow-Headers header (the header may be missing). '*' is not allowed as the main request includes credentials."sv, StringView { unsafe_name }))));
  1930. return;
  1931. }
  1932. if (!header_names.contains_slow("*"sv.bytes())) {
  1933. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE(String::formatted("CORS-unsafe request-header '{}' not found in the CORS-preflight response's Access-Control-Allow-Headers header and there was no '*' entry. The header may be missing."sv, StringView { unsafe_name }))));
  1934. return;
  1935. }
  1936. }
  1937. }
  1938. // FIXME: 8. Let max-age be the result of extracting header list values given `Access-Control-Max-Age` and response’s header list.
  1939. // FIXME: 9. If max-age is failure or null, then set max-age to 5.
  1940. // FIXME: 10. If max-age is greater than an imposed limit on max-age, then set max-age to the imposed limit.
  1941. // 11. If the user agent does not provide for a cache, then return response.
  1942. // NOTE: Since we don't currently have a cache, this is always true.
  1943. returned_pending_response->resolve(response);
  1944. return;
  1945. // FIXME: 12. For each method in methods for which there is a method cache entry match using request, set matching entry’s max-age
  1946. // to max-age.
  1947. // FIXME: 13. For each method in methods for which there is no method cache entry match using request, create a new cache entry
  1948. // with request, max-age, method, and null.
  1949. // FIXME: 14. For each headerName in headerNames for which there is a header-name cache entry match using request, set matching
  1950. // entry’s max-age to max-age.
  1951. // FIXME: 15. For each headerName in headerNames for which there is no header-name cache entry match using request, create a
  1952. // new cache entry with request, max-age, null, and headerName.
  1953. // FIXME: 16. Return response.
  1954. }
  1955. // 8. Otherwise, return a network error.
  1956. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, "CORS-preflight check failed"_string));
  1957. });
  1958. return returned_pending_response;
  1959. }
  1960. // https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-dest
  1961. void set_sec_fetch_dest_header(Infrastructure::Request& request)
  1962. {
  1963. // 1. Assert: r’s url is a potentially trustworthy URL.
  1964. VERIFY(SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy);
  1965. // 2. Let header be a Structured Header whose value is a token.
  1966. // FIXME: This is handled below, as Serenity doesn't have APIs for RFC 8941.
  1967. // 3. If r’s destination is the empty string, set header’s value to the string "empty". Otherwise, set header’s value to r’s destination.
  1968. ByteBuffer header_value;
  1969. if (!request.destination().has_value()) {
  1970. header_value = MUST(ByteBuffer::copy("empty"sv.bytes()));
  1971. } else {
  1972. header_value = MUST(ByteBuffer::copy(Infrastructure::request_destination_to_string(request.destination().value()).bytes()));
  1973. }
  1974. // 4. Set a structured field value `Sec-Fetch-Dest`/header in r’s header list.
  1975. auto header = Infrastructure::Header {
  1976. .name = MUST(ByteBuffer::copy("Sec-Fetch-Dest"sv.bytes())),
  1977. .value = move(header_value),
  1978. };
  1979. request.header_list()->append(move(header));
  1980. }
  1981. // https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-dest
  1982. void set_sec_fetch_mode_header(Infrastructure::Request& request)
  1983. {
  1984. // 1. Assert: r’s url is a potentially trustworthy URL.
  1985. VERIFY(SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy);
  1986. // 2. Let header be a Structured Header whose value is a token.
  1987. // FIXME: This is handled below, as Serenity doesn't have APIs for RFC 8941.
  1988. // 3. Set header’s value to r’s mode.
  1989. auto header_value = MUST(ByteBuffer::copy(Infrastructure::request_mode_to_string(request.mode()).bytes()));
  1990. // 4. Set a structured field value `Sec-Fetch-Mode`/header in r’s header list.
  1991. auto header = Infrastructure::Header {
  1992. .name = MUST(ByteBuffer::copy("Sec-Fetch-Mode"sv.bytes())),
  1993. .value = move(header_value),
  1994. };
  1995. request.header_list()->append(move(header));
  1996. }
  1997. // https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-site
  1998. void set_sec_fetch_site_header(Infrastructure::Request& request)
  1999. {
  2000. // 1. Assert: r’s url is a potentially trustworthy URL.
  2001. VERIFY(SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy);
  2002. // 2. Let header be a Structured Header whose value is a token.
  2003. // FIXME: This is handled below, as Serenity doesn't have APIs for RFC 8941.
  2004. // 3. Set header’s value to same-origin.
  2005. auto header_value = "same-origin"sv;
  2006. // FIXME: 4. If r is a navigation request that was explicitly caused by a user’s interaction with the user agent (by typing an address
  2007. // into the user agent directly, for example, or by clicking a bookmark, etc.), then set header’s value to none.
  2008. // 5. If header’s value is not none, then for each url in r’s url list:
  2009. if (!header_value.equals_ignoring_ascii_case("none"sv)) {
  2010. for (auto& url : request.url_list()) {
  2011. // 1. If url is same origin with r’s origin, continue.
  2012. if (DOMURL::url_origin(url).is_same_origin(DOMURL::url_origin(request.current_url())))
  2013. continue;
  2014. // 2. Set header’s value to cross-site.
  2015. header_value = "cross-site"sv;
  2016. // FIXME: 3. If r’s origin is not same site with url’s origin, then break.
  2017. // FIXME: 4. Set header’s value to same-site.
  2018. }
  2019. }
  2020. // 6. Set a structured field value `Sec-Fetch-Site`/header in r’s header list.
  2021. auto header = Infrastructure::Header {
  2022. .name = MUST(ByteBuffer::copy("Sec-Fetch-Site"sv.bytes())),
  2023. .value = MUST(ByteBuffer::copy(header_value.bytes())),
  2024. };
  2025. request.header_list()->append(move(header));
  2026. }
  2027. // https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-set-user
  2028. void set_sec_fetch_user_header(Infrastructure::Request& request)
  2029. {
  2030. // 1. Assert: r’s url is a potentially trustworthy URL.
  2031. VERIFY(SecureContexts::is_url_potentially_trustworthy(request.url()) == SecureContexts::Trustworthiness::PotentiallyTrustworthy);
  2032. // 2. If r is not a navigation request, or if r’s user-activation is false, return.
  2033. if (!request.is_navigation_request() || !request.user_activation())
  2034. return;
  2035. // 3. Let header be a Structured Header whose value is a token.
  2036. // FIXME: This is handled below, as Serenity doesn't have APIs for RFC 8941.
  2037. // 4. Set header’s value to true.
  2038. // NOTE: See https://datatracker.ietf.org/doc/html/rfc8941#name-booleans for boolean format in RFC 8941.
  2039. auto header_value = MUST(ByteBuffer::copy("?1"sv.bytes()));
  2040. // 5. Set a structured field value `Sec-Fetch-User`/header in r’s header list.
  2041. auto header = Infrastructure::Header {
  2042. .name = MUST(ByteBuffer::copy("Sec-Fetch-User"sv.bytes())),
  2043. .value = move(header_value),
  2044. };
  2045. request.header_list()->append(move(header));
  2046. }
  2047. // https://w3c.github.io/webappsec-fetch-metadata/#abstract-opdef-append-the-fetch-metadata-headers-for-a-request
  2048. void append_fetch_metadata_headers_for_request(Infrastructure::Request& request)
  2049. {
  2050. // 1. If r’s url is not an potentially trustworthy URL, return.
  2051. if (SecureContexts::is_url_potentially_trustworthy(request.url()) != SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  2052. return;
  2053. // 2. Set the Sec-Fetch-Dest header for r.
  2054. set_sec_fetch_dest_header(request);
  2055. // 3. Set the Sec-Fetch-Mode header for r.
  2056. set_sec_fetch_mode_header(request);
  2057. // 4. Set the Sec-Fetch-Site header for r.
  2058. set_sec_fetch_site_header(request);
  2059. // 5. Set the Sec-Fetch-User header for r.
  2060. set_sec_fetch_user_header(request);
  2061. }
  2062. }