Ejemplo n.º 1
0
void CAppWindow::ProcessAvatarResponse()
{
	ASSERT(m_lpAvatarRequest != NULL);

	CCurlBinaryReader * lpReader = (CCurlBinaryReader *)m_lpAvatarRequest->GetReader();

	CWaveContact * lpContact = GetWaveContact(m_szRequestingAvatar);

	if (lpContact != NULL)
	{
		lpContact->SetRequestedAvatar(TRUE);

		if (m_lpAvatarRequest->GetResult() == CURLE_OK && m_lpAvatarRequest->GetStatus() == 200)
		{
			SIZE szSize = { PL_CO_ICON_SIZE, PL_CO_ICON_SIZE };

			wstring szContentType(m_lpAvatarRequest->GetHeader(L"Content-Type"));

			if (!szContentType.empty())
			{
				lpContact->SetAvatar(
					CAvatar::Create(lpReader->GetData(), szSize, szContentType)
				);
			}
		}
		else
		{
			LOG2("Could not download avatar %S (%d)",
				m_lpAvatarRequest->GetUrl().c_str(),
				(int)m_lpAvatarRequest->GetStatus());
		}
	}

	delete lpReader;
	delete m_lpAvatarRequest;

	m_lpAvatarRequest = NULL;
	m_szRequestingAvatar = L"";

	SeedAvatars();
}
Ejemplo n.º 2
0
BOOL CHTTPServer::ParseRequest(string szRequest, string &szResponse, BOOL &bKeepAlive)
{
	//
	// Simple Parsing of Request
	//
	string szMethod;
	string szFileName;
	string szFileExt;
	string szStatusCode("200 OK");
	string szContentType("text/html");
	string szConnectionType("close");
	string szNotFoundMessage;
	string szDateTime;
	char pResponseHeader[2048];
	unsigned int lengthActual = 0, length = 0;
	char *pBuf = NULL;
	int n;
				
	//
	// Check Method
	//
	n = szRequest.find(" ", 0);
	if(n != string::npos)
	{
		szMethod = szRequest.substr(0, n);
		if(szMethod == "GET")
		{
			//
			// Get file name
			// 
			int n1 = szRequest.find(" ", n + 1);
			if(n != string::npos)
			{
				szFileName = szRequest.substr(n + 1, n1 - n - 1);
				if(szFileName == "/")
				{
					szFileName = m_DefIndex;
				}
			}
			else
			{
				LogMessage(LOGFILENAME, _T("No 'space' found in Request String #1"), _T("ParseRequest"));
				return FALSE;
			}
		}
		else
		{
			szStatusCode = "501 Not Implemented";
			szFileName = ERROR501;
		}
	}
	else
	{
		LogMessage(LOGFILENAME, _T("No 'space' found in Request String #2"), _T("ParseRequest"));
		return FALSE;
	}

	//
	// Determine Connection type
	//
	n = szRequest.find("\nConnection: Keep-Alive", 0);
	if(n != string::npos)
		bKeepAlive = TRUE;

	//
	// Figure out content type
	//
	int nPointPos = szFileName.rfind(".");
	if(nPointPos != string::npos)
	{
		szFileExt = szFileName.substr(nPointPos + 1, szFileName.size());
		strlwr((char*)szFileExt.c_str());
		MIMETYPES::iterator it;
		it = MimeTypes.find(szFileExt);
		if(it != MimeTypes.end())
			szContentType = (*it).second;
	}

	//
	// Obtain current GMT date/time
	//
	char szDT[128];
	struct tm *newtime;
	time_t ltime;
	
	time(&ltime);
	newtime = gmtime(&ltime);
	strftime(szDT, 128,
		"%a, %d %b %Y %H:%M:%S GMT", newtime);

	//
	// Read the file
	//
	HANDLE f;
	f = m_pFileReader->Open((m_HomeDir + szFileName).c_str());
	if(f != NULL)				
	{
		// Retrive file size
        lengthActual = (unsigned int)m_pFileReader->Length(f);
		
		pBuf = new char[lengthActual + 1];
        
		length = (unsigned int)m_pFileReader->Read(f,pBuf, lengthActual);
		m_pFileReader->Close(f);

		//
		// Make Response
		//
		sprintf(pResponseHeader, "HTTP/1.0 %s\r\nDate: %s\r\nServer: %s\r\nAccept-Ranges: bytes\r\nContent-Length: %d\r\nConnection: %s\r\nContent-Type: %s\r\n\r\n",
			szStatusCode.c_str(), szDT, SERVERNAME, (int)length, bKeepAlive ? "Keep-Alive" : "close", szContentType.c_str());
	}
	else
	{
		//
		// In case of file not found	   
		//
        f = m_pFileReader->Open((m_HomeDir + ERROR404).c_str());
		if(f != NULL)				
		{
			// Retrive file size
            lengthActual = (unsigned int)m_pFileReader->Length(f);

            pBuf = new char[lengthActual + 1];

            length = (unsigned int)m_pFileReader->Read(f,pBuf, lengthActual);
            m_pFileReader->Close(f);
			szNotFoundMessage = string(pBuf, length);
			delete pBuf;
			pBuf = NULL;
		}
		szStatusCode = "404 Resource not found";

		sprintf(pResponseHeader, "HTTP/1.0 %s\r\nContent-Length: %d\r\nContent-Type: text/html\r\nDate: %s\r\nServer: %s\r\n\r\n%s",
			szStatusCode.c_str(), szNotFoundMessage.size(), szDT, SERVERNAME, szNotFoundMessage.c_str());
		bKeepAlive = FALSE;  
	}

	szResponse = string(pResponseHeader);
	if(pBuf)
		szResponse += string(pBuf, length);
	delete pBuf;
	pBuf = NULL;
	

	return TRUE;
}