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

Userland: Convert TLS::TLSv12 to a Core::Stream::Socket

This commit converts TLS::TLSv12 to a Core::Stream object, and in the
process allows TLS to now wrap other Core::Stream::Socket objects.
As a large part of LibHTTP and LibGemini depend on LibTLS's interface,
this also converts those to support Core::Stream, which leads to a
simplification of LibHTTP (as there's no need to care about the
underlying socket type anymore).
Note that RequestServer now controls the TLS socket options, which is a
better place anyway, as RS is the first receiver of the user-requested
options (though this is currently not particularly useful).
This commit is contained in:
Ali Mohammad Pur 2022-02-02 19:21:55 +03:30 committed by Andreas Kling
parent 7a95c451a3
commit aafc451016
47 changed files with 841 additions and 1157 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2021-2022, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,10 +10,10 @@
namespace RequestServer::ConnectionCache {
HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<Core::TCPSocket>>>> g_tcp_connection_cache {};
HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<Core::Stream::TCPSocket>>>> g_tcp_connection_cache {};
HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<TLS::TLSv12>>>> g_tls_connection_cache {};
void request_did_finish(URL const& url, Core::Socket const* socket)
void request_did_finish(URL const& url, Core::Stream::Socket const* socket)
{
if (!socket) {
dbgln("Request with a null socket finished for URL {}", url);
@ -37,34 +37,44 @@ void request_did_finish(URL const& url, Core::Socket const* socket)
auto& connection = *connection_it;
if (connection->request_queue.is_empty()) {
connection->has_started = false;
connection->current_url = {};
connection->removal_timer->on_timeout = [ptr = connection.ptr(), &cache_entry = *it->value, key = it->key, &cache]() mutable {
Core::deferred_invoke([&, key = move(key), ptr] {
dbgln_if(REQUESTSERVER_DEBUG, "Removing no-longer-used connection {} (socket {})", ptr, ptr->socket);
auto did_remove = cache_entry.remove_first_matching([&](auto& entry) { return entry == ptr; });
VERIFY(did_remove);
if (cache_entry.is_empty())
cache.remove(key);
});
};
connection->removal_timer->start();
Core::deferred_invoke([&connection, &cache_entry = *it->value, key = it->key, &cache] {
connection->socket->set_notifications_enabled(false);
connection->has_started = false;
connection->current_url = {};
connection->job_data = {};
connection->removal_timer->on_timeout = [ptr = connection.ptr(), &cache_entry, key = move(key), &cache]() mutable {
Core::deferred_invoke([&, key = move(key), ptr] {
dbgln_if(REQUESTSERVER_DEBUG, "Removing no-longer-used connection {} (socket {})", ptr, ptr->socket);
auto did_remove = cache_entry.remove_first_matching([&](auto& entry) { return entry == ptr; });
VERIFY(did_remove);
if (cache_entry.is_empty())
cache.remove(key);
});
};
connection->removal_timer->start();
});
} else {
recreate_socket_if_needed(*connection, url);
dbgln_if(REQUESTSERVER_DEBUG, "Running next job in queue for connection {} @{}", &connection, connection->socket);
auto request = connection->request_queue.take_first();
connection->timer.start();
connection->current_url = url;
request(connection->socket);
if (auto result = recreate_socket_if_needed(*connection, url); result.is_error()) {
dbgln("ConnectionCache request finish handler, reconnection failed with {}", result.error());
connection->job_data.fail(Core::NetworkJob::Error::ConnectionFailed);
return;
}
Core::deferred_invoke([&, url] {
dbgln_if(REQUESTSERVER_DEBUG, "Running next job in queue for connection {} @{}", &connection, connection->socket);
connection->timer.start();
connection->current_url = url;
connection->job_data = connection->request_queue.take_first();
connection->job_data.start(*connection->socket);
});
}
};
if (is<TLS::TLSv12>(socket))
if (is<Core::Stream::BufferedSocket<TLS::TLSv12>>(socket))
fire_off_next_job(g_tls_connection_cache);
else if (is<Core::TCPSocket>(socket))
else if (is<Core::Stream::BufferedSocket<Core::Stream::TCPSocket>>(socket))
fire_off_next_job(g_tcp_connection_cache);
else
dbgln("Unknown socket {} finished for URL {}", *socket, url);
dbgln("Unknown socket {} finished for URL {}", socket, url);
}
void dump_jobs()
@ -74,7 +84,7 @@ void dump_jobs()
dbgln(" - {}:{}", connection.key.hostname, connection.key.port);
for (auto& entry : *connection.value) {
dbgln(" - Connection {} (started={}) (socket={})", &entry, entry.has_started, entry.socket);
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.elapsed());
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.is_valid() ? entry.timer.elapsed() : 0);
dbgln(" Request Queue:");
for (auto& job : entry.request_queue)
dbgln(" - {}", &job);
@ -85,7 +95,7 @@ void dump_jobs()
dbgln(" - {}:{}", connection.key.hostname, connection.key.port);
for (auto& entry : *connection.value) {
dbgln(" - Connection {} (started={}) (socket={})", &entry, entry.has_started, entry.socket);
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.elapsed());
dbgln(" Currently loading {} ({} elapsed)", entry.current_url, entry.timer.is_valid() ? entry.timer.elapsed() : 0);
dbgln(" Request Queue:");
for (auto& job : entry.request_queue)
dbgln(" - {}", &job);