From: Michael Friedrich Date: Tue, 9 Oct 2018 13:40:16 +0000 (+0200) Subject: Cache the peer address in the HTTP server X-Git-Tag: v2.10.0~7^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=refs%2Fpull%2F6661%2Fhead;p=icinga2 Cache the peer address in the HTTP server Later socket calls are expensive and might lead into a race condition on close when logging it. refs #6655 --- diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index f4bb14439..9d8f192c4 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -47,6 +47,17 @@ HttpServerConnection::HttpServerConnection(const String& identity, bool authenti if (authenticated) m_ApiUser = ApiUser::GetByClientCN(identity); + + /* Cache the peer address. */ + m_PeerAddress = ""; + + if (stream) { + Socket::Ptr socket = m_Stream->GetSocket(); + + if (socket) { + m_PeerAddress = socket->GetPeerAddress(); + } + } } void HttpServerConnection::StaticInitialize() @@ -84,7 +95,7 @@ void HttpServerConnection::Disconnect() } Log(LogInformation, "HttpServerConnection") - << "HTTP client disconnected (from " << m_Stream->GetSocket()->GetPeerAddress() << ")"; + << "HTTP client disconnected (from " << m_PeerAddress << ")"; ApiListener::Ptr listener = ApiListener::GetInstance(); listener->RemoveHttpClient(this); @@ -201,11 +212,9 @@ bool HttpServerConnection::ManageHeaders(HttpResponse& response) String requestUrl = m_CurrentRequest.RequestUrl->Format(); - Socket::Ptr socket = m_Stream->GetSocket(); - Log(LogInformation, "HttpServerConnection") << "Request: " << m_CurrentRequest.RequestMethod << " " << requestUrl - << " (from " << (socket ? socket->GetPeerAddress() : "") + << " (from " << m_PeerAddress << ")" << ", user: " << (m_AuthenticatedUser ? m_AuthenticatedUser->GetName() : "") << ")"; ApiListener::Ptr listener = ApiListener::GetInstance(); diff --git a/lib/remote/httpserverconnection.hpp b/lib/remote/httpserverconnection.hpp index 6d1c3bd46..7712ad66d 100644 --- a/lib/remote/httpserverconnection.hpp +++ b/lib/remote/httpserverconnection.hpp @@ -59,6 +59,7 @@ private: boost::recursive_mutex m_DataHandlerMutex; WorkQueue m_RequestQueue; int m_PendingRequests; + String m_PeerAddress; StreamReadContext m_Context;