-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
497 lines (385 loc) · 11.9 KB
/
HttpClient.cpp
File metadata and controls
497 lines (385 loc) · 11.9 KB
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
#include "include/HttpClient.h"
#include "include/upgrade.h"
#include <sys/stat.h>
#undef LOG_TAG
#define LOG_NDDBUG 0
#define LOG_TAG "upgrade"
#include <utils/Log.h>
#define DOWNLOAD_LOG_FILE "/data/upgrade.log"
#define ZLOG(...) ALOGI("%s",__VA_ARGS__)
HttpClient *HttpClient::instance = NULL;
double HttpClient::downloadFileLength = -1;
curl_off_t HttpClient::resumeByte = -1;
time_t HttpClient::lastTime = 0;
bool HttpClient::stopCurl = false;
HttpClient::HttpClient()
{
}
HttpClient::~HttpClient()
{
}
HttpClient *HttpClient::getInstance()
{
if (instance == NULL)
{
instance = new HttpClient();
instance->init();
stopCurl = false;
}
return instance;
}
void HttpClient::destroyInstance()
{
if (instance != NULL)
{
stopCurl = true;
// curl_global_cleanup() will crash on Qt windows
delete instance;
instance = NULL;
}
}
bool HttpClient::init()
{
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
return false;
return true;
}
int HttpClient::HttpGetNoBlock(const string requestURL, const string saveTo, void *sender, event_callback cb, int file_size)
{
string partPath = saveTo;
CURL *multi_handle = NULL;
CURL *easy_handle = NULL;
FILE *fp = NULL;
int ret = UPGRADE_DOWNLOADING;
mFileSize = file_size;
do
{
// Get the file size on the server
downloadFileLength = getDownloadFileLength(requestURL);
if (downloadFileLength < 0)
{
ZLOG("getDownloadFileLength error");
ret = UPGRADE_DOWNLOAD_FAIL_REMOTE_ACCESS;
break;
}
if (mFileSize != -1 && mFileSize != downloadFileLength)
{
ALOGE(" File Error, get %d, http url %f ", mFileSize, downloadFileLength);
ret = UPGRADE_DOWNLOAD_FAIL_FILE_REMOTE_SIZE;;
break;
}
multi_handle = curl_multi_init();
if (!multi_handle)
{
ZLOG("curl_multi_init error");
ret = UPGRADE_DOWNLOAD_FAIL_CURL_INIT;
break;
}
easy_handle = curl_easy_init();
unlink(partPath.c_str());
#ifdef _WIN32
fopen_s(&fp, partPath.c_str(), "ab+");
#else
fp = fopen(partPath.c_str(), "wb");
#endif
if (fp == NULL)
{
ZLOG("file open failed");
ZLOG(partPath.c_str());
ret = UPGRADE_DOWNLOAD_FAIL_LOCAL_ACCESS;
break;
}
// Set the url
ret = curl_easy_setopt(easy_handle, CURLOPT_URL, requestURL.c_str());
// Save data from the server
ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &HttpClient::write_callback);
ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
Progress_User_Data data = { sender, easy_handle, NULL };
// Get the download progress
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, 0L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFOFUNCTION, &HttpClient::progress_callback);
ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFODATA, &data);
// Fail the request if the HTTP code returned is equal to or larger than 400
ret |= curl_easy_setopt(easy_handle, CURLOPT_FAILONERROR, 1L);
// The maximum time that allow the connection parse to the server
ret |= curl_easy_setopt(easy_handle, CURLOPT_CONNECTTIMEOUT, 10L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_LOW_SPEED_TIME, 60L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_LOW_SPEED_LIMIT, 30L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL,1);
resumeByte = getLocalFileLength(partPath);
if (resumeByte > 0)
{
// Set a point to resume transfer
ret |= curl_easy_setopt(easy_handle, CURLOPT_RESUME_FROM_LARGE, resumeByte);
}
if (ret != CURLE_OK)
{
ret = UPGRADE_DOWNLOAD_FAIL_FILE_REMOTE_SIZE;
ZLOG("curl_easy_setopt error");
break;
}
// ret = curl_easy_perform(easy_handle);
ret = curl_multi_add_handle(multi_handle, easy_handle);
ALOGI("curl multl add handle %d\n", ret);
int running_handle_count;
int time_tick = 0;
do
{
ret = curl_multi_perform(multi_handle, &running_handle_count);
if (ret != CURLE_OK)
{
ret = UPGRADE_DOWNLOAD_FAIL_NETWORK;
break;
}
if (stopCurl)
{
//失败处理
ret = UPGRADE_DOWNLOAD_FAIL_POSITIVE_CANCEL;
ALOGI("get stop curl \n");
break;
}
usleep(1);
}
while(running_handle_count > 0);
} while (0);
ALOGI("http client exit ");
int seek_get_size = 0;
if (fp != NULL)
{
seek_get_size = ftell(fp);
fflush(fp);
fsync(fileno(fp));
fclose(fp);
fp = NULL;
}
curl_easy_cleanup(easy_handle);
curl_multi_cleanup(multi_handle);
easy_handle = NULL;
//增加枪查文件大小
if (ret == CURLE_OK)
{
ALOGI("seek file size %d, request size %d", seek_get_size, mFileSize);
if (seek_get_size != mFileSize)
{
ret = UPGRADE_DOWNLOAD_FAIL_LOCAL_SIZE;
}
}
char tmpbuf[512];
sprintf(tmpbuf,"echo \"%d,%d\" > %s", seek_get_size, mFileSize, DOWNLOAD_LOG_FILE);
system(tmpbuf);
sprintf(tmpbuf, "md5sum %s >> %s", saveTo.c_str(), DOWNLOAD_LOG_FILE);
system(tmpbuf);
if (cb)
cb(ret);
return ret;
}
// Return 0 if success, otherwise return error code
int HttpClient::HttpGet(const string requestURL, const string saveTo, void *sender, progress_info_callback cb, long timeout_s)
{
string partPath = saveTo + ".part";
CURL *easy_handle = NULL;
FILE *fp = NULL;
int ret = HTTP_REQUEST_ERROR;
do
{
// Get the file size on the server
downloadFileLength = getDownloadFileLength(requestURL);
if (downloadFileLength < 0)
{
ZLOG("getDownloadFileLength error");
break;
}
easy_handle = curl_easy_init();
if (!easy_handle)
{
ZLOG("curl_easy_init error");
break;
}
#ifdef _WIN32
fopen_s(&fp, partPath.c_str(), "ab+");
#else
fp = fopen(partPath.c_str(), "ab+");
#endif
if (fp == NULL)
{
ZLOG("file open failed");
ZLOG(partPath.c_str());
break;
}
// Set the url
ret = curl_easy_setopt(easy_handle, CURLOPT_URL, requestURL.c_str());
// Save data from the server
ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, &HttpClient::write_callback);
ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, fp);
Progress_User_Data data = { sender, easy_handle, cb};
// Get the download progress
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, 0L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFOFUNCTION, &HttpClient::progress_callback);
ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFODATA, &data);
// Fail the request if the HTTP code returned is equal to or larger than 400
ret |= curl_easy_setopt(easy_handle, CURLOPT_FAILONERROR, 1L);
// The maximum time that allow the connection parse to the server
ret |= curl_easy_setopt(easy_handle, CURLOPT_CONNECTTIMEOUT, 10L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL,1);
ret |= curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT,timeout_s);
resumeByte = getLocalFileLength(partPath);
if (resumeByte > 0)
{
// Set a point to resume transfer
ret |= curl_easy_setopt(easy_handle, CURLOPT_RESUME_FROM_LARGE, resumeByte);
}
if (ret != CURLE_OK)
{
ret = HTTP_REQUEST_ERROR;
ZLOG("curl_easy_setopt error");
break;
}
ret = curl_easy_perform(easy_handle);
if (ret != CURLE_OK)
{
char s[100] = { 0 };
#ifdef _WIN32
sprintf_s(s, sizeof(s), "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));
#else
sprintf(s, "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));
#endif
ZLOG(s);
switch (ret)
{
case CURLE_HTTP_RETURNED_ERROR:
{
int code = 0;
curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &code);
char s[100] = { 0 };
#ifdef _WIN32
sprintf_s(s, sizeof(s), "HTTP error code:%d", code);
#else
sprintf(s, "HTTP error code:%d", code);
#endif
ZLOG(s);
break;
}
}
}
} while (0);
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
curl_easy_cleanup(easy_handle);
easy_handle = NULL;
if (ret == CURLE_OK)
{
remove(saveTo.c_str());
rename(partPath.c_str(), saveTo.c_str());
}
return ret;
}
size_t HttpClient::write_callback(char *buffer, size_t size, size_t nmemb, void *userdata)
{
FILE *fp = static_cast<FILE *>(userdata);
size_t length = fwrite(buffer, size, nmemb, fp);
if (length != nmemb)
{
return length;
}
return size * nmemb;
}
int HttpClient::progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
(void)ultotal;
(void)ulnow;
if(stopCurl)
return 1;
time_t now = time(NULL);
if (now - lastTime < 1)
{
return 0;
}
lastTime = now;
Progress_User_Data *data = static_cast<Progress_User_Data *>(userdata);
CURL *easy_handle = data->handle;
// Defaults to bytes/second
double speed = 0;
curl_easy_getinfo(easy_handle, CURLINFO_SPEED_DOWNLOAD, &speed);
// Time remaining
double leftTime = 0;
// Progress percentage
double progress = 0;
if (dltotal != 0 && speed != 0)
{
progress = (dlnow + resumeByte) / downloadFileLength * 100;
leftTime = (downloadFileLength - dlnow - resumeByte) / speed;
// ALOGI("\t%.2f%%\tRemaing time:%s\n", progress, timeFormat);
ALOGI("progress %f leftTime %f", progress, leftTime);
}
if (data->cb != NULL)
data->cb(data->sender, speed, leftTime, progress);
return 0;
}
// Get the local file size, return -1 if failed
p_off_t HttpClient::getLocalFileLength(string path)
{
p_off_t ret;
struct stat fileStat;
ret = stat(path.c_str(), &fileStat);
if (ret == 0)
{
return fileStat.st_size;
}
return ret;
}
size_t nousecb(char *buffer, size_t x, size_t y, void *userdata)
{
(void)buffer;
(void)userdata;
return x * y;
}
// Get the file size on the server
double HttpClient::getDownloadFileLength(string url)
{
CURL *easy_handle = NULL;
int ret = CURLE_OK;
double size = -1;
do
{
easy_handle = curl_easy_init();
if (!easy_handle)
{
ZLOG("curl_easy_init error");
break;
}
// Only get the header data
ret = curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
ret |= curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOBODY, 1L);
ret |= curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, nousecb); // libcurl_a.lib will return error code 23 without this sentence on windows
if (ret != CURLE_OK)
{
ZLOG("curl_easy_setopt error");
break;
}
ret = curl_easy_perform(easy_handle);
if (ret != CURLE_OK)
{
char s[100] = {0};
#ifdef _WIN32
sprintf_s(s, sizeof(s), "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));
#else
sprintf(s, "error:%d:%s", ret, curl_easy_strerror(static_cast<CURLcode>(ret)));
#endif
ZLOG(s);
break;
}
// size = -1 if no Content-Length return or Content-Length=0
ret = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
if (ret != CURLE_OK)
{
ZLOG("curl_easy_getinfo error");
break;
}
} while (0);
curl_easy_cleanup(easy_handle);
return size;
}