-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathwebget.cpp
463 lines (404 loc) · 15.2 KB
/
webget.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
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
//#include <mutex>
#include <thread>
#include <atomic>
#include <curl/curl.h>
#include "webget.h"
#include "version.h"
#include "misc.h"
#include "logger.h"
#ifdef _WIN32
#ifndef _stat
#define _stat stat
#endif // _stat
#endif // _WIN32
extern bool gPrintDbgInfo, gServeCacheOnFetchFail;
extern int gLogLevel;
/*
typedef std::lock_guard<std::mutex> guarded_mutex;
std::mutex cache_rw_lock;
*/
class RWLock
{
#define WRITE_LOCK_STATUS -1
#define FREE_STATUS 0
private:
const std::thread::id NULL_THREAD;
const bool WRITE_FIRST;
std::thread::id m_write_thread_id;
std::atomic_int m_lockCount;
std::atomic_uint m_writeWaitCount;
public:
RWLock(const RWLock&) = delete;
RWLock& operator=(const RWLock&) = delete;
RWLock(bool writeFirst = true): WRITE_FIRST(writeFirst), m_write_thread_id(), m_lockCount(0), m_writeWaitCount(0) {}
virtual ~RWLock() = default;
int readLock()
{
if (std::this_thread::get_id() != m_write_thread_id)
{
int count;
if (WRITE_FIRST)
do {
while ((count = m_lockCount) == WRITE_LOCK_STATUS || m_writeWaitCount > 0);
} while (!m_lockCount.compare_exchange_weak(count, count + 1));
else
do {
while ((count = m_lockCount) == WRITE_LOCK_STATUS);
} while (!m_lockCount.compare_exchange_weak(count, count + 1));
}
return m_lockCount;
}
int readUnlock()
{
if (std::this_thread::get_id() != m_write_thread_id)
--m_lockCount;
return m_lockCount;
}
int writeLock()
{
if (std::this_thread::get_id() != m_write_thread_id)
{
++m_writeWaitCount;
for (int zero = FREE_STATUS; !m_lockCount.compare_exchange_weak(zero, WRITE_LOCK_STATUS); zero = FREE_STATUS);
--m_writeWaitCount;
m_write_thread_id = std::this_thread::get_id();
}
return m_lockCount;
}
int writeUnlock()
{
if (std::this_thread::get_id() != m_write_thread_id)
{
throw std::runtime_error("writeLock/Unlock mismatch");
}
if (WRITE_LOCK_STATUS != m_lockCount)
{
throw std::runtime_error("RWLock internal error");
}
m_write_thread_id = NULL_THREAD;
m_lockCount.store(FREE_STATUS);
return m_lockCount;
}
};
RWLock cache_rw_lock;
long gMaxAllowedDownloadSize = 1048576L;
//std::string user_agent_str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
std::string user_agent_str = "subconverter/" VERSION " cURL/" LIBCURL_VERSION;
struct curl_progress_data
{
long size_limit = 0L;
};
static inline void curl_init()
{
static bool init = false;
if(!init)
{
curl_global_init(CURL_GLOBAL_ALL);
init = true;
}
}
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData)
{
if(writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
static int dummy_writer(char *data, size_t size, size_t nmemb, void *writerData)
{
/// dummy writer, do not save anything
(void)data;
(void)writerData;
return size * nmemb;
}
static int size_checker(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
if(clientp)
{
curl_progress_data *data = reinterpret_cast<curl_progress_data*>(clientp);
if(data->size_limit)
{
if(dltotal > data->size_limit || dlnow > data->size_limit)
return 1;
}
}
return 0;
}
static inline void curl_set_common_options(CURL *curl_handle, const char *url, curl_progress_data *data)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, gLogLevel == LOG_LEVEL_VERBOSE ? 1L : 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 20L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 15L);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, user_agent_str.data());
if(data)
{
if(data->size_limit)
curl_easy_setopt(curl_handle, CURLOPT_MAXFILESIZE, data->size_limit);
curl_easy_setopt(curl_handle, CURLOPT_XFERINFOFUNCTION, size_checker);
curl_easy_setopt(curl_handle, CURLOPT_XFERINFODATA, data);
}
}
//static std::string curlGet(const std::string &url, const std::string &proxy, std::string &response_headers, CURLcode &return_code, const string_map &request_headers)
static int curlGet(const FetchArgument &argument, FetchResult &result)
{
CURL *curl_handle;
std::string *data = result.content, new_url = argument.url;
struct curl_slist *list = NULL;
defer(curl_slist_free_all(list);)
long retVal = 0;
curl_init();
curl_handle = curl_easy_init();
if(argument.proxy.size())
{
if(startsWith(argument.proxy, "cors:"))
{
list = curl_slist_append(list, "X-Requested-With: subconverter " VERSION);
new_url = argument.proxy.substr(5) + argument.url;
}
else
curl_easy_setopt(curl_handle, CURLOPT_PROXY, argument.proxy.data());
}
curl_progress_data limit;
limit.size_limit = gMaxAllowedDownloadSize;
curl_set_common_options(curl_handle, new_url.data(), &limit);
if(argument.request_headers)
{
for(auto &x : *argument.request_headers)
list = curl_slist_append(list, (x.first + ": " + x.second).data());
}
list = curl_slist_append(list, "SubConverter-Request: 1");
list = curl_slist_append(list, "SubConverter-Version: " VERSION);
if(list)
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
if(result.content)
{
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, result.content);
}
else
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, dummy_writer);
if(result.response_headers)
{
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, result.response_headers);
}
else
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, dummy_writer);
unsigned int fail_count = 0, max_fails = 1;
while(true)
{
*result.status_code = curl_easy_perform(curl_handle);
if(*result.status_code == CURLE_OK || max_fails >= fail_count)
break;
else
fail_count++;
}
curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
curl_easy_cleanup(curl_handle);
if(data)
{
if(*result.status_code != CURLE_OK || retVal != 200)
data->clear();
data->shrink_to_fit();
}
return *result.status_code;
}
// data:[<mediatype>][;base64],<data>
static std::string dataGet(const std::string &url)
{
if (!startsWith(url, "data:"))
return std::string();
std::string::size_type comma = url.find(',');
if (comma == std::string::npos || comma == url.size() - 1)
return std::string();
std::string data = UrlDecode(url.substr(comma + 1));
if (endsWith(url.substr(0, comma), ";base64")) {
return urlsafe_base64_decode(data);
} else {
return data;
}
}
std::string buildSocks5ProxyString(const std::string &addr, int port, const std::string &username, const std::string &password)
{
std::string authstr = username.size() && password.size() ? username + ":" + password + "@" : "";
std::string proxystr = "socks5://" + authstr + addr + ":" + std::to_string(port);
return proxystr;
}
std::string webGet(const std::string &url, const std::string &proxy, unsigned int cache_ttl, std::string *response_headers, string_map *request_headers)
{
int return_code = 0;
std::string content;
FetchArgument argument {url, proxy, request_headers, cache_ttl};
FetchResult fetch_res {&return_code, &content, response_headers};
if (startsWith(url, "data:"))
return dataGet(url);
// cache system
if(cache_ttl > 0)
{
md("cache");
const std::string url_md5 = getMD5(url);
const std::string path = "cache/" + url_md5, path_header = path + "_header";
struct stat result;
if(stat(path.data(), &result) == 0) // cache exist
{
time_t mtime = result.st_mtime, now = time(NULL); // get cache modified time and current time
if(difftime(now, mtime) <= cache_ttl) // within TTL
{
writeLog(0, "CACHE HIT: '" + url + "', using local cache.");
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.readLock();
defer(cache_rw_lock.readUnlock();)
if(response_headers)
*response_headers = fileGet(path_header, true);
return fileGet(path, true);
}
writeLog(0, "CACHE MISS: '" + url + "', TTL timeout, creating new cache."); // out of TTL
}
else
writeLog(0, "CACHE NOT EXIST: '" + url + "', creating new cache.");
//content = curlGet(url, proxy, response_headers, return_code); // try to fetch data
curlGet(argument, fetch_res);
if(return_code == CURLE_OK) // success, save new cache
{
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.writeLock();
defer(cache_rw_lock.writeUnlock();)
fileWrite(path, content, true);
if(response_headers)
fileWrite(path_header, *response_headers, true);
}
else
{
if(fileExist(path) && gServeCacheOnFetchFail) // failed, check if cache exist
{
writeLog(0, "Fetch failed. Serving cached content."); // cache exist, serving cache
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.readLock();
defer(cache_rw_lock.readUnlock();)
content = fileGet(path, true);
if(response_headers)
*response_headers = fileGet(path_header, true);
}
else
writeLog(0, "Fetch failed. No local cache available."); // cache not exist or not allow to serve cache, serving nothing
}
return content;
}
//return curlGet(url, proxy, response_headers, return_code);
curlGet(argument, fetch_res);
return content;
}
void flushCache()
{
//guarded_mutex guard(cache_rw_lock);
cache_rw_lock.writeLock();
defer(cache_rw_lock.writeUnlock();)
operateFiles("cache", [](const std::string &file){ remove(("cache/" + file).data()); return 0; });
}
int curlPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
{
CURL *curl_handle;
CURLcode res;
struct curl_slist *list = NULL;
long retVal = 0;
curl_init();
curl_handle = curl_easy_init();
list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
for(const std::string &x : request_headers)
list = curl_slist_append(list, x.data());
curl_progress_data limit;
curl_set_common_options(curl_handle, url.data(), &limit);
curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data.data());
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, data.size());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
if(proxy.size())
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
res = curl_easy_perform(curl_handle);
curl_slist_free_all(list);
if(res == CURLE_OK)
{
curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
}
curl_easy_cleanup(curl_handle);
return retVal;
}
int webPost(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
{
return curlPost(url, data, proxy, request_headers, retData);
}
int curlPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
{
CURL *curl_handle;
CURLcode res;
long retVal = 0;
struct curl_slist *list = NULL;
curl_init();
curl_handle = curl_easy_init();
list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
for(const std::string &x : request_headers)
list = curl_slist_append(list, x.data());
curl_progress_data limit;
curl_set_common_options(curl_handle, url.data(), &limit);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data.data());
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, data.size());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, retData);
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
if(proxy.size())
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
res = curl_easy_perform(curl_handle);
curl_slist_free_all(list);
if(res == CURLE_OK)
{
res = curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
}
curl_easy_cleanup(curl_handle);
return retVal;
}
int webPatch(const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
{
return curlPatch(url, data, proxy, request_headers, retData);
}
int curlHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
{
CURL *curl_handle;
CURLcode res;
long retVal = 0;
struct curl_slist *list = NULL;
curl_init();
curl_handle = curl_easy_init();
list = curl_slist_append(list, "Content-Type: application/json;charset='utf-8'");
for(const std::string &x : request_headers)
list = curl_slist_append(list, x.data());
curl_progress_data limit;
curl_set_common_options(curl_handle, url.data(), &limit);
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, writer);
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, &response_headers);
curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
if(proxy.size())
curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy.data());
res = curl_easy_perform(curl_handle);
curl_slist_free_all(list);
if(res == CURLE_OK)
res = curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &retVal);
curl_easy_cleanup(curl_handle);
return retVal;
}
int webHead(const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
{
return curlHead(url, proxy, request_headers, response_headers);
}