2013-09-27 12:14:26 -04:00
|
|
|
|
|
|
|
// HTTPMessage.cpp
|
|
|
|
|
|
|
|
// Declares the cHTTPMessage class representing the common ancestor for HTTP request and response classes
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "HTTPMessage.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
// Disable MSVC warnings:
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4355) // 'this' : used in base member initializer list
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cHTTPMessage:
|
|
|
|
|
|
|
|
cHTTPMessage::cHTTPMessage(eKind a_Kind) :
|
2013-10-04 07:07:57 -04:00
|
|
|
m_Kind(a_Kind),
|
|
|
|
m_ContentLength(-1)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPMessage::AddHeader(const AString & a_Key, const AString & a_Value)
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
AString Key = a_Key;
|
|
|
|
StrToLower(Key);
|
|
|
|
cNameValueMap::iterator itr = m_Headers.find(Key);
|
2013-09-27 12:14:26 -04:00
|
|
|
if (itr == m_Headers.end())
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_Headers[Key] = a_Value;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The header-field key is specified multiple times, combine into comma-separated list (RFC 2616 @ 4.2)
|
|
|
|
itr->second.append(", ");
|
|
|
|
itr->second.append(a_Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special processing for well-known headers:
|
2013-10-04 07:07:57 -04:00
|
|
|
if (Key == "content-type")
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_ContentType = m_Headers[Key];
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
else if (Key == "content-length")
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_ContentLength = atoi(m_Headers[Key].c_str());
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cHTTPRequest:
|
|
|
|
|
|
|
|
cHTTPRequest::cHTTPRequest(void) :
|
2013-09-27 15:38:54 -04:00
|
|
|
super(mkRequest),
|
2013-10-04 07:07:57 -04:00
|
|
|
m_EnvelopeParser(*this),
|
|
|
|
m_IsValid(true),
|
2013-10-04 14:28:30 -04:00
|
|
|
m_UserData(NULL),
|
2014-01-18 14:20:56 -05:00
|
|
|
m_HasAuth(false),
|
|
|
|
m_AllowKeepAlive(false)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
int cHTTPRequest::ParseHeaders(const char * a_Data, int a_Size)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
if (!m_IsValid)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
return -1;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
if (m_Method.empty())
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
// The first line hasn't been processed yet
|
|
|
|
int res = ParseRequestLine(a_Data, a_Size);
|
|
|
|
if ((res < 0) || (res == a_Size))
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
int res2 = m_EnvelopeParser.Parse(a_Data + res, a_Size - res);
|
|
|
|
if (res2 < 0)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IsValid = false;
|
|
|
|
return res2;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
return res2 + res;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
if (m_EnvelopeParser.IsInHeaders())
|
2013-09-27 14:33:18 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
int res = m_EnvelopeParser.Parse(a_Data, a_Size);
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
m_IsValid = false;
|
|
|
|
}
|
|
|
|
return res;
|
2013-09-27 14:33:18 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
return 0;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-28 13:30:25 -04:00
|
|
|
|
2013-10-05 17:08:16 -04:00
|
|
|
AString cHTTPRequest::GetBareURL(void) const
|
|
|
|
{
|
|
|
|
size_t idxQM = m_URL.find('?');
|
|
|
|
if (idxQM != AString::npos)
|
|
|
|
{
|
|
|
|
return m_URL.substr(0, idxQM);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_URL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
int cHTTPRequest::ParseRequestLine(const char * a_Data, int a_Size)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IncomingHeaderData.append(a_Data, a_Size);
|
|
|
|
size_t IdxEnd = m_IncomingHeaderData.size();
|
|
|
|
|
2013-09-27 12:14:26 -04:00
|
|
|
// Ignore the initial CRLFs (HTTP spec's "should")
|
|
|
|
size_t LineStart = 0;
|
|
|
|
while (
|
2013-10-04 07:07:57 -04:00
|
|
|
(LineStart < IdxEnd) &&
|
2013-09-27 12:14:26 -04:00
|
|
|
(
|
2013-10-04 07:07:57 -04:00
|
|
|
(m_IncomingHeaderData[LineStart] == '\r') ||
|
|
|
|
(m_IncomingHeaderData[LineStart] == '\n')
|
2013-09-27 12:14:26 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
LineStart++;
|
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
if (LineStart >= IdxEnd)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IsValid = false;
|
|
|
|
return -1;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int NumSpaces = 0;
|
2013-10-04 07:07:57 -04:00
|
|
|
size_t MethodEnd = 0;
|
|
|
|
size_t URLEnd = 0;
|
|
|
|
for (size_t i = LineStart; i < IdxEnd; i++)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
switch (m_IncomingHeaderData[i])
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
{
|
|
|
|
switch (NumSpaces)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
MethodEnd = i;
|
2013-09-27 12:14:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
URLEnd = i;
|
2013-09-27 12:14:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Too many spaces in the request
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IsValid = false;
|
|
|
|
return -1;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NumSpaces += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\n':
|
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
if ((i == 0) || (m_IncomingHeaderData[i - 1] != '\r') || (NumSpaces != 2) || (i < URLEnd + 7))
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
|
|
|
// LF too early, without a CR, without two preceeding spaces or too soon after the second space
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IsValid = false;
|
|
|
|
return -1;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
// Check that there's HTTP/version at the end
|
2013-10-04 07:07:57 -04:00
|
|
|
if (strncmp(a_Data + URLEnd + 1, "HTTP/1.", 7) != 0)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 07:07:57 -04:00
|
|
|
m_IsValid = false;
|
|
|
|
return -1;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
m_Method = m_IncomingHeaderData.substr(LineStart, MethodEnd - LineStart);
|
|
|
|
m_URL = m_IncomingHeaderData.substr(MethodEnd + 1, URLEnd - MethodEnd - 1);
|
2013-09-27 12:14:26 -04:00
|
|
|
return i + 1;
|
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
} // switch (m_IncomingHeaderData[i])
|
2013-09-27 12:14:26 -04:00
|
|
|
} // for i - m_IncomingHeaderData[]
|
2013-10-04 07:07:57 -04:00
|
|
|
|
|
|
|
// CRLF hasn't been encountered yet, consider all data consumed
|
|
|
|
return a_Size;
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
void cHTTPRequest::OnHeaderLine(const AString & a_Key, const AString & a_Value)
|
2013-09-27 12:14:26 -04:00
|
|
|
{
|
2013-10-04 14:28:30 -04:00
|
|
|
if (
|
|
|
|
(NoCaseCompare(a_Key, "Authorization") == 0) &&
|
|
|
|
(strncmp(a_Value.c_str(), "Basic ", 6) == 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
AString UserPass = Base64Decode(a_Value.substr(6));
|
|
|
|
size_t idxCol = UserPass.find(':');
|
|
|
|
if (idxCol != AString::npos)
|
|
|
|
{
|
|
|
|
m_AuthUsername = UserPass.substr(0, idxCol);
|
|
|
|
m_AuthPassword = UserPass.substr(idxCol + 1);
|
|
|
|
m_HasAuth = true;
|
|
|
|
}
|
|
|
|
}
|
2014-01-18 14:20:56 -05:00
|
|
|
if ((a_Key == "Connection") && (NoCaseCompare(a_Value, "keep-alive") == 0))
|
|
|
|
{
|
|
|
|
m_AllowKeepAlive = true;
|
|
|
|
}
|
2013-10-04 07:07:57 -04:00
|
|
|
AddHeader(a_Key, a_Value);
|
2013-09-27 12:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cHTTPResponse:
|
|
|
|
|
|
|
|
cHTTPResponse::cHTTPResponse(void) :
|
|
|
|
super(mkResponse)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cHTTPResponse::AppendToData(AString & a_DataStream) const
|
|
|
|
{
|
2013-09-27 14:33:18 -04:00
|
|
|
a_DataStream.append("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: ");
|
2013-09-27 12:14:26 -04:00
|
|
|
a_DataStream.append(m_ContentType);
|
|
|
|
a_DataStream.append("\r\n");
|
|
|
|
for (cNameValueMap::const_iterator itr = m_Headers.begin(), end = m_Headers.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
if ((itr->first == "Content-Type") || (itr->first == "Content-Length"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
a_DataStream.append(itr->first);
|
|
|
|
a_DataStream.append(": ");
|
|
|
|
a_DataStream.append(itr->second);
|
|
|
|
a_DataStream.append("\r\n");
|
|
|
|
} // for itr - m_Headers[]
|
|
|
|
a_DataStream.append("\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|