Fetching.cpp 94 KB

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