DWORD DownloadThread(LPVOID lpdwThreadParam)
{
CDownload* Download = (CDownload*)lpdwThreadParam;
WSADATA WS2Info;
if (!WSAStartup(MAKEWORD(2,2), &WS2Info))
{
SOCKET IPv4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (IPv4 != INVALID_SOCKET)
{
hostent *ResolveHost;
char *HostName = new char[wcslen(Download->HostName)+1];
wcstombs(HostName, Download->HostName, wcslen(Download->HostName)+1);
HostName[wcslen(Download->HostName)] = '\0';
ResolveHost = gethostbyname(HostName);
if (ResolveHost)
{
SOCKADDR_IN HostService;
HostService.sin_family = AF_INET;
HostService.sin_addr.s_addr=*((unsigned long*)ResolveHost->h_addr);
HostService.sin_port = htons(Download->Port);
if (connect(IPv4, (SOCKADDR*) &HostService, sizeof(HostService)) != SOCKET_ERROR)
{
char *HTTPrequest = new char[DEFAULT_BUFLEN];
char *RequestURI = new char[wcslen(Download->HostFileLocation)+1];
wcstombs(RequestURI, Download->HostFileLocation, wcslen(Download->HostFileLocation)+1);
RequestURI[wcslen(Download->HostFileLocation)] = '\0';
char *Host = new char[wcslen(Download->HostName)+1];
wcstombs(Host, Download->HostName, wcslen(Download->HostName)+1);
Host[wcslen(Download->HostName)] = '\0';
char *UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2";
sprintf(HTTPrequest,"GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: */*\r\nAccept-Encoding:\r\nRange: bytes=%lld-\r\n\r\n",
RequestURI, Host, UserAgent, Download->BytesDownloaded);
send(IPv4,HTTPrequest,strlen(HTTPrequest),0);
delete[] HTTPrequest;
delete[] RequestURI, Host, UserAgent;
char *HTTPresponse = new char[DEFAULT_BUFLEN];
SecureZeroMemory(HTTPresponse, sizeof(HTTPresponse));
recv(IPv4, HTTPresponse, sizeof(HTTPresponse), 0);
********
******** HTTP parsing code
********
int PFDB = 0;
for (int n = 0; n < DEFAULT_BUFLEN; n++)
{
if (HTTPresponse[n] == '\r' && HTTPresponse[n-2] == '\r')
if (HTTPresponse[n-1] == '\n' && HTTPresponse[n+1] == '\n')
PFDB = n+2;
}
DWORD BytesReceived = sizeof(HTTPresponse)-PFDB;
int SPEED = 512;
char *Data = new char[SPEED];
SecureZeroMemory(Data, sizeof(Data));
for (int n1 = 0, n2 = PFDB; n2 < DEFAULT_BUFLEN; n1++, n2++)
Data[n1] = HTTPresponse[n2];
Data[DEFAULT_BUFLEN-PFDB] = '\0';
delete[] HTTPresponse;
int DestinationLength = (wcslen(Download->FileLocation)+wcslen(Download->FileName));
wchar_t *LocalPathPFile = new wchar_t[DestinationLength+1];
wcscpy(LocalPathPFile, Download->FileLocation);
wcscat(LocalPathPFile, Download->FileName);
LocalPathPFile[DestinationLength] = '\0';
HANDLE File;
if (Download->BytesDownloaded == 0)
{
File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_FLAG_NO_BUFFERING, NULL)
if (File == INVALID_HANDLE_VALUE)
Tu(L"File Error!");
}
else
{
File = CreateFile(LocalPathPFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING, NULL);
if (File == INVALID_HANDLE_VALUE)
Tu(L"File Error!");
SetFilePointer(File, 0, NULL, FILE_END);
}
OVERLAPPED *Struc;
do
{
Download->BytesDownloaded += BytesReceived;
TotalBytesDownloaded += BytesReceived;
if (!WriteFile(File, Data, BytesReceived, NULL, NULL))
{
wchar_t Error[100];
swprintf(Error, L"Write Error: %d", GetLastError());
Tu(Error);
}
BytesReceived = recv(IPv4, Data, sizeof(Data), 0);
}
while (Download->DownloadState == Download->ACTIVE
&& Download->BytesDownloaded < Download->BytesToDownload);
delete[] Data;
shutdown(IPv4, SD_BOTH);
CloseHandle(File);
if (Download->BytesToDownload == Download->BytesDownloaded)
{
TotalBytesDownloaded -= Download->BytesToDownload;
TotalBytesToDownload -= Download->BytesToDownload;
Download->DownloadState = Download->FINISHED;
}
else
{
TotalBytesDownloaded -= Download->BytesDownloaded;
TotalBytesToDownload -= Download->BytesToDownload;
Download->DownloadState = Download->PAUSED;
}
}
}
}
else
closesocket(IPv4);
}
WSACleanup();
ExitThread(0);
}
|