Truly independent web browser
LibWeb: Discard a response's body through its fetch params
Luke Wilde3dbe34928189
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex 19d071ae1b0..c3d2e099ca0 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -321,13 +321,6 @@ static void store_response_in_cache(HTTP::MemoryCache& http_cache, Infrastructurhttp_cache.create_entry(request.current_url(), request.method(), request.header_list(), request.request_time(), response.status(), response.status_message(), response.header_list());}-// Ends the delivery of a response's body that the fetch will not read.-static void stop_discarded_response_body(Infrastructure::FetchController& controller, Infrastructure::Response const& response)-{- response.release_request_transfer_lease();- controller.stop_body_transport();-}-// https://fetch.spec.whatwg.org/#concept-fetchGC::Ref<Infrastructure::FetchController> fetch(JS::Realm& realm, Infrastructure::Request& request, Infrastructure::FetchAlgorithms const& algorithms, UseParallelQueue use_parallel_queue, CreateResponseBodyTransferLease create_response_body_transfer_lease){@@ -936,7 +929,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam// - should internalResponse to request be blocked due to nosniff|| Infrastructure::should_response_to_request_be_blocked_due_to_nosniff(internal_response, request) == Infrastructure::RequestOrResponseBlocking::Blocked)) {// then set response and internalResponse to a network error.- stop_discarded_response_body(*controller, *internal_response);+ fetch_params->discard_response_body(*internal_response);response = internal_response = Infrastructure::Response::network_error("Response was blocked"_string);}@@ -950,7 +943,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam&& internal_response->status() == 206&& internal_response->range_requested()&& !request->header_list()->contains("Range"sv)) {- stop_discarded_response_body(*controller, *internal_response);+ fetch_params->discard_response_body(*internal_response);response = internal_response = Infrastructure::Response::network_error("Response has status 206 and 'range-requested' flag set, but request has no 'Range' header"_string);}@@ -1870,7 +1863,7 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParam// a service worker for that matter, it is applied here.if (request->response_tainting() == Infrastructure::Request::ResponseTainting::CORS&& !cors_check(request, *response)) {- stop_discarded_response_body(*controller, *response);+ fetch_params->discard_response_body(*response);returned_pending_response->resolve(Infrastructure::Response::network_error("Request with 'cors' response tainting failed CORS check"_string));return;}@@ -1893,7 +1886,7 @@ GC::Ref<PendingResponse> http_fetch(JS::Realm& realm, Infrastructure::FetchParamif (!internal_response->is_network_error()&& (request->response_tainting() == Infrastructure::Request::ResponseTainting::Opaque || response->type() == Infrastructure::Response::Type::Opaque)&& cross_origin_resource_policy_check(request->origin().get<URL::Origin>(), client_embedder_policy, *internal_response) == Infrastructure::RequestOrResponseBlocking::Blocked) {- stop_discarded_response_body(*controller, *internal_response);+ fetch_params->discard_response_body(*internal_response);returned_pending_response->resolve(Infrastructure::Response::network_error("Response was blocked by cross-origin resource policy check"_string));return;}@@ -1984,7 +1977,7 @@ GC::Ptr<PendingResponse> http_redirect_fetch(JS::Realm& realm, Infrastructure::F// AD-HOC: Navigation responses have a RequestServer transfer lease so they can move through the UI process.// This response will be discarded in favor of either a network error or the redirect response, so// release its lease and stop the request if it is still active.- stop_discarded_response_body(*fetch_params.controller(), *internal_response);+ fetch_params.discard_response_body(*internal_response);// 5. If locationURL is failure, then return a network error.if (location_url_or_error.is_error())@@ -2159,7 +2152,7 @@ static void process_http_network_or_cache_response(JS::Object& global_object, In// 1. If request’s body’s source is null, then return a network error.auto source = request->body().get<Infrastructure::FetchBody>().source();if (!source.has_value()) {- stop_discarded_response_body(*fetch_params.controller(), *response);+ fetch_params.discard_response_body(*response);returned_pending_response->resolve(Infrastructure::Response::network_error("Request has body but no body source"_string));return;}@@ -2172,7 +2165,7 @@ static void process_http_network_or_cache_response(JS::Object& global_object, Inif (!request->use_url_credentials() || is_authentication_fetch == IsAuthenticationFetch::Yes) {// 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.if (fetch_params.is_canceled()) {- stop_discarded_response_body(*fetch_params.controller(), *response);+ fetch_params.discard_response_body(*response);returned_pending_response->resolve(Infrastructure::Response::appropriate_network_error(fetch_params));return;}@@ -2182,7 +2175,7 @@ static void process_http_network_or_cache_response(JS::Object& global_object, Inif (!intercepts.is_empty()) {WebDriver::BiDi::block_network_request(fetch_params, response, "authRequired"_string, move(intercepts), WebDriver::BiDi::NetworkContinuation::create(realm.heap(), GC::create_function(realm.heap(), GC::captures(realm, NonnullRefPtr { fetch_params }, http_request, returned_pending_response, is_authentication_fetch, is_new_connection_fetch, include_credentials), [](auto& realm, auto& fetch_params, auto& http_request, auto& returned_pending_response, auto& is_authentication_fetch, auto& is_new_connection_fetch, auto& include_credentials, WebDriver::BiDi::NetworkContinuationAction action, RefPtr<Infrastructure::Response> continued_response) {if (action == WebDriver::BiDi::NetworkContinuationAction::ProvideCredentials) {- stop_discarded_response_body(*fetch_params->controller(), *continued_response);+ fetch_params->discard_response_body(*continued_response);auto pending_response = http_network_or_cache_fetch(realm, *fetch_params, IsAuthenticationFetch::Yes);pending_response->when_loaded(GC::create_function(GC::Heap::the(), GC::captures(returned_pending_response), [](GC::Ref<PendingResponse>& returned_pending_response, NonnullRefPtr<Infrastructure::Response> response) { returned_pending_response->resolve(response); }));} else if (first_is_one_of(action, WebDriver::BiDi::NetworkContinuationAction::CancelAuthentication, WebDriver::BiDi::NetworkContinuationAction::Fail, WebDriver::BiDi::NetworkContinuationAction::ProvideResponse)) {@@ -2200,14 +2193,14 @@ static void process_http_network_or_cache_response(JS::Object& global_object, In// respectively, in request’s window.// FIXME: 3. Set the username given request’s current URL and username.// FIXME: 4. Set the password given request’s current URL and password.- stop_discarded_response_body(*fetch_params.controller(), *response);+ fetch_params.discard_response_body(*response);dbgln("Fetch: Username/password prompt is not implemented. Aborting the request.");returned_pending_response->resolve(Infrastructure::Response::aborted_network_error());return;}// 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true.- stop_discarded_response_body(*fetch_params.controller(), *response);+ fetch_params.discard_response_body(*response);inner_pending_response = http_network_or_cache_fetch(realm, fetch_params, IsAuthenticationFetch::Yes);}diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cppindex bd1e5f14637..b91b82b240d 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp@@ -54,6 +54,12 @@ Engine::StartFetchInput FetchParams::start_fetch_input(Engine::FetchId fetch_id,return Engine::StartFetchInput { {}, fetch_id, m_control_block->body_intent(), m_blob_url_entry, move(event_sink) };}+void FetchParams::discard_response_body(Response const& response) const+{+ response.release_request_transfer_lease();+ controller()->stop_body_transport();+}+GC::Ref<FetchController> FetchParams::controller() const{VERIFY(m_controller_registry);diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.hindex d4ccbb60610..1dc3e17161f 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h@@ -74,6 +74,9 @@ public:[[nodiscard]] bool is_cors_preflight() const { return m_is_cors_preflight; }void set_is_cors_preflight() { m_is_cors_preflight = true; }+ // Ends the delivery of a response's body that the fetch will not read.+ void discard_response_body(Response const&) const;+private:FetchParams(NonnullRefPtr<Request>, GC::Ref<FetchAlgorithms const>, FetchController&, NonnullRefPtr<FetchTimingInfo>, NonnullRefPtr<LoaderConfigSnapshot const>);FetchParams(FetchParams const&);diff --git a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp b/Libraries/LibWeb/WebDriver/BiDi/Network.cppindex db8a8b4cde2..c074912a25f 100644--- a/Libraries/LibWeb/WebDriver/BiDi/Network.cpp+++ b/Libraries/LibWeb/WebDriver/BiDi/Network.cpp@@ -118,25 +118,19 @@ static GC::Ptr<NetworkContinuation> blocked_request(FetchController const& contrreturn blocked && blocked->controller.ptr() == &controller ? blocked : nullptr;}-static void stop_response_transport(FetchController& controller, Response const& response)-{- response.release_request_transfer_lease();- controller.stop_body_transport();-}-static void resume_blocked_request(String id, NetworkContinuationAction action, RefPtr<Fetch::Infrastructure::Response> response){auto it = blocked_requests().find(id);if (it == blocked_requests().end())return;GC::Ptr<NetworkContinuation> continuation = it->value.ptr();- auto controller = continuation->controller;+ auto fetch_params = continuation->fetch_params;auto original_response = continuation->response;// NB: Remove the entry before stopping transport or invoking a continuation, which can reenter Fetch.blocked_requests().remove(it);continuation->clear_blocked_state();if (action == NetworkContinuationAction::Fail && original_response)- stop_response_transport(*controller, *original_response);+ fetch_params->discard_response_body(*original_response);continuation->invoke(action, response);}@@ -561,7 +555,7 @@ void block_network_request(FetchParams const& fetch_params, RefPtr<Fetch::Infrasauto state = network_request_of(fetch_params);if (!fetch_params.control_block().is_ongoing()) {if (response)- stop_response_transport(*fetch_params.controller(), *response);+ fetch_params.discard_response_body(*response);continuation->invoke(NetworkContinuationAction::Fail, Response::network_error("Intercepted fetch was stopped"_string));return;}@@ -690,10 +684,8 @@ static ErrorOr<JsonValue, Error> continue_network_request_impl(StringView methodaction = NetworkContinuationAction::ProvideResponse;if (method == "network.provideResponse"sv && overrides.body.has_value()) {// NB: A replacement body has no RequestServer transport to transfer to a new navigation process.- if (blocked->response) {- blocked->response->release_request_transfer_lease();- blocked->controller->stop_body_transport();- }+ if (blocked->response)+ blocked->fetch_params->discard_response_body(*blocked->response);auto replacement = Response::create();replacement->set_url_list(response->url_list());replacement->set_status(response->status());