Fetching.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Base64.h>
  8. #include <AK/Debug.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibWeb/Bindings/MainThreadVM.h>
  12. #include <LibWeb/Cookie/Cookie.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/Fetch/BodyInit.h>
  15. #include <LibWeb/Fetch/Fetching/Checks.h>
  16. #include <LibWeb/Fetch/Fetching/Fetching.h>
  17. #include <LibWeb/Fetch/Fetching/PendingResponse.h>
  18. #include <LibWeb/Fetch/Fetching/RefCountedFlag.h>
  19. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  20. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  21. #include <LibWeb/Fetch/Infrastructure/FetchParams.h>
  22. #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
  23. #include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
  24. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  25. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  26. #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
  27. #include <LibWeb/Fetch/Infrastructure/MimeTypeBlocking.h>
  28. #include <LibWeb/Fetch/Infrastructure/NoSniffBlocking.h>
  29. #include <LibWeb/Fetch/Infrastructure/PortBlocking.h>
  30. #include <LibWeb/Fetch/Infrastructure/Task.h>
  31. #include <LibWeb/Fetch/Infrastructure/URL.h>
  32. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  33. #include <LibWeb/HTML/Scripting/Environments.h>
  34. #include <LibWeb/HTML/Window.h>
  35. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  36. #include <LibWeb/Loader/LoadRequest.h>
  37. #include <LibWeb/Loader/ResourceLoader.h>
  38. #include <LibWeb/Platform/EventLoopPlugin.h>
  39. #include <LibWeb/ReferrerPolicy/AbstractOperations.h>
  40. #include <LibWeb/URL/URL.h>
  41. #include <LibWeb/WebIDL/DOMException.h>
  42. namespace Web::Fetch::Fetching {
  43. #define TRY_OR_IGNORE(expression) \
  44. ({ \
  45. auto&& _temporary_result = (expression); \
  46. if (_temporary_result.is_error()) \
  47. return; \
  48. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  49. "Do not return a reference from a fallible expression"); \
  50. _temporary_result.release_value(); \
  51. })
  52. // https://fetch.spec.whatwg.org/#concept-fetch
  53. WebIDL::ExceptionOr<JS::NonnullGCPtr<Infrastructure::FetchController>> fetch(JS::Realm& realm, Infrastructure::Request& request, Infrastructure::FetchAlgorithms const& algorithms, UseParallelQueue use_parallel_queue)
  54. {
  55. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'fetch' with: request @ {}", &request);
  56. auto& vm = realm.vm();
  57. // 1. Assert: request’s mode is "navigate" or processEarlyHintsResponse is null.
  58. VERIFY(request.mode() == Infrastructure::Request::Mode::Navigate || !algorithms.process_early_hints_response().has_value());
  59. // 2. Let taskDestination be null.
  60. JS::GCPtr<JS::Object> task_destination;
  61. // 3. Let crossOriginIsolatedCapability be false.
  62. auto cross_origin_isolated_capability = HTML::CanUseCrossOriginIsolatedAPIs::No;
  63. // 4. If request’s client is non-null, then:
  64. if (request.client() != nullptr) {
  65. // 1. Set taskDestination to request’s client’s global object.
  66. task_destination = request.client()->global_object();
  67. // 2. Set crossOriginIsolatedCapability to request’s client’s cross-origin isolated capability.
  68. cross_origin_isolated_capability = request.client()->cross_origin_isolated_capability();
  69. }
  70. // FIXME: 5. If useParallelQueue is true, then set taskDestination to the result of starting a new parallel queue.
  71. (void)use_parallel_queue;
  72. (void)task_destination;
  73. // 6. Let timingInfo be a new fetch timing info whose start time and post-redirect start time are the coarsened
  74. // shared current time given crossOriginIsolatedCapability, and render-blocking is set to request’s
  75. // render-blocking.
  76. auto timing_info = Infrastructure::FetchTimingInfo::create(vm);
  77. auto now = HighResolutionTime::coarsened_shared_current_time(cross_origin_isolated_capability == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  78. timing_info->set_start_time(now);
  79. timing_info->set_post_redirect_start_time(now);
  80. timing_info->set_render_blocking(request.render_blocking());
  81. // 7. Let fetchParams be a new fetch params whose request is request, timing info is timingInfo, process request
  82. // body chunk length is processRequestBodyChunkLength, process request end-of-body is processRequestEndOfBody,
  83. // process early hints response is processEarlyHintsResponse, process response is processResponse, process
  84. // response consume body is processResponseConsumeBody, process response end-of-body is processResponseEndOfBody,
  85. // task destination is taskDestination, and cross-origin isolated capability is crossOriginIsolatedCapability.
  86. auto fetch_params = Infrastructure::FetchParams::create(vm, request, timing_info);
  87. fetch_params->set_algorithms(algorithms);
  88. if (task_destination)
  89. fetch_params->set_task_destination({ *task_destination });
  90. fetch_params->set_cross_origin_isolated_capability(cross_origin_isolated_capability);
  91. // 8. If request’s body is a byte sequence, then set request’s body to request’s body as a body.
  92. if (auto const* buffer = request.body().get_pointer<ByteBuffer>())
  93. request.set_body(TRY(Infrastructure::byte_sequence_as_body(realm, buffer->bytes())));
  94. // 9. If request’s window is "client", then set request’s window to request’s client, if request’s client’s global
  95. // object is a Window object; otherwise "no-window".
  96. auto const* window = request.window().get_pointer<Infrastructure::Request::Window>();
  97. if (window && *window == Infrastructure::Request::Window::Client) {
  98. if (is<HTML::Window>(request.client()->global_object())) {
  99. request.set_window(request.client());
  100. } else {
  101. request.set_window(Infrastructure::Request::Window::NoWindow);
  102. }
  103. }
  104. // 10. If request’s origin is "client", then set request’s origin to request’s client’s origin.
  105. auto const* origin = request.origin().get_pointer<Infrastructure::Request::Origin>();
  106. if (origin && *origin == Infrastructure::Request::Origin::Client)
  107. request.set_origin(request.client()->origin());
  108. // 12. If request’s policy container is "client", then:
  109. auto const* policy_container = request.policy_container().get_pointer<Infrastructure::Request::PolicyContainer>();
  110. if (policy_container) {
  111. VERIFY(*policy_container == Infrastructure::Request::PolicyContainer::Client);
  112. // 1. If request’s client is non-null, then set request’s policy container to a clone of request’s client’s
  113. // policy container.
  114. if (request.client() != nullptr)
  115. request.set_policy_container(request.client()->policy_container());
  116. // 2. Otherwise, set request’s policy container to a new policy container.
  117. else
  118. request.set_policy_container(HTML::PolicyContainer {});
  119. }
  120. // 13. If request’s header list does not contain `Accept`, then:
  121. if (!request.header_list()->contains("Accept"sv.bytes())) {
  122. // 1. Let value be `*/*`.
  123. auto value = "*/*"sv;
  124. // 2. A user agent should set value to the first matching statement, if any, switching on request’s
  125. // destination:
  126. if (request.destination().has_value()) {
  127. switch (*request.destination()) {
  128. // -> "document"
  129. // -> "frame"
  130. // -> "iframe"
  131. case Infrastructure::Request::Destination::Document:
  132. case Infrastructure::Request::Destination::Frame:
  133. case Infrastructure::Request::Destination::IFrame:
  134. // `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`
  135. value = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"sv;
  136. break;
  137. // -> "image"
  138. case Infrastructure::Request::Destination::Image:
  139. // `image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5`
  140. value = "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"sv;
  141. break;
  142. // -> "style"
  143. case Infrastructure::Request::Destination::Style:
  144. // `text/css,*/*;q=0.1`
  145. value = "text/css,*/*;q=0.1"sv;
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. // 3. Append (`Accept`, value) to request’s header list.
  152. auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Accept"sv, value.bytes()));
  153. TRY_OR_THROW_OOM(vm, request.header_list()->append(move(header)));
  154. }
  155. // 14. If request’s header list does not contain `Accept-Language`, then user agents should append
  156. // (`Accept-Language, an appropriate header value) to request’s header list.
  157. if (!request.header_list()->contains("Accept-Language"sv.bytes())) {
  158. auto header = MUST(Infrastructure::Header::from_string_pair("Accept-Language"sv, "*"sv));
  159. TRY_OR_THROW_OOM(vm, request.header_list()->append(move(header)));
  160. }
  161. // 15. If request’s priority is null, then use request’s initiator, destination, and render-blocking appropriately
  162. // in setting request’s priority to a user-agent-defined object.
  163. // NOTE: The user-agent-defined object could encompass stream weight and dependency for HTTP/2, and equivalent
  164. // information used to prioritize dispatch and processing of HTTP/1 fetches.
  165. // 16. If request is a subresource request, then:
  166. if (request.is_subresource_request()) {
  167. // FIXME: 1. Let record be a new fetch record whose request is request and controller is fetchParams’s controller.
  168. // FIXME: 2. Append record to request’s client’s fetch group list of fetch records.
  169. }
  170. // 17. Run main fetch given fetchParams.
  171. (void)TRY(main_fetch(realm, fetch_params));
  172. // 18. Return fetchParams’s controller.
  173. return fetch_params->controller();
  174. }
  175. // https://fetch.spec.whatwg.org/#concept-main-fetch
  176. WebIDL::ExceptionOr<Optional<JS::NonnullGCPtr<PendingResponse>>> main_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Recursive recursive)
  177. {
  178. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' with: fetch_params @ {}", &fetch_params);
  179. auto& vm = realm.vm();
  180. // 1. Let request be fetchParams’s request.
  181. auto request = fetch_params.request();
  182. // 2. Let response be null.
  183. JS::GCPtr<Infrastructure::Response> response;
  184. // 3. If request’s local-URLs-only flag is set and request’s current URL is not local, then set response to a
  185. // network error.
  186. if (request->local_urls_only() && !Infrastructure::is_local_url(request->current_url()))
  187. response = Infrastructure::Response::network_error(vm, "Request with 'local-URLs-only' flag must have a local URL"sv);
  188. // FIXME: 4. Run report Content Security Policy violations for request.
  189. // FIXME: 5. Upgrade request to a potentially trustworthy URL, if appropriate.
  190. // FIXME: 6. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate.
  191. // 7. If should request be blocked due to a bad port, should fetching request be blocked as mixed content, or
  192. // should request be blocked by Content Security Policy returns blocked, then set response to a network error.
  193. if (Infrastructure::block_bad_port(request) == Infrastructure::RequestOrResponseBlocking::Blocked
  194. || false // FIXME: "should fetching request be blocked as mixed content"
  195. || false // FIXME: "should request be blocked by Content Security Policy returns blocked"
  196. ) {
  197. response = Infrastructure::Response::network_error(vm, "Request was blocked"sv);
  198. }
  199. // 8. If request’s referrer policy is the empty string, then set request’s referrer policy to request’s policy
  200. // container’s referrer policy.
  201. if (!request->referrer_policy().has_value()) {
  202. VERIFY(request->policy_container().has<HTML::PolicyContainer>());
  203. request->set_referrer_policy(request->policy_container().get<HTML::PolicyContainer>().referrer_policy);
  204. }
  205. // 9. If request’s referrer is not "no-referrer", then set request’s referrer to the result of invoking determine
  206. // request’s referrer.
  207. // NOTE: As stated in Referrer Policy, user agents can provide the end user with options to override request’s
  208. // referrer to "no-referrer" or have it expose less sensitive information.
  209. auto const* referrer = request->referrer().get_pointer<Infrastructure::Request::Referrer>();
  210. if (!referrer || *referrer != Infrastructure::Request::Referrer::NoReferrer) {
  211. auto determined_referrer = ReferrerPolicy::determine_requests_referrer(request);
  212. if (determined_referrer.has_value())
  213. request->set_referrer(*determined_referrer);
  214. else
  215. request->set_referrer(Infrastructure::Request::Referrer::NoReferrer);
  216. }
  217. // 10. Set request’s current URL’s scheme to "https" if all of the following conditions are true:
  218. if (
  219. // - request’s current URL’s scheme is "http"
  220. request->current_url().scheme() == "http"sv
  221. // - request’s current URL’s host is a domain
  222. && URL::host_is_domain(request->current_url().host())
  223. // FIXME: - Matching request’s current URL’s host per Known HSTS Host Domain Name Matching results in either a
  224. // superdomain match with an asserted includeSubDomains directive or a congruent match (with or without an
  225. // asserted includeSubDomains directive) [HSTS]; or DNS resolution for the request finds a matching HTTPS RR
  226. // per section 9.5 of [SVCB].
  227. && false
  228. ) {
  229. request->current_url().set_scheme("https"sv);
  230. }
  231. JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>>()> get_response = [&realm, &vm, &fetch_params, request]() -> WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> {
  232. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' get_response() function");
  233. // -> fetchParams’s preloaded response candidate is not null
  234. if (!fetch_params.preloaded_response_candidate().has<Empty>()) {
  235. // 1. Wait until fetchParams’s preloaded response candidate is not "pending".
  236. HTML::main_thread_event_loop().spin_until([&] {
  237. return !fetch_params.preloaded_response_candidate().has<Infrastructure::FetchParams::PreloadedResponseCandidatePendingTag>();
  238. });
  239. // 2. Assert: fetchParams’s preloaded response candidate is a response.
  240. VERIFY(fetch_params.preloaded_response_candidate().has<JS::NonnullGCPtr<Infrastructure::Response>>());
  241. // 3. Return fetchParams’s preloaded response candidate.
  242. return PendingResponse::create(vm, request, fetch_params.preloaded_response_candidate().get<JS::NonnullGCPtr<Infrastructure::Response>>());
  243. }
  244. // -> request’s current URL’s origin is same origin with request’s origin, and request’s response tainting
  245. // is "basic"
  246. // -> request’s current URL’s scheme is "data"
  247. // -> request’s mode is "navigate" or "websocket"
  248. else if (
  249. (request->origin().has<HTML::Origin>() && URL::url_origin(request->current_url()).is_same_origin(request->origin().get<HTML::Origin>()) && request->response_tainting() == Infrastructure::Request::ResponseTainting::Basic)
  250. || request->current_url().scheme() == "data"sv
  251. || (request->mode() == Infrastructure::Request::Mode::Navigate || request->mode() == Infrastructure::Request::Mode::WebSocket)) {
  252. // 1. Set request’s response tainting to "basic".
  253. request->set_response_tainting(Infrastructure::Request::ResponseTainting::Basic);
  254. // 2. Return the result of running scheme fetch given fetchParams.
  255. return scheme_fetch(realm, fetch_params);
  256. // NOTE: HTML assigns any documents and workers created from URLs whose scheme is "data" a unique
  257. // opaque origin. Service workers can only be created from URLs whose scheme is an HTTP(S) scheme.
  258. }
  259. // -> request’s mode is "same-origin"
  260. else if (request->mode() == Infrastructure::Request::Mode::SameOrigin) {
  261. // Return a network error.
  262. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'same-origin' mode must have same URL and request origin"sv));
  263. }
  264. // -> request’s mode is "no-cors"
  265. else if (request->mode() == Infrastructure::Request::Mode::NoCORS) {
  266. // 1. If request’s redirect mode is not "follow", then return a network error.
  267. if (request->redirect_mode() != Infrastructure::Request::RedirectMode::Follow)
  268. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'no-cors' mode must have redirect mode set to 'follow'"sv));
  269. // 2. Set request’s response tainting to "opaque".
  270. request->set_response_tainting(Infrastructure::Request::ResponseTainting::Opaque);
  271. // 3. Return the result of running scheme fetch given fetchParams.
  272. return scheme_fetch(realm, fetch_params);
  273. }
  274. // -> request’s current URL’s scheme is not an HTTP(S) scheme
  275. else if (!Infrastructure::is_http_or_https_scheme(request->current_url().scheme())) {
  276. // NOTE: At this point all other request modes have been handled. Ensure we're not lying in the error message :^)
  277. VERIFY(request->mode() == Infrastructure::Request::Mode::CORS);
  278. // Return a network error.
  279. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' mode must have URL with HTTP or HTTPS scheme"sv));
  280. }
  281. // -> request’s use-CORS-preflight flag is set
  282. // -> request’s unsafe-request flag is set and either request’s method is not a CORS-safelisted method or
  283. // CORS-unsafe request-header names with request’s header list is not empty
  284. else if (
  285. request->use_cors_preflight()
  286. || (request->unsafe_request()
  287. && (!Infrastructure::is_cors_safelisted_method(request->method())
  288. || !TRY_OR_THROW_OOM(vm, Infrastructure::get_cors_unsafe_header_names(request->header_list())).is_empty()))) {
  289. // 1. Set request’s response tainting to "cors".
  290. request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  291. auto returned_pending_response = PendingResponse::create(vm, request);
  292. // 2. Let corsWithPreflightResponse be the result of running HTTP fetch given fetchParams and true.
  293. auto cors_with_preflight_response = TRY(http_fetch(realm, fetch_params, MakeCORSPreflight::Yes));
  294. cors_with_preflight_response->when_loaded([returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> cors_with_preflight_response) {
  295. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' cors_with_preflight_response load callback");
  296. // 3. If corsWithPreflightResponse is a network error, then clear cache entries using request.
  297. if (cors_with_preflight_response->is_network_error()) {
  298. // FIXME: Clear cache entries
  299. }
  300. // 4. Return corsWithPreflightResponse.
  301. returned_pending_response->resolve(cors_with_preflight_response);
  302. });
  303. return returned_pending_response;
  304. }
  305. // -> Otherwise
  306. else {
  307. // 1. Set request’s response tainting to "cors".
  308. request->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  309. // 2. Return the result of running HTTP fetch given fetchParams.
  310. return http_fetch(realm, fetch_params);
  311. }
  312. };
  313. if (recursive == Recursive::Yes) {
  314. // 12. If response is null, then set response to the result of running the steps corresponding to the first
  315. // matching statement:
  316. auto pending_response = !response
  317. ? TRY(get_response())
  318. : PendingResponse::create(vm, request, *response);
  319. // 13. If recursive is true, then return response.
  320. return pending_response;
  321. }
  322. // 11. If recursive is false, then run the remaining steps in parallel.
  323. Platform::EventLoopPlugin::the().deferred_invoke([&realm, &vm, &fetch_params, request, response, get_response = move(get_response)] {
  324. // 12. If response is null, then set response to the result of running the steps corresponding to the first
  325. // matching statement:
  326. auto pending_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  327. if (!response) {
  328. auto pending_response_or_error = get_response();
  329. if (pending_response_or_error.is_error())
  330. return;
  331. pending_response = pending_response_or_error.release_value();
  332. }
  333. pending_response->when_loaded([&realm, &vm, &fetch_params, request, response, response_was_null = !response](JS::NonnullGCPtr<Infrastructure::Response> resolved_response) mutable {
  334. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'main fetch' pending_response load callback");
  335. if (response_was_null)
  336. response = resolved_response;
  337. // 14. If response is not a network error and response is not a filtered response, then:
  338. if (!response->is_network_error() && !is<Infrastructure::FilteredResponse>(*response)) {
  339. // 1. If request’s response tainting is "cors", then:
  340. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS) {
  341. // FIXME: 1. Let headerNames be the result of extracting header list values given
  342. // `Access-Control-Expose-Headers` and response’s header list.
  343. // FIXME: 2. If request’s credentials mode is not "include" and headerNames contains `*`, then set
  344. // response’s CORS-exposed header-name list to all unique header names in response’s header
  345. // list.
  346. // FIXME: 3. Otherwise, if headerNames is not null or failure, then set response’s CORS-exposed
  347. // header-name list to headerNames.
  348. }
  349. // 2. Set response to the following filtered response with response as its internal response, depending
  350. // on request’s response tainting:
  351. response = TRY_OR_IGNORE([&]() -> WebIDL::ExceptionOr<JS::NonnullGCPtr<Infrastructure::Response>> {
  352. switch (request->response_tainting()) {
  353. // -> "basic"
  354. case Infrastructure::Request::ResponseTainting::Basic:
  355. // basic filtered response
  356. return TRY_OR_THROW_OOM(vm, Infrastructure::BasicFilteredResponse::create(vm, *response));
  357. // -> "cors"
  358. case Infrastructure::Request::ResponseTainting::CORS:
  359. // CORS filtered response
  360. return TRY_OR_THROW_OOM(vm, Infrastructure::CORSFilteredResponse::create(vm, *response));
  361. // -> "opaque"
  362. case Infrastructure::Request::ResponseTainting::Opaque:
  363. // opaque filtered response
  364. return Infrastructure::OpaqueFilteredResponse::create(vm, *response);
  365. default:
  366. VERIFY_NOT_REACHED();
  367. }
  368. }());
  369. }
  370. // 15. Let internalResponse be response, if response is a network error, and response’s internal response
  371. // otherwise.
  372. auto internal_response = response->is_network_error()
  373. ? JS::NonnullGCPtr { *response }
  374. : static_cast<Infrastructure::FilteredResponse&>(*response).internal_response();
  375. // 16. If internalResponse’s URL list is empty, then set it to a clone of request’s URL list.
  376. // NOTE: A response’s URL list can be empty (for example, when the response represents an about URL).
  377. if (internal_response->url_list().is_empty())
  378. internal_response->set_url_list(request->url_list());
  379. // 17. If request has a redirect-tainted origin, then set internalResponse’s has-cross-origin-redirects to true.
  380. if (request->has_redirect_tainted_origin())
  381. internal_response->set_has_cross_origin_redirects(true);
  382. // 18. If request’s timing allow failed flag is unset, then set internalResponse’s timing allow passed flag.
  383. if (!request->timing_allow_failed())
  384. internal_response->set_timing_allow_passed(true);
  385. // 19. If response is not a network error and any of the following returns blocked
  386. if (!response->is_network_error() && (
  387. // FIXME: - should internalResponse to request be blocked as mixed content
  388. false
  389. // FIXME: - should internalResponse to request be blocked by Content Security Policy
  390. || false
  391. // - should internalResponse to request be blocked due to its MIME type
  392. || TRY_OR_IGNORE(Infrastructure::should_response_to_request_be_blocked_due_to_its_mime_type(internal_response, request)) == Infrastructure::RequestOrResponseBlocking::Blocked
  393. // - should internalResponse to request be blocked due to nosniff
  394. || TRY_OR_IGNORE(Infrastructure::should_response_to_request_be_blocked_due_to_nosniff(internal_response, request)) == Infrastructure::RequestOrResponseBlocking::Blocked)) {
  395. // then set response and internalResponse to a network error.
  396. response = internal_response = Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Response was blocked"_string));
  397. }
  398. // 20. If response’s type is "opaque", internalResponse’s status is 206, internalResponse’s range-requested
  399. // flag is set, and request’s header list does not contain `Range`, then set response and
  400. // internalResponse to a network error.
  401. // NOTE: Traditionally, APIs accept a ranged response even if a range was not requested. This prevents a
  402. // partial response from an earlier ranged request being provided to an API that did not make a range
  403. // request.
  404. if (response->type() == Infrastructure::Response::Type::Opaque
  405. && internal_response->status() == 206
  406. && internal_response->range_requested()
  407. && !request->header_list()->contains("Range"sv.bytes())) {
  408. response = internal_response = Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Response has status 206 and 'range-requested' flag set, but request has no 'Range' header"_string));
  409. }
  410. // 21. If response is not a network error and either request’s method is `HEAD` or `CONNECT`, or
  411. // internalResponse’s status is a null body status, set internalResponse’s body to null and disregard
  412. // any enqueuing toward it (if any).
  413. // NOTE: This standardizes the error handling for servers that violate HTTP.
  414. if (!response->is_network_error() && (StringView { request->method() }.is_one_of("HEAD"sv, "CONNECT"sv) || Infrastructure::is_null_body_status(internal_response->status())))
  415. internal_response->set_body({});
  416. // 22. If request’s integrity metadata is not the empty string, then:
  417. if (!request->integrity_metadata().is_empty()) {
  418. // 1. Let processBodyError be this step: run fetch response handover given fetchParams and a network
  419. // error.
  420. auto process_body_error = [&]() -> WebIDL::ExceptionOr<void> {
  421. return fetch_response_handover(realm, fetch_params, Infrastructure::Response::network_error(vm, "Response body could not be processed"sv));
  422. };
  423. // 2. If response’s body is null, then run processBodyError and abort these steps.
  424. if (!response->body().has_value()) {
  425. TRY_OR_IGNORE(process_body_error());
  426. return;
  427. }
  428. // FIXME: 3. Let processBody given bytes be these steps:
  429. // 1. If bytes do not match request’s integrity metadata, then run processBodyError and abort these steps.
  430. // 2. Set response’s body to bytes as a body.
  431. // 3. Run fetch response handover given fetchParams and response.
  432. // FIXME: 4. Fully read response’s body given processBody and processBodyError.
  433. }
  434. // 23. Otherwise, run fetch response handover given fetchParams and response.
  435. else {
  436. TRY_OR_IGNORE(fetch_response_handover(realm, fetch_params, *response));
  437. }
  438. });
  439. });
  440. return Optional<JS::NonnullGCPtr<PendingResponse>> {};
  441. }
  442. // https://fetch.spec.whatwg.org/#fetch-finale
  443. WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Infrastructure::Response& response)
  444. {
  445. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'fetch response handover' with: fetch_params @ {}, response @ {}", &fetch_params, &response);
  446. auto& vm = realm.vm();
  447. // 1. Let timingInfo be fetchParams’s timing info.
  448. auto timing_info = fetch_params.timing_info();
  449. // 2. If response is not a network error and fetchParams’s request’s client is a secure context, then set
  450. // timingInfo’s server-timing headers to the result of getting, decoding, and splitting `Server-Timing` from
  451. // response’s header list.
  452. // The user agent may decide to expose `Server-Timing` headers to non-secure contexts requests as well.
  453. auto client = fetch_params.request()->client();
  454. if (!response.is_network_error() && client != nullptr && HTML::is_secure_context(*client)) {
  455. auto server_timing_headers = TRY_OR_THROW_OOM(vm, response.header_list()->get_decode_and_split("Server-Timing"sv.bytes()));
  456. if (server_timing_headers.has_value())
  457. timing_info->set_server_timing_headers(server_timing_headers.release_value());
  458. }
  459. // 3. Let processResponseEndOfBody be the following steps:
  460. auto process_response_end_of_body = [&vm, &response, &fetch_params, timing_info] {
  461. // 1. Let unsafeEndTime be the unsafe shared current time.
  462. auto unsafe_end_time = HighResolutionTime::unsafe_shared_current_time();
  463. // 2. If fetchParams’s request’s destination is "document", then set fetchParams’s controller’s full timing
  464. // info to fetchParams’s timing info.
  465. if (fetch_params.request()->destination() == Infrastructure::Request::Destination::Document)
  466. fetch_params.controller()->set_full_timing_info(fetch_params.timing_info());
  467. // 3. Set fetchParams’s controller’s report timing steps to the following steps given a global object global:
  468. fetch_params.controller()->set_report_timing_steps([&vm, &response, &fetch_params, timing_info, unsafe_end_time](JS::Object const& global) mutable {
  469. // 1. If fetchParams’s request’s URL’s scheme is not an HTTP(S) scheme, then return.
  470. if (!Infrastructure::is_http_or_https_scheme(fetch_params.request()->url().scheme()))
  471. return;
  472. // 2. Set timingInfo’s end time to the relative high resolution time given unsafeEndTime and global.
  473. timing_info->set_end_time(HighResolutionTime::relative_high_resolution_time(unsafe_end_time, global));
  474. // 3. Let cacheState be response’s cache state.
  475. auto cache_state = response.cache_state();
  476. // 4. Let bodyInfo be response’s body info.
  477. auto body_info = response.body_info();
  478. // 5. If response’s timing allow passed flag is not set, then set timingInfo to the result of creating an
  479. // opaque timing info for timingInfo, set bodyInfo to a new response body info, and set cacheState to
  480. // the empty string.
  481. // NOTE: This covers the case of response being a network error.
  482. if (!response.timing_allow_passed()) {
  483. timing_info = Infrastructure::create_opaque_timing_info(vm, timing_info);
  484. body_info = Infrastructure::Response::BodyInfo {};
  485. cache_state = {};
  486. }
  487. // 6. Let responseStatus be 0 if fetchParams’s request’s mode is "navigate" and response’s has-cross-origin-redirects is true; otherwise response’s status.
  488. auto response_status = fetch_params.request()->mode() == Infrastructure::Request::Mode::Navigate && response.has_cross_origin_redirects()
  489. ? 0
  490. : response.status();
  491. // FIXME: 7. If fetchParams’s request’s initiator type is not null, then mark resource timing given timingInfo,
  492. // request’s URL, request’s initiator type, global, cacheState, bodyInfo, and responseStatus.
  493. (void)timing_info;
  494. (void)global;
  495. (void)cache_state;
  496. (void)body_info;
  497. (void)response_status;
  498. });
  499. // 4. Let processResponseEndOfBodyTask be the following steps:
  500. auto process_response_end_of_body_task = [&fetch_params, &response] {
  501. // 1. Set fetchParams’s request’s done flag.
  502. fetch_params.request()->set_done(true);
  503. // 2. If fetchParams’s process response end-of-body is non-null, then run fetchParams’s process response
  504. // end-of-body given response.
  505. if (fetch_params.algorithms()->process_response_end_of_body().has_value())
  506. (*fetch_params.algorithms()->process_response_end_of_body())(response);
  507. // 3. If fetchParams’s request’s initiator type is non-null and fetchParams’s request’s client’s global
  508. // object is fetchParams’s task destination, then run fetchParams’s controller’s report timing steps
  509. // given fetchParams’s request’s client’s global object.
  510. auto client = fetch_params.request()->client();
  511. auto const* task_destination_global_object = fetch_params.task_destination().get_pointer<JS::NonnullGCPtr<JS::Object>>();
  512. if (client != nullptr && task_destination_global_object != nullptr) {
  513. if (fetch_params.request()->initiator_type().has_value() && &client->global_object() == task_destination_global_object->ptr())
  514. fetch_params.controller()->report_timing(client->global_object());
  515. }
  516. };
  517. // FIXME: Handle 'parallel queue' task destination
  518. auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
  519. // 5. Queue a fetch task to run processResponseEndOfBodyTask with fetchParams’s task destination.
  520. Infrastructure::queue_fetch_task(task_destination, move(process_response_end_of_body_task));
  521. };
  522. // FIXME: Handle 'parallel queue' task destination
  523. auto task_destination = fetch_params.task_destination().get<JS::NonnullGCPtr<JS::Object>>();
  524. // 4. If fetchParams’s process response is non-null, then queue a fetch task to run fetchParams’s process response
  525. // given response, with fetchParams’s task destination.
  526. if (fetch_params.algorithms()->process_response().has_value()) {
  527. Infrastructure::queue_fetch_task(task_destination, [&fetch_params, &response]() {
  528. (*fetch_params.algorithms()->process_response())(response);
  529. });
  530. }
  531. // 5. If response’s body is null, then run processResponseEndOfBody.
  532. if (!response.body().has_value()) {
  533. process_response_end_of_body();
  534. }
  535. // 6. Otherwise:
  536. else {
  537. // FIXME: 1. Let transformStream be a new TransformStream.
  538. // FIXME: 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.
  539. // FIXME: 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm set
  540. // to processResponseEndOfBody.
  541. // FIXME: 4. Set response’s body’s stream to the result of response’s body’s stream piped through transformStream.
  542. }
  543. // 7. If fetchParams’s process response consume body is non-null, then:
  544. if (fetch_params.algorithms()->process_response_consume_body().has_value()) {
  545. // 1. Let processBody given nullOrBytes be this step: run fetchParams’s process response consume body given
  546. // response and nullOrBytes.
  547. auto process_body = [&fetch_params, &response](Variant<ByteBuffer, Empty> const& null_or_bytes) {
  548. (*fetch_params.algorithms()->process_response_consume_body())(response, null_or_bytes);
  549. };
  550. // 2. Let processBodyError be this step: run fetchParams’s process response consume body given response and
  551. // failure.
  552. auto process_body_error = [&fetch_params, &response](auto&) {
  553. (*fetch_params.algorithms()->process_response_consume_body())(response, Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag {});
  554. };
  555. // 3. If response’s body is null, then queue a fetch task to run processBody given null, with fetchParams’s
  556. // task destination.
  557. if (!response.body().has_value()) {
  558. Infrastructure::queue_fetch_task(task_destination, [process_body = move(process_body)]() {
  559. process_body({});
  560. });
  561. }
  562. // 4. Otherwise, fully read response’s body given processBody, processBodyError, and fetchParams’s task
  563. // destination.
  564. else {
  565. TRY(response.body()->fully_read(realm, move(process_body), move(process_body_error), fetch_params.task_destination()));
  566. }
  567. }
  568. return {};
  569. }
  570. // https://fetch.spec.whatwg.org/#concept-scheme-fetch
  571. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> scheme_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params)
  572. {
  573. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'scheme fetch' with: fetch_params @ {}", &fetch_params);
  574. auto& vm = realm.vm();
  575. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  576. if (fetch_params.is_canceled())
  577. return PendingResponse::create(vm, fetch_params.request(), Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  578. // 2. Let request be fetchParams’s request.
  579. auto request = fetch_params.request();
  580. // 3. Switch on request’s current URL’s scheme and run the associated steps:
  581. // -> "about"
  582. if (request->current_url().scheme() == "about"sv) {
  583. // If request’s current URL’s path is the string "blank", then return a new response whose status message is
  584. // `OK`, header list is « (`Content-Type`, `text/html;charset=utf-8`) », and body is the empty byte sequence as
  585. // a body.
  586. // NOTE: URLs such as "about:config" are handled during navigation and result in a network error in the context
  587. // of fetching.
  588. if (request->current_url().serialize_path() == "blank"sv) {
  589. auto response = Infrastructure::Response::create(vm);
  590. response->set_status_message(MUST(ByteBuffer::copy("OK"sv.bytes())));
  591. auto header = MUST(Infrastructure::Header::from_string_pair("Content-Type"sv, "text/html;charset=utf-8"sv));
  592. TRY_OR_THROW_OOM(vm, response->header_list()->append(move(header)));
  593. response->set_body(MUST(Infrastructure::byte_sequence_as_body(realm, ""sv.bytes())));
  594. return PendingResponse::create(vm, request, response);
  595. }
  596. }
  597. // -> "blob"
  598. else if (request->current_url().scheme() == "blob"sv) {
  599. // FIXME: Support 'blob://' URLs
  600. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has 'blob:' URL which is currently unsupported"sv));
  601. }
  602. // -> "data"
  603. else if (request->current_url().scheme() == "data"sv) {
  604. // 1. Let dataURLStruct be the result of running the data: URL processor on request’s current URL.
  605. auto const& url = request->current_url();
  606. auto data_or_error = url.data_payload_is_base64()
  607. ? decode_base64(url.data_payload())
  608. : TRY_OR_THROW_OOM(vm, ByteBuffer::copy(url.data_payload().bytes()));
  609. // 2. If dataURLStruct is failure, then return a network error.
  610. if (data_or_error.is_error())
  611. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has invalid base64 'data:' URL"sv));
  612. // 3. Let mimeType be dataURLStruct’s MIME type, serialized.
  613. auto const& mime_type = url.data_mime_type();
  614. // 4. Return a new response whose status message is `OK`, header list is « (`Content-Type`, mimeType) », and
  615. // body is dataURLStruct’s body as a body.
  616. auto response = Infrastructure::Response::create(vm);
  617. response->set_status_message(MUST(ByteBuffer::copy("OK"sv.bytes())));
  618. auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Content-Type"sv, mime_type));
  619. TRY_OR_THROW_OOM(vm, response->header_list()->append(move(header)));
  620. response->set_body(TRY(Infrastructure::byte_sequence_as_body(realm, data_or_error.value().span())));
  621. return PendingResponse::create(vm, request, response);
  622. }
  623. // -> "file"
  624. else if (request->current_url().scheme() == "file"sv) {
  625. // For now, unfortunate as it is, file: URLs are left as an exercise for the reader.
  626. // When in doubt, return a network error.
  627. return TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, fetch_params));
  628. }
  629. // -> HTTP(S) scheme
  630. else if (Infrastructure::is_http_or_https_scheme(request->current_url().scheme())) {
  631. // Return the result of running HTTP fetch given fetchParams.
  632. return http_fetch(realm, fetch_params);
  633. }
  634. // 4. Return a network error.
  635. auto message = request->current_url().scheme() == "about"sv
  636. ? TRY_OR_THROW_OOM(vm, "Request has invalid 'about:' URL, only 'about:blank' can be fetched"_string)
  637. : TRY_OR_THROW_OOM(vm, "Request URL has invalid scheme, must be one of 'about', 'blob', 'data', 'file', 'http', or 'https'"_string);
  638. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, move(message)));
  639. }
  640. // https://fetch.spec.whatwg.org/#concept-http-fetch
  641. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, MakeCORSPreflight make_cors_preflight)
  642. {
  643. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' with: fetch_params @ {}, make_cors_preflight = {}",
  644. &fetch_params, make_cors_preflight == MakeCORSPreflight::Yes ? "Yes"sv : "No"sv);
  645. auto& vm = realm.vm();
  646. // 1. Let request be fetchParams’s request.
  647. auto request = fetch_params.request();
  648. // 2. Let response be null.
  649. JS::GCPtr<Infrastructure::Response> response;
  650. // 3. Let actualResponse be null.
  651. JS::GCPtr<Infrastructure::Response> actual_response;
  652. // 4. If request’s service-workers mode is "all", then:
  653. if (request->service_workers_mode() == Infrastructure::Request::ServiceWorkersMode::All) {
  654. // 1. Let requestForServiceWorker be a clone of request.
  655. auto request_for_service_worker = TRY(request->clone(realm));
  656. // 2. If requestForServiceWorker’s body is non-null, then:
  657. if (!request_for_service_worker->body().has<Empty>()) {
  658. // FIXME: 1. Let transformStream be a new TransformStream.
  659. // FIXME: 2. Let transformAlgorithm given chunk be these steps:
  660. // FIXME: 3. Set up transformStream with transformAlgorithm set to transformAlgorithm.
  661. // FIXME: 4. Set requestForServiceWorker’s body’s stream to the result of requestForServiceWorker’s body’s stream
  662. // piped through transformStream.
  663. }
  664. // 3. Let serviceWorkerStartTime be the coarsened shared current time given fetchParams’s cross-origin isolated
  665. // capability.
  666. auto service_worker_start_time = HighResolutionTime::coarsened_shared_current_time(fetch_params.cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  667. // FIXME: 4. Set response to the result of invoking handle fetch for requestForServiceWorker, with fetchParams’s
  668. // controller and fetchParams’s cross-origin isolated capability.
  669. // 5. If response is not null, then:
  670. if (response) {
  671. // 1. Set fetchParams’s timing info’s final service worker start time to serviceWorkerStartTime.
  672. fetch_params.timing_info()->set_final_service_worker_start_time(service_worker_start_time);
  673. // 2. If request’s body is non-null, then cancel request’s body with undefined.
  674. if (!request->body().has<Empty>()) {
  675. // FIXME: Implement cancelling streams
  676. }
  677. // 3. Set actualResponse to response, if response is not a filtered response, and to response’s internal
  678. // response otherwise.
  679. actual_response = !is<Infrastructure::FilteredResponse>(*response)
  680. ? JS::NonnullGCPtr { *response }
  681. : static_cast<Infrastructure::FilteredResponse const&>(*response).internal_response();
  682. // 4. If one of the following is true
  683. if (
  684. // - response’s type is "error"
  685. response->type() == Infrastructure::Response::Type::Error
  686. // - request’s mode is "same-origin" and response’s type is "cors"
  687. || (request->mode() == Infrastructure::Request::Mode::SameOrigin && response->type() == Infrastructure::Response::Type::CORS)
  688. // - request’s mode is not "no-cors" and response’s type is "opaque"
  689. || (request->mode() != Infrastructure::Request::Mode::NoCORS && response->type() == Infrastructure::Response::Type::Opaque)
  690. // - request’s redirect mode is not "manual" and response’s type is "opaqueredirect"
  691. || (request->redirect_mode() != Infrastructure::Request::RedirectMode::Manual && response->type() == Infrastructure::Response::Type::OpaqueRedirect)
  692. // - request’s redirect mode is not "follow" and response’s URL list has more than one item.
  693. || (request->redirect_mode() != Infrastructure::Request::RedirectMode::Follow && response->url_list().size() > 1)) {
  694. // then return a network error.
  695. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Invalid request/response state combination"sv));
  696. }
  697. }
  698. }
  699. JS::GCPtr<PendingResponse> pending_actual_response;
  700. auto returned_pending_response = PendingResponse::create(vm, request);
  701. // 5. If response is null, then:
  702. if (!response) {
  703. // 1. If makeCORSPreflight is true and one of these conditions is true:
  704. // NOTE: This step checks the CORS-preflight cache and if there is no suitable entry it performs a
  705. // CORS-preflight fetch which, if successful, populates the cache. The purpose of the CORS-preflight
  706. // fetch is to ensure the fetched resource is familiar with the CORS protocol. The cache is there to
  707. // minimize the number of CORS-preflight fetches.
  708. JS::GCPtr<PendingResponse> pending_preflight_response;
  709. if (make_cors_preflight == MakeCORSPreflight::Yes && (
  710. // - There is no method cache entry match for request’s method using request, and either request’s
  711. // method is not a CORS-safelisted method or request’s use-CORS-preflight flag is set.
  712. // FIXME: We currently have no cache, so there will always be no method cache entry.
  713. (!Infrastructure::is_cors_safelisted_method(request->method()) || request->use_cors_preflight())
  714. // - There is at least one item in the CORS-unsafe request-header names with request’s header list for
  715. // which there is no header-name cache entry match using request.
  716. // FIXME: We currently have no cache, so there will always be no header-name cache entry.
  717. || !TRY_OR_THROW_OOM(vm, Infrastructure::get_cors_unsafe_header_names(request->header_list())).is_empty())) {
  718. // 1. Let preflightResponse be the result of running CORS-preflight fetch given request.
  719. pending_preflight_response = TRY(cors_preflight_fetch(realm, request));
  720. // NOTE: Step 2 is performed in pending_preflight_response's load callback below.
  721. }
  722. 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>> {
  723. // 2. If request’s redirect mode is "follow", then set request’s service-workers mode to "none".
  724. // NOTE: Redirects coming from the network (as opposed to from a service worker) are not to be exposed to a
  725. // service worker.
  726. if (request->redirect_mode() == Infrastructure::Request::RedirectMode::Follow)
  727. request->set_service_workers_mode(Infrastructure::Request::ServiceWorkersMode::None);
  728. // 3. Set response and actualResponse to the result of running HTTP-network-or-cache fetch given fetchParams.
  729. return http_network_or_cache_fetch(*realm, *fetch_params);
  730. };
  731. if (pending_preflight_response) {
  732. pending_actual_response = PendingResponse::create(vm, request);
  733. pending_preflight_response->when_loaded([returned_pending_response, pending_actual_response, fetch_main_content = move(fetch_main_content)](JS::NonnullGCPtr<Infrastructure::Response> preflight_response) {
  734. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_preflight_response load callback");
  735. // 2. If preflightResponse is a network error, then return preflightResponse.
  736. if (preflight_response->is_network_error()) {
  737. returned_pending_response->resolve(preflight_response);
  738. return;
  739. }
  740. auto pending_main_content_response = TRY_OR_IGNORE(fetch_main_content());
  741. pending_main_content_response->when_loaded([pending_actual_response](JS::NonnullGCPtr<Infrastructure::Response> main_content_response) {
  742. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_main_content_response load callback");
  743. pending_actual_response->resolve(main_content_response);
  744. });
  745. });
  746. } else {
  747. pending_actual_response = TRY(fetch_main_content());
  748. }
  749. } else {
  750. pending_actual_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  751. }
  752. pending_actual_response->when_loaded([&realm, &vm, &fetch_params, request, response, actual_response, returned_pending_response, response_was_null = !response](JS::NonnullGCPtr<Infrastructure::Response> resolved_actual_response) mutable {
  753. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' pending_actual_response load callback");
  754. if (response_was_null) {
  755. response = actual_response = resolved_actual_response;
  756. // 4. If request’s response tainting is "cors" and a CORS check for request and response returns failure,
  757. // then return a network error.
  758. // NOTE: As the CORS check is not to be applied to responses whose status is 304 or 407, or responses from
  759. // a service worker for that matter, it is applied here.
  760. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS
  761. && !TRY_OR_IGNORE(cors_check(request, *response))) {
  762. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Request with 'cors' response tainting failed CORS check"_string)));
  763. return;
  764. }
  765. // 5. If the TAO check for request and response returns failure, then set request’s timing allow failed flag.
  766. if (!TRY_OR_IGNORE(tao_check(request, *response)))
  767. request->set_timing_allow_failed(true);
  768. }
  769. // 6. If either request’s response tainting or response’s type is "opaque", and the cross-origin resource
  770. // policy check with request’s origin, request’s client, request’s destination, and actualResponse returns
  771. // blocked, then return a network error.
  772. // NOTE: The cross-origin resource policy check runs for responses coming from the network and responses coming
  773. // from the service worker. This is different from the CORS check, as request’s client and the service
  774. // worker can have different embedder policies.
  775. if ((request->response_tainting() == Infrastructure::Request::ResponseTainting::Opaque || response->type() == Infrastructure::Response::Type::Opaque)
  776. && false // FIXME: "and the cross-origin resource policy check with request’s origin, request’s client, request’s destination, and actualResponse returns blocked"
  777. ) {
  778. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Response was blocked by cross-origin resource policy check"_string)));
  779. return;
  780. }
  781. JS::GCPtr<PendingResponse> inner_pending_response;
  782. // 7. If actualResponse’s status is a redirect status, then:
  783. if (Infrastructure::is_redirect_status(actual_response->status())) {
  784. // FIXME: 1. If actualResponse’s status is not 303, request’s body is not null, and the connection uses HTTP/2,
  785. // then user agents may, and are even encouraged to, transmit an RST_STREAM frame.
  786. // NOTE: 303 is excluded as certain communities ascribe special status to it.
  787. // 2. Switch on request’s redirect mode:
  788. switch (request->redirect_mode()) {
  789. // -> "error"
  790. case Infrastructure::Request::RedirectMode::Error:
  791. // Set response to a network error.
  792. response = Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Request with 'error' redirect mode received redirect response"_string));
  793. break;
  794. // -> "manual"
  795. case Infrastructure::Request::RedirectMode::Manual:
  796. // 1. If request’s mode is "navigate", then set fetchParams’s controller’s next manual redirect steps
  797. // to run HTTP-redirect fetch given fetchParams and response.
  798. if (request->mode() == Infrastructure::Request::Mode::Navigate) {
  799. fetch_params.controller()->set_next_manual_redirect_steps([&realm, &fetch_params, response] {
  800. (void)http_redirect_fetch(realm, fetch_params, *response);
  801. });
  802. }
  803. // 2. Otherwise, set response to an opaque-redirect filtered response whose internal response is
  804. // actualResponse.
  805. else {
  806. response = Infrastructure::OpaqueRedirectFilteredResponse::create(vm, *actual_response);
  807. }
  808. break;
  809. // -> "follow"
  810. case Infrastructure::Request::RedirectMode::Follow:
  811. // Set response to the result of running HTTP-redirect fetch given fetchParams and response.
  812. inner_pending_response = TRY_OR_IGNORE(http_redirect_fetch(realm, fetch_params, *response));
  813. break;
  814. default:
  815. VERIFY_NOT_REACHED();
  816. }
  817. }
  818. if (inner_pending_response) {
  819. inner_pending_response->when_loaded([returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> response) {
  820. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP fetch' inner_pending_response load callback");
  821. returned_pending_response->resolve(response);
  822. });
  823. } else {
  824. returned_pending_response->resolve(*response);
  825. }
  826. });
  827. // 8. Return response.
  828. // NOTE: Typically actualResponse’s body’s stream is still being enqueued to after returning.
  829. return returned_pending_response;
  830. }
  831. // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch
  832. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_redirect_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Infrastructure::Response& response)
  833. {
  834. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-redirect fetch' with: fetch_params @ {}, response = {}", &fetch_params, &response);
  835. auto& vm = realm.vm();
  836. // 1. Let request be fetchParams’s request.
  837. auto request = fetch_params.request();
  838. // 2. Let actualResponse be response, if response is not a filtered response, and response’s internal response
  839. // otherwise.
  840. auto actual_response = !is<Infrastructure::FilteredResponse>(response)
  841. ? JS::NonnullGCPtr { response }
  842. : static_cast<Infrastructure::FilteredResponse const&>(response).internal_response();
  843. // 3. Let locationURL be actualResponse’s location URL given request’s current URL’s fragment.
  844. auto const& fragment = request->current_url().fragment();
  845. auto fragment_string = fragment.is_null() ? Optional<String> {} : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(fragment));
  846. auto location_url_or_error = actual_response->location_url(fragment_string);
  847. // 4. If locationURL is null, then return response.
  848. if (!location_url_or_error.is_error() && !location_url_or_error.value().has_value())
  849. return PendingResponse::create(vm, request, response);
  850. // 5. If locationURL is failure, then return a network error.
  851. if (location_url_or_error.is_error())
  852. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request redirect URL is invalid"sv));
  853. auto location_url = location_url_or_error.release_value().release_value();
  854. // 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network error.
  855. if (!Infrastructure::is_http_or_https_scheme(location_url.scheme()))
  856. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request redirect URL must have HTTP or HTTPS scheme"sv));
  857. // 7. If request’s redirect count is 20, then return a network error.
  858. if (request->redirect_count() == 20)
  859. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has reached maximum redirect count of 20"sv));
  860. // 8. Increase request’s redirect count by 1.
  861. request->set_redirect_count(request->redirect_count() + 1);
  862. // 8. If request’s mode is "cors", locationURL includes credentials, and request’s origin is not same origin with
  863. // locationURL’s origin, then return a network error.
  864. if (request->mode() == Infrastructure::Request::Mode::CORS
  865. && location_url.includes_credentials()
  866. && request->origin().has<HTML::Origin>()
  867. && !request->origin().get<HTML::Origin>().is_same_origin(URL::url_origin(location_url))) {
  868. 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));
  869. }
  870. // 10. If request’s response tainting is "cors" and locationURL includes credentials, then return a network error.
  871. // NOTE: This catches a cross-origin resource redirecting to a same-origin URL.
  872. if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS && location_url.includes_credentials())
  873. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'cors' response tainting must not include credentials in redirect URL"sv));
  874. // 11. If actualResponse’s status is not 303, request’s body is non-null, and request’s body’s source is null, then
  875. // return a network error.
  876. if (actual_response->status() != 303
  877. && !request->body().has<Empty>()
  878. && request->body().get<Infrastructure::Body>().source().has<Empty>()) {
  879. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request has body but no body source"sv));
  880. }
  881. // 12. If one of the following is true
  882. if (
  883. // - actualResponse’s status is 301 or 302 and request’s method is `POST`
  884. ((actual_response->status() == 301 || actual_response->status() == 302) && request->method() == "POST"sv.bytes())
  885. // - actualResponse’s status is 303 and request’s method is not `GET` or `HEAD`
  886. || (actual_response->status() == 303 && !(request->method() == "GET"sv.bytes() || request->method() == "HEAD"sv.bytes()))
  887. // then:
  888. ) {
  889. // 1. Set request’s method to `GET` and request’s body to null.
  890. request->set_method(MUST(ByteBuffer::copy("GET"sv.bytes())));
  891. request->set_body({});
  892. static constexpr Array request_body_header_names {
  893. "Content-Encoding"sv,
  894. "Content-Language"sv,
  895. "Content-Location"sv,
  896. "Content-Type"sv
  897. };
  898. // 2. For each headerName of request-body-header name, delete headerName from request’s header list.
  899. for (auto header_name : request_body_header_names.span())
  900. request->header_list()->delete_(header_name.bytes());
  901. }
  902. // 13. If request’s current URL’s origin is not same origin with locationURL’s origin, then for each headerName of
  903. // CORS non-wildcard request-header name, delete headerName from request’s header list.
  904. // NOTE: I.e., the moment another origin is seen after the initial request, the `Authorization` header is removed.
  905. if (!URL::url_origin(request->current_url()).is_same_origin(URL::url_origin(location_url))) {
  906. static constexpr Array cors_non_wildcard_request_header_names {
  907. "Authorization"sv
  908. };
  909. for (auto header_name : cors_non_wildcard_request_header_names)
  910. request->header_list()->delete_(header_name.bytes());
  911. }
  912. // 14. If request’s body is non-null, then set request’s body to the body of the result of safely extracting
  913. // request’s body’s source.
  914. // NOTE: request’s body’s source’s nullity has already been checked.
  915. if (!request->body().has<Empty>()) {
  916. auto const& source = request->body().get<Infrastructure::Body>().source();
  917. // NOTE: BodyInitOrReadableBytes is a superset of Body::SourceType
  918. auto converted_source = source.has<ByteBuffer>()
  919. ? BodyInitOrReadableBytes { source.get<ByteBuffer>() }
  920. : BodyInitOrReadableBytes { source.get<JS::Handle<FileAPI::Blob>>() };
  921. auto [body, _] = TRY(safely_extract_body(realm, converted_source));
  922. request->set_body(move(body));
  923. }
  924. // 15. Let timingInfo be fetchParams’s timing info.
  925. auto timing_info = fetch_params.timing_info();
  926. // 16. Set timingInfo’s redirect end time and post-redirect start time to the coarsened shared current time given
  927. // fetchParams’s cross-origin isolated capability.
  928. auto now = HighResolutionTime::coarsened_shared_current_time(fetch_params.cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  929. timing_info->set_redirect_end_time(now);
  930. timing_info->set_post_redirect_start_time(now);
  931. // 17. If timingInfo’s redirect start time is 0, then set timingInfo’s redirect start time to timingInfo’s start
  932. // time.
  933. if (timing_info->redirect_start_time() == 0)
  934. timing_info->set_redirect_start_time(timing_info->start_time());
  935. // 18. Append locationURL to request’s URL list.
  936. request->url_list().append(location_url);
  937. // FIXME: 19. Invoke set request’s referrer policy on redirect on request and actualResponse.
  938. // 20. Return the result of running main fetch given fetchParams and true.
  939. return TRY(main_fetch(realm, fetch_params, Recursive::Yes)).release_value();
  940. }
  941. // https://fetch.spec.whatwg.org/#concept-http-network-or-cache-fetch
  942. 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)
  943. {
  944. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-network-or-cache fetch' with: fetch_params @ {}, is_authentication_fetch = {}, is_new_connection_fetch = {}",
  945. &fetch_params, is_authentication_fetch == IsAuthenticationFetch::Yes ? "Yes"sv : "No"sv, is_new_connection_fetch == IsNewConnectionFetch::Yes ? "Yes"sv : "No"sv);
  946. auto& vm = realm.vm();
  947. // 1. Let request be fetchParams’s request.
  948. auto request = fetch_params.request();
  949. // 2. Let httpFetchParams be null.
  950. JS::GCPtr<Infrastructure::FetchParams const> http_fetch_params;
  951. // 3. Let httpRequest be null.
  952. JS::GCPtr<Infrastructure::Request> http_request;
  953. // 4. Let response be null.
  954. JS::GCPtr<Infrastructure::Response> response;
  955. // 5. Let storedResponse be null.
  956. JS::GCPtr<Infrastructure::Response> stored_response;
  957. // 6. Let httpCache be null.
  958. // (Typeless until we actually implement it, needed for checks below)
  959. void* http_cache = nullptr;
  960. // 7. Let the revalidatingFlag be unset.
  961. auto revalidating_flag = RefCountedFlag::create(false);
  962. auto include_credentials = IncludeCredentials::No;
  963. // 8. Run these steps, but abort when fetchParams is canceled:
  964. // NOTE: There's an 'if aborted' check after this anyway, so not doing this is fine and only incurs a small delay.
  965. // For now, support for aborting fetch requests is limited anyway as ResourceLoader doesn't support it.
  966. auto aborted = false;
  967. {
  968. ScopeGuard set_aborted = [&] {
  969. if (fetch_params.is_canceled())
  970. aborted = true;
  971. };
  972. // 1. If request’s window is "no-window" and request’s redirect mode is "error", then set httpFetchParams to
  973. // fetchParams and httpRequest to request.
  974. if (request->window().has<Infrastructure::Request::Window>()
  975. && request->window().get<Infrastructure::Request::Window>() == Infrastructure::Request::Window::NoWindow
  976. && request->redirect_mode() == Infrastructure::Request::RedirectMode::Error) {
  977. http_fetch_params = fetch_params;
  978. http_request = request;
  979. }
  980. // 2. Otherwise:
  981. else {
  982. // 1. Set httpRequest to a clone of request.
  983. // NOTE: Implementations are encouraged to avoid teeing request’s body’s stream when request’s body’s
  984. // source is null as only a single body is needed in that case. E.g., when request’s body’s source
  985. // is null, redirects and authentication will end up failing the fetch.
  986. http_request = TRY(request->clone(realm));
  987. // 2. Set httpFetchParams to a copy of fetchParams.
  988. // 3. Set httpFetchParams’s request to httpRequest.
  989. auto new_http_fetch_params = Infrastructure::FetchParams::create(vm, *http_request, fetch_params.timing_info());
  990. new_http_fetch_params->set_algorithms(fetch_params.algorithms());
  991. new_http_fetch_params->set_task_destination(fetch_params.task_destination());
  992. new_http_fetch_params->set_cross_origin_isolated_capability(fetch_params.cross_origin_isolated_capability());
  993. new_http_fetch_params->set_preloaded_response_candidate(fetch_params.preloaded_response_candidate());
  994. http_fetch_params = new_http_fetch_params;
  995. }
  996. // 3. Let includeCredentials be true if one of
  997. if (
  998. // - request’s credentials mode is "include"
  999. request->credentials_mode() == Infrastructure::Request::CredentialsMode::Include
  1000. // - request’s credentials mode is "same-origin" and request’s response tainting is "basic"
  1001. || (request->credentials_mode() == Infrastructure::Request::CredentialsMode::SameOrigin
  1002. && request->response_tainting() == Infrastructure::Request::ResponseTainting::Basic)
  1003. // is true; otherwise false.
  1004. ) {
  1005. include_credentials = IncludeCredentials::Yes;
  1006. } else {
  1007. include_credentials = IncludeCredentials::No;
  1008. }
  1009. // 4. If Cross-Origin-Embedder-Policy allows credentials with request returns false, then set
  1010. // includeCredentials to false.
  1011. if (!request->cross_origin_embedder_policy_allows_credentials())
  1012. include_credentials = IncludeCredentials::No;
  1013. // 5. Let contentLength be httpRequest’s body’s length, if httpRequest’s body is non-null; otherwise null.
  1014. auto content_length = http_request->body().has<Infrastructure::Body>()
  1015. ? http_request->body().get<Infrastructure::Body>().length()
  1016. : Optional<u64> {};
  1017. // 6. Let contentLengthHeaderValue be null.
  1018. auto content_length_header_value = Optional<ByteBuffer> {};
  1019. // 7. If httpRequest’s body is null and httpRequest’s method is `POST` or `PUT`, then set
  1020. // contentLengthHeaderValue to `0`.
  1021. if (http_request->body().has<Empty>() && StringView { http_request->method() }.is_one_of("POST"sv, "PUT"sv))
  1022. content_length_header_value = MUST(ByteBuffer::copy("0"sv.bytes()));
  1023. // 8. If contentLength is non-null, then set contentLengthHeaderValue to contentLength, serialized and
  1024. // isomorphic encoded.
  1025. if (content_length.has_value())
  1026. content_length_header_value = MUST(ByteBuffer::copy(TRY_OR_THROW_OOM(vm, String::number(*content_length)).bytes()));
  1027. // 9. If contentLengthHeaderValue is non-null, then append (`Content-Length`, contentLengthHeaderValue) to
  1028. // httpRequest’s header list.
  1029. if (content_length_header_value.has_value()) {
  1030. auto header = Infrastructure::Header {
  1031. .name = MUST(ByteBuffer::copy("Content-Length"sv.bytes())),
  1032. .value = content_length_header_value.release_value(),
  1033. };
  1034. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1035. }
  1036. // FIXME: 10. If contentLength is non-null and httpRequest’s keepalive is true, then:
  1037. if (content_length.has_value() && http_request->keepalive()) {
  1038. // FIXME: 1-5., requires 'fetch records' and 'fetch group' concepts.
  1039. // NOTE: The above limit ensures that requests that are allowed to outlive the environment settings object
  1040. // and contain a body, have a bounded size and are not allowed to stay alive indefinitely.
  1041. }
  1042. // 11. If httpRequest’s referrer is a URL, then:
  1043. if (http_request->referrer().has<AK::URL>()) {
  1044. // 1. Let referrerValue be httpRequest’s referrer, serialized and isomorphic encoded.
  1045. auto referrer_value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(http_request->referrer().get<AK::URL>().serialize().bytes()));
  1046. // 2. Append (`Referer`, referrerValue) to httpRequest’s header list.
  1047. auto header = Infrastructure::Header {
  1048. .name = MUST(ByteBuffer::copy("Referer"sv.bytes())),
  1049. .value = move(referrer_value),
  1050. };
  1051. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1052. }
  1053. // 12. Append a request `Origin` header for httpRequest.
  1054. TRY_OR_THROW_OOM(vm, http_request->add_origin_header());
  1055. // FIXME: 13. Append the Fetch metadata headers for httpRequest.
  1056. // 14. If httpRequest’s header list does not contain `User-Agent`, then user agents should append
  1057. // (`User-Agent`, default `User-Agent` value) to httpRequest’s header list.
  1058. if (!http_request->header_list()->contains("User-Agent"sv.bytes())) {
  1059. auto header = Infrastructure::Header {
  1060. .name = MUST(ByteBuffer::copy("User-Agent"sv.bytes())),
  1061. .value = TRY_OR_THROW_OOM(vm, Infrastructure::default_user_agent_value()),
  1062. };
  1063. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1064. }
  1065. // 15. If httpRequest’s cache mode is "default" and httpRequest’s header list contains `If-Modified-Since`,
  1066. // `If-None-Match`, `If-Unmodified-Since`, `If-Match`, or `If-Range`, then set httpRequest’s cache mode to
  1067. // "no-store".
  1068. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::Default
  1069. && (http_request->header_list()->contains("If-Modified-Since"sv.bytes())
  1070. || http_request->header_list()->contains("If-None-Match"sv.bytes())
  1071. || http_request->header_list()->contains("If-Unmodified-Since"sv.bytes())
  1072. || http_request->header_list()->contains("If-Match"sv.bytes())
  1073. || http_request->header_list()->contains("If-Range"sv.bytes()))) {
  1074. http_request->set_cache_mode(Infrastructure::Request::CacheMode::NoStore);
  1075. }
  1076. // 16. If httpRequest’s cache mode is "no-cache", httpRequest’s prevent no-cache cache-control header
  1077. // modification flag is unset, and httpRequest’s header list does not contain `Cache-Control`, then append
  1078. // (`Cache-Control`, `max-age=0`) to httpRequest’s header list.
  1079. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::NoCache
  1080. && !http_request->prevent_no_cache_cache_control_header_modification()
  1081. && !http_request->header_list()->contains("Cache-Control"sv.bytes())) {
  1082. auto header = MUST(Infrastructure::Header::from_string_pair("Cache-Control"sv, "max-age=0"sv));
  1083. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1084. }
  1085. // 17. If httpRequest’s cache mode is "no-store" or "reload", then:
  1086. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::NoStore
  1087. || http_request->cache_mode() == Infrastructure::Request::CacheMode::Reload) {
  1088. // 1. If httpRequest’s header list does not contain `Pragma`, then append (`Pragma`, `no-cache`) to
  1089. // httpRequest’s header list.
  1090. if (!http_request->header_list()->contains("Pragma"sv.bytes())) {
  1091. auto header = MUST(Infrastructure::Header::from_string_pair("Pragma"sv, "no-cache"sv));
  1092. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1093. }
  1094. // 2. If httpRequest’s header list does not contain `Cache-Control`, then append
  1095. // (`Cache-Control`, `no-cache`) to httpRequest’s header list.
  1096. if (!http_request->header_list()->contains("Cache-Control"sv.bytes())) {
  1097. auto header = MUST(Infrastructure::Header::from_string_pair("Cache-Control"sv, "no-cache"sv));
  1098. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1099. }
  1100. }
  1101. // 18. If httpRequest’s header list contains `Range`, then append (`Accept-Encoding`, `identity`) to
  1102. // httpRequest’s header list.
  1103. // NOTE: This avoids a failure when handling content codings with a part of an encoded response.
  1104. // Additionally, many servers mistakenly ignore `Range` headers if a non-identity encoding is accepted.
  1105. if (http_request->header_list()->contains("Range"sv.bytes())) {
  1106. auto header = MUST(Infrastructure::Header::from_string_pair("Accept-Encoding"sv, "identity"sv));
  1107. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1108. }
  1109. // 19. Modify httpRequest’s header list per HTTP. Do not append a given header if httpRequest’s header list
  1110. // contains that header’s name.
  1111. // NOTE: It would be great if we could make this more normative somehow. At this point headers such as
  1112. // `Accept-Encoding`, `Connection`, `DNT`, and `Host`, are to be appended if necessary.
  1113. // `Accept`, `Accept-Charset`, and `Accept-Language` must not be included at this point.
  1114. // NOTE: `Accept` and `Accept-Language` are already included (unless fetch() is used, which does not include
  1115. // the latter by default), and `Accept-Charset` is a waste of bytes. See HTTP header layer division for
  1116. // more details.
  1117. // 20. If includeCredentials is true, then:
  1118. if (include_credentials == IncludeCredentials::Yes) {
  1119. // 1. If the user agent is not configured to block cookies for httpRequest (see section 7 of [COOKIES]),
  1120. // then:
  1121. if (true) {
  1122. // 1. Let cookies be the result of running the "cookie-string" algorithm (see section 5.4 of [COOKIES])
  1123. // with the user agent’s cookie store and httpRequest’s current URL.
  1124. auto cookies = ([&] {
  1125. // FIXME: Getting to the page client reliably is way too complicated, and going via the document won't work in workers.
  1126. auto document = Bindings::host_defined_environment_settings_object(realm).responsible_document();
  1127. if (!document)
  1128. return DeprecatedString::empty();
  1129. auto* page = document->page();
  1130. if (!page)
  1131. return DeprecatedString::empty();
  1132. return page->client().page_did_request_cookie(http_request->current_url(), Cookie::Source::Http);
  1133. })();
  1134. // 2. If cookies is not the empty string, then append (`Cookie`, cookies) to httpRequest’s header list.
  1135. if (!cookies.is_empty()) {
  1136. auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Cookie"sv, cookies));
  1137. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1138. }
  1139. }
  1140. // 2. If httpRequest’s header list does not contain `Authorization`, then:
  1141. if (!http_request->header_list()->contains("Authorization"sv.bytes())) {
  1142. // 1. Let authorizationValue be null.
  1143. auto authorization_value = Optional<String> {};
  1144. // 2. If there’s an authentication entry for httpRequest and either httpRequest’s use-URL-credentials
  1145. // flag is unset or httpRequest’s current URL does not include credentials, then set
  1146. // authorizationValue to authentication entry.
  1147. if (false // FIXME: "If there’s an authentication entry for httpRequest"
  1148. && (!http_request->use_url_credentials() || !http_request->current_url().includes_credentials())) {
  1149. // FIXME: "set authorizationValue to authentication entry."
  1150. }
  1151. // 3. Otherwise, if httpRequest’s current URL does include credentials and isAuthenticationFetch is
  1152. // true, set authorizationValue to httpRequest’s current URL, converted to an `Authorization` value.
  1153. else if (http_request->current_url().includes_credentials() && is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1154. auto const& url = http_request->current_url();
  1155. auto payload = TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.username(), url.password()));
  1156. authorization_value = TRY_OR_THROW_OOM(vm, encode_base64(payload.bytes()));
  1157. }
  1158. // 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to
  1159. // httpRequest’s header list.
  1160. if (authorization_value.has_value()) {
  1161. auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Authorization"sv, *authorization_value));
  1162. TRY_OR_THROW_OOM(vm, http_request->header_list()->append(move(header)));
  1163. }
  1164. }
  1165. }
  1166. // FIXME: 21. If there’s a proxy-authentication entry, use it as appropriate.
  1167. // NOTE: This intentionally does not depend on httpRequest’s credentials mode.
  1168. // FIXME: 22. Set httpCache to the result of determining the HTTP cache partition, given httpRequest.
  1169. // 23. If httpCache is null, then set httpRequest’s cache mode to "no-store".
  1170. if (!http_cache)
  1171. http_request->set_cache_mode(Infrastructure::Request::CacheMode::NoStore);
  1172. // 24. If httpRequest’s cache mode is neither "no-store" nor "reload", then:
  1173. if (http_request->cache_mode() != Infrastructure::Request::CacheMode::NoStore
  1174. && http_request->cache_mode() != Infrastructure::Request::CacheMode::Reload) {
  1175. // 1. Set storedResponse to the result of selecting a response from the httpCache, possibly needing
  1176. // validation, as per the "Constructing Responses from Caches" chapter of HTTP Caching [HTTP-CACHING],
  1177. // if any.
  1178. // NOTE: As mandated by HTTP, this still takes the `Vary` header into account.
  1179. stored_response = nullptr;
  1180. // 2. If storedResponse is non-null, then:
  1181. if (stored_response) {
  1182. // FIXME: Caching is not implemented yet.
  1183. VERIFY_NOT_REACHED();
  1184. }
  1185. }
  1186. }
  1187. // 9. If aborted, then return the appropriate network error for fetchParams.
  1188. if (aborted)
  1189. return PendingResponse::create(vm, request, Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1190. JS::GCPtr<PendingResponse> pending_forward_response;
  1191. // 10. If response is null, then:
  1192. if (!response) {
  1193. // 1. If httpRequest’s cache mode is "only-if-cached", then return a network error.
  1194. if (http_request->cache_mode() == Infrastructure::Request::CacheMode::OnlyIfCached)
  1195. return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Request with 'only-if-cached' cache mode doesn't have a cached response"sv));
  1196. // 2. Let forwardResponse be the result of running HTTP-network fetch given httpFetchParams, includeCredentials,
  1197. // and isNewConnectionFetch.
  1198. pending_forward_response = TRY(nonstandard_resource_loader_file_or_http_network_fetch(realm, *http_fetch_params, include_credentials, is_new_connection_fetch));
  1199. } else {
  1200. pending_forward_response = PendingResponse::create(vm, request, Infrastructure::Response::create(vm));
  1201. }
  1202. auto returned_pending_response = PendingResponse::create(vm, request);
  1203. 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 {
  1204. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP-network-or-cache fetch' pending_forward_response load callback");
  1205. if (response_was_null) {
  1206. auto forward_response = resolved_forward_response;
  1207. // NOTE: TRACE is omitted as it is a forbidden method in Fetch.
  1208. auto method_is_unsafe = StringView { http_request->method() }.is_one_of("GET"sv, "HEAD"sv, "OPTIONS"sv);
  1209. // 3. If httpRequest’s method is unsafe and forwardResponse’s status is in the range 200 to 399, inclusive,
  1210. // invalidate appropriate stored responses in httpCache, as per the "Invalidation" chapter of HTTP
  1211. // Caching, and set storedResponse to null.
  1212. if (method_is_unsafe && forward_response->status() >= 200 && forward_response->status() <= 399) {
  1213. // FIXME: "invalidate appropriate stored responses in httpCache, as per the "Invalidation" chapter of HTTP Caching"
  1214. stored_response = nullptr;
  1215. }
  1216. // 4. If the revalidatingFlag is set and forwardResponse’s status is 304, then:
  1217. if (revalidating_flag->value() && forward_response->status() == 304) {
  1218. // FIXME: 1. Update storedResponse’s header list using forwardResponse’s header list, as per the "Freshening
  1219. // Stored Responses upon Validation" chapter of HTTP Caching.
  1220. // NOTE: This updates the stored response in cache as well.
  1221. // 2. Set response to storedResponse.
  1222. response = stored_response;
  1223. // 3. Set response’s cache state to "validated".
  1224. if (response)
  1225. response->set_cache_state(Infrastructure::Response::CacheState::Validated);
  1226. }
  1227. // 5. If response is null, then:
  1228. if (!response) {
  1229. // 1. Set response to forwardResponse.
  1230. response = forward_response;
  1231. // FIXME: 2. Store httpRequest and forwardResponse in httpCache, as per the "Storing Responses in Caches"
  1232. // chapter of HTTP Caching.
  1233. // NOTE: If forwardResponse is a network error, this effectively caches the network error, which is
  1234. // sometimes known as "negative caching".
  1235. // NOTE: The associated body info is stored in the cache alongside the response.
  1236. }
  1237. }
  1238. // 11. Set response’s URL list to a clone of httpRequest’s URL list.
  1239. response->set_url_list(http_request->url_list());
  1240. // 12. If httpRequest’s header list contains `Range`, then set response’s range-requested flag.
  1241. if (http_request->header_list()->contains("Range"sv.bytes()))
  1242. response->set_range_requested(true);
  1243. // 13. Set response’s request-includes-credentials to includeCredentials.
  1244. response->set_request_includes_credentials(include_credentials == IncludeCredentials::Yes);
  1245. auto inner_pending_response = PendingResponse::create(vm, request, *response);
  1246. // 14. If response’s status is 401, httpRequest’s response tainting is not "cors", includeCredentials is true,
  1247. // and request’s window is an environment settings object, then:
  1248. if (response->status() == 401
  1249. && http_request->response_tainting() != Infrastructure::Request::ResponseTainting::CORS
  1250. && include_credentials == IncludeCredentials::Yes
  1251. && request->window().has<JS::GCPtr<HTML::EnvironmentSettingsObject>>()) {
  1252. // 1. Needs testing: multiple `WWW-Authenticate` headers, missing, parsing issues.
  1253. // (Red box in the spec, no-op)
  1254. // 2. If request’s body is non-null, then:
  1255. if (!request->body().has<Empty>()) {
  1256. // 1. If request’s body’s source is null, then return a network error.
  1257. if (request->body().get<Infrastructure::Body>().source().has<Empty>()) {
  1258. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Request has body but no body source"_string)));
  1259. return;
  1260. }
  1261. // 2. Set request’s body to the body of the result of safely extracting request’s body’s source.
  1262. auto const& source = request->body().get<Infrastructure::Body>().source();
  1263. // NOTE: BodyInitOrReadableBytes is a superset of Body::SourceType
  1264. auto converted_source = source.has<ByteBuffer>()
  1265. ? BodyInitOrReadableBytes { source.get<ByteBuffer>() }
  1266. : BodyInitOrReadableBytes { source.get<JS::Handle<FileAPI::Blob>>() };
  1267. auto [body, _] = TRY_OR_IGNORE(safely_extract_body(realm, converted_source));
  1268. request->set_body(move(body));
  1269. }
  1270. // 3. If request’s use-URL-credentials flag is unset or isAuthenticationFetch is true, then:
  1271. if (!request->use_url_credentials() || is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1272. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1273. if (fetch_params.is_canceled()) {
  1274. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1275. return;
  1276. }
  1277. // FIXME: 2. Let username and password be the result of prompting the end user for a username and password,
  1278. // respectively, in request’s window.
  1279. dbgln("Fetch: Username/password prompt is not implemented, using empty strings. This request will probably fail.");
  1280. auto username = DeprecatedString::empty();
  1281. auto password = DeprecatedString::empty();
  1282. // 3. Set the username given request’s current URL and username.
  1283. request->current_url().set_username(move(username));
  1284. // 4. Set the password given request’s current URL and password.
  1285. request->current_url().set_password(move(password));
  1286. }
  1287. // 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true.
  1288. inner_pending_response = TRY_OR_IGNORE(http_network_or_cache_fetch(realm, fetch_params, IsAuthenticationFetch::Yes));
  1289. }
  1290. 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) {
  1291. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'HTTP network-or-cache fetch' inner_pending_response load callback");
  1292. // 15. If response’s status is 407, then:
  1293. if (response->status() == 407) {
  1294. // 1. If request’s window is "no-window", then return a network error.
  1295. if (request->window().has<Infrastructure::Request::Window>()
  1296. && request->window().get<Infrastructure::Request::Window>() == Infrastructure::Request::Window::NoWindow) {
  1297. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("Request requires proxy authentication but has 'no-window' set"_string)));
  1298. return;
  1299. }
  1300. // 2. Needs testing: multiple `Proxy-Authenticate` headers, missing, parsing issues.
  1301. // (Red box in the spec, no-op)
  1302. // 3. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1303. if (fetch_params.is_canceled()) {
  1304. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1305. return;
  1306. }
  1307. // FIXME: 4. Prompt the end user as appropriate in request’s window and store the result as a
  1308. // proxy-authentication entry.
  1309. // NOTE: Remaining details surrounding proxy authentication are defined by HTTP.
  1310. // FIXME: 5. Set response to the result of running HTTP-network-or-cache fetch given fetchParams.
  1311. // (Doing this without step 4 would potentially lead to an infinite request cycle.)
  1312. }
  1313. auto inner_pending_response = PendingResponse::create(vm, request, *response);
  1314. // 16. If all of the following are true
  1315. if (
  1316. // - response’s status is 421
  1317. response->status() == 421
  1318. // - isNewConnectionFetch is false
  1319. && is_new_connection_fetch == IsNewConnectionFetch::No
  1320. // - request’s body is null, or request’s body is non-null and request’s body’s source is non-null
  1321. && (request->body().has<Empty>() || !request->body().get<Infrastructure::Body>().source().has<Empty>())
  1322. // then:
  1323. ) {
  1324. // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
  1325. if (fetch_params.is_canceled()) {
  1326. returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(vm, fetch_params));
  1327. return;
  1328. }
  1329. // 2. Set response to the result of running HTTP-network-or-cache fetch given fetchParams,
  1330. // isAuthenticationFetch, and true.
  1331. inner_pending_response = TRY_OR_IGNORE(http_network_or_cache_fetch(realm, fetch_params, is_authentication_fetch, IsNewConnectionFetch::Yes));
  1332. }
  1333. inner_pending_response->when_loaded([returned_pending_response, is_authentication_fetch](JS::NonnullGCPtr<Infrastructure::Response> response) {
  1334. // 17. If isAuthenticationFetch is true, then create an authentication entry for request and the given
  1335. // realm.
  1336. if (is_authentication_fetch == IsAuthenticationFetch::Yes) {
  1337. // FIXME: "create an authentication entry for request and the given realm"
  1338. }
  1339. returned_pending_response->resolve(response);
  1340. });
  1341. });
  1342. });
  1343. // 18. Return response.
  1344. // NOTE: Typically response’s body’s stream is still being enqueued to after returning.
  1345. return returned_pending_response;
  1346. }
  1347. #if defined(WEB_FETCH_DEBUG)
  1348. static void log_load_request(auto const& load_request)
  1349. {
  1350. dbgln("Fetch: Invoking ResourceLoader");
  1351. dbgln("> {} {} HTTP/1.1", load_request.method(), load_request.url());
  1352. for (auto const& [name, value] : load_request.headers())
  1353. dbgln("> {}: {}", name, value);
  1354. dbgln(">");
  1355. for (auto line : StringView { load_request.body() }.split_view('\n', SplitBehavior::KeepEmpty))
  1356. dbgln("> {}", line);
  1357. }
  1358. static void log_response(auto const& status_code, auto const& headers, auto const& data)
  1359. {
  1360. dbgln("< HTTP/1.1 {}", status_code.value_or(0));
  1361. for (auto const& [name, value] : headers)
  1362. dbgln("< {}: {}", name, value);
  1363. dbgln("<");
  1364. for (auto line : StringView { data }.split_view('\n', SplitBehavior::KeepEmpty))
  1365. dbgln("< {}", line);
  1366. }
  1367. #endif
  1368. // https://fetch.spec.whatwg.org/#concept-http-network-fetch
  1369. // Drop-in replacement for 'HTTP-network fetch', but obviously non-standard :^)
  1370. // It also handles file:// URLs since those can also go through ResourceLoader.
  1371. 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)
  1372. {
  1373. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'non-standard HTTP-network fetch' with: fetch_params @ {}", &fetch_params);
  1374. auto& vm = realm.vm();
  1375. (void)include_credentials;
  1376. (void)is_new_connection_fetch;
  1377. auto request = fetch_params.request();
  1378. Page* page = nullptr;
  1379. auto& global_object = realm.global_object();
  1380. if (is<HTML::Window>(global_object))
  1381. page = static_cast<HTML::Window&>(global_object).page();
  1382. // NOTE: Using LoadRequest::create_for_url_on_page here will unconditionally add cookies as long as there's a page available.
  1383. // However, it is up to http_network_or_cache_fetch to determine if cookies should be added to the request.
  1384. LoadRequest load_request;
  1385. load_request.set_url(request->current_url());
  1386. if (page)
  1387. load_request.set_page(*page);
  1388. load_request.set_method(DeprecatedString::copy(request->method()));
  1389. for (auto const& header : *request->header_list())
  1390. load_request.set_header(DeprecatedString::copy(header.name), DeprecatedString::copy(header.value));
  1391. if (auto const* body = request->body().get_pointer<Infrastructure::Body>()) {
  1392. TRY(body->source().visit(
  1393. [&](ByteBuffer const& byte_buffer) -> WebIDL::ExceptionOr<void> {
  1394. load_request.set_body(TRY_OR_THROW_OOM(vm, ByteBuffer::copy(byte_buffer)));
  1395. return {};
  1396. },
  1397. [&](JS::Handle<FileAPI::Blob> const& blob_handle) -> WebIDL::ExceptionOr<void> {
  1398. load_request.set_body(TRY_OR_THROW_OOM(vm, ByteBuffer::copy(blob_handle->bytes())));
  1399. return {};
  1400. },
  1401. [](Empty) -> WebIDL::ExceptionOr<void> {
  1402. return {};
  1403. }));
  1404. }
  1405. auto pending_response = PendingResponse::create(vm, request);
  1406. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Invoking ResourceLoader");
  1407. if constexpr (WEB_FETCH_DEBUG)
  1408. log_load_request(load_request);
  1409. ResourceLoader::the().load(
  1410. load_request,
  1411. [&realm, &vm, request, pending_response](auto data, auto& response_headers, auto status_code) {
  1412. dbgln_if(WEB_FETCH_DEBUG, "Fetch: ResourceLoader load for '{}' complete", request->url());
  1413. if constexpr (WEB_FETCH_DEBUG)
  1414. log_response(status_code, response_headers, data);
  1415. auto [body, _] = TRY_OR_IGNORE(extract_body(realm, data));
  1416. auto response = Infrastructure::Response::create(vm);
  1417. response->set_status(status_code.value_or(200));
  1418. response->set_body(move(body));
  1419. for (auto const& [name, value] : response_headers) {
  1420. auto header = TRY_OR_IGNORE(Infrastructure::Header::from_string_pair(name, value));
  1421. TRY_OR_IGNORE(response->header_list()->append(header));
  1422. }
  1423. // FIXME: Set response status message
  1424. pending_response->resolve(response);
  1425. },
  1426. [&vm, request, pending_response](auto& error, auto status_code) {
  1427. dbgln_if(WEB_FETCH_DEBUG, "Fetch: ResourceLoader load for '{}' failed: {} (status {})", request->url(), error, status_code.value_or(0));
  1428. auto response = Infrastructure::Response::create(vm);
  1429. // FIXME: This is ugly, ResourceLoader should tell us.
  1430. if (status_code.value_or(0) == 0) {
  1431. response = Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("HTTP request failed"_string));
  1432. } else {
  1433. response->set_type(Infrastructure::Response::Type::Error);
  1434. response->set_status(status_code.value_or(400));
  1435. // FIXME: Set response status message and body
  1436. }
  1437. pending_response->resolve(response);
  1438. });
  1439. return pending_response;
  1440. }
  1441. // https://fetch.spec.whatwg.org/#cors-preflight-fetch-0
  1442. WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> cors_preflight_fetch(JS::Realm& realm, Infrastructure::Request& request)
  1443. {
  1444. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' with request @ {}", &request);
  1445. auto& vm = realm.vm();
  1446. // 1. Let preflight be a new request whose method is `OPTIONS`, URL list is a clone of request’s URL list, initiator is
  1447. // request’s initiator, destination is request’s destination, origin is request’s origin, referrer is request’s referrer,
  1448. // referrer policy is request’s referrer policy, mode is "cors", and response tainting is "cors".
  1449. auto preflight = Fetch::Infrastructure::Request::create(vm);
  1450. preflight->set_method(TRY_OR_THROW_OOM(vm, ByteBuffer::copy("OPTIONS"sv.bytes())));
  1451. preflight->set_url_list(request.url_list());
  1452. preflight->set_initiator(request.initiator());
  1453. preflight->set_destination(request.destination());
  1454. preflight->set_origin(request.origin());
  1455. preflight->set_referrer(request.referrer());
  1456. preflight->set_referrer_policy(request.referrer_policy());
  1457. preflight->set_mode(Infrastructure::Request::Mode::CORS);
  1458. preflight->set_response_tainting(Infrastructure::Request::ResponseTainting::CORS);
  1459. // 2. Append (`Accept`, `*/*`) to preflight’s header list.
  1460. auto temp_header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Accept"sv, "*/*"sv));
  1461. TRY_OR_THROW_OOM(vm, preflight->header_list()->append(move(temp_header)));
  1462. // 3. Append (`Access-Control-Request-Method`, request’s method) to preflight’s header list.
  1463. temp_header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Access-Control-Request-Method"sv, request.method()));
  1464. TRY_OR_THROW_OOM(vm, preflight->header_list()->append(move(temp_header)));
  1465. // 4. Let headers be the CORS-unsafe request-header names with request’s header list.
  1466. auto headers = TRY_OR_THROW_OOM(vm, Infrastructure::get_cors_unsafe_header_names(request.header_list()));
  1467. // 5. If headers is not empty, then:
  1468. if (!headers.is_empty()) {
  1469. // 1. Let value be the items in headers separated from each other by `,`.
  1470. // NOTE: This intentionally does not use combine, as 0x20 following 0x2C is not the way this was implemented,
  1471. // for better or worse.
  1472. ByteBuffer value;
  1473. bool first = true;
  1474. for (auto const& header : headers) {
  1475. if (!first)
  1476. TRY_OR_THROW_OOM(vm, value.try_append(','));
  1477. TRY_OR_THROW_OOM(vm, value.try_append(header));
  1478. first = false;
  1479. }
  1480. // 2. Append (`Access-Control-Request-Headers`, value) to preflight’s header list.
  1481. temp_header = Infrastructure::Header {
  1482. .name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("Access-Control-Request-Headers"sv.bytes())),
  1483. .value = move(value),
  1484. };
  1485. TRY_OR_THROW_OOM(vm, preflight->header_list()->append(move(temp_header)));
  1486. }
  1487. // 6. Let response be the result of running HTTP-network-or-cache fetch given a new fetch params whose request is preflight.
  1488. // FIXME: The spec doesn't say anything about timing_info here, but FetchParams requires a non-null FetchTimingInfo object.
  1489. auto timing_info = Infrastructure::FetchTimingInfo::create(vm);
  1490. auto fetch_params = Infrastructure::FetchParams::create(vm, preflight, timing_info);
  1491. auto returned_pending_response = PendingResponse::create(vm, request);
  1492. auto preflight_response = TRY(http_network_or_cache_fetch(realm, fetch_params));
  1493. preflight_response->when_loaded([&vm, &request, returned_pending_response](JS::NonnullGCPtr<Infrastructure::Response> response) {
  1494. dbgln_if(WEB_FETCH_DEBUG, "Fetch: Running 'CORS-preflight fetch' preflight_response load callback");
  1495. // 7. If a CORS check for request and response returns success and response’s status is an ok status, then:
  1496. // NOTE: The CORS check is done on request rather than preflight to ensure the correct credentials mode is used.
  1497. if (TRY_OR_IGNORE(cors_check(request, response)) && Infrastructure::is_ok_status(response->status())) {
  1498. // 1. Let methods be the result of extracting header list values given `Access-Control-Allow-Methods` and response’s header list.
  1499. auto methods_or_failure = TRY_OR_IGNORE(Infrastructure::extract_header_list_values("Access-Control-Allow-Methods"sv.bytes(), response->header_list()));
  1500. // 2. Let headerNames be the result of extracting header list values given `Access-Control-Allow-Headers` and
  1501. // response’s header list.
  1502. auto header_names_or_failure = TRY_OR_IGNORE(Infrastructure::extract_header_list_values("Access-Control-Allow-Headers"sv.bytes(), response->header_list()));
  1503. // 3. If either methods or headerNames is failure, return a network error.
  1504. if (methods_or_failure.has<Infrastructure::ExtractHeaderParseFailure>()) {
  1505. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("The Access-Control-Allow-Methods in the CORS-preflight response is syntactically invalid"_string)));
  1506. return;
  1507. }
  1508. if (header_names_or_failure.has<Infrastructure::ExtractHeaderParseFailure>()) {
  1509. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("The Access-Control-Allow-Headers in the CORS-preflight response is syntactically invalid"_string)));
  1510. return;
  1511. }
  1512. // NOTE: We treat "methods_or_failure" being `Empty` as empty Vector here.
  1513. auto methods = methods_or_failure.has<Vector<ByteBuffer>>() ? methods_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
  1514. // NOTE: We treat "header_names_or_failure" being `Empty` as empty Vector here.
  1515. auto header_names = header_names_or_failure.has<Vector<ByteBuffer>>() ? header_names_or_failure.get<Vector<ByteBuffer>>() : Vector<ByteBuffer> {};
  1516. // FIXME: Remove this once extract_header_list_values validates the header and returns multiple values.
  1517. if (!methods.is_empty()) {
  1518. VERIFY(methods.size() == 1);
  1519. auto split_methods = StringView { methods.first() }.split_view(',');
  1520. Vector<ByteBuffer> trimmed_methods;
  1521. for (auto const& method : split_methods) {
  1522. auto trimmed_method = method.trim(" \t"sv);
  1523. auto trimmed_method_as_byte_buffer = TRY_OR_IGNORE(ByteBuffer::copy(trimmed_method.bytes()));
  1524. TRY_OR_IGNORE(trimmed_methods.try_append(move(trimmed_method_as_byte_buffer)));
  1525. }
  1526. methods = move(trimmed_methods);
  1527. }
  1528. // FIXME: Remove this once extract_header_list_values validates the header and returns multiple values.
  1529. if (!header_names.is_empty()) {
  1530. VERIFY(header_names.size() == 1);
  1531. auto split_header_names = StringView { header_names.first() }.split_view(',');
  1532. Vector<ByteBuffer> trimmed_header_names;
  1533. for (auto const& header_name : split_header_names) {
  1534. auto trimmed_header_name = header_name.trim(" \t"sv);
  1535. auto trimmed_header_name_as_byte_buffer = TRY_OR_IGNORE(ByteBuffer::copy(trimmed_header_name.bytes()));
  1536. TRY_OR_IGNORE(trimmed_header_names.try_append(move(trimmed_header_name_as_byte_buffer)));
  1537. }
  1538. header_names = move(trimmed_header_names);
  1539. }
  1540. // 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.
  1541. // NOTE: This ensures that a CORS-preflight fetch that happened due to request’s use-CORS-preflight flag being set is cached.
  1542. if (methods.is_empty() && request.use_cors_preflight())
  1543. methods = Vector { TRY_OR_IGNORE(ByteBuffer::copy(request.method())) };
  1544. // 5. If request’s method is not in methods, request’s method is not a CORS-safelisted method, and request’s credentials mode
  1545. // is "include" or methods does not contain `*`, then return a network error.
  1546. if (!methods.contains_slow(request.method()) && !Infrastructure::is_cors_safelisted_method(request.method())) {
  1547. if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) {
  1548. 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() }))));
  1549. return;
  1550. }
  1551. if (!methods.contains_slow("*"sv.bytes())) {
  1552. 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() }))));
  1553. return;
  1554. }
  1555. }
  1556. // 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
  1557. // for an item in headerNames, then return a network error.
  1558. for (auto const& header : *request.header_list()) {
  1559. if (Infrastructure::is_cors_non_wildcard_request_header_name(header.name)) {
  1560. bool is_in_header_names = false;
  1561. for (auto const& allowed_header_name : header_names) {
  1562. if (StringView { allowed_header_name }.equals_ignoring_ascii_case(header.name)) {
  1563. is_in_header_names = true;
  1564. break;
  1565. }
  1566. }
  1567. if (!is_in_header_names) {
  1568. 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 }))));
  1569. return;
  1570. }
  1571. }
  1572. }
  1573. // 7. For each unsafeName of the CORS-unsafe request-header names with request’s header list, if unsafeName is not a
  1574. // byte-case-insensitive match for an item in headerNames and request’s credentials mode is "include" or headerNames
  1575. // does not contain `*`, return a network error.
  1576. auto unsafe_names = TRY_OR_IGNORE(Infrastructure::get_cors_unsafe_header_names(request.header_list()));
  1577. for (auto const& unsafe_name : unsafe_names) {
  1578. bool is_in_header_names = false;
  1579. for (auto const& header_name : header_names) {
  1580. if (StringView { unsafe_name }.equals_ignoring_ascii_case(header_name)) {
  1581. is_in_header_names = true;
  1582. break;
  1583. }
  1584. }
  1585. if (!is_in_header_names) {
  1586. if (request.credentials_mode() == Infrastructure::Request::CredentialsMode::Include) {
  1587. 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 }))));
  1588. return;
  1589. }
  1590. if (!header_names.contains_slow("*"sv.bytes())) {
  1591. 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 }))));
  1592. return;
  1593. }
  1594. }
  1595. }
  1596. // FIXME: 8. Let max-age be the result of extracting header list values given `Access-Control-Max-Age` and response’s header list.
  1597. // FIXME: 9. If max-age is failure or null, then set max-age to 5.
  1598. // FIXME: 10. If max-age is greater than an imposed limit on max-age, then set max-age to the imposed limit.
  1599. // 11. If the user agent does not provide for a cache, then return response.
  1600. // NOTE: Since we don't currently have a cache, this is always true.
  1601. returned_pending_response->resolve(response);
  1602. return;
  1603. // FIXME: 12. For each method in methods for which there is a method cache entry match using request, set matching entry’s max-age
  1604. // to max-age.
  1605. // FIXME: 13. For each method in methods for which there is no method cache entry match using request, create a new cache entry
  1606. // with request, max-age, method, and null.
  1607. // FIXME: 14. For each headerName in headerNames for which there is a header-name cache entry match using request, set matching
  1608. // entry’s max-age to max-age.
  1609. // FIXME: 15. For each headerName in headerNames for which there is no header-name cache entry match using request, create a
  1610. // new cache entry with request, max-age, null, and headerName.
  1611. // FIXME: 16. Return response.
  1612. }
  1613. // 8. Otherwise, return a network error.
  1614. returned_pending_response->resolve(Infrastructure::Response::network_error(vm, TRY_OR_IGNORE("CORS-preflight check failed"_string)));
  1615. });
  1616. return returned_pending_response;
  1617. }
  1618. }