From a73862b954209fd1adce0f07b8f80b8cf27f2486 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 9 Sep 2021 16:29:24 +0200 Subject: Added compile time logic to toggle http.sys / null http implementation on/off --- zenhttp/httpnull.cpp | 65 ++++++++++++++++++++++++++++++++++++ zenhttp/httpnull.h | 27 +++++++++++++++ zenhttp/httpserver.cpp | 5 +++ zenhttp/httpsys.cpp | 19 +++++------ zenhttp/httpsys.h | 20 ++++++++--- zenhttp/include/zenhttp/httpserver.h | 15 ++++----- zenhttp/zenhttp.vcxproj | 3 ++ zenhttp/zenhttp.vcxproj.filters | 3 ++ 8 files changed, 133 insertions(+), 24 deletions(-) create mode 100644 zenhttp/httpnull.cpp create mode 100644 zenhttp/httpnull.h diff --git a/zenhttp/httpnull.cpp b/zenhttp/httpnull.cpp new file mode 100644 index 000000000..571c4241c --- /dev/null +++ b/zenhttp/httpnull.cpp @@ -0,0 +1,65 @@ +#include "httpnull.h" + +#include +#include + +namespace zen { + +HttpNullServer::HttpNullServer() +{ +} + +HttpNullServer::~HttpNullServer() +{ +} + +void +HttpNullServer::RegisterService(HttpService& Service) +{ + ZEN_UNUSED(Service); +} + +void +HttpNullServer::Initialize(int BasePort) +{ + ZEN_UNUSED(BasePort); +} + +void +HttpNullServer::Run(bool TestMode) +{ + if (TestMode == false) + { + zen::logging::ConsoleLog().info("Zen Server running (null HTTP). Press ESC or Q to quit"); + } + + do + { + int WaitTimeout = -1; + + if (!TestMode) + { + WaitTimeout = 1000; + } + + if (!TestMode && _kbhit() != 0) + { + char c = (char)_getch(); + + if (c == 27 || c == 'Q' || c == 'q') + { + RequestApplicationExit(0); + } + } + + m_ShutdownEvent.Wait(WaitTimeout); + } while (!IsApplicationExitRequested()); +} + +void +HttpNullServer::RequestExit() +{ + m_ShutdownEvent.Set(); +} + +} // namespace zen diff --git a/zenhttp/httpnull.h b/zenhttp/httpnull.h new file mode 100644 index 000000000..9cec33c98 --- /dev/null +++ b/zenhttp/httpnull.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +namespace zen { + +/** + * @brief Null implementation of "http" server. Does nothing +*/ + +class HttpNullServer : public HttpServer +{ +public: + HttpNullServer(); + ~HttpNullServer(); + + virtual void RegisterService(HttpService& Service) override; + virtual void Initialize(int BasePort) override; + virtual void Run(bool TestMode) override; + virtual void RequestExit() override; + +private: + Event m_ShutdownEvent; +}; + +} // namespace zen diff --git a/zenhttp/httpserver.cpp b/zenhttp/httpserver.cpp index a0b17fe44..3999b3b6c 100644 --- a/zenhttp/httpserver.cpp +++ b/zenhttp/httpserver.cpp @@ -3,6 +3,7 @@ #include #include "httpsys.h" +#include "httpnull.h" #include #include @@ -353,7 +354,11 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) Ref CreateHttpServer() { +#if ZEN_WITH_HTTPSYS return new HttpSysServer{32}; +#else + return new HttpNullServer; +#endif } ////////////////////////////////////////////////////////////////////////// diff --git a/zenhttp/httpsys.cpp b/zenhttp/httpsys.cpp index da07a13dd..2041be5c3 100644 --- a/zenhttp/httpsys.cpp +++ b/zenhttp/httpsys.cpp @@ -5,11 +5,10 @@ #include #include -#include +#if ZEN_WITH_HTTPSYS -#if ZEN_PLATFORM_WINDOWS -# pragma comment(lib, "httpapi.lib") -#endif +#include +#pragma comment(lib, "httpapi.lib") std::wstring UTF8_to_wstring(const char* in) @@ -246,7 +245,6 @@ ReasonStringForHttpResultCode(int HttpCode) } } -#if ZEN_PLATFORM_WINDOWS class HttpSysServer; class HttpSysTransaction; class HttpMessageResponseRequest; @@ -848,22 +846,22 @@ HttpSysServerRequest::HttpSysServerRequest(HttpSysTransaction& Tx, HttpService& // we just have to live with it WideToUtf8({(char16_t*)HttpRequestPtr->CookedUrl.pAbsPath + PrefixLength, gsl::narrow(AbsPathLength - PrefixLength)}, - m_Uri); + m_UriUtf8); } else { - m_Uri.Reset(); + m_UriUtf8.Reset(); } if (auto QueryStringLength = HttpRequestPtr->CookedUrl.QueryStringLength) { --QueryStringLength; - WideToUtf8({(char16_t*)(HttpRequestPtr->CookedUrl.pQueryString) + 1, QueryStringLength / sizeof(char16_t)}, m_QueryString); + WideToUtf8({(char16_t*)(HttpRequestPtr->CookedUrl.pQueryString) + 1, QueryStringLength / sizeof(char16_t)}, m_QueryStringUtf8); } else { - m_QueryString.Reset(); + m_QueryStringUtf8.Reset(); } switch (HttpRequestPtr->Verb) @@ -1245,6 +1243,5 @@ HttpSysServer::RegisterService(HttpService& Service) RegisterService(Service.BaseUri(), Service); } -#endif // ZEN_PLATFORM_WINDOWS - } // namespace zen +#endif \ No newline at end of file diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index ed4a6a182..ec8964f13 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -2,12 +2,21 @@ #include -#define _WINSOCKAPI_ -#include -#include "iothreadpool.h" +#ifndef ZEN_WITH_HTTPSYS +# if ZEN_PLATFORM_WINDOWS +# define ZEN_WITH_HTTPSYS 1 +#else +# define ZEN_WITH_HTTPSYS 0 +# endif +#endif -#include -#include +#if ZEN_WITH_HTTPSYS +# define _WINSOCKAPI_ +# include +# include "iothreadpool.h" + +# include +# include namespace zen { @@ -59,3 +68,4 @@ private: }; } // namespace zen +#endif diff --git a/zenhttp/include/zenhttp/httpserver.h b/zenhttp/include/zenhttp/httpserver.h index 95824b8f4..d9637dff5 100644 --- a/zenhttp/include/zenhttp/httpserver.h +++ b/zenhttp/include/zenhttp/httpserver.h @@ -175,8 +175,8 @@ public: // Synchronous operations - [[nodiscard]] inline std::string_view RelativeUri() const { return m_Uri; } // Returns URI without service prefix - [[nodiscard]] inline std::string_view QueryString() const { return m_QueryString; } + [[nodiscard]] inline std::string_view RelativeUri() const { return m_UriUtf8; } // Returns URI without service prefix + [[nodiscard]] inline std::string_view QueryString() const { return m_QueryStringUtf8; } inline bool IsHandled() const { return m_IsHandled; } struct QueryParams @@ -238,15 +238,14 @@ public: Note that this is destructive in the sense that the IoBuffer instances referred to by Blobs will be moved into our response handler array where they are kept alive, in order to reduce ref-counting storms */ - virtual void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, std::span Blobs) = 0; - virtual void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, IoBuffer Blob); - virtual void WriteResponse(HttpResponse HttpResponseCode) = 0; - + virtual void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, std::span Blobs) = 0; + virtual void WriteResponse(HttpResponse HttpResponseCode) = 0; virtual void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, std::u8string_view ResponseString) = 0; void WriteResponse(HttpResponse HttpResponseCode, CbObject Data); void WriteResponse(HttpResponse HttpResponseCode, CbPackage Package); void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, std::string_view ResponseString); + void WriteResponse(HttpResponse HttpResponseCode, HttpContentType ContentType, IoBuffer Blob); protected: bool m_IsHandled = false; @@ -254,8 +253,8 @@ protected: HttpVerb m_Verb = HttpVerb::kGet; uint64_t m_ContentLength = ~0ull; HttpContentType m_ContentType = HttpContentType::kBinary; - ExtendableStringBuilder<256> m_Uri; - ExtendableStringBuilder<256> m_QueryString; + ExtendableStringBuilder<256> m_UriUtf8; + ExtendableStringBuilder<256> m_QueryStringUtf8; }; class HttpServerException : public std::exception diff --git a/zenhttp/zenhttp.vcxproj b/zenhttp/zenhttp.vcxproj index 1e6aba90f..d4a61b313 100644 --- a/zenhttp/zenhttp.vcxproj +++ b/zenhttp/zenhttp.vcxproj @@ -96,14 +96,17 @@ + + + diff --git a/zenhttp/zenhttp.vcxproj.filters b/zenhttp/zenhttp.vcxproj.filters index 16e374d0d..032c97386 100644 --- a/zenhttp/zenhttp.vcxproj.filters +++ b/zenhttp/zenhttp.vcxproj.filters @@ -5,12 +5,15 @@ + + + -- cgit v1.2.3