1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

RequestServer+LibProtocol: Add an 'EnsureConnection' IPC endpoint

This will allow LibWeb (and other components) to request a connection to
be premade and cached, to make subsequent loads faster.
This commit is contained in:
Ali Mohammad Pur 2021-09-28 00:06:52 +03:30 committed by Andreas Kling
parent 6b2e4f896b
commit 3ec39fc62e
6 changed files with 54 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <RequestServer/Protocol.h>
#include <RequestServer/Request.h>
#include <RequestServer/RequestClientEndpoint.h>
#include <netdb.h>
namespace RequestServer {
@ -110,4 +111,37 @@ Messages::RequestServer::SetCertificateResponse ClientConnection::set_certificat
return success;
}
void ClientConnection::ensure_connection(URL const& url, ::RequestServer::CacheLevel const& cache_level)
{
if (!url.is_valid()) {
dbgln("EnsureConnection: Invalid URL requested: '{}'", url);
return;
}
if (cache_level == CacheLevel::ResolveOnly) {
return Core::deferred_invoke([host = url.host()] {
dbgln("EnsureConnection: DNS-preload for {}", host);
(void)gethostbyname(host.characters());
});
}
struct {
URL const& m_url;
void start(NonnullRefPtr<Core::Socket> socket) { socket->connect(m_url.host(), m_url.port_or_default()); }
} job { url };
dbgln("EnsureConnection: Pre-connect to {}", url);
auto do_preconnect = [&](auto& cache) {
auto& connection = ConnectionCache::get_or_create_connection(cache, url, job);
connection.removal_timer->start();
};
if (url.scheme() == "http"sv)
do_preconnect(ConnectionCache::g_tcp_connection_cache);
else if (url.scheme() == "https"sv)
do_preconnect(ConnectionCache::g_tls_connection_cache);
else
dbgln("EnsureConnection: Invalid URL scheme: '{}'", url.scheme());
}
}