diff options
| author | Stefan Boberg <[email protected]> | 2021-09-09 16:29:24 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-09 16:29:24 +0200 |
| commit | a73862b954209fd1adce0f07b8f80b8cf27f2486 (patch) | |
| tree | 07f2898998f884e871c47a2979dfb01434443584 | |
| parent | Factored out http server related code into zenhttp module since it feels out ... (diff) | |
| download | zen-a73862b954209fd1adce0f07b8f80b8cf27f2486.tar.xz zen-a73862b954209fd1adce0f07b8f80b8cf27f2486.zip | |
Added compile time logic to toggle http.sys / null http implementation on/off
| -rw-r--r-- | zenhttp/httpnull.cpp | 65 | ||||
| -rw-r--r-- | zenhttp/httpnull.h | 27 | ||||
| -rw-r--r-- | zenhttp/httpserver.cpp | 5 | ||||
| -rw-r--r-- | zenhttp/httpsys.cpp | 19 | ||||
| -rw-r--r-- | zenhttp/httpsys.h | 20 | ||||
| -rw-r--r-- | zenhttp/include/zenhttp/httpserver.h | 15 | ||||
| -rw-r--r-- | zenhttp/zenhttp.vcxproj | 3 | ||||
| -rw-r--r-- | zenhttp/zenhttp.vcxproj.filters | 3 |
8 files changed, 133 insertions, 24 deletions
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 <zencore/logging.h> +#include <conio.h> + +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 <zenhttp/httpserver.h> +#include <zencore/thread.h> + +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 <zenhttp/httpserver.h> #include "httpsys.h" +#include "httpnull.h" #include <zencore/compactbinary.h> #include <zencore/compactbinarypackage.h> @@ -353,7 +354,11 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) Ref<HttpServer> 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 <zencore/logging.h> #include <zencore/string.h> -#include <conio.h> +#if ZEN_WITH_HTTPSYS -#if ZEN_PLATFORM_WINDOWS -# pragma comment(lib, "httpapi.lib") -#endif +#include <conio.h> +#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<size_t>(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 <zenhttp/httpserver.h> -#define _WINSOCKAPI_ -#include <zencore/windows.h> -#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 <atlbase.h> -#include <http.h> +#if ZEN_WITH_HTTPSYS +# define _WINSOCKAPI_ +# include <zencore/windows.h> +# include "iothreadpool.h" + +# include <atlbase.h> +# include <http.h> 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<IoBuffer> 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<IoBuffer> 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 @@ </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="httpclient.cpp" /> + <ClCompile Include="httpnull.cpp" /> <ClCompile Include="httpserver.cpp" /> <ClCompile Include="httpsys.cpp" /> <ClCompile Include="iothreadpool.cpp" /> </ItemGroup> <ItemGroup> + <ClInclude Include="httpnull.h" /> <ClInclude Include="httpsys.h" /> <ClInclude Include="include\zenhttp\httpclient.h" /> <ClInclude Include="include\zenhttp\httpserver.h" /> + <ClInclude Include="include\zenhttp\zenhttp.h" /> <ClInclude Include="iothreadpool.h" /> </ItemGroup> <ItemGroup> 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 @@ <ClCompile Include="httpserver.cpp" /> <ClCompile Include="httpsys.cpp" /> <ClCompile Include="iothreadpool.cpp" /> + <ClCompile Include="httpnull.cpp" /> </ItemGroup> <ItemGroup> <ClInclude Include="include\zenhttp\httpclient.h" /> <ClInclude Include="include\zenhttp\httpserver.h" /> <ClInclude Include="httpsys.h" /> <ClInclude Include="iothreadpool.h" /> + <ClInclude Include="include\zenhttp\zenhttp.h" /> + <ClInclude Include="httpnull.h" /> </ItemGroup> <ItemGroup> <None Include="xmake.lua" /> |