diff options
| author | zousar <[email protected]> | 2021-11-26 08:23:41 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-26 08:23:41 -0700 |
| commit | 39e30216589b20da3a400d4252e8530cfe4b148e (patch) | |
| tree | 9dd38fec2cf52fbd134ac6e5b3b1e522337384fb /zenhttp/httpasio.cpp | |
| parent | Added .gdb_history to .gitignore (diff) | |
| parent | Address review feedback. (diff) | |
| download | zen-39e30216589b20da3a400d4252e8530cfe4b148e.tar.xz zen-39e30216589b20da3a400d4252e8530cfe4b148e.zip | |
Merge pull request #28 from EpicGames/non-elevated-asio
ASIO Mode Fixes
Diffstat (limited to 'zenhttp/httpasio.cpp')
| -rw-r--r-- | zenhttp/httpasio.cpp | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/zenhttp/httpasio.cpp b/zenhttp/httpasio.cpp index d5fe9adbb..b9db108db 100644 --- a/zenhttp/httpasio.cpp +++ b/zenhttp/httpasio.cpp @@ -15,6 +15,14 @@ ZEN_THIRD_PARTY_INCLUDES_START #include <asio.hpp> ZEN_THIRD_PARTY_INCLUDES_END +#define ASIO_VERBOSE_TRACE 0 + +#if ASIO_VERBOSE_TRACE +#define ZEN_TRACE_VERBOSE ZEN_TRACE +#else +#define ZEN_TRACE_VERBOSE(fmtstr, ...) +#endif + namespace zen::asio_http { using namespace std::literals; @@ -318,6 +326,7 @@ private: std::unique_ptr<asio::ip::tcp::socket> m_Socket; std::atomic<uint32_t> m_RequestCounter{0}; uint32_t m_ConnectionId = 0; + Ref<IHttpPackageHandler> m_PackageHandler; RwLock m_ResponsesLock; std::deque<std::unique_ptr<HttpResponse>> m_Responses; @@ -330,12 +339,12 @@ HttpServerConnection::HttpServerConnection(HttpAsioServerImpl& Server, std::uniq , m_Socket(std::move(Socket)) , m_ConnectionId(g_ConnectionIdCounter.fetch_add(1)) { - ZEN_TRACE("new connection #{}", m_ConnectionId); + ZEN_TRACE_VERBOSE("new connection #{}", m_ConnectionId); } HttpServerConnection::~HttpServerConnection() { - ZEN_TRACE("destroying connection #{}", m_ConnectionId); + ZEN_TRACE_VERBOSE("destroying connection #{}", m_ConnectionId); } void @@ -371,18 +380,18 @@ HttpServerConnection::EnqueueRead() asio::async_read(*m_Socket.get(), m_RequestBuffer, - asio::transfer_at_least(16), + asio::transfer_at_least(1), [Conn = AsSharedPtr()](const asio::error_code& Ec, std::size_t ByteCount) { Conn->OnDataReceived(Ec, ByteCount); }); } void -HttpServerConnection::OnDataReceived(const asio::error_code& Ec, std::size_t ByteCount) +HttpServerConnection::OnDataReceived(const asio::error_code& Ec, [[maybe_unused]] std::size_t ByteCount) { if (Ec) { if (m_RequestState == RequestState::kDone || m_RequestState == RequestState::kInitialRead) { - ZEN_TRACE("on data received ERROR (EXPECTED), connection '{}' reason '{}'", m_ConnectionId, Ec.message()); + ZEN_TRACE_VERBOSE("on data received ERROR (EXPECTED), connection '{}' reason '{}'", m_ConnectionId, Ec.message()); return; } else @@ -392,7 +401,7 @@ HttpServerConnection::OnDataReceived(const asio::error_code& Ec, std::size_t Byt } } - ZEN_TRACE("on data received, connection '{}', request '{}', thread '{}', bytes '{}'", + ZEN_TRACE_VERBOSE("on data received, connection '{}', request '{}', thread '{}', bytes '{}'", m_ConnectionId, m_RequestCounter.load(std::memory_order_relaxed), GetCurrentThreadId(), @@ -421,7 +430,7 @@ HttpServerConnection::OnDataReceived(const asio::error_code& Ec, std::size_t Byt } void -HttpServerConnection::OnResponseDataSent(const asio::error_code& Ec, std::size_t ByteCount, bool Pop) +HttpServerConnection::OnResponseDataSent(const asio::error_code& Ec, [[maybe_unused]] std::size_t ByteCount, bool Pop) { if (Ec) { @@ -430,7 +439,7 @@ HttpServerConnection::OnResponseDataSent(const asio::error_code& Ec, std::size_t } else { - ZEN_TRACE("on data sent, connection '{}', request '{}', thread '{}', bytes '{}'", + ZEN_TRACE_VERBOSE("on data sent, connection '{}', request '{}', thread '{}', bytes '{}'", m_ConnectionId, m_RequestCounter.load(std::memory_order_relaxed), GetCurrentThreadId(), @@ -485,9 +494,13 @@ HttpServerConnection::HandleRequest() { HttpAsioServerRequest Request(m_RequestData, *Service, m_RequestData.Body()); - ZEN_TRACE("handle request, connection '{}' request '{}'", m_ConnectionId, m_RequestCounter.load(std::memory_order_relaxed)); - Service->HandleRequest(Request); + ZEN_TRACE_VERBOSE("handle request, connection '{}' request '{}'", m_ConnectionId, m_RequestCounter.load(std::memory_order_relaxed)); + + if (!HandlePackageOffers(*Service, Request, m_PackageHandler)) + { + Service->HandleRequest(Request); + } if (std::unique_ptr<HttpResponse> Response = std::move(Request.m_Response)) { @@ -935,7 +948,7 @@ HttpAsioServerRequest::HttpAsioServerRequest(asio_http::HttpRequest& Request, Ht const int PrefixLength = Service.UriPrefixLength(); std::string_view Uri = Request.Url(); - Uri.remove_prefix(PrefixLength); + Uri.remove_prefix(std::min(PrefixLength, static_cast<int>(Uri.size()))); m_Uri = Uri; m_QueryString = Request.QueryString(); @@ -1099,6 +1112,10 @@ HttpAsioServerImpl::RegisterService(const char* InUrlPath, HttpService& Service) { std::string_view UrlPath(InUrlPath); Service.SetUriPrefixLength(UrlPath.size()); + if (!UrlPath.empty() && UrlPath.back() == '/') + { + UrlPath.remove_suffix(1); + } RwLock::ExclusiveLockScope _(m_Lock); m_UriHandlers.push_back({std::string(UrlPath), &Service}); @@ -1109,16 +1126,22 @@ HttpAsioServerImpl::RouteRequest(std::string_view Url) { RwLock::SharedLockScope _(m_Lock); + HttpService* CandidateService = nullptr; + std::string::size_type CandidateMatchSize = 0; for (const ServiceEntry& SvcEntry : m_UriHandlers) { const std::string& SvcUrl = SvcEntry.ServiceUrlPath; - if (Url.compare(0, SvcUrl.size(), SvcUrl) == 0) + const std::string::size_type SvcUrlSize = SvcUrl.size(); + if ((SvcUrlSize >= CandidateMatchSize) && + Url.compare(0, SvcUrlSize, SvcUrl) == 0 && + ((SvcUrlSize == Url.size()) || (Url[SvcUrlSize] == '/'))) { - return SvcEntry.Service; + CandidateMatchSize = SvcUrl.size(); + CandidateService = SvcEntry.Service; } } - return nullptr; + return CandidateService; } } // namespace zen::asio_http |