1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:57:44 +00:00

Services: Rename ProtocolServer to RequestServer

The current ProtocolServer was really only used for requests, and with
the recent introduction of the WebSocket service, long-lasting
connections with another server are not part of it. To better reflect
this, this commit renames it to RequestServer.

This commit also changes the existing 'protocol' portal to 'request',
the existing 'protocol' user and group to 'request', and most mentions
of the 'download' aspect of the request to 'request' when relevant, to
make everything consistent across the system.

Note that LibProtocol still exists as-is, but the more generic Client
class and the more specific Download class have both been renamed to a
more accurate RequestClient and Request to match the new names.

This commit only change names, not behaviors.
This commit is contained in:
DexesTTP 2021-04-23 22:45:52 +02:00 committed by Linus Groh
parent 22413ef729
commit 71d27abb97
58 changed files with 786 additions and 788 deletions

View file

@ -1,13 +1,13 @@
set(SOURCES
Client.cpp
Download.cpp
Request.cpp
RequestClient.cpp
WebSocket.cpp
WebSocketClient.cpp
)
set(GENERATED_SOURCES
../../Services/ProtocolServer/ProtocolClientEndpoint.h
../../Services/ProtocolServer/ProtocolServerEndpoint.h
../../Services/RequestServer/RequestClientEndpoint.h
../../Services/RequestServer/RequestServerEndpoint.h
../../Services/WebSocket/WebSocketClientEndpoint.h
../../Services/WebSocket/WebSocketServerEndpoint.h
)

View file

@ -1,98 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FileStream.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
namespace Protocol {
Client::Client()
: IPC::ServerConnection<ProtocolClientEndpoint, ProtocolServerEndpoint>(*this, "/tmp/portal/protocol")
{
handshake();
}
void Client::handshake()
{
send_sync<Messages::ProtocolServer::Greet>();
}
bool Client::is_supported_protocol(const String& protocol)
{
return send_sync<Messages::ProtocolServer::IsSupportedProtocol>(protocol)->supported();
}
template<typename RequestHashMapTraits>
RefPtr<Download> Client::start_download(const String& method, const String& url, const HashMap<String, String, RequestHashMapTraits>& request_headers, ReadonlyBytes request_body)
{
IPC::Dictionary header_dictionary;
for (auto& it : request_headers)
header_dictionary.add(it.key, it.value);
auto response = send_sync<Messages::ProtocolServer::StartDownload>(method, url, header_dictionary, ByteBuffer::copy(request_body));
auto download_id = response->download_id();
if (download_id < 0 || !response->response_fd().has_value())
return nullptr;
auto response_fd = response->response_fd().value().take_fd();
auto download = Download::create_from_id({}, *this, download_id);
download->set_download_fd({}, response_fd);
m_downloads.set(download_id, download);
return download;
}
bool Client::stop_download(Badge<Download>, Download& download)
{
if (!m_downloads.contains(download.id()))
return false;
return send_sync<Messages::ProtocolServer::StopDownload>(download.id())->success();
}
bool Client::set_certificate(Badge<Download>, Download& download, String certificate, String key)
{
if (!m_downloads.contains(download.id()))
return false;
return send_sync<Messages::ProtocolServer::SetCertificate>(download.id(), move(certificate), move(key))->success();
}
void Client::handle(const Messages::ProtocolClient::DownloadFinished& message)
{
RefPtr<Download> download;
if ((download = m_downloads.get(message.download_id()).value_or(nullptr))) {
download->did_finish({}, message.success(), message.total_size());
}
m_downloads.remove(message.download_id());
}
void Client::handle(const Messages::ProtocolClient::DownloadProgress& message)
{
if (auto download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr))) {
download->did_progress({}, message.total_size(), message.downloaded_size());
}
}
void Client::handle(const Messages::ProtocolClient::HeadersBecameAvailable& message)
{
if (auto download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr))) {
HashMap<String, String, CaseInsensitiveStringTraits> headers;
message.response_headers().for_each_entry([&](auto& name, auto& value) { headers.set(name, value); });
download->did_receive_headers({}, headers, message.status_code());
}
}
OwnPtr<Messages::ProtocolClient::CertificateRequestedResponse> Client::handle(const Messages::ProtocolClient::CertificateRequested& message)
{
if (auto download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr))) {
download->did_request_certificates({});
}
return make<Messages::ProtocolClient::CertificateRequestedResponse>();
}
}
template RefPtr<Protocol::Download> Protocol::Client::start_download(const String& method, const String& url, const HashMap<String, String>& request_headers, ReadonlyBytes request_body);
template RefPtr<Protocol::Download> Protocol::Client::start_download(const String& method, const String& url, const HashMap<String, String, CaseInsensitiveStringTraits>& request_headers, ReadonlyBytes request_body);

View file

@ -1,44 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <LibIPC/ServerConnection.h>
#include <ProtocolServer/ProtocolClientEndpoint.h>
#include <ProtocolServer/ProtocolServerEndpoint.h>
namespace Protocol {
class Download;
class Client
: public IPC::ServerConnection<ProtocolClientEndpoint, ProtocolServerEndpoint>
, public ProtocolClientEndpoint {
C_OBJECT(Client);
public:
virtual void handshake() override;
bool is_supported_protocol(const String&);
template<typename RequestHashMapTraits = Traits<String>>
RefPtr<Download> start_download(const String& method, const String& url, const HashMap<String, String, RequestHashMapTraits>& request_headers = {}, ReadonlyBytes request_body = {});
bool stop_download(Badge<Download>, Download&);
bool set_certificate(Badge<Download>, Download&, String, String);
private:
Client();
virtual void handle(const Messages::ProtocolClient::DownloadProgress&) override;
virtual void handle(const Messages::ProtocolClient::DownloadFinished&) override;
virtual OwnPtr<Messages::ProtocolClient::CertificateRequestedResponse> handle(const Messages::ProtocolClient::CertificateRequested&) override;
virtual void handle(const Messages::ProtocolClient::HeadersBecameAvailable&) override;
HashMap<i32, RefPtr<Download>> m_downloads;
};
}

View file

@ -4,23 +4,23 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
#include <LibProtocol/Request.h>
#include <LibProtocol/RequestClient.h>
namespace Protocol {
Download::Download(Client& client, i32 download_id)
Request::Request(RequestClient& client, i32 request_id)
: m_client(client)
, m_download_id(download_id)
, m_request_id(request_id)
{
}
bool Download::stop()
bool Request::stop()
{
return m_client->stop_download({}, *this);
return m_client->stop_request({}, *this);
}
void Download::stream_into(OutputStream& stream)
void Request::stream_into(OutputStream& stream)
{
VERIFY(!m_internal_stream_data);
@ -33,7 +33,7 @@ void Download::stream_into(OutputStream& stream)
on_finish = [this](auto success, auto total_size) {
m_internal_stream_data->success = success;
m_internal_stream_data->total_size = total_size;
m_internal_stream_data->download_done = true;
m_internal_stream_data->request_done = true;
};
notifier->on_ready_to_read = [this, &stream, user_on_finish = move(user_on_finish)] {
@ -45,7 +45,7 @@ void Download::stream_into(OutputStream& stream)
TODO();
}
if (m_internal_stream_data->read_stream.eof() && m_internal_stream_data->download_done) {
if (m_internal_stream_data->read_stream.eof() && m_internal_stream_data->request_done) {
m_internal_stream_data->read_notifier->close();
user_on_finish(m_internal_stream_data->success, m_internal_stream_data->total_size);
} else {
@ -54,7 +54,7 @@ void Download::stream_into(OutputStream& stream)
};
}
void Download::set_should_buffer_all_input(bool value)
void Request::set_should_buffer_all_input(bool value)
{
if (m_should_buffer_all_input == value)
return;
@ -67,7 +67,7 @@ void Download::set_should_buffer_all_input(bool value)
VERIFY(!m_internal_stream_data);
VERIFY(!m_internal_buffered_data);
VERIFY(on_buffered_download_finish); // Not having this set makes no sense.
VERIFY(on_buffered_request_finish); // Not having this set makes no sense.
m_internal_buffered_data = make<InternalBufferedData>(fd());
m_should_buffer_all_input = true;
@ -78,7 +78,7 @@ void Download::set_should_buffer_all_input(bool value)
on_finish = [this](auto success, u32 total_size) {
auto output_buffer = m_internal_buffered_data->payload_stream.copy_into_contiguous_buffer();
on_buffered_download_finish(
on_buffered_request_finish(
success,
total_size,
m_internal_buffered_data->response_headers,
@ -89,7 +89,7 @@ void Download::set_should_buffer_all_input(bool value)
stream_into(m_internal_buffered_data->payload_stream);
}
void Download::did_finish(Badge<Client>, bool success, u32 total_size)
void Request::did_finish(Badge<RequestClient>, bool success, u32 total_size)
{
if (!on_finish)
return;
@ -97,24 +97,24 @@ void Download::did_finish(Badge<Client>, bool success, u32 total_size)
on_finish(success, total_size);
}
void Download::did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size)
void Request::did_progress(Badge<RequestClient>, Optional<u32> total_size, u32 downloaded_size)
{
if (on_progress)
on_progress(total_size, downloaded_size);
}
void Download::did_receive_headers(Badge<Client>, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code)
void Request::did_receive_headers(Badge<RequestClient>, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code)
{
if (on_headers_received)
on_headers_received(response_headers, response_code);
}
void Download::did_request_certificates(Badge<Client>)
void Request::did_request_certificates(Badge<RequestClient>)
{
if (on_certificate_requested) {
auto result = on_certificate_requested();
if (!m_client->set_certificate({}, *this, result.certificate, result.key)) {
dbgln("Download: set_certificate failed");
dbgln("Request: set_certificate failed");
}
}
}

View file

@ -19,49 +19,49 @@
namespace Protocol {
class Client;
class RequestClient;
class Download : public RefCounted<Download> {
class Request : public RefCounted<Request> {
public:
struct CertificateAndKey {
String certificate;
String key;
};
static NonnullRefPtr<Download> create_from_id(Badge<Client>, Client& client, i32 download_id)
static NonnullRefPtr<Request> create_from_id(Badge<RequestClient>, RequestClient& client, i32 request_id)
{
return adopt_ref(*new Download(client, download_id));
return adopt_ref(*new Request(client, request_id));
}
int id() const { return m_download_id; }
int id() const { return m_request_id; }
int fd() const { return m_fd; }
bool stop();
void stream_into(OutputStream&);
bool should_buffer_all_input() const { return m_should_buffer_all_input; }
/// Note: Will override `on_finish', and `on_headers_received', and expects `on_buffered_download_finish' to be set!
/// Note: Will override `on_finish', and `on_headers_received', and expects `on_buffered_request_finish' to be set!
void set_should_buffer_all_input(bool);
/// Note: Must be set before `set_should_buffer_all_input(true)`.
Function<void(bool success, u32 total_size, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code, ReadonlyBytes payload)> on_buffered_download_finish;
Function<void(bool success, u32 total_size, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code, ReadonlyBytes payload)> on_buffered_request_finish;
Function<void(bool success, u32 total_size)> on_finish;
Function<void(Optional<u32> total_size, u32 downloaded_size)> on_progress;
Function<void(const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code)> on_headers_received;
Function<CertificateAndKey()> on_certificate_requested;
void did_finish(Badge<Client>, bool success, u32 total_size);
void did_progress(Badge<Client>, Optional<u32> total_size, u32 downloaded_size);
void did_receive_headers(Badge<Client>, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code);
void did_request_certificates(Badge<Client>);
void did_finish(Badge<RequestClient>, bool success, u32 total_size);
void did_progress(Badge<RequestClient>, Optional<u32> total_size, u32 downloaded_size);
void did_receive_headers(Badge<RequestClient>, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers, Optional<u32> response_code);
void did_request_certificates(Badge<RequestClient>);
RefPtr<Core::Notifier>& write_notifier(Badge<Client>) { return m_write_notifier; }
void set_download_fd(Badge<Client>, int fd) { m_fd = fd; }
RefPtr<Core::Notifier>& write_notifier(Badge<RequestClient>) { return m_write_notifier; }
void set_request_fd(Badge<RequestClient>, int fd) { m_fd = fd; }
private:
explicit Download(Client&, i32 download_id);
WeakPtr<Client> m_client;
int m_download_id { -1 };
explicit Request(RequestClient&, i32 request_id);
WeakPtr<RequestClient> m_client;
int m_request_id { -1 };
RefPtr<Core::Notifier> m_write_notifier;
int m_fd { -1 };
bool m_should_buffer_all_input { false };
@ -88,7 +88,7 @@ private:
RefPtr<Core::Notifier> read_notifier;
bool success;
u32 total_size { 0 };
bool download_done { false };
bool request_done { false };
};
OwnPtr<InternalBufferedData> m_internal_buffered_data;

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FileStream.h>
#include <LibProtocol/Request.h>
#include <LibProtocol/RequestClient.h>
namespace Protocol {
RequestClient::RequestClient()
: IPC::ServerConnection<RequestClientEndpoint, RequestServerEndpoint>(*this, "/tmp/portal/request")
{
handshake();
}
void RequestClient::handshake()
{
send_sync<Messages::RequestServer::Greet>();
}
bool RequestClient::is_supported_protocol(const String& protocol)
{
return send_sync<Messages::RequestServer::IsSupportedProtocol>(protocol)->supported();
}
template<typename RequestHashMapTraits>
RefPtr<Request> RequestClient::start_request(const String& method, const String& url, const HashMap<String, String, RequestHashMapTraits>& request_headers, ReadonlyBytes request_body)
{
IPC::Dictionary header_dictionary;
for (auto& it : request_headers)
header_dictionary.add(it.key, it.value);
auto response = send_sync<Messages::RequestServer::StartRequest>(method, url, header_dictionary, ByteBuffer::copy(request_body));
auto request_id = response->request_id();
if (request_id < 0 || !response->response_fd().has_value())
return nullptr;
auto response_fd = response->response_fd().value().take_fd();
auto request = Request::create_from_id({}, *this, request_id);
request->set_request_fd({}, response_fd);
m_requests.set(request_id, request);
return request;
}
bool RequestClient::stop_request(Badge<Request>, Request& request)
{
if (!m_requests.contains(request.id()))
return false;
return send_sync<Messages::RequestServer::StopRequest>(request.id())->success();
}
bool RequestClient::set_certificate(Badge<Request>, Request& request, String certificate, String key)
{
if (!m_requests.contains(request.id()))
return false;
return send_sync<Messages::RequestServer::SetCertificate>(request.id(), move(certificate), move(key))->success();
}
void RequestClient::handle(const Messages::RequestClient::RequestFinished& message)
{
RefPtr<Request> request;
if ((request = m_requests.get(message.request_id()).value_or(nullptr))) {
request->did_finish({}, message.success(), message.total_size());
}
m_requests.remove(message.request_id());
}
void RequestClient::handle(const Messages::RequestClient::RequestProgress& message)
{
if (auto request = const_cast<Request*>(m_requests.get(message.request_id()).value_or(nullptr))) {
request->did_progress({}, message.total_size(), message.downloaded_size());
}
}
void RequestClient::handle(const Messages::RequestClient::HeadersBecameAvailable& message)
{
if (auto request = const_cast<Request*>(m_requests.get(message.request_id()).value_or(nullptr))) {
HashMap<String, String, CaseInsensitiveStringTraits> headers;
message.response_headers().for_each_entry([&](auto& name, auto& value) { headers.set(name, value); });
request->did_receive_headers({}, headers, message.status_code());
}
}
OwnPtr<Messages::RequestClient::CertificateRequestedResponse> RequestClient::handle(const Messages::RequestClient::CertificateRequested& message)
{
if (auto request = const_cast<Request*>(m_requests.get(message.request_id()).value_or(nullptr))) {
request->did_request_certificates({});
}
return make<Messages::RequestClient::CertificateRequestedResponse>();
}
}
template RefPtr<Protocol::Request> Protocol::RequestClient::start_request(const String& method, const String& url, const HashMap<String, String>& request_headers, ReadonlyBytes request_body);
template RefPtr<Protocol::Request> Protocol::RequestClient::start_request(const String& method, const String& url, const HashMap<String, String, CaseInsensitiveStringTraits>& request_headers, ReadonlyBytes request_body);

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <LibIPC/ServerConnection.h>
#include <RequestServer/RequestClientEndpoint.h>
#include <RequestServer/RequestServerEndpoint.h>
namespace Protocol {
class Request;
class RequestClient
: public IPC::ServerConnection<RequestClientEndpoint, RequestServerEndpoint>
, public RequestClientEndpoint {
C_OBJECT(RequestClient);
public:
virtual void handshake() override;
bool is_supported_protocol(const String&);
template<typename RequestHashMapTraits = Traits<String>>
RefPtr<Request> start_request(const String& method, const String& url, const HashMap<String, String, RequestHashMapTraits>& request_headers = {}, ReadonlyBytes request_body = {});
bool stop_request(Badge<Request>, Request&);
bool set_certificate(Badge<Request>, Request&, String, String);
private:
RequestClient();
virtual void handle(const Messages::RequestClient::RequestProgress&) override;
virtual void handle(const Messages::RequestClient::RequestFinished&) override;
virtual OwnPtr<Messages::RequestClient::CertificateRequestedResponse> handle(const Messages::RequestClient::CertificateRequested&) override;
virtual void handle(const Messages::RequestClient::HeadersBecameAvailable&) override;
HashMap<i32, RefPtr<Request>> m_requests;
};
}

View file

@ -225,8 +225,8 @@ set(SOURCES
)
set(GENERATED_SOURCES
../../Services/ProtocolServer/ProtocolClientEndpoint.h
../../Services/ProtocolServer/ProtocolServerEndpoint.h
../../Services/RequestServer/RequestClientEndpoint.h
../../Services/RequestServer/RequestServerEndpoint.h
../../Services/WebContent/WebContentClientEndpoint.h
../../Services/WebContent/WebContentServerEndpoint.h
)

View file

@ -9,8 +9,8 @@
#include <AK/JsonObject.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
#include <LibProtocol/Request.h>
#include <LibProtocol/RequestClient.h>
#include <LibWeb/Loader/ContentFilter.h>
#include <LibWeb/Loader/LoadRequest.h>
#include <LibWeb/Loader/Resource.h>
@ -27,7 +27,7 @@ ResourceLoader& ResourceLoader::the()
}
ResourceLoader::ResourceLoader()
: m_protocol_client(Protocol::Client::construct())
: m_protocol_client(Protocol::RequestClient::construct())
, m_user_agent(default_user_agent)
{
}
@ -156,13 +156,13 @@ void ResourceLoader::load(const LoadRequest& request, Function<void(ReadonlyByte
headers.set(it.key, it.value);
}
auto download = protocol_client().start_download(request.method(), url.to_string_encoded(), headers, request.body());
if (!download) {
auto protocol_request = protocol_client().start_request(request.method(), url.to_string_encoded(), headers, request.body());
if (!protocol_request) {
if (error_callback)
error_callback("Failed to initiate load", {});
return;
}
download->on_buffered_download_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback), download](bool success, auto, auto& response_headers, auto status_code, ReadonlyBytes payload) {
protocol_request->on_buffered_request_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback), protocol_request](bool success, auto, auto& response_headers, auto status_code, ReadonlyBytes payload) {
--m_pending_loads;
if (on_load_counter_change)
on_load_counter_change();
@ -171,14 +171,14 @@ void ResourceLoader::load(const LoadRequest& request, Function<void(ReadonlyByte
error_callback("HTTP load failed", {});
return;
}
deferred_invoke([download](auto&) {
// Clear circular reference of `download` captured by copy
const_cast<Protocol::Download&>(*download).on_buffered_download_finish = nullptr;
deferred_invoke([protocol_request](auto&) {
// Clear circular reference of `protocol_request` captured by copy
const_cast<Protocol::Request&>(*protocol_request).on_buffered_request_finish = nullptr;
});
success_callback(payload, response_headers, status_code);
};
download->set_should_buffer_all_input(true);
download->on_certificate_requested = []() -> Protocol::Download::CertificateAndKey {
protocol_request->set_should_buffer_all_input(true);
protocol_request->on_certificate_requested = []() -> Protocol::Request::CertificateAndKey {
return {};
};
++m_pending_loads;

View file

@ -12,7 +12,7 @@
#include <LibWeb/Loader/Resource.h>
namespace Protocol {
class Client;
class RequestClient;
}
namespace Web {
@ -34,7 +34,7 @@ public:
int pending_loads() const { return m_pending_loads; }
Protocol::Client& protocol_client() { return *m_protocol_client; }
Protocol::RequestClient& protocol_client() { return *m_protocol_client; }
const String& user_agent() const { return m_user_agent; }
void set_user_agent(const String& user_agent) { m_user_agent = user_agent; }
@ -47,7 +47,7 @@ private:
int m_pending_loads { 0 };
RefPtr<Protocol::Client> m_protocol_client;
RefPtr<Protocol::RequestClient> m_protocol_client;
String m_user_agent;
};