2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
// HTTPConnection.cpp
|
|
|
|
|
|
|
|
// Implements the cHTTPConnection class representing a single persistent connection in the HTTP server.
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "HTTPConnection.h"
|
|
|
|
#include "HTTPMessage.h"
|
|
|
|
#include "HTTPServer.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cHTTPConnection::cHTTPConnection(cHTTPServer & a_HTTPServer) :
|
|
|
|
m_HTTPServer(a_HTTPServer),
|
|
|
|
m_State(wcsRecvHeaders),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_CurrentRequest(nullptr),
|
2014-08-21 16:39:53 -04:00
|
|
|
m_CurrentRequestBodyRemaining(0)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
2013-10-06 10:40:28 -04:00
|
|
|
// LOGD("HTTP: New connection at %p", this);
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-06 10:40:28 -04:00
|
|
|
|
|
|
|
cHTTPConnection::~cHTTPConnection()
|
|
|
|
{
|
2014-05-01 05:34:15 -04:00
|
|
|
// LOGD("HTTP: Connection deleting: %p", this);
|
2014-01-19 13:31:43 -05:00
|
|
|
delete m_CurrentRequest;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_CurrentRequest = nullptr;
|
2013-10-06 10:40:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-27 13:34:46 -04:00
|
|
|
void cHTTPConnection::SendStatusAndReason(int a_StatusCode, const AString & a_Response)
|
|
|
|
{
|
2013-10-05 15:52:14 -04:00
|
|
|
AppendPrintf(m_OutgoingData, "%d %s\r\nContent-Length: 0\r\n\r\n", a_StatusCode, a_Response.c_str());
|
2013-09-27 14:33:18 -04:00
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
2013-10-04 14:28:30 -04:00
|
|
|
m_State = wcsRecvHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPConnection::SendNeedAuth(const AString & a_Realm)
|
|
|
|
{
|
2013-10-05 15:52:14 -04:00
|
|
|
AppendPrintf(m_OutgoingData, "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"%s\"\r\nContent-Length: 0\r\n\r\n", a_Realm.c_str());
|
2013-10-04 14:28:30 -04:00
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
|
|
|
m_State = wcsRecvHeaders;
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPConnection::Send(const cHTTPResponse & a_Response)
|
|
|
|
{
|
2014-01-06 16:22:33 -05:00
|
|
|
ASSERT(m_State == wcsRecvIdle);
|
2013-09-27 13:34:46 -04:00
|
|
|
a_Response.AppendToData(m_OutgoingData);
|
|
|
|
m_State = wcsSendingResp;
|
2013-09-27 14:33:18 -04:00
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
void cHTTPConnection::Send(const void * a_Data, size_t a_Size)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
|
|
|
ASSERT(m_State == wcsSendingResp);
|
2014-04-07 02:11:56 -04:00
|
|
|
AppendPrintf(m_OutgoingData, SIZE_T_FMT_HEX "\r\n", a_Size);
|
2013-09-27 13:34:46 -04:00
|
|
|
m_OutgoingData.append((const char *)a_Data, a_Size);
|
2013-09-27 14:33:18 -04:00
|
|
|
m_OutgoingData.append("\r\n");
|
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPConnection::FinishResponse(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_State == wcsSendingResp);
|
2013-09-27 14:33:18 -04:00
|
|
|
m_OutgoingData.append("0\r\n\r\n");
|
2013-09-27 13:34:46 -04:00
|
|
|
m_State = wcsRecvHeaders;
|
2013-09-27 14:33:18 -04:00
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-27 15:28:41 -04:00
|
|
|
void cHTTPConnection::AwaitNextRequest(void)
|
|
|
|
{
|
|
|
|
switch (m_State)
|
|
|
|
{
|
2013-10-04 14:28:30 -04:00
|
|
|
case wcsRecvHeaders:
|
|
|
|
{
|
2014-07-21 09:19:48 -04:00
|
|
|
// Nothing has been received yet, or a special response was given (SendStatusAndReason() or SendNeedAuth())
|
2013-10-04 14:28:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-27 15:28:41 -04:00
|
|
|
case wcsRecvIdle:
|
|
|
|
{
|
|
|
|
// The client is waiting for a response, send an "Internal server error":
|
|
|
|
m_OutgoingData.append("HTTP/1.1 500 Internal Server Error\r\n\r\n");
|
|
|
|
m_HTTPServer.NotifyConnectionWrite(*this);
|
|
|
|
m_State = wcsRecvHeaders;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case wcsSendingResp:
|
|
|
|
{
|
|
|
|
// The response headers have been sent, we need to terminate the response body:
|
|
|
|
m_OutgoingData.append("0\r\n\r\n");
|
|
|
|
m_State = wcsRecvHeaders;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled state recovery");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-06 10:40:28 -04:00
|
|
|
void cHTTPConnection::Terminate(void)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_CurrentRequest != nullptr)
|
2013-10-06 10:40:28 -04:00
|
|
|
{
|
|
|
|
m_HTTPServer.RequestFinished(*this, *m_CurrentRequest);
|
|
|
|
}
|
|
|
|
m_HTTPServer.CloseConnection(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:08:15 -04:00
|
|
|
bool cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
|
|
|
switch (m_State)
|
|
|
|
{
|
|
|
|
case wcsRecvHeaders:
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_CurrentRequest == nullptr)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_CurrentRequest = new cHTTPRequest;
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t BytesConsumed = m_CurrentRequest->ParseHeaders(a_Data, a_Size);
|
|
|
|
if (BytesConsumed == AString::npos)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
|
|
|
delete m_CurrentRequest;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_CurrentRequest = nullptr;
|
2013-09-27 13:34:46 -04:00
|
|
|
m_State = wcsInvalid;
|
|
|
|
m_HTTPServer.CloseConnection(*this);
|
2014-05-01 09:08:15 -04:00
|
|
|
return true;
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
if (m_CurrentRequest->IsInHeaders())
|
|
|
|
{
|
|
|
|
// The request headers are not yet complete
|
2014-05-01 09:08:15 -04:00
|
|
|
return false;
|
2013-10-04 07:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The request has finished parsing its headers successfully, notify of it:
|
2013-09-27 13:34:46 -04:00
|
|
|
m_State = wcsRecvBody;
|
|
|
|
m_HTTPServer.NewRequest(*this, *m_CurrentRequest);
|
2013-09-27 14:33:18 -04:00
|
|
|
m_CurrentRequestBodyRemaining = m_CurrentRequest->GetContentLength();
|
2014-04-01 10:36:00 -04:00
|
|
|
if (m_CurrentRequestBodyRemaining == AString::npos)
|
2013-10-04 07:07:57 -04:00
|
|
|
{
|
|
|
|
// The body length was not specified in the request, assume zero
|
|
|
|
m_CurrentRequestBodyRemaining = 0;
|
|
|
|
}
|
2013-09-27 13:34:46 -04:00
|
|
|
|
|
|
|
// Process the rest of the incoming data into the request body:
|
2014-04-02 09:36:25 -04:00
|
|
|
if (a_Size > BytesConsumed)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
2014-05-01 09:08:15 -04:00
|
|
|
return cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed);
|
2013-09-27 14:33:18 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 09:08:15 -04:00
|
|
|
return cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case wcsRecvBody:
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(m_CurrentRequest != nullptr);
|
2013-09-27 14:33:18 -04:00
|
|
|
if (m_CurrentRequestBodyRemaining > 0)
|
|
|
|
{
|
2014-04-01 10:36:00 -04:00
|
|
|
size_t BytesToConsume = std::min(m_CurrentRequestBodyRemaining, (size_t)a_Size);
|
2013-09-27 14:33:18 -04:00
|
|
|
m_HTTPServer.RequestBody(*this, *m_CurrentRequest, a_Data, BytesToConsume);
|
|
|
|
m_CurrentRequestBodyRemaining -= BytesToConsume;
|
|
|
|
}
|
|
|
|
if (m_CurrentRequestBodyRemaining == 0)
|
|
|
|
{
|
2013-09-27 15:28:41 -04:00
|
|
|
m_State = wcsRecvIdle;
|
2013-09-27 14:33:18 -04:00
|
|
|
m_HTTPServer.RequestFinished(*this, *m_CurrentRequest);
|
2014-01-18 14:20:56 -05:00
|
|
|
if (!m_CurrentRequest->DoesAllowKeepAlive())
|
|
|
|
{
|
|
|
|
m_State = wcsInvalid;
|
|
|
|
m_HTTPServer.CloseConnection(*this);
|
2014-05-01 09:08:15 -04:00
|
|
|
return true;
|
2014-01-18 14:20:56 -05:00
|
|
|
}
|
2013-09-27 14:48:44 -04:00
|
|
|
delete m_CurrentRequest;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_CurrentRequest = nullptr;
|
2013-09-27 14:33:18 -04:00
|
|
|
}
|
2013-09-27 13:34:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// TODO: Should we be receiving data in this state?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 09:08:15 -04:00
|
|
|
return false;
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPConnection::GetOutgoingData(AString & a_Data)
|
|
|
|
{
|
|
|
|
std::swap(a_Data, m_OutgoingData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPConnection::SocketClosed(void)
|
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_CurrentRequest != nullptr)
|
2013-09-27 13:34:46 -04:00
|
|
|
{
|
|
|
|
m_HTTPServer.RequestFinished(*this, *m_CurrentRequest);
|
|
|
|
}
|
2013-10-06 10:40:28 -04:00
|
|
|
m_HTTPServer.CloseConnection(*this);
|
2013-09-27 13:34:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|