diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-09-18 03:48:22 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-09-19 21:10:23 +0430 |
commit | 65f7e45a75c44fd0af7af6b8373999358b2ca81c (patch) | |
tree | ada960a6cfce11ce97e1706945a027c5bccfd702 /Userland | |
parent | c5d7eb86189e0fbd9f6a9da1afe29b6a9ab533a4 (diff) | |
download | serenity-65f7e45a75c44fd0af7af6b8373999358b2ca81c.zip |
RequestServer+LibHTTP+LibGemini: Cache connections to the same host
This makes connections (particularly TLS-based ones) do the handshaking
stuff only once.
Currently the cache is configured to keep at most two connections evenly
balanced in queue size, and with a grace period of 10s after the last
queued job has finished (after which the connection will be dropped).
Diffstat (limited to 'Userland')
22 files changed, 295 insertions, 51 deletions
diff --git a/Userland/Libraries/LibCore/IODevice.h b/Userland/Libraries/LibCore/IODevice.h index a13f6ea996..86e4ac8f8a 100644 --- a/Userland/Libraries/LibCore/IODevice.h +++ b/Userland/Libraries/LibCore/IODevice.h @@ -82,6 +82,7 @@ public: bool can_read_line() const; bool can_read() const; + bool can_read_only_from_buffer() const { return !m_buffered_data.is_empty() && !can_read_from_fd(); } bool seek(i64, SeekMode = SeekMode::SetPosition, off_t* = nullptr); diff --git a/Userland/Libraries/LibCore/NetworkJob.cpp b/Userland/Libraries/LibCore/NetworkJob.cpp index f393cc93af..dd8b15ed00 100644 --- a/Userland/Libraries/LibCore/NetworkJob.cpp +++ b/Userland/Libraries/LibCore/NetworkJob.cpp @@ -19,7 +19,7 @@ NetworkJob::~NetworkJob() { } -void NetworkJob::start() +void NetworkJob::start(NonnullRefPtr<Core::Socket>) { } diff --git a/Userland/Libraries/LibCore/NetworkJob.h b/Userland/Libraries/LibCore/NetworkJob.h index 911a556ac7..781dc825a8 100644 --- a/Userland/Libraries/LibCore/NetworkJob.h +++ b/Userland/Libraries/LibCore/NetworkJob.h @@ -35,7 +35,7 @@ public: NetworkResponse* response() { return m_response.ptr(); } const NetworkResponse* response() const { return m_response.ptr(); } - virtual void start() = 0; + virtual void start(NonnullRefPtr<Core::Socket>) = 0; virtual void shutdown() = 0; void cancel() diff --git a/Userland/Libraries/LibGemini/GeminiJob.cpp b/Userland/Libraries/LibGemini/GeminiJob.cpp index 6ab80ae571..340d470644 100644 --- a/Userland/Libraries/LibGemini/GeminiJob.cpp +++ b/Userland/Libraries/LibGemini/GeminiJob.cpp @@ -14,15 +14,11 @@ namespace Gemini { -void GeminiJob::start() +void GeminiJob::start(NonnullRefPtr<Core::Socket> socket) { VERIFY(!m_socket); - m_socket = TLS::TLSv12::construct(this); - m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates()); - m_socket->on_tls_connected = [this] { - dbgln_if(GEMINIJOB_DEBUG, "GeminiJob: on_connected callback"); - on_socket_connected(); - }; + VERIFY(is<TLS::TLSv12>(*socket)); + m_socket = static_ptr_cast<TLS::TLSv12>(socket); m_socket->on_tls_error = [this](TLS::AlertDescription error) { if (error == TLS::AlertDescription::HandshakeFailure) { deferred_invoke([this] { @@ -45,11 +41,20 @@ void GeminiJob::start() if (on_certificate_requested) on_certificate_requested(*this); }; - bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default()); - if (!success) { - deferred_invoke([this] { - return did_fail(Core::NetworkJob::Error::ConnectionFailed); - }); + + if (m_socket->is_established()) { + deferred_invoke([this] { on_socket_connected(); }); + } else { + m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates()); + m_socket->on_tls_connected = [this] { + on_socket_connected(); + }; + bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default()); + if (!success) { + deferred_invoke([this] { + return did_fail(Core::NetworkJob::Error::ConnectionFailed); + }); + } } } @@ -59,7 +64,6 @@ void GeminiJob::shutdown() return; m_socket->on_tls_ready_to_read = nullptr; m_socket->on_tls_connected = nullptr; - remove_child(*m_socket); m_socket = nullptr; } diff --git a/Userland/Libraries/LibGemini/GeminiJob.h b/Userland/Libraries/LibGemini/GeminiJob.h index 2407d2cca5..20cc8afcf9 100644 --- a/Userland/Libraries/LibGemini/GeminiJob.h +++ b/Userland/Libraries/LibGemini/GeminiJob.h @@ -27,10 +27,13 @@ public: { } - virtual void start() override; + virtual void start(NonnullRefPtr<Core::Socket>) override; virtual void shutdown() override; void set_certificate(String certificate, String key); + Core::Socket const* socket() const { return m_socket; } + URL url() const { return m_request.url(); } + Function<void(GeminiJob&)> on_certificate_requested; protected: diff --git a/Userland/Libraries/LibGemini/Job.h b/Userland/Libraries/LibGemini/Job.h index 01eac13e11..5bc50216f7 100644 --- a/Userland/Libraries/LibGemini/Job.h +++ b/Userland/Libraries/LibGemini/Job.h @@ -19,7 +19,7 @@ public: explicit Job(const GeminiRequest&, OutputStream&); virtual ~Job() override; - virtual void start() override = 0; + virtual void start(NonnullRefPtr<Core::Socket>) override = 0; virtual void shutdown() override = 0; GeminiResponse* response() { return static_cast<GeminiResponse*>(Core::NetworkJob::response()); } diff --git a/Userland/Libraries/LibHTTP/HttpJob.cpp b/Userland/Libraries/LibHTTP/HttpJob.cpp index 98ffbace48..58ab0e28d6 100644 --- a/Userland/Libraries/LibHTTP/HttpJob.cpp +++ b/Userland/Libraries/LibHTTP/HttpJob.cpp @@ -12,26 +12,35 @@ #include <unistd.h> namespace HTTP { -void HttpJob::start() +void HttpJob::start(NonnullRefPtr<Core::Socket> socket) { VERIFY(!m_socket); - m_socket = Core::TCPSocket::construct(this); - m_socket->on_connected = [this] { - dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback"); - on_socket_connected(); - }; + m_socket = move(socket); m_socket->on_error = [this] { dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_error callback"); deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ConnectionFailed); }); }; - bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default()); - if (!success) { + if (m_socket->is_connected()) { + dbgln("Reusing previous connection for {}", url()); deferred_invoke([this] { - return did_fail(Core::NetworkJob::Error::ConnectionFailed); + dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback"); + on_socket_connected(); }); - } + } else { + dbgln("Creating new connection for {}", url()); + m_socket->on_connected = [this] { + dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback"); + on_socket_connected(); + }; + bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default()); + if (!success) { + deferred_invoke([this] { + return did_fail(Core::NetworkJob::Error::ConnectionFailed); + }); + } + }; } void HttpJob::shutdown() @@ -40,13 +49,24 @@ void HttpJob::shutdown() return; m_socket->on_ready_to_read = nullptr; m_socket->on_connected = nullptr; - remove_child(*m_socket); m_socket = nullptr; } void HttpJob::register_on_ready_to_read(Function<void()> callback) { - m_socket->on_ready_to_read = move(callback); + m_socket->on_ready_to_read = [callback = move(callback), this] { + callback(); + // As IODevice so graciously buffers everything, there's a possible + // scenario where it buffers the entire response, and we get stuck waiting + // for select() in the notifier (which will never return). + // So handle this case by exhausting the buffer here. + if (m_socket->can_read_only_from_buffer() && m_state != State::Finished && !has_error()) { + deferred_invoke([this] { + if (m_socket && m_socket->on_ready_to_read) + m_socket->on_ready_to_read(); + }); + } + }; } void HttpJob::register_on_ready_to_write(Function<void()> callback) diff --git a/Userland/Libraries/LibHTTP/HttpJob.h b/Userland/Libraries/LibHTTP/HttpJob.h index 07100cd0a0..16ccb3abfb 100644 --- a/Userland/Libraries/LibHTTP/HttpJob.h +++ b/Userland/Libraries/LibHTTP/HttpJob.h @@ -27,9 +27,12 @@ public: { } - virtual void start() override; + virtual void start(NonnullRefPtr<Core::Socket>) override; virtual void shutdown() override; + Core::Socket const* socket() const { return m_socket; } + URL url() const { return m_request.url(); } + protected: virtual bool should_fail_on_empty_payload() const override { return false; } virtual void register_on_ready_to_read(Function<void()>) override; diff --git a/Userland/Libraries/LibHTTP/HttpRequest.cpp b/Userland/Libraries/LibHTTP/HttpRequest.cpp index 2bf198290b..5175f75d9c 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.cpp +++ b/Userland/Libraries/LibHTTP/HttpRequest.cpp @@ -55,7 +55,6 @@ ByteBuffer HttpRequest::to_raw_request() const builder.append(header.value); builder.append("\r\n"); } - builder.append("Connection: close\r\n"); if (!m_body.is_empty()) { builder.appendff("Content-Length: {}\r\n\r\n", m_body.size()); builder.append((char const*)m_body.data(), m_body.size()); diff --git a/Userland/Libraries/LibHTTP/HttpsJob.cpp b/Userland/Libraries/LibHTTP/HttpsJob.cpp index 37e4b7502e..0f249fc5fb 100644 --- a/Userland/Libraries/LibHTTP/HttpsJob.cpp +++ b/Userland/Libraries/LibHTTP/HttpsJob.cpp @@ -14,15 +14,12 @@ namespace HTTP { -void HttpsJob::start() +void HttpsJob::start(NonnullRefPtr<Core::Socket> socket) { VERIFY(!m_socket); - m_socket = TLS::TLSv12::construct(this); - m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates()); - m_socket->on_tls_connected = [this] { - dbgln_if(HTTPSJOB_DEBUG, "HttpsJob: on_connected callback"); - on_socket_connected(); - }; + VERIFY(is<TLS::TLSv12>(*socket)); + + m_socket = static_ptr_cast<TLS::TLSv12>(socket); m_socket->on_tls_error = [&](TLS::AlertDescription error) { if (error == TLS::AlertDescription::HandshakeFailure) { deferred_invoke([this] { @@ -46,11 +43,22 @@ void HttpsJob::start() if (on_certificate_requested) on_certificate_requested(*this); }; - bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default()); - if (!success) { - deferred_invoke([this] { - return did_fail(Core::NetworkJob::Error::ConnectionFailed); - }); + if (m_socket->is_established()) { + dbgln("Reusing previous connection for {}", url()); + deferred_invoke([this] { on_socket_connected(); }); + } else { + dbgln("Creating a new connection for {}", url()); + m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates()); + m_socket->on_tls_connected = [this] { + dbgln_if(HTTPSJOB_DEBUG, "HttpsJob: on_connected callback"); + on_socket_connected(); + }; + bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port_or_default()); + if (!success) { + deferred_invoke([this] { + return did_fail(Core::NetworkJob::Error::ConnectionFailed); + }); + } } } @@ -60,7 +68,6 @@ void HttpsJob::shutdown() return; m_socket->on_tls_ready_to_read = nullptr; m_socket->on_tls_connected = nullptr; - remove_child(*m_socket); m_socket = nullptr; } diff --git a/Userland/Libraries/LibHTTP/HttpsJob.h b/Userland/Libraries/LibHTTP/HttpsJob.h index d3fce85aeb..c7c20ea7e6 100644 --- a/Userland/Libraries/LibHTTP/HttpsJob.h +++ b/Userland/Libraries/LibHTTP/HttpsJob.h @@ -28,10 +28,13 @@ public: { } - virtual void start() override; + virtual void start(NonnullRefPtr<Core::Socket>) override; virtual void shutdown() override; void set_certificate(String certificate, String key); + Core::Socket const* socket() const { return m_socket; } + URL url() const { return m_request.url(); } + Function<void(HttpsJob&)> on_certificate_requested; protected: diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 5c75997db8..97c4b317bd 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -85,7 +85,7 @@ void Job::flush_received_buffers() { if (!m_can_stream_response || m_buffered_size == 0) return; - dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size()); + dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url()); for (size_t i = 0; i < m_received_buffers.size(); ++i) { auto& payload = m_received_buffers[i]; auto written = do_write(payload); @@ -100,7 +100,7 @@ void Job::flush_received_buffers() payload = payload.slice(written, payload.size() - written); break; } - dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size()); + dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url()); } void Job::on_socket_connected() @@ -121,6 +121,7 @@ void Job::on_socket_connected() deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); }); }); register_on_ready_to_read([&] { + dbgln_if(JOB_DEBUG, "Ready to read for {}, state = {}, cancelled = {}", m_request.url(), to_underlying(m_state), is_cancelled()); if (is_cancelled()) return; @@ -132,9 +133,12 @@ void Job::on_socket_connected() } if (m_state == State::InStatus) { - if (!can_read_line()) + if (!can_read_line()) { + dbgln_if(JOB_DEBUG, "Job {} cannot read line", m_request.url()); return; + } auto line = read_line(PAGE_SIZE); + dbgln_if(JOB_DEBUG, "Job {} read line of length {}", m_request.url(), line.length()); if (line.is_null()) { dbgln("Job: Expected HTTP status"); return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); }); @@ -287,7 +291,9 @@ void Job::on_socket_connected() } } + dbgln_if(JOB_DEBUG, "Waiting for payload for {}", m_request.url()); auto payload = receive(read_size); + dbgln_if(JOB_DEBUG, "Received {} bytes of payload from {}", payload.size(), m_request.url()); if (payload.is_empty()) { if (eof()) { finish_up(); diff --git a/Userland/Libraries/LibHTTP/Job.h b/Userland/Libraries/LibHTTP/Job.h index 338d43192f..99a6e0607f 100644 --- a/Userland/Libraries/LibHTTP/Job.h +++ b/Userland/Libraries/LibHTTP/Job.h @@ -21,7 +21,7 @@ public: explicit Job(const HttpRequest&, OutputStream&); virtual ~Job() override; - virtual void start() override = 0; + virtual void start(NonnullRefPtr<Core::Socket>) override = 0; virtual void shutdown() override = 0; HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); } diff --git a/Userland/Services/RequestServer/CMakeLists.txt b/Userland/Services/RequestServer/CMakeLists.txt index 5033367604..3ad98628bc 100644 --- a/Userland/Services/RequestServer/CMakeLists.txt +++ b/Userland/Services/RequestServer/CMakeLists.txt @@ -8,6 +8,7 @@ compile_ipc(RequestClient.ipc RequestClientEndpoint.h) set(SOURCES ClientConnection.cpp + ConnectionCache.cpp Request.cpp RequestClientEndpoint.h RequestServerEndpoint.h diff --git a/Userland/Services/RequestServer/ConnectionCache.cpp b/Userland/Services/RequestServer/ConnectionCache.cpp new file mode 100644 index 0000000000..e9ef533240 --- /dev/null +++ b/Userland/Services/RequestServer/ConnectionCache.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "ConnectionCache.h" +#include <LibCore/EventLoop.h> + +namespace RequestServer::ConnectionCache { + +HashMap<ConnectionKey, Vector<Connection<Core::TCPSocket>>> g_tcp_connection_cache {}; +HashMap<ConnectionKey, Vector<Connection<TLS::TLSv12>>> g_tls_connection_cache {}; + +void request_did_finish(URL const& url, Core::Socket const* socket) +{ + if (!socket) { + dbgln("Request with a null socket finished for URL {}", url); + return; + } + + dbgln("Request for {} finished", url); + + ConnectionKey key { url.host(), url.port_or_default() }; + auto fire_off_next_job = [&](auto& cache) { + auto it = cache.find(key); + if (it == cache.end()) { + dbgln("Request for URL {} finished, but we don't own that!", url); + return; + } + auto connection_it = it->value.find_if([&](auto& connection) { return connection.socket == socket; }); + if (connection_it.is_end()) { + dbgln("Request for URL {} finished, but we don't have a socket for that!", url); + return; + } + + auto& connection = *connection_it; + if (connection.request_queue.is_empty()) { + connection.has_started = false; + connection.removal_timer->on_timeout = [&connection, &cache_entry = it->value] { + Core::deferred_invoke([&] { + dbgln("Removing no-longer-used connection {}", &connection); + cache_entry.remove_first_matching([&](auto& entry) { return &entry == &connection; }); + }); + }; + connection.removal_timer->start(); + } else { + using SocketType = RemoveCVReference<decltype(*connection.socket)>; + bool is_connected; + if constexpr (IsSame<SocketType, TLS::TLSv12>) + is_connected = connection.socket->is_established(); + else + is_connected = connection.socket->is_connected(); + if (!is_connected) { + // Create another socket for the connection. + dbgln("Creating a new socket for {}", url); + connection.socket = SocketType::construct(nullptr); + } + dbgln("Running next job in queue for connection {}", &connection); + auto request = connection.request_queue.take_first(); + request(connection.socket); + } + }; + + if (is<TLS::TLSv12>(socket)) + fire_off_next_job(g_tls_connection_cache); + else if (is<Core::TCPSocket>(socket)) + fire_off_next_job(g_tcp_connection_cache); + else + dbgln("Unknown socket {} finished for URL {}", *socket, url); +} + +} diff --git a/Userland/Services/RequestServer/ConnectionCache.h b/Userland/Services/RequestServer/ConnectionCache.h new file mode 100644 index 0000000000..d9bbc0975c --- /dev/null +++ b/Userland/Services/RequestServer/ConnectionCache.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/HashMap.h> +#include <AK/URL.h> +#include <AK/Vector.h> +#include <LibCore/TCPSocket.h> +#include <LibCore/Timer.h> +#include <LibTLS/TLSv12.h> + +namespace RequestServer::ConnectionCache { + +template<typename Socket> +struct Connection { + using QueueType = Vector<Function<void(Core::Socket&)>>; + using SocketType = Socket; + + NonnullRefPtr<Socket> socket; + QueueType request_queue; + NonnullRefPtr<Core::Timer> removal_timer; + bool has_started { false }; +}; + +struct ConnectionKey { + String hostname; + u16 port { 0 }; + + bool operator==(ConnectionKey const&) const = default; +}; + +}; + +template<> +struct AK::Traits<RequestServer::ConnectionCache::ConnectionKey> : public AK::GenericTraits<RequestServer::ConnectionCache::ConnectionKey> { + static u32 hash(RequestServer::ConnectionCache::ConnectionKey const& key) + { + return pair_int_hash(key.hostname.hash(), key.port); + } +}; + +namespace RequestServer::ConnectionCache { + +extern HashMap<ConnectionKey, Vector<Connection<Core::TCPSocket>>> g_tcp_connection_cache; +extern HashMap<ConnectionKey, Vector<Connection<TLS::TLSv12>>> g_tls_connection_cache; + +void request_did_finish(URL const&, Core::Socket const*); + +constexpr static inline size_t MaxConcurrentConnectionsPerURL = 2; +constexpr static inline size_t ConnectionKeepAliveTimeMilliseconds = 10'000; + +decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job) +{ + auto start_job = [&job](auto& socket) { + job.start(socket); + }; + auto& sockets_for_url = cache.ensure({ url.host(), url.port_or_default() }); + auto it = sockets_for_url.find_if([](auto& connection) { return connection.request_queue.is_empty(); }); + auto did_add_new_connection = false; + if (it.is_end() && sockets_for_url.size() < ConnectionCache::MaxConcurrentConnectionsPerURL) { + sockets_for_url.append({ + RemoveCVReference<decltype(cache.begin()->value.at(0))>::SocketType::construct(nullptr), + {}, + Core::Timer::create_single_shot(ConnectionKeepAliveTimeMilliseconds, nullptr), + }); + did_add_new_connection = true; + } + size_t index; + if (it.is_end()) { + if (did_add_new_connection) { + index = sockets_for_url.size() - 1; + } else { + // Find the least backed-up connection (based on how many entries are in their request queue. + index = 0; + auto min_queue_size = (size_t)-1; + for (auto it = sockets_for_url.begin(); it != sockets_for_url.end(); ++it) { + if (auto queue_size = it->request_queue.size(); min_queue_size > queue_size) { + index = it.index(); + min_queue_size = queue_size; + } + } + } + } else { + index = it.index(); + } + auto& connection = sockets_for_url[index]; + if (!connection.has_started) { + dbgln("Immediately start request for url {} in {}", url, &connection); + connection.has_started = true; + connection.removal_timer->stop(); + start_job(*connection.socket); + } else { + dbgln("Enqueue request for URL {} in {}", url, &connection); + connection.request_queue.append(move(start_job)); + } + return connection; +} + +} diff --git a/Userland/Services/RequestServer/GeminiProtocol.cpp b/Userland/Services/RequestServer/GeminiProtocol.cpp index e02ed8413f..45686caa75 100644 --- a/Userland/Services/RequestServer/GeminiProtocol.cpp +++ b/Userland/Services/RequestServer/GeminiProtocol.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "ConnectionCache.h" #include <LibGemini/GeminiJob.h> #include <LibGemini/GeminiRequest.h> #include <RequestServer/GeminiProtocol.h> @@ -34,7 +35,9 @@ OwnPtr<Request> GeminiProtocol::start_request(ClientConnection& client, const St auto job = Gemini::GeminiJob::construct(request, *output_stream); auto protocol_request = GeminiRequest::create_with_job({}, client, (Gemini::GeminiJob&)*job, move(output_stream)); protocol_request->set_request_fd(pipe_result.value().read_fd); - job->start(); + + ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job); + return protocol_request; } diff --git a/Userland/Services/RequestServer/GeminiRequest.cpp b/Userland/Services/RequestServer/GeminiRequest.cpp index 6ff619af88..d98feb0546 100644 --- a/Userland/Services/RequestServer/GeminiRequest.cpp +++ b/Userland/Services/RequestServer/GeminiRequest.cpp @@ -4,6 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "ConnectionCache.h" +#include <LibCore/EventLoop.h> #include <LibGemini/GeminiJob.h> #include <LibGemini/GeminiResponse.h> #include <RequestServer/GeminiRequest.h> @@ -15,6 +17,9 @@ GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::Gem , m_job(job) { m_job->on_finish = [this](bool success) { + Core::deferred_invoke([url = m_job->url(), socket = m_job->socket()] { + ConnectionCache::request_did_finish(url, socket); + }); if (auto* response = m_job->response()) { set_downloaded_size(this->output_stream().size()); if (!response->meta().is_empty()) { diff --git a/Userland/Services/RequestServer/GeminiRequest.h b/Userland/Services/RequestServer/GeminiRequest.h index 1111a416d0..c481da653d 100644 --- a/Userland/Services/RequestServer/GeminiRequest.h +++ b/Userland/Services/RequestServer/GeminiRequest.h @@ -18,6 +18,8 @@ public: virtual ~GeminiRequest() override; static NonnullOwnPtr<GeminiRequest> create_with_job(Badge<GeminiProtocol>, ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&); + Gemini::GeminiJob const& job() const { return *m_job; } + private: explicit GeminiRequest(ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&); diff --git a/Userland/Services/RequestServer/HttpCommon.h b/Userland/Services/RequestServer/HttpCommon.h index bdc158faa6..5a4866a53d 100644 --- a/Userland/Services/RequestServer/HttpCommon.h +++ b/Userland/Services/RequestServer/HttpCommon.h @@ -15,6 +15,7 @@ #include <AK/Types.h> #include <LibHTTP/HttpRequest.h> #include <RequestServer/ClientConnection.h> +#include <RequestServer/ConnectionCache.h> #include <RequestServer/Request.h> namespace RequestServer::Detail { @@ -29,6 +30,9 @@ void init(TSelf* self, TJob job) }; job->on_finish = [self](bool success) { + Core::deferred_invoke([url = self->job().url(), socket = self->job().socket()] { + ConnectionCache::request_did_finish(url, socket); + }); if (auto* response = self->job().response()) { self->set_status_code(response->code()); self->set_response_headers(response->headers()); @@ -80,7 +84,12 @@ OwnPtr<Request> start_request(TBadgedProtocol&& protocol, ClientConnection& clie auto job = TJob::construct(request, *output_stream); auto protocol_request = TRequest::create_with_job(forward<TBadgedProtocol>(protocol), client, (TJob&)*job, move(output_stream)); protocol_request->set_request_fd(pipe_result.value().read_fd); - job->start(); + + if constexpr (IsSame<typename TBadgedProtocol::Type, HttpsProtocol>) + ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job); + else + ConnectionCache::get_or_create_connection(ConnectionCache::g_tcp_connection_cache, url, *job); + return protocol_request; } diff --git a/Userland/Services/RequestServer/HttpRequest.h b/Userland/Services/RequestServer/HttpRequest.h index aa7e7f999d..d2968061c1 100644 --- a/Userland/Services/RequestServer/HttpRequest.h +++ b/Userland/Services/RequestServer/HttpRequest.h @@ -20,6 +20,7 @@ public: static NonnullOwnPtr<HttpRequest> create_with_job(Badge<HttpProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&); HTTP::HttpJob& job() { return m_job; } + HTTP::HttpJob const& job() const { return m_job; } private: explicit HttpRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&); diff --git a/Userland/Services/RequestServer/HttpsRequest.h b/Userland/Services/RequestServer/HttpsRequest.h index 7d15fb4792..2f26b0d6fe 100644 --- a/Userland/Services/RequestServer/HttpsRequest.h +++ b/Userland/Services/RequestServer/HttpsRequest.h @@ -19,6 +19,7 @@ public: static NonnullOwnPtr<HttpsRequest> create_with_job(Badge<HttpsProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&); HTTP::HttpsJob& job() { return m_job; } + HTTP::HttpsJob const& job() const { return m_job; } private: explicit HttpsRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&); |