From f753c3c2ebb49dfde76d33bd656360d2f4059ade Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 18 Jan 2024 12:47:45 +0100 Subject: [PATCH] HttpServerConnection: always log request filter if any, for debugging Even if it's in the request body. --- lib/remote/httphandler.cpp | 6 ++-- lib/remote/httphandler.hpp | 2 ++ lib/remote/httpserverconnection.cpp | 47 +++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/remote/httphandler.cpp b/lib/remote/httphandler.cpp index 71a89e4ce21..5d7a637e499 100644 --- a/lib/remote/httphandler.cpp +++ b/lib/remote/httphandler.cpp @@ -50,6 +50,8 @@ void HttpHandler::ProcessRequest( AsioTlsStream& stream, const ApiUser::Ptr& user, boost::beast::http::request& request, + Url::Ptr& url, + Dictionary::Ptr& params, boost::beast::http::response& response, boost::asio::yield_context& yc, HttpServerConnection& server @@ -58,7 +60,7 @@ void HttpHandler::ProcessRequest( Dictionary::Ptr node = m_UrlTree; std::vector handlers; - Url::Ptr url = new Url(std::string(request.target())); + url = new Url(std::string(request.target())); auto& path (url->GetPath()); for (std::vector::size_type i = 0; i <= path.size(); i++) { @@ -89,8 +91,6 @@ void HttpHandler::ProcessRequest( std::reverse(handlers.begin(), handlers.end()); - Dictionary::Ptr params; - try { params = HttpUtility::FetchRequestParameters(url, request.body()); } catch (const std::exception& ex) { diff --git a/lib/remote/httphandler.hpp b/lib/remote/httphandler.hpp index a6a730255f7..ec1e39ab49e 100644 --- a/lib/remote/httphandler.hpp +++ b/lib/remote/httphandler.hpp @@ -42,6 +42,8 @@ class HttpHandler : public Object AsioTlsStream& stream, const ApiUser::Ptr& user, boost::beast::http::request& request, + Url::Ptr& url, + Dictionary::Ptr& params, boost::beast::http::response& response, boost::asio::yield_context& yc, HttpServerConnection& server diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index 76cfd3c5269..7fedcc186e2 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -442,7 +442,9 @@ static inline bool ProcessRequest( AsioTlsStream& stream, boost::beast::http::request& request, + Url::Ptr& url, ApiUser::Ptr& authenticatedUser, + Dictionary::Ptr& params, boost::beast::http::response& response, HttpServerConnection& server, bool& hasStartedStreaming, @@ -454,7 +456,7 @@ bool ProcessRequest( try { CpuBoundWork handlingRequest (yc); - HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server); + HttpHandler::ProcessRequest(stream, authenticatedUser, request, url, params, response, yc, server); } catch (const std::exception& ex) { if (hasStartedStreaming) { return false; @@ -542,6 +544,8 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) authenticatedUser = ApiUser::GetByAuthHeader(std::string(request[http::field::authorization])); } + Url::Ptr url; + Dictionary::Ptr params; Log logMsg (LogInformation, "HttpServerConnection"); logMsg << "Request " << request.method_string() << ' ' << request.target() @@ -549,9 +553,46 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) << "), user: " << (authenticatedUser ? authenticatedUser->GetName() : "") << ", agent: " << request[http::field::user_agent]; //operator[] - Returns the value for a field, or "" if it does not exist. - Defer addRespCode ([&response, start, &logMsg]() { + Defer completeLogMessage ([&response, start, &authenticatedUser, &url, ¶ms, &logMsg]() { logMsg << ", status: " << response.result() << ") took " << ch::duration_cast(ch::steady_clock::now() - start).count() << "ms."; + + if (authenticatedUser && params) { + auto filter (HttpUtility::GetLastParameter(params, "filter")); + + if (filter.GetType() != ValueEmpty) { + bool urlHasFilter = false; + + if (url) { + for (auto& kv : url->GetQuery()) { + if (kv.first == "filter") { + urlHasFilter = true; + break; + } + } + } + + if (!urlHasFilter) { + auto type (HttpUtility::GetLastParameter(params, "type")); + + if (type.GetType() == ValueEmpty) { + logMsg << " Filter: "; + } else { + logMsg << ' ' << type << " filter: "; + } + + String filterStr = filter; + const auto filterLimit (1024u); + + if (filterStr.GetLength() > filterLimit) { + filterStr.erase(filterStr.Begin() + filterLimit, filterStr.End()); + filterStr += "..."; + } + + logMsg << filterStr; + } + } + } }); if (!HandleAccessControl(*m_Stream, request, response, yc)) { @@ -572,7 +613,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) m_Seen = std::numeric_limits::max(); - if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) { + if (!ProcessRequest(*m_Stream, request, url, authenticatedUser, params, response, *this, m_HasStartedStreaming, yc)) { break; }