From 987198782cab9b2293e44525a3236b357b7f9d32 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Fri, 3 May 2024 20:44:06 +0100 Subject: [PATCH] LibWeb/Fetch: Use "json" destination See: - https://github.com/SerenityOS/serenity/commit/da8d0d8 - https://github.com/whatwg/fetch/commit/49bff76 - https://github.com/whatwg/html/commit/37659e9 --- Userland/Libraries/LibWeb/Fetch/Enums.cpp | 2 ++ .../Libraries/LibWeb/Fetch/Fetching/Fetching.cpp | 5 +++++ .../LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp | 3 ++- Userland/Libraries/LibWeb/Fetch/Request.idl | 2 +- .../Libraries/LibWeb/HTML/Scripting/Fetching.cpp | 14 ++++++++------ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Enums.cpp b/Userland/Libraries/LibWeb/Fetch/Enums.cpp index ef452d44b3b..25377d17dda 100644 --- a/Userland/Libraries/LibWeb/Fetch/Enums.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Enums.cpp @@ -160,6 +160,8 @@ Bindings::RequestDestination to_bindings_enum(Optional> fetch(JS: // `image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5` value = "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"sv; break; + // -> "json" + case Infrastructure::Request::Destination::JSON: + // `application/json,*/*;q=0.5` + value = "application/json,*/*;q=0.5"sv; + break; // -> "style" case Infrastructure::Request::Destination::Style: // `text/css,*/*;q=0.1` diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp index c52ac6bad52..0ecbd23a1a4 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp @@ -99,12 +99,13 @@ bool Request::destination_is_script_like() const // https://fetch.spec.whatwg.org/#subresource-request bool Request::is_subresource_request() const { - // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string. + // A subresource request is a request whose destination is "audio", "audioworklet", "font", "image", "json", "manifest", "paintworklet", "script", "style", "track", "video", "xslt", or the empty string. static constexpr Array subresource_request_destinations = { Destination::Audio, Destination::AudioWorklet, Destination::Font, Destination::Image, + Destination::JSON, Destination::Manifest, Destination::PaintWorklet, Destination::Script, diff --git a/Userland/Libraries/LibWeb/Fetch/Request.idl b/Userland/Libraries/LibWeb/Fetch/Request.idl index 232d18b3942..1916d737d32 100644 --- a/Userland/Libraries/LibWeb/Fetch/Request.idl +++ b/Userland/Libraries/LibWeb/Fetch/Request.idl @@ -49,7 +49,7 @@ dictionary RequestInit { any window; // can only be set to null }; -enum RequestDestination { "", "audio", "audioworklet", "document", "embed", "font", "frame", "iframe", "image", "manifest", "object", "paintworklet", "report", "script", "sharedworker", "style", "track", "video", "worker", "xslt" }; +enum RequestDestination { "", "audio", "audioworklet", "document", "embed", "font", "frame", "iframe", "image", "json", "manifest", "object", "paintworklet", "report", "script", "sharedworker", "style", "track", "video", "worker", "xslt" }; enum RequestMode { "navigate", "same-origin", "no-cors", "cors" }; enum RequestCredentials { "omit", "same-origin", "include" }; enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" }; diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/Fetching.cpp index 3fc112ae5be..1bfe7abb10d 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/Fetching.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/Fetching.cpp @@ -661,25 +661,27 @@ void fetch_single_module_script(JS::Realm& realm, // 7. Set moduleMap[(url, moduleType)] to "fetching". module_map.set(url, module_type, { ModuleMap::EntryType::Fetching, nullptr }); - // 8. Let request be a new request whose URL is url, destination is destination, mode is "cors", referrer is referrer, and client is fetchClient. + // 8. Let request be a new request whose URL is url, mode is "cors", referrer is referrer, and client is fetchClient. auto request = Fetch::Infrastructure::Request::create(realm.vm()); request->set_url(url); - request->set_destination(destination); request->set_mode(Fetch::Infrastructure::Request::Mode::CORS); request->set_referrer(referrer); request->set_client(&fetch_client); - // 9. If destination is "worker", "sharedworker", or "serviceworker", and isTopLevel is true, then set request's mode to "same-origin". + // 9. Set request's destination to the result of running the fetch destination from module type steps given destination and moduleType. + request->set_destination(fetch_destination_from_module_type(destination, module_type)); + + // 10. If destination is "worker", "sharedworker", or "serviceworker", and isTopLevel is true, then set request's mode to "same-origin". if ((destination == Fetch::Infrastructure::Request::Destination::Worker || destination == Fetch::Infrastructure::Request::Destination::SharedWorker || destination == Fetch::Infrastructure::Request::Destination::ServiceWorker) && is_top_level == TopLevelModule::Yes) request->set_mode(Fetch::Infrastructure::Request::Mode::SameOrigin); - // 10. Set request's initiator type to "script". + // 11. Set request's initiator type to "script". request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script); - // 11. Set up the module script request given request and options. + // 12. Set up the module script request given request and options. set_up_module_script_request(request, options); - // 12. If performFetch was given, run performFetch with request, isTopLevel, and with processResponseConsumeBody as defined below. + // 13. If performFetch was given, run performFetch with request, isTopLevel, and with processResponseConsumeBody as defined below. // Otherwise, fetch request with processResponseConsumeBody set to processResponseConsumeBody as defined below. // In both cases, let processResponseConsumeBody given response response and null, failure, or a byte sequence bodyBytes be the following algorithm: auto process_response_consume_body = [&module_map, url, module_type, &settings_object, on_complete](JS::NonnullGCPtr response, Fetch::Infrastructure::FetchAlgorithms::BodyBytes body_bytes) {