bool CPeerCacheUpSocket::ProcessHttpRequest() { if (GetClient() == NULL) throw CString(__FUNCTION__ " - No client attached to HTTP socket"); UINT uHttpRes = GetClient()->ProcessPeerCacheUpHttpRequest(m_astrHttpHeaders); if (uHttpRes != HTTP_STATUS_OK){ CStringA strResponse; strResponse.AppendFormat("HTTP/1.0 %u\r\n", uHttpRes); strResponse.AppendFormat("Content-Length: 0\r\n"); strResponse.AppendFormat("\r\n"); if (thePrefs.GetDebugClientTCPLevel() > 0) Debug(_T("Sending PeerCache HTTP respone:\n%hs"), strResponse); CRawPacket* pHttpPacket = new CRawPacket(strResponse); theStats.AddUpDataOverheadFileRequest(pHttpPacket->size); SendPacket(pHttpPacket); SetHttpState(HttpStateUnknown); // PC-TODO: Problem, the packet which was queued for sending will not be sent, if we immediatly // close that socket. Currently I just let it timeout. //return false; SetTimeOut(SEC2MS(30)); return true; } GetClient()->SetHttpSendState(0); SetHttpState(HttpStateRecvExpected); GetClient()->SetUploadState(US_UPLOADING); return true; }
void CSendGetUrlReqSocket::OnConnect(int nErrorCode) { if (0 != nErrorCode) { if (thePrefs.GetVerbose()) { CString strUrlPath(GetUrlPath()); CString strServer(GetServer()); strUrlPath.Replace(_T("%"), _T("%%")); AddDebugLogLine(false, _T("将要取%s,但连接%s返回失败。"), strUrlPath, strServer); } return; } CStringA strHttpRequest; strHttpRequest.AppendFormat("%s %s HTTP/1.0\r\n", m_bIsPost ? "POST" : "GET", GetUrlPath()); strHttpRequest.AppendFormat("Host: %s\r\n", GetServer()); strHttpRequest.AppendFormat("Accept: */*\r\n"); if(m_bIsPost) { strHttpRequest.AppendFormat("Accept-Encoding: none\r\n"); strHttpRequest.AppendFormat("Content-Type: application/x-www-form-urlencoded\r\n"); strHttpRequest.AppendFormat("Content-Length: %d\r\n", m_strPost.GetLength()); } //strHttpRequest.AppendFormat("Connection: Keep-Alive\r\n"); strHttpRequest.Append("\r\n"); if(m_bIsPost) { strHttpRequest.Append(m_strPost); } if (thePrefs.GetVerbose()) { CString strRequest(strHttpRequest); strRequest.Replace(_T("%"), _T("%%")); AddDebugLogLine(false, _T("与服务器 %s 连接成功,准备发送:"), CString(GetServer())); AddDebugLogLine(false, strRequest); } CRawPacket* pHttpPacket = new CRawPacket(strHttpRequest); SendPacket(pHttpPacket); SetHttpState(HttpStateRecvExpected); }
void CSendGetUrlReqSocket::OnConnect(int nErrorCode) { if (0 != nErrorCode) { if (thePrefs.GetVerbose()) { CString strUrlPath(GetUrlPath()); CString strServer(GetServer()); strUrlPath.Replace(_T("%"), _T("%%")); AddDebugLogLine(false, _T("将要取%s,但连接%s返回失败。"), strUrlPath, strServer); } return; } CStringA strHttpRequest; strHttpRequest.AppendFormat("GET %s HTTP/1.0\r\n", GetUrlPath()); strHttpRequest.AppendFormat("Host: %s\r\n", GetServer()); strHttpRequest.AppendFormat("Accept: */*\r\n"); //strHttpRequest.AppendFormat("Connection: Keep-Alive\r\n"); strHttpRequest.AppendFormat("\r\n"); if (thePrefs.GetVerbose()) { CString strRequest(strHttpRequest); strRequest.Replace(_T("%"), _T("%%")); AddDebugLogLine(false, _T("与服务器 %s 连接成功,准备发送:"), CString(GetServer())); AddDebugLogLine(false, strRequest); } CRawPacket* pHttpPacket = new CRawPacket(strHttpRequest); SendPacket(pHttpPacket); SetHttpState(HttpStateRecvExpected); }
CHttpClientReqSocket::CHttpClientReqSocket(CUpDownClient* client) : CClientReqSocket(client) { SetHttpState(HttpStateUnknown); SetConnectionEncryption(false, NULL, false); // just to make sure - disable protocol encryption explicit }
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; }