diff --git a/Ladybird/Android/src/main/cpp/RequestServerService.cpp b/Ladybird/Android/src/main/cpp/RequestServerService.cpp index b3e11f8ed4..b900176d9e 100644 --- a/Ladybird/Android/src/main/cpp/RequestServerService.cpp +++ b/Ladybird/Android/src/main/cpp/RequestServerService.cpp @@ -37,10 +37,9 @@ ErrorOr service_main(int ipc_socket, int fd_passing_socket) Core::EventLoop event_loop; - // FIXME: Don't leak these :V - [[maybe_unused]] auto* gemini = new RequestServer::GeminiProtocol; - [[maybe_unused]] auto* http = new RequestServer::HttpProtocol; - [[maybe_unused]] auto* https = new RequestServer::HttpsProtocol; + RequestServer::GeminiProtocol::install(); + RequestServer::HttpProtocol::install(); + RequestServer::HttpsProtocol::install(); auto socket = TRY(Core::LocalSocket::adopt_fd(ipc_socket)); auto client = TRY(RequestServer::ConnectionFromClient::try_create(move(socket))); diff --git a/Ladybird/RequestServer/main.cpp b/Ladybird/RequestServer/main.cpp index 6f1a834dc2..0678a0e7bb 100644 --- a/Ladybird/RequestServer/main.cpp +++ b/Ladybird/RequestServer/main.cpp @@ -51,17 +51,12 @@ ErrorOr serenity_main(Main::Arguments arguments) Core::EventLoop event_loop; - [[maybe_unused]] auto gemini = make(); - [[maybe_unused]] auto http = make(); - [[maybe_unused]] auto https = make(); + RequestServer::GeminiProtocol::install(); + RequestServer::HttpProtocol::install(); + RequestServer::HttpsProtocol::install(); auto client = TRY(IPC::take_over_accepted_client_from_system_server()); client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket))); - auto result = event_loop.exec(); - - // FIXME: We exit instead of returning, so that protocol destructors don't get called. - // The Protocol base class should probably do proper de-registration instead of - // just VERIFY_NOT_REACHED(). - exit(result); + return event_loop.exec(); } diff --git a/Userland/Services/RequestServer/GeminiProtocol.cpp b/Userland/Services/RequestServer/GeminiProtocol.cpp index d29f94a1de..99b67daba8 100644 --- a/Userland/Services/RequestServer/GeminiProtocol.cpp +++ b/Userland/Services/RequestServer/GeminiProtocol.cpp @@ -38,4 +38,9 @@ OwnPtr GeminiProtocol::start_request(i32 request_id, ConnectionFromClie return protocol_request; } +void GeminiProtocol::install() +{ + Protocol::install(adopt_own(*new GeminiProtocol())); +} + } diff --git a/Userland/Services/RequestServer/GeminiProtocol.h b/Userland/Services/RequestServer/GeminiProtocol.h index 421e4bba49..fff3142b59 100644 --- a/Userland/Services/RequestServer/GeminiProtocol.h +++ b/Userland/Services/RequestServer/GeminiProtocol.h @@ -12,9 +12,13 @@ namespace RequestServer { class GeminiProtocol final : public Protocol { public: - GeminiProtocol(); virtual ~GeminiProtocol() override = default; + static void install(); + +private: + GeminiProtocol(); + virtual OwnPtr start_request(i32, ConnectionFromClient&, ByteString const& method, const URL&, HashMap const&, ReadonlyBytes body, Core::ProxyData proxy_data = {}) override; }; diff --git a/Userland/Services/RequestServer/HttpProtocol.cpp b/Userland/Services/RequestServer/HttpProtocol.cpp index 5ffe3f00af..726deb9c0c 100644 --- a/Userland/Services/RequestServer/HttpProtocol.cpp +++ b/Userland/Services/RequestServer/HttpProtocol.cpp @@ -27,4 +27,9 @@ OwnPtr HttpProtocol::start_request(i32 request_id, ConnectionFromClient return Detail::start_request(Badge {}, request_id, client, method, url, headers, body, get_pipe_for_request(), proxy_data); } +void HttpProtocol::install() +{ + Protocol::install(adopt_own(*new HttpProtocol())); +} + } diff --git a/Userland/Services/RequestServer/HttpProtocol.h b/Userland/Services/RequestServer/HttpProtocol.h index bfa9d8064c..c868b78b42 100644 --- a/Userland/Services/RequestServer/HttpProtocol.h +++ b/Userland/Services/RequestServer/HttpProtocol.h @@ -24,9 +24,13 @@ public: using JobType = HTTP::Job; using RequestType = HttpRequest; - HttpProtocol(); ~HttpProtocol() override = default; + static void install(); + +private: + HttpProtocol(); + virtual OwnPtr start_request(i32, ConnectionFromClient&, ByteString const& method, const URL&, HashMap const& headers, ReadonlyBytes body, Core::ProxyData proxy_data = {}) override; }; diff --git a/Userland/Services/RequestServer/HttpsProtocol.cpp b/Userland/Services/RequestServer/HttpsProtocol.cpp index 49c017132f..3524465e45 100644 --- a/Userland/Services/RequestServer/HttpsProtocol.cpp +++ b/Userland/Services/RequestServer/HttpsProtocol.cpp @@ -27,4 +27,9 @@ OwnPtr HttpsProtocol::start_request(i32 request_id, ConnectionFromClien return Detail::start_request(Badge {}, request_id, client, method, url, headers, body, get_pipe_for_request(), proxy_data); } +void HttpsProtocol::install() +{ + Protocol::install(adopt_own(*new HttpsProtocol())); +} + } diff --git a/Userland/Services/RequestServer/HttpsProtocol.h b/Userland/Services/RequestServer/HttpsProtocol.h index 00afb03455..34c7350ade 100644 --- a/Userland/Services/RequestServer/HttpsProtocol.h +++ b/Userland/Services/RequestServer/HttpsProtocol.h @@ -24,9 +24,13 @@ public: using JobType = HTTP::HttpsJob; using RequestType = HttpsRequest; - HttpsProtocol(); ~HttpsProtocol() override = default; + static void install(); + +private: + HttpsProtocol(); + virtual OwnPtr start_request(i32, ConnectionFromClient&, ByteString const& method, const URL&, HashMap const& headers, ReadonlyBytes body, Core::ProxyData proxy_data = {}) override; }; diff --git a/Userland/Services/RequestServer/Protocol.cpp b/Userland/Services/RequestServer/Protocol.cpp index a50496b8aa..552ae2bef4 100644 --- a/Userland/Services/RequestServer/Protocol.cpp +++ b/Userland/Services/RequestServer/Protocol.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -13,26 +14,20 @@ namespace RequestServer { -static HashMap& all_protocols() +static HashMap>& all_protocols() { - static HashMap map; + static HashMap> map; return map; } Protocol* Protocol::find_by_name(ByteString const& name) { - return all_protocols().get(name).value_or(nullptr); + return all_protocols().get(name).map([](auto& p) -> Protocol* { return p; }).value_or(nullptr); } Protocol::Protocol(ByteString const& name) + : m_name(name) { - all_protocols().set(name, this); -} - -Protocol::~Protocol() -{ - // FIXME: Do proper de-registration. - VERIFY_NOT_REACHED(); } ErrorOr Protocol::get_pipe_for_request() @@ -47,4 +42,10 @@ ErrorOr Protocol::get_pipe_for_request() return Pipe { fd_pair[0], fd_pair[1] }; } +void Protocol::install(NonnullOwnPtr protocol) +{ + auto name = protocol->name(); + all_protocols().set(move(name), move(protocol)); +} + } diff --git a/Userland/Services/RequestServer/Protocol.h b/Userland/Services/RequestServer/Protocol.h index 7379564349..0637a6810b 100644 --- a/Userland/Services/RequestServer/Protocol.h +++ b/Userland/Services/RequestServer/Protocol.h @@ -15,7 +15,7 @@ namespace RequestServer { class Protocol { public: - virtual ~Protocol(); + virtual ~Protocol() = default; ByteString const& name() const { return m_name; } virtual OwnPtr start_request(i32, ConnectionFromClient&, ByteString const& method, const URL&, HashMap const& headers, ReadonlyBytes body, Core::ProxyData proxy_data = {}) = 0; @@ -30,6 +30,8 @@ protected: }; static ErrorOr get_pipe_for_request(); + static void install(NonnullOwnPtr); + private: ByteString m_name; }; diff --git a/Userland/Services/RequestServer/main.cpp b/Userland/Services/RequestServer/main.cpp index f2bbe7e50e..17b2234f86 100644 --- a/Userland/Services/RequestServer/main.cpp +++ b/Userland/Services/RequestServer/main.cpp @@ -45,16 +45,11 @@ ErrorOr serenity_main(Main::Arguments) TRY(Core::System::unveil("/home/anon", "rwc")); TRY(Core::System::unveil(nullptr, nullptr)); - [[maybe_unused]] auto gemini = make(); - [[maybe_unused]] auto http = make(); - [[maybe_unused]] auto https = make(); + RequestServer::GeminiProtocol::install(); + RequestServer::HttpProtocol::install(); + RequestServer::HttpsProtocol::install(); auto client = TRY(IPC::take_over_accepted_client_from_system_server()); - auto result = event_loop.exec(); - - // FIXME: We exit instead of returning, so that protocol destructors don't get called. - // The Protocol base class should probably do proper de-registration instead of - // just VERIFY_NOT_REACHED(). - exit(result); + return event_loop.exec(); }