bool CHttpClientReqSocket::ProcessHttpPacket(const BYTE* pucData, UINT uSize)
{
	if (GetHttpState() == HttpStateRecvExpected || GetHttpState() == HttpStateRecvHeaders)
	{
		// search for EOH
		LPBYTE pBody = NULL;
		int iSizeBody = 0;
		ProcessHttpHeaderPacket((const char*)pucData, uSize, pBody, iSizeBody);

		if (pBody) // EOH found, packet may contain partial body
		{
			if (thePrefs.GetDebugClientTCPLevel() > 0){
				Debug(_T("Received HTTP\n"));
				DebugHttpHeaders(m_astrHttpHeaders);
			}

			// PC-TODO: Should be done right in 'ProcessHttpHeaderPacket'
			int iSizeHeader = 2;
			for (int i = 0; i < m_astrHttpHeaders.GetCount(); i++)
				iSizeHeader += m_astrHttpHeaders[i].GetLength() + 2;
			theStats.AddDownDataOverheadFileRequest(iSizeHeader);

			if (iSizeBody < 0)
				throw CString(_T("Internal HTTP header/body parsing error"));

			if (m_astrHttpHeaders[0].GetLength() >= 4 && memcmp((LPCSTR)m_astrHttpHeaders[0], "HTTP", 4) == 0)
			{
				if (!ProcessHttpResponse())
					return false;

				SetHttpState(HttpStateRecvBody);
				if (iSizeBody > 0){
					// packet contained HTTP headers and (partial) body
					ProcessHttpResponseBody(pBody, iSizeBody);
				}
				else{
					// packet contained HTTP headers but no body (packet terminates with EOH)
					// body will be processed because of HTTP state 'HttpStateRecvBody' with next recv
					;
				}
			}
			else if (m_astrHttpHeaders[0].GetLength() >= 3 && memcmp((LPCSTR)m_astrHttpHeaders[0], "GET", 3) == 0)
			{
				if (!ProcessHttpRequest())
					return false;
				if (iSizeBody != 0){
					ASSERT(0); // no body for GET requests allowed yet
					return false;
				}
			}
			else
				throw CString(_T("Invalid HTTP header received"));
		}
		else
		{
			TRACE("+++ Received partial HTTP header packet\n");
		}
	}
	else if (GetHttpState() == HttpStateRecvBody)
	{
		ProcessHttpResponseBody(pucData, uSize);
	}
	else{
		theStats.AddDownDataOverheadFileRequest(uSize);
		throw CString(_T("Invalid HTTP socket state"));
	}

	return true;
}
Example #2
0
void ProcessHttpMessage()
{
	ProcessHttpRequest();
	ProcessHttpResponse();
}