📄 httprequest.cpp
字号:
/*
* Openmysee
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// HttpDownload.cpp: implementation of the HttpDownload class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <LIMITS.H>
#include <TCHAR.H>
#include "HttpRequest.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef _MYDEBUG
#undef THIS_FILE
static TCHAR THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BOOL HttpRequest::GetCookieEx(LPCTSTR lpszUrlName, LPCTSTR lpszCookieDataName, LPTSTR lpszCookieData, LPDWORD lpdwSize)
{
DWORD len = 0;
InternetGetCookie(lpszUrlName, NULL, NULL, &len);
if(len == 0)
{
return FALSE; //no cookie exist
}
len += 10;
LPTSTR bufcookiedata = new TCHAR[len];
memset(bufcookiedata, 0, sizeof(TCHAR)*len);
if(InternetGetCookie(lpszUrlName, NULL, bufcookiedata, &len) != TRUE)
{
delete[] bufcookiedata;
return FALSE;
}
bool parseOK = false;
while(1)
{
LPTSTR datastart = _tcsstr(bufcookiedata, lpszCookieDataName);
if(datastart == NULL)
return FALSE;
LPTSTR indexspace = NULL;
LPTSTR indexequal = NULL;
indexequal = _tcschr(datastart, _T('='));
if(indexequal == NULL)
break;
indexequal++;
indexspace = _tcschr(datastart, _T(';'));
if(indexspace == NULL) //这是最后一个data
indexspace = _tcschr(datastart, 0);
assert(indexspace > indexequal);
DWORD m_actuallen = indexspace - indexequal + 1; //len + null terminnal
assert(lpdwSize != NULL);
if(lpszCookieData == NULL || *lpdwSize < m_actuallen)
{
*lpdwSize = m_actuallen;
}
else
{
memset(lpszCookieData, 0, sizeof(TCHAR)*(*lpdwSize));
_tcsncpy(lpszCookieData, indexequal, indexspace - indexequal);
}
parseOK = true;
break;
}
delete[] bufcookiedata;
if(!parseOK)
{
return FALSE;
}
else
return TRUE;
}
//HTTP信标
#define HTTP_REQUEST_LIMIT_NAME _T("_GAOV_HTTP_REQUEST_LIMIT")
static HANDLE m_hSemaphore = CreateSemaphore(NULL, 5, 5, HTTP_REQUEST_LIMIT_NAME);
int HttpRequest::b_useproxy = 0;
TCHAR HttpRequest::m_proxystr[256] = _T("");
#define INTERNET_CONNECT_ADAGENT _T("Mysee_Client")
void HttpRequest::SetProxyName(LPCWSTR s)
{
#ifdef _UNICODE
wcscpy(m_proxystr, s);
#else
char* m_innerbufferA = new char[1024];
WideCharToMultiByte(CP_ACP, 0, s, -1, m_innerbufferA, 1024, NULL, NULL);
strcpy(m_proxystr, m_innerbufferA);
delete[] m_innerbufferA;
#endif
}
void HttpRequest::SetProxyName(LPCSTR s)
{
#ifdef _UNICODE
WCHAR* m_innerbufferW = new WCHAR[1024];
MultiByteToWideChar(CP_ACP, 0, s, -1, m_innerbufferW, 1024);
wcscpy(m_proxystr, m_innerbufferW);
delete[] m_innerbufferW;
#else
strcpy(m_proxystr, s);
#endif
}
HttpRequest::HttpRequest()
{
Connect_timeout = 60000;
Request_timeout = 120000;
recvBuf = NULL;
// HSO = NULL;
m_hInternet = NULL;
errorcode = 0;
// InitializeCriticalSection(&m_hSync);
for(int j = 0; j<EVENT_SERIES; j++)
m_hEvent[j] = CreateEvent(NULL, TRUE, FALSE, NULL);
ResetEvent(m_hEvent[0]);
fWide = FALSE;
}
BOOL HttpRequest::Create(UINT cto, UINT rto, BOOL wide)
{
// b_userStop = FALSE;
fWide = wide;
h_File = NULL;
Connect_timeout = cto;
Request_timeout = rto;
errorcode = 0;
m_dwOffset = m_dwOffsetEnd = UINT_MAX;
// DWORD wtmp;
// ResetEvent(m_hEvent[0]);
// ::CreateThread(NULL, 0, InitialCallback, this, 0, &wtmp);
// WaitForSingleObject(m_hEvent[0], INFINITE);
// if(errorcode != 0)return FALSE;
return TRUE;
}
HttpRequest::~HttpRequest()
{
for(int j = 0; j<EVENT_SERIES;j++)
CloseHandle(m_hEvent[j]);
// DeleteCriticalSection(&m_hSync);
if(recvBuf)delete[] recvBuf;
}
void HttpRequest::SetStopRequest()
{
SetEvent(m_hEvent[0]);
}
void HttpRequest::ResumeStopRequest()
{
ResetEvent(m_hEvent[0]);
}
UINT HttpRequest::WaitingEvent(DWORD timeout)
{
if((errorcode = GetLastError()) != ERROR_IO_PENDING)
{
return 1;
}
DWORD result = WaitForMultipleObjects(EVENT_SERIES, m_hEvent,
FALSE, timeout);
switch(result)
{
case WAIT_OBJECT_0: //外部中止
errorcode = USER_STOP;
return 1;
case WAIT_OBJECT_0 + 1: //内部中止
if(errorcode)
return 1;
else
return 0;
case WAIT_TIMEOUT: //连接超时
errorcode = 32808;
return 1;
case WAIT_FAILED: //严重错误
errorcode = ERROR_STOP;
return 1;
}
return 1;
}
void HttpRequest::HDResetEvent()
{
for(int i = 1; i< EVENT_SERIES; i++)
ResetEvent(m_hEvent[i]);
}
int HttpRequest::ReadStringEx()
{
if(recvBuf == NULL)return -1;
if(readstrpos >= buflength)return -1;
int oldpos = -1;
if(fWide)
{
WCHAR* head = (WCHAR*) recvBuf;
for(UINT i = readstrpos; (i*sizeof(WCHAR))<buflength; i++)
{
if(*(head + i) == L'\n')break;
}
oldpos = readstrpos;
readstrpos = i+1;
if(i != readstrpos && *(head + i - 1) == L'\r')
*(head + i - 1) = L'\0';
*(head + i) = L'\0';
}
else
{
char* head = (char*) recvBuf;
for(UINT i = readstrpos; i<buflength; i++)
{
if(*(head + i) == '\n')break;
}
oldpos = readstrpos;
readstrpos = i+1;
if(i != readstrpos && *(head + i - 1) == '\r')
*(head + i - 1) = '\0';
*(head + i) = '\0';
}
return oldpos;
}
BOOL HttpRequest::ReadString(LPWSTR readstr)
{
int pos = HttpRequest::ReadStringEx();
if(pos == -1)return FALSE;
if(fWide)
wcscpy(readstr,(LPCWSTR) recvBuf + pos);
else
{
MultiByteToWideChar(CP_ACP, 0, (LPCSTR) recvBuf + pos, -1, readstr, 2048);
}
return TRUE;
}
BOOL HttpRequest::ReadString(LPSTR readstr)
{
int pos = HttpRequest::ReadStringEx();
if(pos == -1)return FALSE;
if(!fWide)
strcpy(readstr,(LPCSTR) recvBuf + pos);
else
{
WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) recvBuf + pos, -1, readstr, 2048, NULL, NULL);
}
return TRUE;
}
void HttpRequest::InitWrite(DWORD initialbufsize)
{
if(h_File)
{
if(GetFileSize(h_File, NULL) != -1)
{
m_dwOffset = 0;
buflength = UINT_MAX;
return; //a valid file
}
CloseHandle(h_File);
h_File = NULL; //clear the invalid file handle and using inner buffer
initialbufsize = 1024;
}
if(recvBuf)delete[] recvBuf;
recvBuf = new BYTE[initialbufsize + 16];
memset(recvBuf, 0, initialbufsize + 16);
buflength = initialbufsize;
m_dwOffset = 0;
}
BOOL HttpRequest::WriteBuffer(const BYTE* buf, const DWORD m_buflength)
{
if(h_File)
{
DWORD tmpLength = m_buflength;
const BYTE* start = buf;
while(tmpLength > 0)
{
DWORD tmp = 0;
if(WriteFile(h_File, start, tmpLength, &tmp, NULL) == FALSE)
{
errorcode = GetLastError();
return FALSE;
}
tmpLength -= tmp;
start += tmp;
}
}
else
{
if(m_dwOffset + m_buflength > buflength)
{
BYTE* tmp = new BYTE[buflength*3 + 16];
memset(tmp, 0, buflength*3 + 16);
memcpy(tmp, recvBuf, buflength);
delete[] recvBuf;
recvBuf = tmp;
buflength = buflength* 3;
}
memcpy(recvBuf + m_dwOffset, buf, m_buflength);
}
m_dwOffset += m_buflength;
return TRUE;
}
void HttpRequest::InternetCloseHandleAsync(HINTERNET Hcls)
{
if(Hcls == NULL)
return;
ResetEvent(m_hEvent[1]);
if(!InternetCloseHandle(Hcls))
{
if(GetLastError() == ERROR_IO_PENDING)
WaitForSingleObject(m_hEvent[1], INFINITE);
}
SetEvent(m_hEvent[1]);
}
static const TCHAR _defaultContentType[] = _T("Content-Type: application/x-www-form-urlencoded");
DWORD WINAPI HttpRequest::InitialCallback(LPVOID params)
{
/* HttpRequest* pObj = (HttpRequest*) params;
if(::InternetSetStatusCallback(pObj->HSO, AsyncInternetCallback) == INTERNET_INVALID_STATUS_CALLBACK)
pObj->errorcode = 1;
SetEvent(pObj->m_hEvent[0]);*/
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -