1
0

Fixed memory leaks in the HTTP framework

This commit is contained in:
madmaxoft 2013-10-06 16:40:28 +02:00
parent 4bf596a586
commit f55b77a98a
5 changed files with 43 additions and 6 deletions

View File

@ -17,11 +17,22 @@ cHTTPConnection::cHTTPConnection(cHTTPServer & a_HTTPServer) :
m_State(wcsRecvHeaders), m_State(wcsRecvHeaders),
m_CurrentRequest(NULL) 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) 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()); 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) void cHTTPConnection::DataReceived(const char * a_Data, int a_Size)
{ {
switch (m_State) switch (m_State)
@ -214,6 +238,7 @@ void cHTTPConnection::SocketClosed(void)
{ {
m_HTTPServer.RequestFinished(*this, *m_CurrentRequest); m_HTTPServer.RequestFinished(*this, *m_CurrentRequest);
} }
m_HTTPServer.CloseConnection(*this);
} }

View File

@ -39,6 +39,7 @@ public:
} ; } ;
cHTTPConnection(cHTTPServer & a_HTTPServer); cHTTPConnection(cHTTPServer & a_HTTPServer);
~cHTTPConnection();
/// Sends HTTP status code together with a_Reason (used for HTTP errors) /// Sends HTTP status code together with a_Reason (used for HTTP errors)
void SendStatusAndReason(int a_StatusCode, const AString & a_Reason); 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" /// Resets the connection for a new request. Depending on the state, this will send an "InternalServerError" status or a "ResponseEnd"
void AwaitNextRequest(void); void AwaitNextRequest(void);
/// Terminates the connection; finishes any request being currently processed
void Terminate(void);
protected: protected:
typedef std::map<AString, AString> cNameValueMap; typedef std::map<AString, AString> cNameValueMap;

View File

@ -179,9 +179,9 @@ void cHTTPServer::Stop(void)
// Drop all current connections: // Drop all current connections:
cCSLock Lock(m_CSConnections); 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[] } // for itr - m_Connections[]
} }
@ -213,6 +213,7 @@ void cHTTPServer::CloseConnection(cHTTPConnection & a_Connection)
break; break;
} }
} }
delete &a_Connection;
} }

View File

@ -488,15 +488,20 @@ void cWebAdmin::OnRequestFinished(cHTTPConnection & a_Connection, cHTTPRequest &
) )
{ {
HandleWebadminRequest(a_Connection, a_Request); HandleWebadminRequest(a_Connection, a_Request);
return;
} }
if (URL == "/") else if (URL == "/")
{ {
// The root needs no body handler and is fully handled in the OnRequestFinished() call // The root needs no body handler and is fully handled in the OnRequestFinished() call
HandleRootRequest(a_Connection, a_Request); HandleRootRequest(a_Connection, a_Request);
return;
} }
// TODO: Handle other requests else
{
// TODO: Handle other requests
}
// Delete any request data assigned to the request:
cRequestData * Data = (cRequestData *)(a_Request.GetUserData());
delete Data;
} }

View File

@ -134,6 +134,8 @@ protected:
class cRequestData class cRequestData
{ {
public: public:
virtual ~cRequestData() {} // Force a virtual destructor in all descendants
/// Called when a new chunk of body data is received /// Called when a new chunk of body data is received
virtual void OnBody(const char * a_Data, int a_Size) = 0; virtual void OnBody(const char * a_Data, int a_Size) = 0;
} ; } ;