-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdwnl.c
492 lines (409 loc) · 13.5 KB
/
dwnl.c
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
#define COBJMACROS
#include <urlmon.h>
#include <wininet.h>
#include <tchar.h>
#define STRSAFEAPI HRESULT WINAPI
typedef char *STRSAFE_LPSTR;
typedef const char *STRSAFE_LPCSTR;
typedef wchar_t *STRSAFE_LPWSTR;
typedef const wchar_t *STRSAFE_LPCWSTR;
STRSAFEAPI StringVPrintfWorkerW(STRSAFE_LPWSTR pszDest,size_t cchDest,STRSAFE_LPCWSTR pszFormat,va_list argList);
STRSAFEAPI StringCbVPrintfW(STRSAFE_LPWSTR pszDest,size_t cbDest,STRSAFE_LPCWSTR pszFormat,va_list argList);
/* FIXME: add correct definitions to urlmon.idl */
#ifdef UNICODE
#define StringCbVPrintf StringCbVPrintfW
#else
#define StringCbVPrintf StringCbVPrintfA
#endif
#define DWNL_E_LASTERROR 0
#define DWNL_E_NEEDTARGETFILENAME -1
#define DWNL_E_UNSUPPORTEDSCHEME -2
typedef struct
{
const IBindStatusCallbackVtbl* lpIBindStatusCallbackVtbl;
LONG ref;
TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH + 1];
TCHAR szMimeType[128];
UINT64 Size;
UINT64 Progress;
UINT bResolving : 1;
UINT bConnecting : 1;
UINT bSendingReq : 1;
UINT bBeginTransfer : 1;
} CBindStatusCallback;
#define impl_to_interface(impl,iface) (struct iface *)(&(impl)->lp##iface##Vtbl)
#define interface_to_impl(instance,iface) ((CBindStatusCallback*)((ULONG_PTR)instance - FIELD_OFFSET(CBindStatusCallback,lp##iface##Vtbl)))
static void
CBindStatusCallback_Destroy(CBindStatusCallback *This)
{
return;
}
static void
write_status(LPCTSTR lpFmt, ...)
{
va_list args;
TCHAR szTxt[128];
CONSOLE_SCREEN_BUFFER_INFO csbi;
va_start(args, lpFmt);
StringCbVPrintf(szTxt, sizeof(szTxt), lpFmt, args);
va_end(args);
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
_tprintf(_T("\r%*.*s"), -(csbi.dwSize.X - 1), csbi.dwSize.X - 1, szTxt);
}
else
{
_putts(szTxt);
}
}
static void
CBindStatusCallback_UpdateProgress(CBindStatusCallback *This)
{
/* FIXME: better output */
if (This->Size != 0)
{
UINT Percentage;
Percentage = (UINT)((This->Progress * 100) / This->Size);
if (Percentage > 99)
Percentage = 99;
write_status(_T("%2d%% (%I64u bytes downloaded)"), Percentage, This->Progress);
}
else
{
/* Unknown size */
write_status(_T("%I64u bytes downloaded"), This->Progress);
}
}
static ULONG STDMETHODCALLTYPE
CBindStatusCallback_AddRef(IBindStatusCallback *iface)
{
CBindStatusCallback *This = interface_to_impl(iface, IBindStatusCallback);
ULONG ret;
ret = InterlockedIncrement((PLONG)&This->ref);
return ret;
}
static ULONG STDMETHODCALLTYPE
CBindStatusCallback_Release(IBindStatusCallback *iface)
{
CBindStatusCallback *This = interface_to_impl(iface, IBindStatusCallback);
ULONG ret;
ret = InterlockedDecrement((PLONG)&This->ref);
if (ret == 0)
{
CBindStatusCallback_Destroy(This);
HeapFree(GetProcessHeap(),
0,
This);
}
return ret;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_QueryInterface(IBindStatusCallback *iface,
REFIID iid,
PVOID *pvObject)
{
CBindStatusCallback *This = interface_to_impl(iface, IBindStatusCallback);
*pvObject = NULL;
if (IsEqualIID(iid,
&IID_IBindStatusCallback) ||
IsEqualIID(iid,
&IID_IUnknown))
{
*pvObject = impl_to_interface(This, IBindStatusCallback);
}
else
return E_NOINTERFACE;
CBindStatusCallback_AddRef(iface);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
DWORD dwReserved,
IBinding* pib)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_GetPriority(IBindStatusCallback *iface,
LONG* pnPriority)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnLowResource(IBindStatusCallback *iface,
DWORD reserved)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnProgress(IBindStatusCallback *iface,
ULONG ulProgress,
ULONG ulProgressMax,
ULONG ulStatusCode,
LPCWSTR szStatusText)
{
CBindStatusCallback *This = interface_to_impl(iface, IBindStatusCallback);
switch (ulStatusCode)
{
case BINDSTATUS_FINDINGRESOURCE:
if (!This->bResolving)
{
_tcscpy(This->szHostName, szStatusText);
This->bResolving = TRUE;
_tprintf(_T("Resolving %s... "), This->szHostName);
}
break;
case BINDSTATUS_CONNECTING:
This->bConnecting = TRUE;
This->bSendingReq = FALSE;
This->bBeginTransfer = FALSE;
This->szMimeType[0] = _T('\0');
if (This->bResolving)
{
_tprintf(_T("done.\n"));
_tprintf(_T("Connecting to %s[%s]... "), This->szHostName, szStatusText);
}
else
_tprintf(_T("Connecting to %s... "), szStatusText);
break;
case BINDSTATUS_REDIRECTING:
This->bResolving = FALSE;
This->bConnecting = FALSE;
This->bSendingReq = FALSE;
This->bBeginTransfer = FALSE;
This->szMimeType[0] = _T('\0');
_tprintf(_T("Redirecting to %s... "), szStatusText);
break;
case BINDSTATUS_SENDINGREQUEST:
This->bBeginTransfer = FALSE;
This->szMimeType[0] = _T('\0');
if (This->bResolving || This->bConnecting)
_tprintf(_T("done.\n"));
if (!This->bSendingReq)
_tprintf(_T("Sending request... "));
This->bSendingReq = TRUE;
break;
case BINDSTATUS_MIMETYPEAVAILABLE:
_tcscpy(This->szMimeType, szStatusText);
break;
case BINDSTATUS_BEGINDOWNLOADDATA:
This->Progress = (UINT64)ulProgress;
This->Size = (UINT64)ulProgressMax;
if (This->bSendingReq)
_tprintf(_T("done.\n"));
if (!This->bBeginTransfer && This->Size != 0)
{
if (This->szMimeType[0] != _T('\0'))
_tprintf(_T("Length: %I64u [%s]\n"), This->Size, This->szMimeType);
else
_tprintf(_T("Length: %I64u\n"), This->Size);
}
_tprintf(_T("\n"));
This->bBeginTransfer = TRUE;
break;
case BINDSTATUS_ENDDOWNLOADDATA:
write_status(_T("File saved."));
_tprintf(_T("\n"));
break;
case BINDSTATUS_DOWNLOADINGDATA:
This->Progress = (UINT64)ulProgress;
This->Size = (UINT64)ulProgressMax;
CBindStatusCallback_UpdateProgress(This);
break;
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
HRESULT hresult,
LPCWSTR szError)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
DWORD* grfBINDF,
BINDINFO* pbindinfo)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
DWORD grfBSCF,
DWORD dwSize,
FORMATETC* pformatetc,
STGMEDIUM* pstgmed)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
CBindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
REFIID riid,
IUnknown* punk)
{
return E_NOTIMPL;
}
static const struct IBindStatusCallbackVtbl vtblIBindStatusCallback =
{
CBindStatusCallback_QueryInterface,
CBindStatusCallback_AddRef,
CBindStatusCallback_Release,
CBindStatusCallback_OnStartBinding,
CBindStatusCallback_GetPriority,
CBindStatusCallback_OnLowResource,
CBindStatusCallback_OnProgress,
CBindStatusCallback_OnStopBinding,
CBindStatusCallback_GetBindInfo,
CBindStatusCallback_OnDataAvailable,
CBindStatusCallback_OnObjectAvailable,
};
static IBindStatusCallback *
CreateBindStatusCallback(void)
{
CBindStatusCallback *This;
This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
if (This == NULL)
return NULL;
This->lpIBindStatusCallbackVtbl = &vtblIBindStatusCallback;
This->ref = 1;
return impl_to_interface(This, IBindStatusCallback);
}
// ToDo: Show status, get file name from webserver, better error reporting
static int
get_display_url(IN LPURL_COMPONENTS purl,
OUT TCHAR *szBuffer,
IN PDWORD pdwBufferSize)
{
URL_COMPONENTS urlc;
/* Hide the password */
urlc = *purl;
urlc.lpszPassword = NULL;
urlc.dwPasswordLength = 0;
if (!InternetCreateUrl(&urlc, ICU_ESCAPE, szBuffer, pdwBufferSize))
return DWNL_E_LASTERROR;
return 1;
}
static int
download_file(IN LPCTSTR pszUrl,
IN LPCTSTR pszFile OPTIONAL)
{
TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH + 1];
TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH + 1];
TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH + 1];
TCHAR szPassWord[INTERNET_MAX_PASSWORD_LENGTH + 1];
TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH + 1];
TCHAR szExtraInfo[INTERNET_MAX_PATH_LENGTH + 1];
TCHAR szUrl[INTERNET_MAX_URL_LENGTH + 1];
DWORD dwUrlLen;
LPTSTR pszFilePart;
URL_COMPONENTS urlc;
IBindStatusCallback *pbsc;
int iRet;
if (pszFile != NULL && pszFile[0] == _T('\0'))
pszFile = NULL;
urlc.dwStructSize = sizeof(urlc);
urlc.lpszScheme = szScheme;
urlc.dwSchemeLength = sizeof(szScheme) / sizeof(szScheme[0]);
urlc.lpszHostName = szHostName;
urlc.dwHostNameLength = sizeof(szHostName) / sizeof(szHostName[0]);
urlc.lpszUserName = szUserName;
urlc.dwUserNameLength = sizeof(szUserName) / sizeof(szUserName[0]);
urlc.lpszPassword = szPassWord;
urlc.dwPasswordLength = sizeof(szPassWord) / sizeof(szPassWord[0]);
urlc.lpszUrlPath = szUrlPath;
urlc.dwUrlPathLength = sizeof(szUrlPath) / sizeof(szUrlPath[0]);
urlc.lpszExtraInfo = szExtraInfo;
urlc.dwExtraInfoLength = sizeof(szExtraInfo) / sizeof(szExtraInfo[0]);
if (!InternetCrackUrl(pszUrl, _tcslen(pszUrl), ICU_ESCAPE, &urlc))
return DWNL_E_LASTERROR;
if (urlc.nScheme != INTERNET_SCHEME_FTP &&
urlc.nScheme != INTERNET_SCHEME_GOPHER &&
urlc.nScheme != INTERNET_SCHEME_HTTP &&
urlc.nScheme != INTERNET_SCHEME_HTTPS)
{
return DWNL_E_UNSUPPORTEDSCHEME;
}
if (urlc.nScheme == INTERNET_SCHEME_FTP && urlc.dwUserNameLength == 0 && urlc.dwPasswordLength == 0)
{
_tcscpy(szUserName, _T("anonymous"));
urlc.dwUserNameLength = _tcslen(szUserName);
}
/* FIXME: Get file name from server */
if (urlc.dwUrlPathLength == 0 && pszFile == NULL)
return DWNL_E_NEEDTARGETFILENAME;
pszFilePart = _tcsrchr(szUrlPath, _T('/'));
if (pszFilePart != NULL)
pszFilePart++;
if (pszFilePart == NULL && pszFile == NULL)
return DWNL_E_NEEDTARGETFILENAME;
if (pszFile == NULL)
pszFile = pszFilePart;
if (urlc.dwUserNameLength == 0)
urlc.lpszUserName = NULL;
if (urlc.dwPasswordLength == 0)
urlc.lpszPassword = NULL;
/* Generate the URL to be displayed (without a password) */
dwUrlLen = sizeof(szUrl) / sizeof(szUrl[0]);
iRet = get_display_url(&urlc, szUrl, &dwUrlLen);
if (iRet <= 0)
return iRet;
_tprintf(_T("Download `%s\'\n\t=> `%s\'\n"), szUrl, pszFile);
/* Generate the URL to download */
dwUrlLen = sizeof(szUrl) / sizeof(szUrl[0]);
if (!InternetCreateUrl(&urlc, ICU_ESCAPE, szUrl, &dwUrlLen))
return DWNL_E_LASTERROR;
pbsc = CreateBindStatusCallback();
if (pbsc == NULL)
return DWNL_E_LASTERROR;
if(!SUCCEEDED(URLDownloadToFile(NULL, szUrl, pszFile, 0, pbsc)))
{
IBindStatusCallback_Release(pbsc);
return DWNL_E_LASTERROR; /* FIXME */
}
IBindStatusCallback_Release(pbsc);
return 1;
}
static int
print_err(int iErr)
{
write_status(_T(""));
if (iErr == DWNL_E_LASTERROR)
{
if (GetLastError() == ERROR_SUCCESS)
{
/* File not found */
_ftprintf(stderr, _T("\nERROR: Download failed.\n"));
}
else
{
/* Display last error code */
_ftprintf(stderr, _T("\nERROR: %u\n"), GetLastError());
}
}
else
{
switch (iErr)
{
case DWNL_E_NEEDTARGETFILENAME:
_ftprintf(stderr, _T("\nERROR: Cannot determine filename, please specify a destination file name.\n"));
break;
case DWNL_E_UNSUPPORTEDSCHEME:
_ftprintf(stderr, _T("\nERROR: Unsupported protocol.\n"));
break;
}
}
return 1;
}
int _tmain(int argc, TCHAR **argv)
{
int iErr, iRet = 0;
if(argc != 2 && argc != 3)
{
_tprintf(_T("Usage: dwnl URL [DESTINATION]"));
return 2;
}
iErr = download_file(argv[1], argc == 3 ? argv[2] : NULL);
if (iErr <= 0)
iRet = print_err(iErr);
return iRet;
}