Repository navigation
Expand file tree
/
Copy pathInternetDownload.cpp
More file actions
268 lines (216 loc) · 8.29 KB
/
Copy pathInternetDownload.cpp
File metadata and controls
268 lines (216 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "precompiled_headers.h"
#include "InternetDownload.h"
#include "libinstall/CancelToken.h"
InternetDownload::InternetDownload(HWND parentHwnd, const tstring& userAgent, const tstring& url, CancelToken cancelToken, std::function<void(int)> progressFunction /* = NULL */)
: m_progressFunction(progressFunction),
m_url(url),
m_hInternet(NULL),
m_hConnect(NULL),
m_hHttp(NULL),
m_cancelToken(cancelToken),
m_parentHwnd(parentHwnd),
m_receivedBytes(0),
m_error(0),
m_flags(0)
{
m_requestComplete = ::CreateEvent(NULL, TRUE /*manualReset*/, FALSE /*initialState*/, NULL /*name*/);
m_responseReceived = ::CreateEvent(NULL, TRUE /*manualReset*/, FALSE /*initialState*/, NULL /*name*/);
m_hInternet = ::InternetOpen(userAgent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL /* proxy*/ , NULL /* proxy bypass */, INTERNET_FLAG_ASYNC /* dwflags */);
INTERNET_STATUS_CALLBACK ourCallback = &InternetDownload::statusCallback;
::InternetSetStatusCallback(m_hInternet, ourCallback);
DWORD timeout = 120000;
::InternetSetOption(m_hInternet, INTERNET_OPTION_RECEIVE_TIMEOUT, &timeout, sizeof(DWORD));
::InternetSetOption(m_hInternet, INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof(DWORD));
}
InternetDownload::~InternetDownload()
{
if (m_hHttp) {
::InternetCloseHandle(m_hHttp);
m_hHttp = NULL;
}
if (m_hConnect) {
::InternetCloseHandle(m_hConnect);
m_hConnect = NULL;
}
if (m_hInternet) {
::InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_responseReceived) {
::CloseHandle(m_responseReceived);
m_responseReceived = NULL;
}
if (m_requestComplete) {
::CloseHandle(m_requestComplete);
m_requestComplete = NULL;
}
}
void InternetDownload::statusCallback( HINTERNET /* hInternet */,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD /* dwStatusInformationLength */)
{
InternetDownload *download = reinterpret_cast<InternetDownload*>(dwContext);
switch(dwInternetStatus) {
case INTERNET_STATUS_RECEIVING_RESPONSE:
download->receivingResponse();
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
download->responseReceived();
break;
case INTERNET_STATUS_HANDLE_CREATED:
download->m_hHttp = (*(HANDLE*)lpvStatusInformation);
break;
default:
break;
}
}
void InternetDownload::disableCache() {
m_flags = INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RESYNCHRONIZE;
}
BOOL InternetDownload::request() {
if (!m_hInternet) {
return FALSE;
}
m_hHttp = InternetOpenUrl(m_hInternet, m_url.c_str(), NULL, 0, m_flags, reinterpret_cast<DWORD_PTR>(this));
return TRUE;
}
BOOL InternetDownload::waitForHandle(HANDLE handle)
{
HANDLE waitHandles[2];
waitHandles[0] = m_cancelToken.getToken();
waitHandles[1] = handle;
int waitResponse = WaitForMultipleObjects(2, waitHandles, FALSE, 60000);
switch(waitResponse) {
case WAIT_OBJECT_0:
{
// cancelled
return FALSE;
}
case WAIT_TIMEOUT:
{
// More than 60seconds for response
return FALSE;
}
case WAIT_FAILED:
{
return FALSE;
}
}
return TRUE;
}
DOWNLOAD_STATUS InternetDownload::getData(writeData_t writeData, void *context)
{
if (m_error) {
return DOWNLOAD_STATUS_FAIL;
}
if (m_cancelToken.isSignalled()) {
return DOWNLOAD_STATUS_FAIL;
}
OutputDebugString(_T("Beginning getData - waiting for request completion\n"));
if (!waitForHandle(m_responseReceived)) {
return DOWNLOAD_STATUS_FAIL;
}
TCHAR headerBuffer[1024] = { 0 };
DWORD headerIndex = 0;
DWORD bufferLength = 1024;
DWORD statusCode = 0;
BOOL statusCodeSuccess = HttpQueryInfo(m_hHttp, HTTP_QUERY_STATUS_CODE, headerBuffer, &bufferLength, &headerIndex);
if (statusCodeSuccess) {
statusCode = _ttol(headerBuffer);
}
if (HTTP_STATUS_PROXY_AUTH_REQ == statusCode) {
LPVOID errorBuffer = 0;
DWORD dwFlags = FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS;
DWORD errorResult = InternetErrorDlg(m_parentHwnd, m_hHttp, ERROR_INTERNET_INCORRECT_PASSWORD, dwFlags, &errorBuffer);
if (errorResult == ERROR_INTERNET_FORCE_RETRY) {
return DOWNLOAD_STATUS_FORCE_RETRY;
}
}
bufferLength = 1024;
headerIndex = 0;
long contentLength = 0;
BOOL contentLengthSuccess = HttpQueryInfo(m_hHttp, HTTP_QUERY_CONTENT_LENGTH, headerBuffer, &bufferLength, &headerIndex);
if (contentLengthSuccess) {
contentLength = _ttol(headerBuffer);
}
bufferLength = 1024;
headerIndex = 0;
BOOL contentTypeSuccess = HttpQueryInfo(m_hHttp, HTTP_QUERY_CONTENT_TYPE, headerBuffer, &bufferLength, &headerIndex);
if (contentTypeSuccess) {
m_contentType = headerBuffer;
}
long bytesWritten = 0;
// Make point-at-which-we-receive-the-headers 5% of the total progress (arbitrarily chosen!)
if (m_progressFunction != nullptr) {
m_progressFunction(5);
}
DWORD bytesToRead = 16384;
BYTE buffer[16384]; // InternetReadFile seems to give back 8k buffers, so an 8k buffer is optimal
DWORD bytesRead = 0;
do {
receivingResponse();
int readFileResponse = InternetReadFile(m_hHttp, buffer, bytesToRead, &bytesRead);
if (!readFileResponse && ERROR_IO_PENDING == GetLastError()) {
if (!waitForHandle(m_responseReceived)) {
return DOWNLOAD_STATUS_CANCELLED;
}
} else if (!readFileResponse) {
return DOWNLOAD_STATUS_FAIL;
}
// Check the cancellation token, because otherwise we only check it if we get an ASYNC result from InternetReadFile
// That tends to happen on slow(ish) connections, but isn't guaranteed. (From what I've seen)
if (m_cancelToken.isSignalled()) {
return DOWNLOAD_STATUS_CANCELLED;
}
(*this.*writeData)(buffer, bytesRead, context);
bytesWritten += bytesRead;
if (contentLength && m_progressFunction != nullptr) {
int percent = static_cast<int>((static_cast<double>(bytesWritten) / static_cast<double>(contentLength)) * 95 + 5);
m_progressFunction(percent);
}
} while (bytesRead != 0);
return DOWNLOAD_STATUS_SUCCESS;
}
BOOL InternetDownload::saveToFile(const tstring& filename) {
if (request()) {
FILE *fp = NULL;
if (_tfopen_s(&fp, filename.c_str(), _T("wb")) == 0) {
DOWNLOAD_STATUS status = getData(&InternetDownload::writeToFile, fp);
if (fp)
{
fclose(fp);
}
if (status == DOWNLOAD_STATUS_FORCE_RETRY) {
// This is where we should have used a second class for the actual request.
::InternetCloseHandle(m_hHttp);
m_hHttp = NULL;
return saveToFile(filename);
}
return DOWNLOAD_STATUS_SUCCESS == status;
}
}
return FALSE;
}
std::string InternetDownload::getContent() {
if (request()) {
std::string result;
DOWNLOAD_STATUS status = getData(&InternetDownload::writeToString, &result);
if (DOWNLOAD_STATUS_FORCE_RETRY == status) {
::InternetCloseHandle(m_hHttp);
m_hHttp = NULL;
return getContent();
}
if (DOWNLOAD_STATUS_SUCCESS == status) {
return result;
}
}
return "";
}
void InternetDownload::writeToFile(BYTE* buffer, DWORD bufferLength, void *context) {
fwrite(buffer, bufferLength, 1, reinterpret_cast<FILE*>(context));
}
void InternetDownload::writeToString(BYTE* buffer, DWORD bufferLength, void* context) {
reinterpret_cast<std::string*>(context)->append(reinterpret_cast<char*>(buffer), static_cast<size_t>(bufferLength));
}