From f55b77a98a41ba784109842cde39ba0e1d2bc262 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 6 Oct 2013 16:40:28 +0200 Subject: Fixed memory leaks in the HTTP framework --- source/HTTPServer/HTTPConnection.cpp | 25 +++++++++++++++++++++++++ source/HTTPServer/HTTPConnection.h | 4 ++++ source/HTTPServer/HTTPServer.cpp | 5 +++-- 3 files changed, 32 insertions(+), 2 deletions(-) (limited to 'source/HTTPServer') diff --git a/source/HTTPServer/HTTPConnection.cpp b/source/HTTPServer/HTTPConnection.cpp index 2addf4cfc..68afdfc11 100644 --- a/source/HTTPServer/HTTPConnection.cpp +++ b/source/HTTPServer/HTTPConnection.cpp @@ -17,11 +17,22 @@ cHTTPConnection::cHTTPConnection(cHTTPServer & a_HTTPServer) : m_State(wcsRecvHeaders), m_CurrentRequest(NULL) { + // LOGD("HTTP: New connection at %p", this); } + +cHTTPConnection::~cHTTPConnection() +{ + // LOGD("HTTP: Del connection at %p", this); +} + + + + + void cHTTPConnection::SendStatusAndReason(int a_StatusCode, const AString & a_Response) { AppendPrintf(m_OutgoingData, "%d %s\r\nContent-Length: 0\r\n\r\n", a_StatusCode, a_Response.c_str()); @@ -120,6 +131,19 @@ void cHTTPConnection::AwaitNextRequest(void) +void cHTTPConnection::Terminate(void) +{ + if (m_CurrentRequest != NULL) + { + m_HTTPServer.RequestFinished(*this, *m_CurrentRequest); + } + m_HTTPServer.CloseConnection(*this); +} + + + + + void cHTTPConnection::DataReceived(const char * a_Data, int a_Size) { switch (m_State) @@ -214,6 +238,7 @@ void cHTTPConnection::SocketClosed(void) { m_HTTPServer.RequestFinished(*this, *m_CurrentRequest); } + m_HTTPServer.CloseConnection(*this); } diff --git a/source/HTTPServer/HTTPConnection.h b/source/HTTPServer/HTTPConnection.h index 941a29000..14603bb70 100644 --- a/source/HTTPServer/HTTPConnection.h +++ b/source/HTTPServer/HTTPConnection.h @@ -39,6 +39,7 @@ public: } ; cHTTPConnection(cHTTPServer & a_HTTPServer); + ~cHTTPConnection(); /// Sends HTTP status code together with a_Reason (used for HTTP errors) void SendStatusAndReason(int a_StatusCode, const AString & a_Reason); @@ -61,6 +62,9 @@ public: /// Resets the connection for a new request. Depending on the state, this will send an "InternalServerError" status or a "ResponseEnd" void AwaitNextRequest(void); + /// Terminates the connection; finishes any request being currently processed + void Terminate(void); + protected: typedef std::map cNameValueMap; diff --git a/source/HTTPServer/HTTPServer.cpp b/source/HTTPServer/HTTPServer.cpp index 9636eb59f..f6f5b0f8b 100644 --- a/source/HTTPServer/HTTPServer.cpp +++ b/source/HTTPServer/HTTPServer.cpp @@ -179,9 +179,9 @@ void cHTTPServer::Stop(void) // Drop all current connections: cCSLock Lock(m_CSConnections); - for (cHTTPConnections::iterator itr = m_Connections.begin(), end = m_Connections.end(); itr != end; ++itr) + while (!m_Connections.empty()) { - m_SocketThreads.RemoveClient(*itr); + m_Connections.front()->Terminate(); } // for itr - m_Connections[] } @@ -213,6 +213,7 @@ void cHTTPServer::CloseConnection(cHTTPConnection & a_Connection) break; } } + delete &a_Connection; } -- cgit v1.2.3