From dff83443367d6ef67af77c77bd64b80194780e48 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 17 Mar 2022 19:33:13 +0330 Subject: RequestServer: Keep the EnsureConnection job alive until it finishes Fixes #12906. --- .../RequestServer/ConnectionFromClient.cpp | 50 ++++++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'Userland/Services') diff --git a/Userland/Services/RequestServer/ConnectionFromClient.cpp b/Userland/Services/RequestServer/ConnectionFromClient.cpp index 9641e3f4a0..2e0e607e30 100644 --- a/Userland/Services/RequestServer/ConnectionFromClient.cpp +++ b/Userland/Services/RequestServer/ConnectionFromClient.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -111,6 +112,40 @@ Messages::RequestServer::SetCertificateResponse ConnectionFromClient::set_certif return success; } +struct Job { + explicit Job(URL url) + : m_url(move(url)) + { + } + + static Job& ensure(URL const& url) + { + if (auto it = s_jobs.find(url); it == s_jobs.end()) { + auto job = make(url); + s_jobs.set(url, move(job)); + } + return *s_jobs.find(url)->value; + } + + void start(Core::Stream::Socket& socket) + { + auto is_connected = socket.is_open(); + VERIFY(is_connected); + ConnectionCache::request_did_finish(m_url, &socket); + s_jobs.remove(m_url); + } + void fail(Core::NetworkJob::Error error) + { + dbgln("Pre-connect to {} failed: {}", m_url, Core::to_string(error)); + s_jobs.remove(m_url); + } + URL m_url; + +private: + static HashMap> s_jobs; +}; +HashMap> Job::s_jobs {}; + void ConnectionFromClient::ensure_connection(URL const& url, ::RequestServer::CacheLevel const& cache_level) { if (!url.is_valid()) { @@ -125,20 +160,7 @@ void ConnectionFromClient::ensure_connection(URL const& url, ::RequestServer::Ca }); } - struct { - URL m_url; - void start(Core::Stream::Socket& socket) - { - auto is_connected = socket.is_open(); - VERIFY(is_connected); - ConnectionCache::request_did_finish(m_url, &socket); - } - void fail(Core::NetworkJob::Error error) - { - dbgln("Pre-connect to {} failed: {}", m_url, Core::to_string(error)); - } - } job { url }; - + auto& job = Job::ensure(url); dbgln("EnsureConnection: Pre-connect to {}", url); auto do_preconnect = [&](auto& cache) { auto it = cache.find({ url.host(), url.port_or_default() }); -- cgit v1.2.3