1
0

Fixed crashes in the SSL HTTP connection.

This commit is contained in:
madmaxoft 2014-05-01 15:08:15 +02:00
parent dc2d2ce53c
commit 60850fe3e8
9 changed files with 36 additions and 23 deletions

View File

@ -2649,12 +2649,13 @@ void cClientHandle::PacketError(unsigned char a_PacketType)
void cClientHandle::DataReceived(const char * a_Data, size_t a_Size) bool cClientHandle::DataReceived(const char * a_Data, size_t a_Size)
{ {
// Data is received from the client, store it in the buffer to be processed by the Tick thread: // Data is received from the client, store it in the buffer to be processed by the Tick thread:
m_TimeSinceLastPacket = 0; m_TimeSinceLastPacket = 0;
cCSLock Lock(m_CSIncomingData); cCSLock Lock(m_CSIncomingData);
m_IncomingData.append(a_Data, a_Size); m_IncomingData.append(a_Data, a_Size);
return false;
} }

View File

@ -387,7 +387,7 @@ private:
void HandleCommandBlockMessage(const char * a_Data, unsigned int a_Length); void HandleCommandBlockMessage(const char * a_Data, unsigned int a_Length);
// cSocketThreads::cCallback overrides: // cSocketThreads::cCallback overrides:
virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client virtual bool DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client
virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client
virtual void SocketClosed (void) override; // The socket has been closed for any reason virtual void SocketClosed (void) override; // The socket has been closed for any reason
}; // tolua_export }; // tolua_export

View File

@ -145,7 +145,7 @@ void cHTTPConnection::Terminate(void)
void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) bool cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
{ {
switch (m_State) switch (m_State)
{ {
@ -163,12 +163,12 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
m_CurrentRequest = NULL; m_CurrentRequest = NULL;
m_State = wcsInvalid; m_State = wcsInvalid;
m_HTTPServer.CloseConnection(*this); m_HTTPServer.CloseConnection(*this);
return; return true;
} }
if (m_CurrentRequest->IsInHeaders()) if (m_CurrentRequest->IsInHeaders())
{ {
// The request headers are not yet complete // The request headers are not yet complete
return; return false;
} }
// The request has finished parsing its headers successfully, notify of it: // The request has finished parsing its headers successfully, notify of it:
@ -184,13 +184,12 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
// Process the rest of the incoming data into the request body: // Process the rest of the incoming data into the request body:
if (a_Size > BytesConsumed) if (a_Size > BytesConsumed)
{ {
cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); return cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed);
} }
else else
{ {
cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away return cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away
} }
break;
} }
case wcsRecvBody: case wcsRecvBody:
@ -210,7 +209,7 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
{ {
m_State = wcsInvalid; m_State = wcsInvalid;
m_HTTPServer.CloseConnection(*this); m_HTTPServer.CloseConnection(*this);
return; return true;
} }
delete m_CurrentRequest; delete m_CurrentRequest;
m_CurrentRequest = NULL; m_CurrentRequest = NULL;
@ -224,6 +223,7 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
break; break;
} }
} }
return false;
} }

View File

@ -91,9 +91,15 @@ protected:
// cSocketThreads::cCallback overrides: // cSocketThreads::cCallback overrides:
virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client /** Data is received from the client.
virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client Returns true if the connection has been closed as the result of parsing the data. */
virtual void SocketClosed (void) override; // The socket has been closed for any reason virtual bool DataReceived(const char * a_Data, size_t a_Size) override;
/** Data can be sent to client */
virtual void GetOutgoingData(AString & a_Data) override;
/** The socket has been closed for any reason */
virtual void SocketClosed(void) override;
} ; } ;
typedef std::vector<cHTTPConnection *> cHTTPConnections; typedef std::vector<cHTTPConnection *> cHTTPConnections;

View File

@ -25,7 +25,7 @@ cSslHTTPConnection::cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509Ce
void cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) bool cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
{ {
// If there is outgoing data in the queue, notify the server that it should write it out: // If there is outgoing data in the queue, notify the server that it should write it out:
if (!m_OutgoingData.empty()) if (!m_OutgoingData.empty())
@ -52,13 +52,17 @@ void cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
int NumRead = m_Ssl.ReadPlain(Buffer, sizeof(Buffer)); int NumRead = m_Ssl.ReadPlain(Buffer, sizeof(Buffer));
if (NumRead > 0) if (NumRead > 0)
{ {
super::DataReceived(Buffer, (size_t)NumRead); if (super::DataReceived(Buffer, (size_t)NumRead))
{
// The socket has been closed, and the object is already deleted. Bail out.
return true;
}
} }
// If both failed, bail out: // If both failed, bail out:
if ((BytesWritten == 0) && (NumRead <= 0)) if ((BytesWritten == 0) && (NumRead <= 0))
{ {
return; return false;
} }
} }
} }

View File

@ -36,7 +36,7 @@ protected:
cPublicKeyPtr m_PrivateKey; cPublicKeyPtr m_PrivateKey;
// cHTTPConnection overrides: // cHTTPConnection overrides:
virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client virtual bool DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client
virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client
} ; } ;

View File

@ -63,8 +63,10 @@ public:
// Force a virtual destructor in all subclasses: // Force a virtual destructor in all subclasses:
virtual ~cCallback() {} virtual ~cCallback() {}
/** Called when data is received from the remote party */ /** Called when data is received from the remote party.
virtual void DataReceived(const char * a_Data, size_t a_Size) = 0; SocketThreads does not care about the return value, others can use it for their specific purpose -
for example HTTPServer uses it to signal if the connection was terminated as a result of the data received. */
virtual bool DataReceived(const char * a_Data, size_t a_Size) = 0;
/** Called when data can be sent to remote party /** Called when data can be sent to remote party
The function is supposed to *set* outgoing data to a_Data (overwrite) */ The function is supposed to *set* outgoing data to a_Data (overwrite) */

View File

@ -169,7 +169,7 @@ cRCONServer::cConnection::cConnection(cRCONServer & a_RCONServer, cSocket & a_So
void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) bool cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size)
{ {
// Append data to the buffer: // Append data to the buffer:
m_Buffer.append(a_Data, a_Size); m_Buffer.append(a_Data, a_Size);
@ -187,12 +187,12 @@ void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size)
m_RCONServer.m_SocketThreads.RemoveClient(this); m_RCONServer.m_SocketThreads.RemoveClient(this);
m_Socket.CloseSocket(); m_Socket.CloseSocket();
delete this; delete this;
return; return false;
} }
if (Length > (int)(m_Buffer.size() + 4)) if (Length > (int)(m_Buffer.size() + 4))
{ {
// Incomplete packet yet, wait for more data to come // Incomplete packet yet, wait for more data to come
return; return false;
} }
int RequestID = IntFromBuffer(m_Buffer.data() + 4); int RequestID = IntFromBuffer(m_Buffer.data() + 4);
@ -202,7 +202,7 @@ void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size)
m_RCONServer.m_SocketThreads.RemoveClient(this); m_RCONServer.m_SocketThreads.RemoveClient(this);
m_Socket.CloseSocket(); m_Socket.CloseSocket();
delete this; delete this;
return; return false;
} }
m_Buffer.erase(0, Length + 4); m_Buffer.erase(0, Length + 4);
} // while (m_Buffer.size() >= 14) } // while (m_Buffer.size() >= 14)

View File

@ -65,7 +65,7 @@ protected:
// cSocketThreads::cCallback overrides: // cSocketThreads::cCallback overrides:
virtual void DataReceived(const char * a_Data, size_t a_Size) override; virtual bool DataReceived(const char * a_Data, size_t a_Size) override;
virtual void GetOutgoingData(AString & a_Data) override; virtual void GetOutgoingData(AString & a_Data) override;
virtual void SocketClosed(void) override; virtual void SocketClosed(void) override;