aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-09 13:58:37 +0200
committerStefan Boberg <[email protected]>2021-09-09 13:58:37 +0200
commit565f08ec3327c6cdad2eeb2fac1f45a1a24b95cf (patch)
treedfce4e0b8cbbafb416c9a7ce7e38a3d25fbdde48
parentMoved http.sys server implementation into dedicated source files (diff)
downloadzen-565f08ec3327c6cdad2eeb2fac1f45a1a24b95cf.tar.xz
zen-565f08ec3327c6cdad2eeb2fac1f45a1a24b95cf.zip
Made HttpServer an abstract interface, and moved remaining implementation specifics for http.sys into the dedicated cpp/h source files
-rw-r--r--zencore/httpserver.cpp97
-rw-r--r--zencore/httpsys.cpp36
-rw-r--r--zencore/httpsys.h31
-rw-r--r--zencore/include/zencore/httpserver.h22
-rw-r--r--zenserver/zenserver.cpp27
5 files changed, 81 insertions, 132 deletions
diff --git a/zencore/httpserver.cpp b/zencore/httpserver.cpp
index b40b74208..4b6bd1c0a 100644
--- a/zencore/httpserver.cpp
+++ b/zencore/httpserver.cpp
@@ -4,8 +4,6 @@
#include "httpsys.h"
-#include <conio.h>
-#include <new.h>
#include <zencore/compactbinary.h>
#include <zencore/compactbinarypackage.h>
#include <zencore/iobuffer.h>
@@ -14,22 +12,17 @@
#include <zencore/stream.h>
#include <zencore/string.h>
#include <zencore/thread.h>
+
+#include <conio.h>
+#include <new.h>
#include <charconv>
#include <span>
#include <string_view>
-#include <spdlog/spdlog.h>
-
#include <doctest/doctest.h>
-//////////////////////////////////////////////////////////////////////////
-
namespace zen {
-using namespace std::literals;
-
-//////////////////////////////////////////////////////////////////////////
-
HttpServerRequest::HttpServerRequest()
{
}
@@ -242,80 +235,6 @@ HttpServerRequest::ReadPayloadPackage()
//////////////////////////////////////////////////////////////////////////
-struct HttpServer::Impl : public RefCounted
-{
- WinIoThreadPool m_ThreadPool;
- HttpSysServer m_HttpServer;
-
- Impl(int ThreadCount) : m_ThreadPool(ThreadCount), m_HttpServer(m_ThreadPool) {}
-
- void Initialize(int BasePort)
- {
- using namespace std::literals;
-
- WideStringBuilder<64> BaseUri;
- BaseUri << u8"http://*:"sv << int64_t(BasePort) << u8"/"sv;
-
- m_HttpServer.Initialize(BaseUri.c_str());
- m_HttpServer.StartServer();
- }
-
- void Run(bool TestMode) { m_HttpServer.Run(TestMode); }
-
- void RequestExit() { m_HttpServer.RequestExit(); }
-
- void Cleanup() { m_HttpServer.StopServer(); }
-
- void AddEndpoint(const char* Endpoint, HttpService& Service) { m_HttpServer.AddEndpoint(Endpoint, Service); }
-
- void AddEndpoint([[maybe_unused]] const char* endpoint, [[maybe_unused]] std::function<void(HttpServerRequest&)> handler)
- {
- ZEN_NOT_IMPLEMENTED();
- }
-};
-
-HttpServer::HttpServer()
-{
- m_Impl = new Impl(32);
-}
-
-HttpServer::~HttpServer()
-{
- m_Impl->Cleanup();
-}
-
-void
-HttpServer::AddEndpoint(HttpService& Service)
-{
- m_Impl->AddEndpoint(Service.BaseUri(), Service);
-}
-
-void
-HttpServer::AddEndpoint(const char* endpoint, std::function<void(HttpServerRequest&)> handler)
-{
- m_Impl->AddEndpoint(endpoint, handler);
-}
-
-void
-HttpServer::Initialize(int BasePort)
-{
- m_Impl->Initialize(BasePort);
-}
-
-void
-HttpServer::Run(bool TestMode)
-{
- m_Impl->Run(TestMode);
-}
-
-void
-HttpServer::RequestExit()
-{
- m_Impl->RequestExit();
-}
-
-//////////////////////////////////////////////////////////////////////////
-
HttpServerException::HttpServerException(const char* Message, uint32_t Error) : m_ErrorCode(Error)
{
using namespace fmt::literals;
@@ -429,6 +348,16 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request)
return false; // No route matched
}
+//////////////////////////////////////////////////////////////////////////
+
+Ref<HttpServer>
+CreateHttpServer()
+{
+ return new HttpSysServer{32};
+}
+
+//////////////////////////////////////////////////////////////////////////
+
TEST_CASE("http")
{
using namespace std::literals;
diff --git a/zencore/httpsys.cpp b/zencore/httpsys.cpp
index 7a17d6b9c..0fc0c0ef6 100644
--- a/zencore/httpsys.cpp
+++ b/zencore/httpsys.cpp
@@ -621,7 +621,7 @@ HttpMessageResponseRequest::IssueRequest()
//////////////////////////////////////////////////////////////////////////
-HttpSysServer::HttpSysServer(WinIoThreadPool& InThreadPool) : m_ThreadPool(InThreadPool)
+HttpSysServer::HttpSysServer(int ThreadCount) : m_ThreadPool(ThreadCount)
{
ULONG Result = HttpInitialize(HTTPAPI_VERSION_2, HTTP_INITIALIZE_SERVER, nullptr);
@@ -784,11 +784,6 @@ HttpSysServer::IssueNewRequestMaybe()
}
void
-HttpSysServer::StopServer()
-{
-}
-
-void
HttpSysServer::AddEndpoint(const char* UrlPath, HttpService& Service)
{
if (UrlPath[0] == '/')
@@ -1221,6 +1216,35 @@ InitialRequestHandler::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesT
return new HttpMessageResponseRequest(Transaction(), 500, ex.what());
}
}
+
+//////////////////////////////////////////////////////////////////////////
+//
+// HttpServer interface implementation
+//
+
+void
+HttpSysServer::Initialize(int BasePort)
+{
+ using namespace std::literals;
+
+ WideStringBuilder<64> BaseUri;
+ BaseUri << u8"http://*:"sv << int64_t(BasePort) << u8"/"sv;
+
+ Initialize(BaseUri.c_str());
+ StartServer();
+}
+
+void
+HttpSysServer::RequestExit()
+{
+ m_ShutdownEvent.Set();
+}
+void
+HttpSysServer::AddEndpoint(HttpService& Service)
+{
+ AddEndpoint(Service.BaseUri(), Service);
+}
+
#endif // ZEN_PLATFORM_WINDOWS
} // namespace zen
diff --git a/zencore/httpsys.h b/zencore/httpsys.h
index 8a7b372dd..7c0543e45 100644
--- a/zencore/httpsys.h
+++ b/zencore/httpsys.h
@@ -13,25 +13,28 @@ namespace zen {
/**
* @brief Windows implementation of HTTP server based on http.sys
- *
+ *
* This requires elevation to function
*/
-class HttpSysServer
+class HttpSysServer : public HttpServer
{
friend class HttpSysTransaction;
public:
- HttpSysServer(WinIoThreadPool& InThreadPool);
+ explicit HttpSysServer(int ThreadCount);
~HttpSysServer();
- void Initialize(const wchar_t* UrlPath);
- void Run(bool TestMode);
+ // HttpServer interface implementation
- void RequestExit() { m_ShutdownEvent.Set(); }
+ virtual void Initialize(int BasePort) override;
+ virtual void Run(bool TestMode) override;
+ virtual void RequestExit() override;
+ virtual void AddEndpoint(HttpService& Service) override;
- void StartServer();
- void StopServer();
+private:
+ void Initialize(const wchar_t* UrlPath);
+ void StartServer();
void OnHandlingRequest();
void IssueNewRequestMaybe();
@@ -41,18 +44,18 @@ public:
void RemoveEndpoint(const char* Endpoint, HttpService& Service);
private:
- bool m_IsOk = false;
- bool m_IsHttpInitialized = false;
- WinIoThreadPool& m_ThreadPool;
+ bool m_IsOk = false;
+ bool m_IsHttpInitialized = false;
+ WinIoThreadPool m_ThreadPool;
std::wstring m_BaseUri; // http://*:nnnn/
HTTP_SERVER_SESSION_ID m_HttpSessionId = 0;
HTTP_URL_GROUP_ID m_HttpUrlGroupId = 0;
HANDLE m_RequestQueueHandle = 0;
std::atomic_int32_t m_PendingRequests{0};
- int32_t m_MinPendingRequests = 4;
- int32_t m_MaxPendingRequests = 32;
+ int32_t m_MinPendingRequests = 16;
+ int32_t m_MaxPendingRequests = 128;
Event m_ShutdownEvent;
};
-}
+} // namespace zen
diff --git a/zencore/include/zencore/httpserver.h b/zencore/include/zencore/httpserver.h
index a0be54665..4a8cef262 100644
--- a/zencore/include/zencore/httpserver.h
+++ b/zencore/include/zencore/httpserver.h
@@ -300,25 +300,17 @@ private:
* Implements the main event loop to service HTTP requests, and handles routing
* requests to the appropriate endpoint handler as registered via AddEndpoint
*/
-class HttpServer
+class HttpServer : public RefCounted
{
public:
- HttpServer();
- ~HttpServer();
-
- void AddEndpoint(const char* endpoint, std::function<void(HttpServerRequest&)> handler);
- void AddEndpoint(HttpService& Service);
-
- void Initialize(int BasePort);
- void Run(bool TestMode);
- void RequestExit();
-
-private:
- struct Impl;
-
- RefPtr<Impl> m_Impl;
+ virtual void AddEndpoint(HttpService& Service) = 0;
+ virtual void Initialize(int BasePort) = 0;
+ virtual void Run(bool TestMode) = 0;
+ virtual void RequestExit() = 0;
};
+Ref<HttpServer> CreateHttpServer();
+
//////////////////////////////////////////////////////////////////////////
class HttpRouterRequest
diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp
index 57e691ea1..db14e2c05 100644
--- a/zenserver/zenserver.cpp
+++ b/zenserver/zenserver.cpp
@@ -227,37 +227,38 @@ public:
spdlog::info("NOT starting mesh");
}
- m_Http.Initialize(BasePort);
- m_Http.AddEndpoint(m_HealthService);
+ m_Http = zen::CreateHttpServer();
+ m_Http->Initialize(BasePort);
+ m_Http->AddEndpoint(m_HealthService);
if (m_TestMode)
{
- m_Http.AddEndpoint(m_TestService);
- m_Http.AddEndpoint(m_TestingService);
+ m_Http->AddEndpoint(m_TestService);
+ m_Http->AddEndpoint(m_TestingService);
}
- m_Http.AddEndpoint(m_AdminService);
+ m_Http->AddEndpoint(m_AdminService);
if (m_HttpProjectService)
{
- m_Http.AddEndpoint(*m_HttpProjectService);
+ m_Http->AddEndpoint(*m_HttpProjectService);
}
- m_Http.AddEndpoint(m_CasService);
+ m_Http->AddEndpoint(m_CasService);
if (m_StructuredCacheService)
{
- m_Http.AddEndpoint(*m_StructuredCacheService);
+ m_Http->AddEndpoint(*m_StructuredCacheService);
}
if (m_HttpLaunchService)
{
- m_Http.AddEndpoint(*m_HttpLaunchService);
+ m_Http->AddEndpoint(*m_HttpLaunchService);
}
if (m_HttpFunctionService)
{
- m_Http.AddEndpoint(*m_HttpFunctionService);
+ m_Http->AddEndpoint(*m_HttpFunctionService);
}
}
@@ -295,7 +296,7 @@ public:
__debugbreak();
}
- m_Http.Run(m_TestMode);
+ m_Http->Run(m_TestMode);
spdlog::info(ZEN_APP_NAME " exiting");
@@ -307,7 +308,7 @@ public:
void RequestExit(int ExitCode)
{
RequestApplicationExit(ExitCode);
- m_Http.RequestExit();
+ m_Http->RequestExit();
}
void Cleanup() { spdlog::info(ZEN_APP_NAME " cleaning up"); }
@@ -369,7 +370,7 @@ private:
zen::ProcessHandle m_Process;
zen::NamedMutex m_ServerMutex;
- zen::HttpServer m_Http;
+ zen::Ref<zen::HttpServer> m_Http;
std::unique_ptr<zen::CasStore> m_CasStore{zen::CreateCasStore()};
std::unique_ptr<zen::CidStore> m_CidStore;
std::unique_ptr<ZenCacheStore> m_CacheStore;