-
Notifications
You must be signed in to change notification settings - Fork 151
/
WinInetHelper.cpp
535 lines (404 loc) · 9.58 KB
/
WinInetHelper.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#pragma once
#include "WinInetHelper.h"
#include "Base.h"
#include <WinInet.h>
#pragma comment(lib,"Wininet.lib")
#include "StreamEx.h"
#include "ComHelper.h"
#include "StringHelper.h"
class CReadWriteMemStream : public IUnknownT<CReadWriteMemStream, IReadWriteStream>
{
public:
CStringA Buffer;
UINT64 Point;
CReadWriteMemStream()
:Point(0)
{
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
/* [annotation] */
_Out_writes_bytes_to_(cb, *pcbRead) void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbRead,
_In_opt_ UINT64* pByteOffset)
{
auto TempPoint = pByteOffset ? *pByteOffset : Point;
auto Size= Buffer.GetLength();
if (TempPoint >= Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
ULONG cbRead = min(cb, Size - TempPoint);
memcpy(pv, Buffer.GetString() + TempPoint, cbRead);
if (!pByteOffset)
Point += cbRead;
if (pcbRead)
*pcbRead = cbRead;
return cbRead == cb ? S_OK : S_FALSE;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Seek(
/* [in] */ INT64 dlibMove,
/* [in] */ DWORD dwOrigin,
/* [annotation] */
_Out_opt_ UINT64 *plibNewPosition)
{
auto Size = Buffer.GetLength();
switch (dwOrigin)
{
case FILE_CURRENT:
dlibMove += Point;
break;
case FILE_BEGIN:
break;
case FILE_END:
dlibMove += Size;
break;
default:
return __HRESULT_FROM_WIN32(87);
break;
}
if (dlibMove<0)
return __HRESULT_FROM_WIN32(87);
if (dlibMove>Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
Point = dlibMove;
if (plibNewPosition)
*plibNewPosition = Point;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Size(_Out_ UINT64* pSize)
{
*pSize = Buffer.GetLength();
return S_OK;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Write(
/* [annotation] */
_In_reads_bytes_(cb) const void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbWritten,
_In_opt_ UINT64* pByteOffset)
{
auto TempPoint = pByteOffset ? *pByteOffset : Point;
auto NewSize = TempPoint + cb;
auto pData= Buffer.GetBuffer(NewSize);
memcpy(pData + TempPoint, pv, cb);
auto Size = Buffer.GetLength();
if (NewSize >= Size)
{
Buffer.ReleaseBufferSetLength(NewSize);
}
if (pcbWritten)
{
*pcbWritten = cb;
}
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_Size(_In_ UINT64 Size)
{
Buffer.Truncate(Size);
return S_OK;
}
};
HRESULT DownloadFile(LPCWSTR Url, CStringA& Buffer, LPCWSTR lpszAgent, DWORD* pCharSet, BaseCallBack callBack, LPVOID pUserData)
{
CReadWriteMemStream MemStream;
HRESULT hr = DownloadStream(Url, (IReadWriteStream*)&MemStream, lpszAgent, pCharSet, callBack, pUserData);
if (hr!=S_OK)
return hr;
Buffer = MemStream.Buffer;
return S_OK;
}
HRESULT DownloadFile(LPCWSTR Url, CStringA& Buffer, DWORD* pCharSet, BaseCallBack callBack, LPVOID pUserData)
{
return DownloadFile(Url, Buffer, L"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko", pCharSet, callBack, pUserData);
}
HRESULT DownloadStream(LPCWSTR Url, IReadWriteStream* pStream, LPCWSTR lpszAgent, DWORD* pCharSet, BaseCallBack callBack, LPVOID pUserData)
{
if (callBack)
{
callBack(/*DISM_MSG_PROGRESS*/38008, 0, 0, pUserData);
}
HRESULT hr = 0;
WinInetURLHelper Info;
int TryCount = 5;
Info.ParseURLW(Url);
Start:
hr=Info.InternetOpenW(lpszAgent);
if (hr!=S_OK)
return hr;
hr=Info.InternetConnectW();
if (hr!=S_OK)
return hr;
hr=Info.HttpOpenRequestW();
if (hr!=S_OK)
return hr;
UINT64 FileSize = 0, UsedSize = 0;
//DWORD dwBytesRead = 0;
//BYTE TempBuffer[10240];
DWORD dwBytesRead;
TCHAR TempBuffer[10240 / sizeof(TCHAR)] = L"Content-Type";
if (pCharSet)
{
//请求字符集
//DWORD WIndex = 0;
*pCharSet = CP_ACP;
dwBytesRead = sizeof(TempBuffer);
if (HttpQueryInfoW(Info.hUrlFile, HTTP_QUERY_CUSTOM, TempBuffer, &dwBytesRead, NULL))
{
LPWSTR Str = StrStrI(TempBuffer, L"charset=");
if (Str)
{
Str += 8;
if (StrCmpI(Str, L"utf-8") == 0)
{
*pCharSet = CP_UTF8;
}
}
}
}
UsedSize = dwBytesRead = 0;
pStream->get_Size(&UsedSize);
//pStream->Seek(LARGE_INTEGER{}, FILE_END, (ULARGE_INTEGER*)&UsedSize);
//if (UsedSize)
//{
//调整指针
{
auto Rang = StrFormat(L"Range: bytes=%I64d-\r\n", UsedSize);
//if (InternetSetFilePointer(hUrlFile, *((DWORD*)&UsedSize), ((PLONG)&UsedSize) + 1, FILE_BEGIN, NULL) == INVALID_SET_FILE_POINTER)
//{
// //不支持断点续传,重新开始
// UsedSize = 0;
// pStream->Seek(LARGE_INTEGER{}, FILE_BEGIN, (ULARGE_INTEGER*)&UsedSize);
//}
hr = Info.HttpSendRequestW(Rang, Rang.GetLength());
}
if (hr!=S_OK)
{
return hr;
}
DWORD Status;
hr=Info.GetStatusCode(Status);
if (hr!=S_OK)
{
return hr;
}
switch (Status)
{
case HTTP_STATUS_PARTIAL_CONTENT:
//支持断点续传
break;
case HTTP_STATUS_OK:
case HTTP_STATUS_RESET_CONTENT:
//不支持断点续传
case 416:
//超出了实际范围
UsedSize = dwBytesRead = 0;
pStream->put_Size(0);
break;
case 0:
return E_FAIL;
case HTTP_STATUS_NOT_FOUND:
if (TryCount--)
{
//等待 1秒
Sleep(1000);
goto Start;
}
default:
return Status;
break;
}
//}
dwBytesRead = sizeof(TempBuffer);
if (HttpQueryInfoW(Info.hUrlFile, HTTP_QUERY_CONTENT_LENGTH, TempBuffer, &dwBytesRead, NULL))
{
//dwBytesRead = UsedSize;
FileSize = _tcstoui64((wchar_t*)TempBuffer, NULL, 10) + UsedSize;
//swscanf((wchar_t*)TempBuffer, L"%I64u", &FileSize);
}
else
{
FileSize = -1;
callBack = NULL;
}
//while (true)
for (;;)
{
if (!InternetReadFile(Info.hUrlFile, TempBuffer, sizeof(TempBuffer), &dwBytesRead))
{
Error:
if (--TryCount)
{
Sleep(1000);
hr = Info.InternetOpenW();
if (hr!=S_OK)
return hr;
hr = Info.InternetConnectW();
if (hr!=S_OK)
return hr;
hr = Info.HttpOpenRequestW();
if (hr!=S_OK)
{
return hr;
}
//重新指定偏移
auto Rang = StrFormat(L"Range: bytes=%I64d-\r\n", UsedSize);
hr = Info.HttpSendRequestW(Rang, Rang.GetLength());
if (hr!=S_OK)
{
return hr;
}
hr = Info.GetStatusCode(Status);
if (hr!=S_OK)
{
return hr;
}
switch (Status)
{
case HTTP_STATUS_PARTIAL_CONTENT:
//支持断点续传
continue;
break;
case HTTP_STATUS_OK:
case HTTP_STATUS_RESET_CONTENT:
//不支持断点续传
case 416:
//超出了实际范围
UsedSize = dwBytesRead = 0;
pStream->put_Size(0);
break;
case 0:
return E_FAIL;
case HTTP_STATUS_NOT_FOUND:
goto Error;
default:
return Status;
break;
}
}
else
{
return HresultFromBool();
}
}
if (dwBytesRead == 0)
{
if(FileSize==-1|| FileSize== UsedSize)
break;
if (FileSize < UsedSize)
{
//pStream->put_Size(0);
//意外的网络错误
return __HRESULT_FROM_WIN32(ERROR_UNEXP_NET_ERR);
}
goto Error;
}
TryCount = 5;
hr = pStream->Write(TempBuffer, dwBytesRead, &dwBytesRead, &UsedSize);
if (hr!=S_OK)
{
return hr;
break;
}
UsedSize += dwBytesRead;
if (callBack)
{
callBack(/*DISM_MSG_PROGRESS*/38008, UsedSize * 100 / FileSize, 0, pUserData);
if (callBack(/*Dism_MSG_QUERY_ABORT*/38030, 0, 0, pUserData))
{
return __HRESULT_FROM_WIN32(ERROR_CANCELLED);
}
}
}
return S_OK;
}
HRESULT DownloadFile(LPCWSTR Url, LPCWSTR FilePath, BaseCallBack callBack, LPVOID pUserData)
{
CComPtr<IReadWriteStream> pStream;
pStream.p = StreamCreate(FilePath, GENERIC_WRITE,FILE_SHARE_READ,OPEN_ALWAYS);
if (!pStream.p)
return HresultFromBool();
return DownloadStream(Url, pStream, L"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko", NULL, callBack, pUserData);
}
//
HRESULT GetUrlString(LPCWSTR Url, CString& String, DWORD CodePage /* -1为自动识别编码*/)
{
CStringA Buffer;
HRESULT hr = DownloadFile(Url, Buffer, CodePage == -1 ? &CodePage : NULL, NULL, NULL);
if (hr!=S_OK)
{
return hr;
}
UTF8ToUnicode(Buffer, Buffer.GetLength(), String);
return S_OK;
}
HRESULT WinInetGetFileSize(LPCWSTR Url, UINT64& FileSize)
{
try
{
HRESULT hr = S_OK;
HINTERNET m_hSession = ::InternetOpenW(L"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko", INTERNET_OPEN_TYPE_DIRECT, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
if (m_hSession == 0)
{
return HresultFromBool();
}
HINTERNET hUrlFile = ::InternetOpenUrlW(m_hSession, Url, NULL, 0, 0, 0);
if (hUrlFile == 0)
{
hr = HresultFromBool();
}
else
{
wchar_t szFileSize[100] = {};
DWORD dwBytesRead = sizeof(szFileSize);
if (HttpQueryInfoW(hUrlFile, HTTP_QUERY_CONTENT_LENGTH, szFileSize, &dwBytesRead, NULL))
{
FileSize = 0;
swscanf(szFileSize, L"%I64u", &FileSize);
}
else
{
hr = HresultFromBool();
}
InternetCloseHandle(hUrlFile);
}
InternetCloseHandle(m_hSession);
return hr;
}
catch (...)
{
return 5;
}
}
HRESULT WinInetURLHelper::ParseURLW(LPCWSTR URL)
{
dwService = INTERNET_SERVICE_HTTP;
if (StrCmpNI(URL, L"http://", ArraySize(L"http://") - 1) == 0)
{
URL += ArraySize(L"http://") - 1;
}
lpszServerName = URL;
if (auto _lpszObjectName = StrChr(lpszServerName, L'/'))
{
lpszObjectName = _lpszObjectName + 1;
cServerName = _lpszObjectName - lpszServerName;
}
else
{
lpszObjectName = NULL;
cServerName = _tcslen(lpszServerName);
}
auto szServerPort = StrRChr(lpszServerName, lpszServerName + cServerName, L':');
if (szServerPort)
{
cServerName = szServerPort - lpszServerName;
nServerPort = _tcstoul(szServerPort + 1, NULL, 10);
}
else
{
nServerPort = INTERNET_DEFAULT_HTTP_PORT;
}
return S_OK;
}