forked from svn2github/freearc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
URL.cpp
385 lines (310 loc) · 12.8 KB
/
URL.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
#include "URL.h"
#include "Compression/Compression.h"
// ****************************************************************************
// ** URL reading library
// ****************************************************************************
#ifdef FREEARC_NOURL
void url_setup_proxy (char *_proxy) {}
void url_setup_bypass_list (char *_bypass_list) {}
URL* url_open (char *_url) {return NULL;}
int url_readp (URL *url, int64 offset, char *buf, int size) {return -1;}
void url_close (URL *url) {}
#elif defined(FREEARC_WIN)
HINTERNET hInternet = NULL;
char* proxy = NULL;
char* bypass_list = "<local>";
int64 url_detect_size (URL *url);
int url_readp (URL *url, int64 offset, char *buf, int size);
// DynaLoad function from wininet.dll
static FARPROC wininet_load (char *funcname, int i)
{
static bool loaded = FALSE;
static HMODULE dll = NULL;
static FARPROC f[10];
if (f[i])
return f[i];
if (!loaded)
{
loaded = TRUE;
dll = LoadLibraryA("wininet.dll");
}
f[i] = GetProcAddress (dll, funcname);
return f[i];
}
#define InternetCloseHandle ((BOOL WINAPI (*) (HINTERNET)) wininet_load("InternetCloseHandle",0))
#define InternetOpenA ((HINTERNET WINAPI (*) (LPCSTR,DWORD,LPCSTR,LPCSTR,DWORD)) wininet_load("InternetOpenA", 1))
#define InternetConnectA ((HINTERNET WINAPI (*) (HINTERNET,LPCSTR,INTERNET_PORT,LPCSTR,LPCSTR,DWORD,DWORD,DWORD)) wininet_load("InternetConnectA", 2))
#define InternetOpenUrlA ((HINTERNET WINAPI (*) (HINTERNET,LPCSTR,LPCSTR,DWORD,DWORD,DWORD)) wininet_load("InternetOpenUrlA", 3))
#define HttpQueryInfoA ((BOOL WINAPI (*) (HINTERNET,DWORD,PVOID,PDWORD,PDWORD)) wininet_load("HttpQueryInfoA", 4))
#define InternetReadFile ((BOOL WINAPI (*) (HINTERNET,PVOID,DWORD,PDWORD)) wininet_load("InternetReadFile", 5))
#define FtpOpenFileA ((HINTERNET WINAPI (*) (HINTERNET,LPCSTR,DWORD,DWORD,DWORD)) wininet_load("FtpOpenFileA", 6))
#define FtpGetFileSize ((DWORD WINAPI (*) (HINTERNET,LPDWORD)) wininet_load("FtpGetFileSize", 7))
#define FtpCommandA ((BOOL WINAPI (*) (HINTERNET,BOOL,DWORD,LPCSTR,DWORD_PTR,HINTERNET*)) wininet_load("FtpCommandA", 8))
void url_setup_proxy (char *_proxy)
{
proxy = strequ(_proxy,"--") ? NULL : strdup_msg (_proxy);
hInternet && InternetCloseHandle (hInternet), hInternet=NULL;
}
void url_setup_bypass_list (char *_bypass_list)
{
bypass_list = _bypass_list[0]? strdup_msg (_bypass_list) : (char*)"<local>";
hInternet && InternetCloseHandle (hInternet), hInternet=NULL;
}
// Open file with given url and return handle for operations on the file
URL *url_open (char *_url)
{
URL *url = (URL*) malloc(sizeof(URL));
if (!url) return NULL;
url->url = strdup_msg (_url);
url->hConnect = NULL;
url->curpos = 0;
url->hURL = NULL;
// èíèöèàëèçèðóåì WinInet
hInternet = hInternet? hInternet :
InternetOpenA(
"FreeArc/0.52",
proxy? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
proxy, bypass_list,
0);
if (!hInternet) {url_close(url); return NULL;}
// True, åñëè ýòî ftp url
url->isFTP = start_with (_url, "ftp://");
if (url->isFTP) {
char *server = url->url + 6;
url->file = strchr (server, '/');
*url->file++ = '\0';
char *user = NULL, *password = NULL;
if (strchr (server, '@')) {
user = server;
server = strchr (server, '@');
*server++ = '\0';
password = strchr (user, ':');
*password++ = '\0';
}
int portnum = INTERNET_DEFAULT_FTP_PORT;
if (strchr (server, ':')) {
char *port = strchr (server, ':');
*port++ = '\0';
portnum = atoi(port);
}
// Ñîçäà¸ì FTP ñåññèþ
url->hConnect = InternetConnectA(
hInternet,
server,
portnum,
user, password,
INTERNET_SERVICE_FTP,
0,
0);
if (!url->hConnect) {url_close(url); return NULL;}
}
url->size = url_detect_size (url);
if (url->size < 0) {url_close(url); return NULL;}
return url;
}
// 4.1Gb - http://download.opensuse.org/distribution/10.3/iso/dvd/openSUSE-10.3-GM-DVD-i386.iso
// 4.1Gb - ftp://ftp.linuxcenter.ru/iso/Linuxcenter-games-collection-v2-dvd/lc-games-dvd.iso
int64 url_detect_size (URL *url)
{
if (url) {
if (url->isFTP)
{
/*
// Fast method of getting filesize
WIN32_FIND_DATA FindFileData;
HINTERNET hURL = FtpFindFirstFile (url->hConnect, url->file, &FindFileData, 0, 0);
InternetCloseHandle (hURL);
if (hURL && FindFileData.nFileSizeLow!=(DWORD)-1)
return (int64(FindFileData.nFileSizeHigh)<<32)+FindFileData.nFileSizeLow;
*/
// Slow method of getting filesize
HINTERNET hURL = FtpOpenFileA (url->hConnect, url->file, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
DWORD SizeHigh, SizeLow = FtpGetFileSize (hURL, &SizeHigh);
InternetCloseHandle (hURL);
//if (SizeLow==-1) return 0; - filesize may be exactly 4gb-1
// DIRTY HACK!
// Because for files >4gb truncated size is returned (with SizeHigh==0),
// we try to download further parts of file in order to find its real size
int64 Size;
for(;;)
{
Size = (int64(SizeHigh)<<32) + SizeLow;
char tmp[10];
int res = url_readp (url, Size, tmp, 10);
if (res<0) return res;
if (res<10) break;
SizeHigh++;
}
return Size;
}
// HTTP
HINTERNET
hURL = InternetOpenUrlA(
hInternet, // session handle
url->url, // URL to access
0, 0, INTERNET_FLAG_EXISTING_CONNECT, 0);
DWORD dwStatusCode = 0;
DWORD dwLengthStatusCode = sizeof(dwStatusCode);
BOOL bQuery1 = HttpQueryInfoA(
hURL,
HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
&dwStatusCode,
&dwLengthStatusCode,
NULL);
char DataSize[100];
DWORD dwLengthDataSize = sizeof(DataSize)-1;
BOOL bQuery2 = HttpQueryInfoA(
hURL,
HTTP_QUERY_CONTENT_LENGTH,
&DataSize,
&dwLengthDataSize,
NULL);
InternetCloseHandle (hURL);
DataSize[dwLengthDataSize] = '\0';
return bQuery1&&bQuery2&&dwStatusCode==HTTP_STATUS_OK? atoll(DataSize): -1;
}
return -1;
}
int url_readp (URL *url, int64 offset, char *buf, int size)
{
if (size==0) return 0;
if (!url) return -1;
// End of data that should be read by the new operation.
// If we asked exactly for 64kb/256kb/8mb - this is probably just beginning of large block,
// so we switch to reading all the data until file end
int64 endpos = size==BUFFER_SIZE || size==LARGE_BUFFER_SIZE || size==HUGE_BUFFER_SIZE
? 0 : offset+size;
// Make connection to desired url. Continue previous read operation
// if it finished exactly where we want to start, otherwise close its handle
if (url->hURL && url->curpos!=offset)
InternetCloseHandle (url->hURL), url->hURL=NULL;
BOOL new_hURL = (url->hURL==NULL);
if (new_hURL) {
if (url->isFTP)
{
char Rest[100];
sprintf (Rest, "REST %.0lf", double(offset));
FtpCommandA (url->hConnect, FALSE, FTP_TRANSFER_TYPE_BINARY, Rest, 0, NULL);
url->hURL = FtpOpenFileA (url->hConnect, url->file, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
} else {
// Ask server for required range of bytes
char Range[100];
if (endpos==0)
then sprintf (Range, "Range: bytes=%.0lf-\r\n", double(offset));
else sprintf (Range, "Range: bytes=%.0lf-%.0lf\r\n", double(offset), double(endpos-1));
url->hURL = InternetOpenUrlA (hInternet, url->url, Range,DWORD(-1L), INTERNET_FLAG_EXISTING_CONNECT, 0);
}
}
if (!url->hURL) return -1;
int bytes; // ñêîëüêî áàéò óæå ïðî÷èòàíî
for (bytes=0; bytes<size;)
{
DWORD dwBytesRead;
// ÷èòàåì äàííûå
InternetReadFile (url->hURL, buf, size-bytes, &dwBytesRead);
// âûõîä èç öèêëà ïðè îøèáêå èëè çàâåðøåíèè
if (dwBytesRead == 0)
break;
buf += dwBytesRead;
bytes += dwBytesRead;
url->curpos += dwBytesRead;
}
// Close read handle if we've read all the data asked by calling procedure
if (url->curpos == endpos) InternetCloseHandle (url->hURL), url->hURL=NULL;
// If not all the data are read and we have chances to read something more - try it!
if (bytes<size && !(new_hURL && bytes==0))
{
InternetCloseHandle (url->hURL), url->hURL=NULL;
int ret = url_readp (url, offset+bytes, buf, size-bytes);
return ret>=0? bytes+ret : ret;
}
return bytes;
}
void url_close (URL *url)
{
if (!url) return;
free (url->url);
InternetCloseHandle (url->hURL);
InternetCloseHandle (url->hConnect);
free (url);
}
#else // Unix
char *proxy = NULL;
void url_setup_proxy (char *_proxy)
{
proxy = strequ(_proxy,"--") ? NULL : strdup_msg (_proxy);
}
void url_setup_bypass_list (char *_bypass_list)
{
}
static size_t NoWriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
return -1;
}
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
char **buf = (char **) data;
memcpy (*buf, ptr, size*nmemb);
*buf += size*nmemb;
return size*nmemb;
}
URL* url_open (char *_url)
{
static bool curl_inited = FALSE;
if (!curl_inited) curl_global_init(CURL_GLOBAL_ALL), curl_inited=TRUE;
URL *url = (URL*) malloc(sizeof(URL));
if (!url) return NULL;
url->url = strdup_msg (_url);
url->curpos = 0;
/* init the curl session */
url->curl_handle = curl_easy_init();
/* set proxy if it was specified by user */
if (proxy) curl_easy_setopt (url->curl_handle, CURLOPT_PROXY, proxy);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt (url->curl_handle, CURLOPT_USERAGENT, "FreeArc/0.60RC");
/* specify URL to get */
curl_easy_setopt (url->curl_handle, CURLOPT_URL, url->url);
// Get file size
curl_easy_setopt (url->curl_handle, CURLOPT_WRITEFUNCTION, NoWriteCallback);
CURLcode res = curl_easy_perform (url->curl_handle);
if (CURLE_OK!=res && CURLE_WRITE_ERROR!=res) {url_close(url); return NULL;}
long response;
curl_easy_getinfo (url->curl_handle, CURLINFO_RESPONSE_CODE, &response);
if (response!=200) {url_close(url); return NULL;}
double size;
res = curl_easy_getinfo (url->curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
if (CURLE_OK != res) {url_close(url); return NULL;}
url->size = (int64)size;
/* send all data read to this function */
curl_easy_setopt (url->curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
return url;
}
int url_readp (URL *url, int64 offset, char *buf, int size)
{
if (size==0) return 0;
if (!url) return -1;
/* specify byte range to get */
char Range[100];
sprintf (Range, "%.0lf-%.0lf", double(offset), double(offset+size-1));
curl_easy_setopt (url->curl_handle, CURLOPT_RANGE, Range);
/* we pass address of 'current write pointer' variable to the callback function */
char *ptr = buf;
curl_easy_setopt (url->curl_handle, CURLOPT_WRITEDATA, (void *)&ptr);
/* get it! */
curl_easy_perform (url->curl_handle);
return ptr-buf;
}
void url_close (URL *url)
{
if (!url) return;
/* cleanup curl stuff */
curl_easy_cleanup (url->curl_handle);
free (url->url);
free (url);
}
#endif // Windows/Unix
int64 url_size (URL *url) {return url? url->size : 0;}
int64 url_pos (URL *url) {return url? url->curpos : 0;}
void url_seek (URL *url, int64 newpos) {if (url) url->curpos = newpos;}
int url_read (URL *url, char *buf, int size) {return url? url_readp (url, url->curpos, buf, size) : -1;}