mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 04:27:45 +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:
parent
7a95c451a3
commit
aafc451016
47 changed files with 841 additions and 1157 deletions
|
@ -127,40 +127,15 @@ void ClientConnection::ensure_connection(URL const& url, ::RequestServer::CacheL
|
|||
|
||||
struct {
|
||||
URL const& m_url;
|
||||
void start(NonnullRefPtr<Core::Socket> socket)
|
||||
void start(Core::Stream::Socket& socket)
|
||||
{
|
||||
auto is_tls = is<TLS::TLSv12>(*socket);
|
||||
auto* tls_instance = is_tls ? static_cast<TLS::TLSv12*>(socket.ptr()) : nullptr;
|
||||
|
||||
auto is_connected = false;
|
||||
if (is_tls && tls_instance->is_established())
|
||||
is_connected = true;
|
||||
if (!is_tls && socket->is_connected())
|
||||
is_connected = true;
|
||||
|
||||
VERIFY(!is_connected);
|
||||
|
||||
bool did_connect;
|
||||
if (is_tls) {
|
||||
tls_instance->set_root_certificates(DefaultRootCACertificates::the().certificates());
|
||||
tls_instance->on_tls_connected = [socket = socket.ptr(), url = m_url, tls_instance] {
|
||||
tls_instance->set_on_tls_ready_to_write([socket, url](auto&) {
|
||||
ConnectionCache::request_did_finish(url, socket);
|
||||
});
|
||||
};
|
||||
tls_instance->on_tls_error = [socket = socket.ptr(), url = m_url](auto) {
|
||||
ConnectionCache::request_did_finish(url, socket);
|
||||
};
|
||||
did_connect = tls_instance->connect(m_url.host(), m_url.port_or_default());
|
||||
} else {
|
||||
socket->on_connected = [socket = socket.ptr(), url = m_url]() mutable {
|
||||
ConnectionCache::request_did_finish(url, socket);
|
||||
};
|
||||
did_connect = socket->connect(m_url.host(), m_url.port_or_default());
|
||||
}
|
||||
|
||||
if (!did_connect)
|
||||
ConnectionCache::request_did_finish(m_url, 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 };
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
|
@ -13,6 +13,8 @@
|
|||
#include <AK/URL.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/ElapsedTimer.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/NetworkJob.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibTLS/TLSv12.h>
|
||||
|
@ -30,15 +32,47 @@ namespace RequestServer::ConnectionCache {
|
|||
|
||||
template<typename Socket>
|
||||
struct Connection {
|
||||
using QueueType = Vector<Function<void(Core::Socket&)>>;
|
||||
struct JobData {
|
||||
Function<void(Core::Stream::Socket&)> start {};
|
||||
Function<void(Core::NetworkJob::Error)> fail {};
|
||||
Function<Vector<TLS::Certificate>()> provide_client_certificates {};
|
||||
|
||||
template<typename T>
|
||||
static JobData create(T& job)
|
||||
{
|
||||
// Clang-format _really_ messes up formatting this, so just format it manually.
|
||||
// clang-format off
|
||||
return JobData {
|
||||
.start = [&job](auto& socket) {
|
||||
job.start(socket);
|
||||
},
|
||||
.fail = [&job](auto error) {
|
||||
job.fail(error);
|
||||
},
|
||||
.provide_client_certificates = [&job] {
|
||||
if constexpr (requires { job.on_certificate_requested; }) {
|
||||
if (job.on_certificate_requested)
|
||||
return job.on_certificate_requested();
|
||||
} else {
|
||||
// "use" `job`, otherwise clang gets sad.
|
||||
(void)job;
|
||||
}
|
||||
return Vector<TLS::Certificate> {};
|
||||
},
|
||||
};
|
||||
// clang-format on
|
||||
}
|
||||
};
|
||||
using QueueType = Vector<JobData>;
|
||||
using SocketType = Socket;
|
||||
|
||||
NonnullRefPtr<Socket> socket;
|
||||
NonnullOwnPtr<Core::Stream::BufferedSocket<Socket>> socket;
|
||||
QueueType request_queue;
|
||||
NonnullRefPtr<Core::Timer> removal_timer;
|
||||
bool has_started { false };
|
||||
URL current_url {};
|
||||
Core::ElapsedTimer timer {};
|
||||
JobData job_data {};
|
||||
};
|
||||
|
||||
struct ConnectionKey {
|
||||
|
@ -60,44 +94,82 @@ struct AK::Traits<RequestServer::ConnectionCache::ConnectionKey> : public AK::Ge
|
|||
|
||||
namespace RequestServer::ConnectionCache {
|
||||
|
||||
extern HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<Core::TCPSocket>>>> g_tcp_connection_cache;
|
||||
extern HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<Core::Stream::TCPSocket>>>> g_tcp_connection_cache;
|
||||
extern HashMap<ConnectionKey, NonnullOwnPtr<NonnullOwnPtrVector<Connection<TLS::TLSv12>>>> g_tls_connection_cache;
|
||||
|
||||
void request_did_finish(URL const&, Core::Socket const*);
|
||||
void request_did_finish(URL const&, Core::Stream::Socket const*);
|
||||
void dump_jobs();
|
||||
|
||||
constexpr static size_t MaxConcurrentConnectionsPerURL = 2;
|
||||
constexpr static size_t ConnectionKeepAliveTimeMilliseconds = 10'000;
|
||||
|
||||
template<typename T>
|
||||
void recreate_socket_if_needed(T& connection, URL const& url)
|
||||
ErrorOr<void> recreate_socket_if_needed(T& connection, URL const& url)
|
||||
{
|
||||
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) {
|
||||
using SocketType = typename T::SocketType;
|
||||
if (!connection.socket->is_open()) {
|
||||
// Create another socket for the connection.
|
||||
connection.socket = SocketType::construct(nullptr);
|
||||
auto set_socket = [&](auto socket) -> ErrorOr<void> {
|
||||
connection.socket = TRY(Core::Stream::BufferedSocket<SocketType>::create(move(socket)));
|
||||
return {};
|
||||
};
|
||||
|
||||
if constexpr (IsSame<TLS::TLSv12, SocketType>) {
|
||||
TLS::Options options;
|
||||
options.set_alert_handler([&connection](TLS::AlertDescription alert) {
|
||||
Core::NetworkJob::Error reason;
|
||||
if (alert == TLS::AlertDescription::HandshakeFailure)
|
||||
reason = Core::NetworkJob::Error::ProtocolFailed;
|
||||
else if (alert == TLS::AlertDescription::DecryptError)
|
||||
reason = Core::NetworkJob::Error::ConnectionFailed;
|
||||
else
|
||||
reason = Core::NetworkJob::Error::TransmissionFailed;
|
||||
|
||||
if (connection.job_data.fail)
|
||||
connection.job_data.fail(reason);
|
||||
});
|
||||
options.set_certificate_provider([&connection]() -> Vector<TLS::Certificate> {
|
||||
if (connection.job_data.provide_client_certificates)
|
||||
return connection.job_data.provide_client_certificates();
|
||||
return {};
|
||||
});
|
||||
TRY(set_socket(TRY(SocketType::connect(url.host(), url.port_or_default(), move(options)))));
|
||||
} else {
|
||||
TRY(set_socket(TRY(SocketType::connect(url.host(), url.port_or_default()))));
|
||||
}
|
||||
dbgln_if(REQUESTSERVER_DEBUG, "Creating a new socket for {} -> {}", url, connection.socket);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job)
|
||||
{
|
||||
using CacheEntryType = RemoveCVReference<decltype(*cache.begin()->value)>;
|
||||
auto start_job = [&job](auto& socket) {
|
||||
job.start(socket);
|
||||
};
|
||||
auto& sockets_for_url = *cache.ensure({ url.host(), url.port_or_default() }, [] { return make<CacheEntryType>(); });
|
||||
|
||||
using ReturnType = decltype(&sockets_for_url[0]);
|
||||
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) {
|
||||
using ConnectionType = RemoveCVReference<decltype(cache.begin()->value->at(0))>;
|
||||
auto connection_result = ConnectionType::SocketType::connect(url.host(), url.port_or_default());
|
||||
if (connection_result.is_error()) {
|
||||
dbgln("ConnectionCache: Connection to {} failed: {}", url, connection_result.error());
|
||||
Core::deferred_invoke([&job] {
|
||||
job.fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
return ReturnType { nullptr };
|
||||
}
|
||||
auto socket_result = Core::Stream::BufferedSocket<typename ConnectionType::SocketType>::create(connection_result.release_value());
|
||||
if (socket_result.is_error()) {
|
||||
dbgln("ConnectionCache: Failed to make a buffered socket for {}: {}", url, socket_result.error());
|
||||
Core::deferred_invoke([&job] {
|
||||
job.fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
return ReturnType { nullptr };
|
||||
}
|
||||
sockets_for_url.append(make<ConnectionType>(
|
||||
ConnectionType::SocketType::construct(nullptr),
|
||||
socket_result.release_value(),
|
||||
typename ConnectionType::QueueType {},
|
||||
Core::Timer::create_single_shot(ConnectionKeepAliveTimeMilliseconds, nullptr)));
|
||||
did_add_new_connection = true;
|
||||
|
@ -107,7 +179,7 @@ decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job)
|
|||
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.
|
||||
// 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) {
|
||||
|
@ -120,20 +192,35 @@ decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job)
|
|||
} else {
|
||||
index = it.index();
|
||||
}
|
||||
if (sockets_for_url.is_empty()) {
|
||||
Core::deferred_invoke([&job] {
|
||||
job.fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
return ReturnType { nullptr };
|
||||
}
|
||||
|
||||
auto& connection = sockets_for_url[index];
|
||||
if (!connection.has_started) {
|
||||
recreate_socket_if_needed(connection, url);
|
||||
if (auto result = recreate_socket_if_needed(connection, url); result.is_error()) {
|
||||
dbgln("ConnectionCache: request failed to start, failed to make a socket: {}", result.error());
|
||||
Core::deferred_invoke([&job] {
|
||||
job.fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
return ReturnType { nullptr };
|
||||
}
|
||||
dbgln_if(REQUESTSERVER_DEBUG, "Immediately start request for url {} in {} - {}", url, &connection, connection.socket);
|
||||
connection.has_started = true;
|
||||
connection.removal_timer->stop();
|
||||
connection.timer.start();
|
||||
connection.current_url = url;
|
||||
start_job(*connection.socket);
|
||||
connection.job_data = decltype(connection.job_data)::create(job);
|
||||
connection.socket->set_notifications_enabled(true);
|
||||
connection.job_data.start(*connection.socket);
|
||||
} else {
|
||||
dbgln_if(REQUESTSERVER_DEBUG, "Enqueue request for URL {} in {} - {}", url, &connection, connection.socket);
|
||||
connection.request_queue.append(move(start_job));
|
||||
connection.request_queue.append(decltype(connection.job_data)::create(job));
|
||||
}
|
||||
return connection;
|
||||
return &connection;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
*/
|
||||
|
||||
#include "ConnectionCache.h"
|
||||
#include <LibGemini/GeminiJob.h>
|
||||
#include <LibGemini/GeminiRequest.h>
|
||||
#include <LibGemini/Job.h>
|
||||
#include <RequestServer/GeminiProtocol.h>
|
||||
#include <RequestServer/GeminiRequest.h>
|
||||
|
||||
|
@ -30,10 +30,9 @@ OwnPtr<Request> GeminiProtocol::start_request(ClientConnection& client, const St
|
|||
if (pipe_result.is_error())
|
||||
return {};
|
||||
|
||||
auto output_stream = make<OutputFileStream>(pipe_result.value().write_fd);
|
||||
output_stream->make_unbuffered();
|
||||
auto job = Gemini::GeminiJob::construct(request, *output_stream);
|
||||
auto protocol_request = GeminiRequest::create_with_job({}, client, (Gemini::GeminiJob&)*job, move(output_stream));
|
||||
auto output_stream = MUST(Core::Stream::File::adopt_fd(pipe_result.value().write_fd, Core::Stream::OpenMode::Write));
|
||||
auto job = Gemini::Job::construct(request, *output_stream);
|
||||
auto protocol_request = GeminiRequest::create_with_job({}, client, *job, move(output_stream));
|
||||
protocol_request->set_request_fd(pipe_result.value().read_fd);
|
||||
|
||||
ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job);
|
||||
|
|
|
@ -6,22 +6,22 @@
|
|||
|
||||
#include "ConnectionCache.h"
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibGemini/GeminiJob.h>
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
#include <LibGemini/Job.h>
|
||||
#include <RequestServer/GeminiRequest.h>
|
||||
|
||||
namespace RequestServer {
|
||||
|
||||
GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
: Request(client, move(output_stream))
|
||||
, m_job(job)
|
||||
, m_job(move(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());
|
||||
set_downloaded_size(MUST(const_cast<Core::Stream::File&>(this->output_stream()).size()));
|
||||
if (!response->meta().is_empty()) {
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> headers;
|
||||
headers.set("meta", response->meta());
|
||||
|
@ -42,16 +42,12 @@ GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::Gem
|
|||
did_finish(success);
|
||||
};
|
||||
m_job->on_progress = [this](Optional<u32> total, u32 current) {
|
||||
did_progress(total, current);
|
||||
};
|
||||
m_job->on_certificate_requested = [this](auto&) {
|
||||
did_request_certificates();
|
||||
did_progress(move(total), current);
|
||||
};
|
||||
}
|
||||
|
||||
void GeminiRequest::set_certificate(String certificate, String key)
|
||||
void GeminiRequest::set_certificate(String, String)
|
||||
{
|
||||
m_job->set_certificate(move(certificate), move(key));
|
||||
}
|
||||
|
||||
GeminiRequest::~GeminiRequest()
|
||||
|
@ -61,7 +57,7 @@ GeminiRequest::~GeminiRequest()
|
|||
m_job->cancel();
|
||||
}
|
||||
|
||||
NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
{
|
||||
return adopt_own(*new GeminiRequest(client, move(job), move(output_stream)));
|
||||
}
|
||||
|
|
|
@ -16,18 +16,18 @@ namespace RequestServer {
|
|||
class GeminiRequest final : public Request {
|
||||
public:
|
||||
virtual ~GeminiRequest() override;
|
||||
static NonnullOwnPtr<GeminiRequest> create_with_job(Badge<GeminiProtocol>, ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
static NonnullOwnPtr<GeminiRequest> create_with_job(Badge<GeminiProtocol>, ClientConnection&, NonnullRefPtr<Gemini::Job>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
Gemini::GeminiJob const& job() const { return *m_job; }
|
||||
Gemini::Job const& job() const { return *m_job; }
|
||||
|
||||
virtual URL url() const override { return m_job->url(); }
|
||||
|
||||
private:
|
||||
explicit GeminiRequest(ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
explicit GeminiRequest(ClientConnection&, NonnullRefPtr<Gemini::Job>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
virtual void set_certificate(String certificate, String key) override;
|
||||
|
||||
NonnullRefPtr<Gemini::GeminiJob> m_job;
|
||||
NonnullRefPtr<Gemini::Job> m_job;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ void init(TSelf* self, TJob job)
|
|||
if (auto* response = self->job().response()) {
|
||||
self->set_status_code(response->code());
|
||||
self->set_response_headers(response->headers());
|
||||
self->set_downloaded_size(self->output_stream().size());
|
||||
self->set_downloaded_size(response->downloaded_size());
|
||||
}
|
||||
|
||||
// if we didn't know the total size, pretend that the request finished successfully
|
||||
|
@ -50,8 +50,12 @@ void init(TSelf* self, TJob job)
|
|||
self->did_progress(total, current);
|
||||
};
|
||||
if constexpr (requires { job->on_certificate_requested; }) {
|
||||
job->on_certificate_requested = [self](auto&) {
|
||||
job->on_certificate_requested = [job, self] {
|
||||
self->did_request_certificates();
|
||||
Core::EventLoop::current().spin_until([&] {
|
||||
return job->received_client_certificates();
|
||||
});
|
||||
return job->take_client_certificates();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -79,8 +83,7 @@ OwnPtr<Request> start_request(TBadgedProtocol&& protocol, ClientConnection& clie
|
|||
return {};
|
||||
request.set_body(allocated_body_result.release_value());
|
||||
|
||||
auto output_stream = make<OutputFileStream>(pipe_result.value().write_fd);
|
||||
output_stream->make_unbuffered();
|
||||
auto output_stream = MUST(Core::Stream::File::adopt_fd(pipe_result.value().write_fd, Core::Stream::OpenMode::Write));
|
||||
auto job = TJob::construct(move(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);
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <AK/OwnPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibHTTP/HttpJob.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
#include <RequestServer/ClientConnection.h>
|
||||
#include <RequestServer/HttpRequest.h>
|
||||
#include <RequestServer/Protocol.h>
|
||||
|
@ -21,7 +21,7 @@ namespace RequestServer {
|
|||
|
||||
class HttpProtocol final : public Protocol {
|
||||
public:
|
||||
using JobType = HTTP::HttpJob;
|
||||
using JobType = HTTP::Job;
|
||||
using RequestType = HttpRequest;
|
||||
|
||||
HttpProtocol();
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibHTTP/HttpJob.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
#include <RequestServer/HttpCommon.h>
|
||||
#include <RequestServer/HttpProtocol.h>
|
||||
#include <RequestServer/HttpRequest.h>
|
||||
|
||||
namespace RequestServer {
|
||||
|
||||
HttpRequest::HttpRequest(ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
HttpRequest::HttpRequest(ClientConnection& client, NonnullRefPtr<HTTP::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
: Request(client, move(output_stream))
|
||||
, m_job(job)
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ HttpRequest::~HttpRequest()
|
|||
m_job->cancel();
|
||||
}
|
||||
|
||||
NonnullOwnPtr<HttpRequest> HttpRequest::create_with_job(Badge<HttpProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
NonnullOwnPtr<HttpRequest> HttpRequest::create_with_job(Badge<HttpProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
{
|
||||
return adopt_own(*new HttpRequest(client, move(job), move(output_stream)));
|
||||
}
|
||||
|
|
|
@ -17,17 +17,17 @@ namespace RequestServer {
|
|||
class HttpRequest final : public Request {
|
||||
public:
|
||||
virtual ~HttpRequest() override;
|
||||
static NonnullOwnPtr<HttpRequest> create_with_job(Badge<HttpProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
static NonnullOwnPtr<HttpRequest> create_with_job(Badge<HttpProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::Job>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
HTTP::HttpJob& job() { return m_job; }
|
||||
HTTP::HttpJob const& job() const { return m_job; }
|
||||
HTTP::Job& job() { return m_job; }
|
||||
HTTP::Job const& job() const { return m_job; }
|
||||
|
||||
virtual URL url() const override { return m_job->url(); }
|
||||
|
||||
private:
|
||||
explicit HttpRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
explicit HttpRequest(ClientConnection&, NonnullRefPtr<HTTP::Job>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
NonnullRefPtr<HTTP::HttpJob> m_job;
|
||||
NonnullRefPtr<HTTP::Job> m_job;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace RequestServer {
|
||||
|
||||
HttpsRequest::HttpsRequest(ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
HttpsRequest::HttpsRequest(ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
: Request(client, move(output_stream))
|
||||
, m_job(job)
|
||||
{
|
||||
|
@ -30,7 +30,7 @@ HttpsRequest::~HttpsRequest()
|
|||
m_job->cancel();
|
||||
}
|
||||
|
||||
NonnullOwnPtr<HttpsRequest> HttpsRequest::create_with_job(Badge<HttpsProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
NonnullOwnPtr<HttpsRequest> HttpsRequest::create_with_job(Badge<HttpsProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
{
|
||||
return adopt_own(*new HttpsRequest(client, move(job), move(output_stream)));
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace RequestServer {
|
|||
class HttpsRequest final : public Request {
|
||||
public:
|
||||
virtual ~HttpsRequest() override;
|
||||
static NonnullOwnPtr<HttpsRequest> create_with_job(Badge<HttpsProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
static NonnullOwnPtr<HttpsRequest> create_with_job(Badge<HttpsProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
HTTP::HttpsJob& job() { return m_job; }
|
||||
HTTP::HttpsJob const& job() const { return m_job; }
|
||||
|
@ -24,7 +24,7 @@ public:
|
|||
virtual URL url() const override { return m_job->url(); }
|
||||
|
||||
private:
|
||||
explicit HttpsRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||
explicit HttpsRequest(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
virtual void set_certificate(String certificate, String key) override;
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace RequestServer {
|
|||
// FIXME: What about rollover?
|
||||
static i32 s_next_id = 1;
|
||||
|
||||
Request::Request(ClientConnection& client, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||
Request::Request(ClientConnection& client, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
||||
: m_client(client)
|
||||
, m_id(s_next_id++)
|
||||
, m_output_stream(move(output_stream))
|
||||
|
|
|
@ -41,10 +41,10 @@ public:
|
|||
void did_request_certificates();
|
||||
void set_response_headers(const HashMap<String, String, CaseInsensitiveStringTraits>&);
|
||||
void set_downloaded_size(size_t size) { m_downloaded_size = size; }
|
||||
const OutputFileStream& output_stream() const { return *m_output_stream; }
|
||||
const Core::Stream::File& output_stream() const { return *m_output_stream; }
|
||||
|
||||
protected:
|
||||
explicit Request(ClientConnection&, NonnullOwnPtr<OutputFileStream>&&);
|
||||
explicit Request(ClientConnection&, NonnullOwnPtr<Core::Stream::File>&&);
|
||||
|
||||
private:
|
||||
ClientConnection& m_client;
|
||||
|
@ -53,7 +53,7 @@ private:
|
|||
Optional<u32> m_status_code;
|
||||
Optional<u32> m_total_size {};
|
||||
size_t m_downloaded_size { 0 };
|
||||
NonnullOwnPtr<OutputFileStream> m_output_stream;
|
||||
NonnullOwnPtr<Core::Stream::File> m_output_stream;
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue