ProtocolServer: Put everything in the ProtocolServer namespace

This commit is contained in:
Andreas Kling 2020-05-17 16:33:09 +02:00
parent 2949c3e5e1
commit a4902e0eec
21 changed files with 158 additions and 56 deletions

View file

@ -2,6 +2,7 @@ compile_ipc(ProtocolServer.ipc ProtocolServerEndpoint.h)
compile_ipc(ProtocolClient.ipc ProtocolClientEndpoint.h)
set(SOURCES
ClientConnection.cpp
Download.cpp
GeminiDownload.cpp
GeminiProtocol.cpp
@ -11,7 +12,6 @@ set(SOURCES
HttpsProtocol.cpp
main.cpp
Protocol.cpp
PSClientConnection.cpp
ProtocolServerEndpoint.h
ProtocolClientEndpoint.h
)

View file

@ -27,34 +27,36 @@
#include <AK/Badge.h>
#include <AK/SharedBuffer.h>
#include <ProtocolServer/Download.h>
#include <ProtocolServer/PSClientConnection.h>
#include <ProtocolServer/ClientConnection.h>
#include <ProtocolServer/Protocol.h>
#include <ProtocolServer/ProtocolClientEndpoint.h>
static HashMap<int, RefPtr<PSClientConnection>> s_connections;
namespace ProtocolServer {
PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id)
static HashMap<int, RefPtr<ClientConnection>> s_connections;
ClientConnection::ClientConnection(Core::LocalSocket& socket, int client_id)
: IPC::ClientConnection<ProtocolServerEndpoint>(*this, socket, client_id)
{
s_connections.set(client_id, *this);
}
PSClientConnection::~PSClientConnection()
ClientConnection::~ClientConnection()
{
}
void PSClientConnection::die()
void ClientConnection::die()
{
s_connections.remove(client_id());
}
OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> PSClientConnection::handle(const Messages::ProtocolServer::IsSupportedProtocol& message)
OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> ClientConnection::handle(const Messages::ProtocolServer::IsSupportedProtocol& message)
{
bool supported = Protocol::find_by_name(message.protocol().to_lowercase());
return make<Messages::ProtocolServer::IsSupportedProtocolResponse>(supported);
}
OwnPtr<Messages::ProtocolServer::StartDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StartDownload& message)
OwnPtr<Messages::ProtocolServer::StartDownloadResponse> ClientConnection::handle(const Messages::ProtocolServer::StartDownload& message)
{
URL url(message.url());
if (!url.is_valid())
@ -70,7 +72,7 @@ OwnPtr<Messages::ProtocolServer::StartDownloadResponse> PSClientConnection::hand
return make<Messages::ProtocolServer::StartDownloadResponse>(id);
}
OwnPtr<Messages::ProtocolServer::StopDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StopDownload& message)
OwnPtr<Messages::ProtocolServer::StopDownloadResponse> ClientConnection::handle(const Messages::ProtocolServer::StopDownload& message)
{
auto* download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr));
bool success = false;
@ -81,7 +83,7 @@ OwnPtr<Messages::ProtocolServer::StopDownloadResponse> PSClientConnection::handl
return make<Messages::ProtocolServer::StopDownloadResponse>(success);
}
void PSClientConnection::did_finish_download(Badge<Download>, Download& download, bool success)
void ClientConnection::did_finish_download(Badge<Download>, Download& download, bool success)
{
RefPtr<SharedBuffer> buffer;
if (success && download.payload().size() > 0 && !download.payload().is_null()) {
@ -101,18 +103,20 @@ void PSClientConnection::did_finish_download(Badge<Download>, Download& download
m_downloads.remove(download.id());
}
void PSClientConnection::did_progress_download(Badge<Download>, Download& download)
void ClientConnection::did_progress_download(Badge<Download>, Download& download)
{
post_message(Messages::ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size()));
}
OwnPtr<Messages::ProtocolServer::GreetResponse> PSClientConnection::handle(const Messages::ProtocolServer::Greet&)
OwnPtr<Messages::ProtocolServer::GreetResponse> ClientConnection::handle(const Messages::ProtocolServer::Greet&)
{
return make<Messages::ProtocolServer::GreetResponse>(client_id());
}
OwnPtr<Messages::ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const Messages::ProtocolServer::DisownSharedBuffer& message)
OwnPtr<Messages::ProtocolServer::DisownSharedBufferResponse> ClientConnection::handle(const Messages::ProtocolServer::DisownSharedBuffer& message)
{
m_shared_buffers.remove(message.shbuf_id());
return make<Messages::ProtocolServer::DisownSharedBufferResponse>();
}
}

View file

@ -29,15 +29,18 @@
#include <AK/HashMap.h>
#include <LibIPC/ClientConnection.h>
#include <ProtocolServer/ProtocolServerEndpoint.h>
#include <ProtocolServer/Forward.h>
class Download;
namespace ProtocolServer {
class PSClientConnection final : public IPC::ClientConnection<ProtocolServerEndpoint>
class ClientConnection final
: public IPC::ClientConnection<ProtocolServerEndpoint>
, public ProtocolServerEndpoint {
C_OBJECT(PSClientConnection)
C_OBJECT(ClientConnection);
public:
explicit PSClientConnection(Core::LocalSocket&, int client_id);
~PSClientConnection() override;
explicit ClientConnection(Core::LocalSocket&, int client_id);
~ClientConnection() override;
virtual void die() override;
@ -54,3 +57,5 @@ private:
HashMap<i32, OwnPtr<Download>> m_downloads;
HashMap<i32, RefPtr<AK::SharedBuffer>> m_shared_buffers;
};
}

View file

@ -26,12 +26,14 @@
#include <AK/Badge.h>
#include <ProtocolServer/Download.h>
#include <ProtocolServer/PSClientConnection.h>
#include <ProtocolServer/ClientConnection.h>
namespace ProtocolServer {
// FIXME: What about rollover?
static i32 s_next_id = 1;
Download::Download(PSClientConnection& client)
Download::Download(ClientConnection& client)
: m_client(client)
, m_id(s_next_id++)
{
@ -68,3 +70,5 @@ void Download::did_progress(Optional<u32> total_size, u32 downloaded_size)
m_downloaded_size = downloaded_size;
m_client.did_progress_download({}, *this);
}
}

View file

@ -31,8 +31,9 @@
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/URL.h>
#include <ProtocolServer/Forward.h>
class PSClientConnection;
namespace ProtocolServer {
class Download {
public:
@ -49,7 +50,7 @@ public:
void stop();
protected:
explicit Download(PSClientConnection&);
explicit Download(ClientConnection&);
void did_finish(bool success);
void did_progress(Optional<u32> total_size, u32 downloaded_size);
@ -57,7 +58,7 @@ protected:
void set_response_headers(const HashMap<String, String, CaseInsensitiveStringTraits>&);
private:
PSClientConnection& m_client;
ClientConnection& m_client;
i32 m_id { 0 };
URL m_url;
Optional<u32> m_total_size {};
@ -65,3 +66,5 @@ private:
ByteBuffer m_payload;
HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
};
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace ProtocolServer {
class ClientConnection;
class Download;
class GeminiProtocol;
class HttpProtocol;
class HttpsProtocol;
class Protocol;
}

View file

@ -24,11 +24,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGemini/GeminiResponse.h>
#include <LibGemini/GeminiJob.h>
#include <LibGemini/GeminiResponse.h>
#include <ProtocolServer/GeminiDownload.h>
GeminiDownload::GeminiDownload(PSClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job)
namespace ProtocolServer {
GeminiDownload::GeminiDownload(ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job)
: Download(client)
, m_job(job)
{
@ -60,7 +62,9 @@ GeminiDownload::~GeminiDownload()
m_job->shutdown();
}
NonnullOwnPtr<GeminiDownload> GeminiDownload::create_with_job(Badge<GeminiProtocol>, PSClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job)
NonnullOwnPtr<GeminiDownload> GeminiDownload::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job)
{
return adopt_own(*new GeminiDownload(client, move(job)));
}
}

View file

@ -28,18 +28,20 @@
#include <AK/Badge.h>
#include <LibCore/Forward.h>
#include <LibGemini/GeminiJob.h>
#include <LibGemini/Forward.h>
#include <ProtocolServer/Download.h>
class GeminiProtocol;
namespace ProtocolServer {
class GeminiDownload final : public Download {
public:
virtual ~GeminiDownload() override;
static NonnullOwnPtr<GeminiDownload> create_with_job(Badge<GeminiProtocol>, PSClientConnection&, NonnullRefPtr<Gemini::GeminiJob>);
static NonnullOwnPtr<GeminiDownload> create_with_job(Badge<GeminiProtocol>, ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>);
private:
explicit GeminiDownload(PSClientConnection&, NonnullRefPtr<Gemini::GeminiJob>);
explicit GeminiDownload(ClientConnection&, NonnullRefPtr<Gemini::GeminiJob>);
NonnullRefPtr<Gemini::GeminiJob> m_job;
};
}

View file

@ -24,11 +24,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGemini/GeminiRequest.h>
#include <LibGemini/GeminiJob.h>
#include <LibGemini/GeminiRequest.h>
#include <ProtocolServer/GeminiDownload.h>
#include <ProtocolServer/GeminiProtocol.h>
namespace ProtocolServer {
GeminiProtocol::GeminiProtocol()
: Protocol("gemini")
{
@ -38,7 +40,7 @@ GeminiProtocol::~GeminiProtocol()
{
}
OwnPtr<Download> GeminiProtocol::start_download(PSClientConnection& client, const URL& url)
OwnPtr<Download> GeminiProtocol::start_download(ClientConnection& client, const URL& url)
{
Gemini::GeminiRequest request;
request.set_url(url);
@ -47,3 +49,5 @@ OwnPtr<Download> GeminiProtocol::start_download(PSClientConnection& client, cons
job->start();
return download;
}
}

View file

@ -28,10 +28,14 @@
#include <ProtocolServer/Protocol.h>
namespace ProtocolServer {
class GeminiProtocol final : public Protocol {
public:
GeminiProtocol();
virtual ~GeminiProtocol() override;
virtual OwnPtr<Download> start_download(PSClientConnection&, const URL&) override;
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
};
}

View file

@ -28,7 +28,9 @@
#include <LibHTTP/HttpResponse.h>
#include <ProtocolServer/HttpDownload.h>
HttpDownload::HttpDownload(PSClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job)
namespace ProtocolServer {
HttpDownload::HttpDownload(ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job)
: Download(client)
, m_job(job)
{
@ -57,7 +59,9 @@ HttpDownload::~HttpDownload()
m_job->shutdown();
}
NonnullOwnPtr<HttpDownload> HttpDownload::create_with_job(Badge<HttpProtocol>, PSClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job)
NonnullOwnPtr<HttpDownload> HttpDownload::create_with_job(Badge<HttpProtocol>, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job)
{
return adopt_own(*new HttpDownload(client, move(job)));
}
}

View file

@ -28,18 +28,20 @@
#include <AK/Badge.h>
#include <LibCore/Forward.h>
#include <LibHTTP/HttpJob.h>
#include <LibHTTP/Forward.h>
#include <ProtocolServer/Download.h>
class HttpProtocol;
namespace ProtocolServer {
class HttpDownload final : public Download {
public:
virtual ~HttpDownload() override;
static NonnullOwnPtr<HttpDownload> create_with_job(Badge<HttpProtocol>, PSClientConnection&, NonnullRefPtr<HTTP::HttpJob>);
static NonnullOwnPtr<HttpDownload> create_with_job(Badge<HttpProtocol>, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>);
private:
explicit HttpDownload(PSClientConnection&, NonnullRefPtr<HTTP::HttpJob>);
explicit HttpDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>);
NonnullRefPtr<HTTP::HttpJob> m_job;
};
}

View file

@ -29,6 +29,8 @@
#include <ProtocolServer/HttpDownload.h>
#include <ProtocolServer/HttpProtocol.h>
namespace ProtocolServer {
HttpProtocol::HttpProtocol()
: Protocol("http")
{
@ -38,7 +40,7 @@ HttpProtocol::~HttpProtocol()
{
}
OwnPtr<Download> HttpProtocol::start_download(PSClientConnection& client, const URL& url)
OwnPtr<Download> HttpProtocol::start_download(ClientConnection& client, const URL& url)
{
HTTP::HttpRequest request;
request.set_method(HTTP::HttpRequest::Method::GET);
@ -48,3 +50,5 @@ OwnPtr<Download> HttpProtocol::start_download(PSClientConnection& client, const
return nullptr;
return HttpDownload::create_with_job({}, client, (HTTP::HttpJob&)*job);
}
}

View file

@ -28,10 +28,14 @@
#include <ProtocolServer/Protocol.h>
namespace ProtocolServer {
class HttpProtocol final : public Protocol {
public:
HttpProtocol();
virtual ~HttpProtocol() override;
virtual OwnPtr<Download> start_download(PSClientConnection&, const URL&) override;
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
};
}

View file

@ -28,7 +28,9 @@
#include <LibHTTP/HttpsJob.h>
#include <ProtocolServer/HttpsDownload.h>
HttpsDownload::HttpsDownload(PSClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job)
namespace ProtocolServer {
HttpsDownload::HttpsDownload(ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job)
: Download(client)
, m_job(job)
{
@ -57,7 +59,9 @@ HttpsDownload::~HttpsDownload()
m_job->shutdown();
}
NonnullOwnPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>, PSClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job)
NonnullOwnPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job)
{
return adopt_own(*new HttpsDownload(client, move(job)));
}
}

View file

@ -31,15 +31,17 @@
#include <LibHTTP/HttpsJob.h>
#include <ProtocolServer/Download.h>
class HttpsProtocol;
namespace ProtocolServer {
class HttpsDownload final : public Download {
public:
virtual ~HttpsDownload() override;
static NonnullOwnPtr<HttpsDownload> create_with_job(Badge<HttpsProtocol>, PSClientConnection&, NonnullRefPtr<HTTP::HttpsJob>);
static NonnullOwnPtr<HttpsDownload> create_with_job(Badge<HttpsProtocol>, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>);
private:
explicit HttpsDownload(PSClientConnection&, NonnullRefPtr<HTTP::HttpsJob>);
explicit HttpsDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>);
NonnullRefPtr<HTTP::HttpsJob> m_job;
};
}

View file

@ -29,6 +29,8 @@
#include <ProtocolServer/HttpsDownload.h>
#include <ProtocolServer/HttpsProtocol.h>
namespace ProtocolServer {
HttpsProtocol::HttpsProtocol()
: Protocol("https")
{
@ -38,7 +40,7 @@ HttpsProtocol::~HttpsProtocol()
{
}
OwnPtr<Download> HttpsProtocol::start_download(PSClientConnection& client, const URL& url)
OwnPtr<Download> HttpsProtocol::start_download(ClientConnection& client, const URL& url)
{
HTTP::HttpRequest request;
request.set_method(HTTP::HttpRequest::Method::GET);
@ -48,3 +50,5 @@ OwnPtr<Download> HttpsProtocol::start_download(PSClientConnection& client, const
job->start();
return download;
}
}

View file

@ -28,10 +28,14 @@
#include <ProtocolServer/Protocol.h>
namespace ProtocolServer {
class HttpsProtocol final : public Protocol {
public:
HttpsProtocol();
virtual ~HttpsProtocol() override;
virtual OwnPtr<Download> start_download(PSClientConnection&, const URL&) override;
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) override;
};
}

View file

@ -27,6 +27,8 @@
#include <AK/HashMap.h>
#include <ProtocolServer/Protocol.h>
namespace ProtocolServer {
static HashMap<String, Protocol*>& all_protocols()
{
static HashMap<String, Protocol*> map;
@ -47,3 +49,5 @@ Protocol::~Protocol()
{
ASSERT_NOT_REACHED();
}
}

View file

@ -28,16 +28,16 @@
#include <AK/RefPtr.h>
#include <AK/URL.h>
#include <ProtocolServer/Forward.h>
class Download;
class PSClientConnection;
namespace ProtocolServer {
class Protocol {
public:
virtual ~Protocol();
const String& name() const { return m_name; }
virtual OwnPtr<Download> start_download(PSClientConnection&, const URL&) = 0;
virtual OwnPtr<Download> start_download(ClientConnection&, const URL&) = 0;
static Protocol* find_by_name(const String&);
@ -47,3 +47,5 @@ protected:
private:
String m_name;
};
}

View file

@ -30,7 +30,7 @@
#include <ProtocolServer/GeminiProtocol.h>
#include <ProtocolServer/HttpProtocol.h>
#include <ProtocolServer/HttpsProtocol.h>
#include <ProtocolServer/PSClientConnection.h>
#include <ProtocolServer/ClientConnection.h>
int main(int, char**)
{
@ -53,9 +53,9 @@ int main(int, char**)
return 1;
}
(void)*new GeminiProtocol;
(void)*new HttpProtocol;
(void)*new HttpsProtocol;
(void)*new ProtocolServer::GeminiProtocol;
(void)*new ProtocolServer::HttpProtocol;
(void)*new ProtocolServer::HttpsProtocol;
auto server = Core::LocalServer::construct();
bool ok = server->take_over_from_system_server();
ASSERT(ok);
@ -67,7 +67,7 @@ int main(int, char**)
}
static int s_next_client_id = 0;
int client_id = ++s_next_client_id;
IPC::new_client_connection<PSClientConnection>(*client_socket, client_id);
IPC::new_client_connection<ProtocolServer::ClientConnection>(*client_socket, client_id);
};
return event_loop.exec();
}