forked from sailxy/HttpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
321 lines (260 loc) · 7.26 KB
/
HttpClient.cpp
File metadata and controls
321 lines (260 loc) · 7.26 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
#include "include/HttpClient.h"
#include <sys/stat.h>
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();
}
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;
}
void HttpClient::zlog(const char *date, const char *time, const char *file, const int line, const char *func, const char *str)
{
#ifdef Z_DEBUG
FILE *fp = NULL;
#ifdef _WIN32
fopen_s(&fp, "log.log", "a+");
#else
fopen("log.log", "a+");
#endif
fprintf(fp, "%s | %s | %s | %d | %s | %s\n", date, time, file, line, func, str);
fclose(fp);
#else
(void)str;
#endif // end Z_DEBUG
}
// Return 0 if success, otherwise return error code
int HttpClient::HttpGet(const string requestURL, const string saveTo, void *sender, progress_info_callback cb)
{
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);
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;
//printf("\t%.2f%%\tRemaing time:%s\n", progress, timeFormat);
}
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;
}