/
curl.c
480 lines (408 loc) · 13.8 KB
/
curl.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
#include "internal.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
#include "version.h"
/* internal multi curl handle */
static CURLM *_pndman_curlm = NULL;
/* \brief init internal curl header */
static void _pndman_curl_header_init(pndman_curl_header *header)
{
memset(header, 0, sizeof(pndman_curl_header));
if (!(header->data = malloc(PNDMAN_CURL_CHUNK)))
return;
memset(header->data, 0, PNDMAN_CURL_CHUNK);
header->size = PNDMAN_CURL_CHUNK;
}
/* \brief free internal curl header */
static void _pndman_curl_header_free(pndman_curl_header *header)
{
IFDO(free, header->data);
memset(header, 0, sizeof(pndman_curl_header));
}
/* \brief initialize the progress struct */
static void _pndman_curl_init_progress(pndman_curl_progress *progress)
{
memset(progress, 0, sizeof(pndman_curl_progress));
}
/* \brief write to file */
static size_t _pndman_curl_write_file(void *data, size_t size, size_t nmemb, void *out)
{
pndman_curl_handle *handle = out;
if (!handle || handle->free) return 0;
return fwrite(data, size, nmemb, handle->file);
}
/* \brief write to header */
static size_t _pndman_curl_write_header(char *data, size_t size, size_t nmemb, void *out)
{
void *buffer;
pndman_curl_handle *handle = out;
pndman_curl_header *header;
if (!handle || handle->free) return 0;
header = &handle->header;
/* init header if needed */
if (!header->size)
_pndman_curl_header_init(header);
/* if we have more data than we can hold */
if (header->pos + size * nmemb >= header->size - 1) {
if (!(buffer = realloc(header->data,
header->size + PNDMAN_CURL_CHUNK))) {
if (!(buffer = malloc(header->size + PNDMAN_CURL_CHUNK)))
return 0;
memcpy(buffer, header->data, header->size);
}
memset(header->data+header->size, 0,
header->size + PNDMAN_CURL_CHUNK);
header->data = buffer;
header->size += PNDMAN_CURL_CHUNK;
}
/* memcpy new data */
memcpy(header->data + header->pos, data, size * nmemb);
header->pos += size * nmemb;
return size * nmemb;
}
/* \brief progressbar */
static int _pndman_curl_progress_func(void *userdata,
double total_to_download, double download, double total_to_upload, double upload)
{
(void)total_to_upload;
(void)upload;
pndman_curl_handle *handle = userdata;
if (!handle || handle->free) return 1;
handle->progress->download = handle->resume + download;
handle->progress->total_to_download = handle->resume + total_to_download;
handle->callback(PNDMAN_CURL_PROGRESS, handle->data, NULL, handle);
return 0;
}
static void _pndman_curl_handle_free_real(pndman_curl_handle *handle)
{
assert(handle);
/* we were not requested to do this */
if (!handle->free) return;
/* cleanup */
if (handle->curl) {
if (_pndman_curlm) curl_multi_remove_handle(_pndman_curlm, handle->curl);
curl_easy_cleanup(handle->curl);
}
_pndman_curl_header_free(&handle->header);
IFDO(curl_slist_free_all, handle->header_list);
IFDO(fclose, handle->file);
/* unlink on free
* commented since we allow download resumes! */
#if 0
if (handle->path) unlink(handle->path);
#endif
memset(handle, 0, sizeof(pndman_curl_handle));
/* free handle */
free(handle);
}
/* INTERNAL API */
/* \brief free curl handle */
void _pndman_curl_handle_free(pndman_curl_handle *handle)
{
assert(handle);
handle->free = 1;
handle->progress = NULL;
handle->callback = NULL;
IFDO(free, handle->path);
IFDO(free, handle->url);
IFDO(free, handle->post);
/* can we free immediatly? */
if (!_pndman_curlm)
_pndman_curl_handle_free_real(handle);
}
/* \brief create new curl handle */
pndman_curl_handle* _pndman_curl_handle_new(void *data,
pndman_curl_progress *progress, pndman_curl_callback callback,
const char *path)
{
pndman_curl_handle *handle;
if (!(handle = calloc(1, sizeof(pndman_curl_handle))))
goto handle_fail;
if (!(handle->curl = curl_easy_init()))
goto curl_fail;
/* set defaults */
if (path) handle->path = strdup(path);
handle->data = data;
handle->callback = callback;
handle->progress = progress;
return handle;
handle_fail:
DEBFAIL(PNDMAN_ALLOC_FAIL, "pndman_curl_handle");
goto fail;
curl_fail:
DEBFAIL(PNDMAN_ALLOC_FAIL, "CURL");
fail:
IFDO(_pndman_curl_handle_free, handle);
return NULL;
}
/* \brief set curl handle's url */
void _pndman_curl_handle_set_url(pndman_curl_handle *handle, const char *url)
{
assert(handle);
IFDO(free, handle->url);
if (url) handle->url = strdup(url);
}
/* \brief set curl handle's post field */
void _pndman_curl_handle_set_post(pndman_curl_handle *handle, const char *post)
{
assert(handle);
IFDO(free, handle->post);
if (post) handle->post = strdup(post);
}
void _pndman_curl_handle_reset(pndman_curl_handle *handle)
{
assert(handle);
curl_multi_remove_handle(_pndman_curlm, handle->curl);
curl_easy_reset(handle->curl);
if (handle->file) {
fclose(handle->file);
handle->file = NULL;
}
}
/* \brief perform curl operation */
int _pndman_curl_handle_perform(pndman_curl_handle *handle)
{
assert(handle);
struct curl_slist *slist = NULL;
/* no callback or data, we will fail */
if (!handle->data || !handle->callback)
goto no_data_or_callback;
/* no url, fail */
if (!handle->url)
goto no_url;
/* reopen handle if needed */
if (handle->file) {
_pndman_curl_handle_reset(handle);
_pndman_curl_header_free(&handle->header);
DEBUG(PNDMAN_LEVEL_CRAP, "CURL REOPEN");
}
if (!handle->path) {
if (!(handle->file = _pndman_get_tmp_file()))
goto fail;
} else {
/* this temporary file exists, lets do resume! */
if ((handle->file = fopen(handle->path, "rb"))) {
fseek(handle->file, 0L, SEEK_END);
handle->resume = ftell(handle->file);
fclose(handle->file);
}
if (!(handle->file = fopen(handle->path, handle->resume?"a+b":"w+b")))
goto open_fail;
}
/* print url */
DEBUG(PNDMAN_LEVEL_CRAP, "url: %s", handle->url);
/* set curl options */
curl_easy_setopt(handle->curl, CURLOPT_USERAGENT, "libpndman ("VERSION")");
curl_easy_setopt(handle->curl, CURLOPT_URL, handle->url);
if (handle->post) {
curl_easy_setopt(handle->curl, CURLOPT_POSTFIELDS, handle->post);
DEBUG(PNDMAN_LEVEL_CRAP, "POST: %s", handle->post);
}
curl_easy_setopt(handle->curl, CURLOPT_HEADERFUNCTION, _pndman_curl_write_header);
curl_easy_setopt(handle->curl, CURLOPT_HEADERFUNCTION, _pndman_curl_write_header);
curl_easy_setopt(handle->curl, CURLOPT_CONNECTTIMEOUT, pndman_get_curl_timeout());
curl_easy_setopt(handle->curl, CURLOPT_NOPROGRESS, handle->progress?0:1);
curl_easy_setopt(handle->curl, CURLOPT_PROGRESSFUNCTION, _pndman_curl_progress_func);
curl_easy_setopt(handle->curl, CURLOPT_HEADERDATA, handle);
curl_easy_setopt(handle->curl, CURLOPT_PROGRESSDATA, handle);
curl_easy_setopt(handle->curl, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(handle->curl, CURLOPT_PRIVATE, handle);
curl_easy_setopt(handle->curl, CURLOPT_WRITEFUNCTION, _pndman_curl_write_file);
curl_easy_setopt(handle->curl, CURLOPT_WRITEDATA, handle);
curl_easy_setopt(handle->curl, CURLOPT_LOW_SPEED_LIMIT, 10240L);
curl_easy_setopt(handle->curl, CURLOPT_LOW_SPEED_TIME, 300L);
if (handle->resume && handle->path) {
curl_easy_setopt(handle->curl, CURLOPT_RESUME_FROM, handle->resume);
DEBUG(PNDMAN_LEVEL_CRAP, "Handle resume: %zu", handle->resume);
}
if (handle->if_modified_since) {
char header[82];
char rfc2822[42];
struct tm *ptm = gmtime(&handle->if_modified_since);
strftime(rfc2822, sizeof(rfc2822)-1, "%a, %d %b %Y %H:%M:%S %z", ptm);
snprintf(header, sizeof(header)-1, "If-Modified-Since: %s", rfc2822);
slist = curl_slist_append(slist, header);
curl_easy_setopt(handle->curl, CURLOPT_HTTPHEADER, slist);
handle->header_list = slist;
DEBUG(PNDMAN_LEVEL_CRAP, header);
}
if (!_pndman_curlm && !(_pndman_curlm = curl_multi_init()))
goto curlm_fail;
/* init progress if needed */
if (handle->progress)
_pndman_curl_init_progress(handle->progress);
if (curl_multi_add_handle(_pndman_curlm, handle->curl) != CURLM_OK)
goto add_fail;
return RETURN_OK;
no_data_or_callback:
DEBFAIL(CURL_NO_DATA_OR_CALLBACK);
goto fail;
no_url:
DEBFAIL(CURL_NO_URL);
goto fail;
open_fail:
DEBFAIL(ACCESS_FAIL, handle->path);
goto fail;
curlm_fail:
DEBFAIL(PNDMAN_ALLOC_FAIL, "CURLM");
goto fail;
add_fail:
DEBFAIL("curl_multi_add_handle failed");
fail:
IFDO(fclose, handle->file);
IFDO(curl_slist_free_all, slist);
if (handle->path) unlink(handle->path);
return RETURN_FAIL;
}
/* \brief query cleanup */
static void _pndman_curl_cleanup(void)
{
if (!_pndman_curlm) return;
curl_multi_cleanup(_pndman_curlm);
curl_global_cleanup();
_pndman_curlm = NULL;
}
/* \brief handle curl message */
static void _pndman_curl_msg(int result, pndman_curl_handle *handle)
{
if (!handle || handle->free)
return;
if (handle->progress) handle->progress->done = 1;
if (pndman_get_verbose() >= PNDMAN_LEVEL_CRAP) {
if (handle->header.size)
DEBUG(PNDMAN_LEVEL_CRAP, "%s", (char*)handle->header.data);
char buffer[256];
memset(buffer, 0, sizeof(buffer));
fseek(handle->file, 0L, SEEK_SET);
if (fgets(buffer, sizeof(buffer)-1, handle->file) && strlen(buffer) > 5)
DEBUG(PNDMAN_LEVEL_CRAP, "%s", buffer);
fseek(handle->file, 0L, SEEK_SET);
}
if (result != CURLE_OK) {
/* is http 1.1 resume supported? */
if (result == CURLE_RANGE_ERROR ||
result == CURLE_BAD_DOWNLOAD_RESUME)
handle->retry = PNDMAN_CURL_MAX_RETRY;
/* try http 1.1 download resume */
if ((result == CURLE_PARTIAL_FILE ||
result == CURLE_WRITE_ERROR ||
result == CURLE_OPERATION_TIMEDOUT ||
result == CURLE_ABORTED_BY_CALLBACK) &&
handle->retry < PNDMAN_CURL_MAX_RETRY) {
/* retry */
DEBUG(PNDMAN_LEVEL_CRAP, "%s", curl_easy_strerror(result));
if (_pndman_curl_handle_perform(handle) == RETURN_OK)
handle->retry++;
else handle->retry = PNDMAN_CURL_MAX_RETRY;
} else handle->retry = PNDMAN_CURL_MAX_RETRY;
/* fail if max retries exceeded */
if (handle->retry >= PNDMAN_CURL_MAX_RETRY) {
handle->resume = 0;
handle->retry = 0;
handle->callback(PNDMAN_CURL_FAIL, handle->data,
curl_easy_strerror(result), handle);
}
} else {
handle->resume = 0;
handle->retry = 0;
fflush(handle->file);
handle->callback(PNDMAN_CURL_DONE, handle->data, NULL, handle);
_pndman_curl_handle_reset(handle);
}
}
/* \brief perform curl operation */
static int _pndman_curl_perform(unsigned long tv_sec, unsigned long tv_usec)
{
int still_running;
int maxfd = -1;
struct timeval timeout;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
long curl_timeout = -1;
CURLMcode ret;
CURLMsg *msg;
int msgs_left;
pndman_curl_handle *handle;
/* perform download */
while ((ret = curl_multi_perform(_pndman_curlm, &still_running)) == CURLM_CALL_MULTI_PERFORM);
/* check error */
if (ret != CURLM_OK)
goto fail;
/* run multi_timeout, if still running */
if (still_running) {
/* zero file descriptions */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
memset(&timeout, 0, sizeof(timeout));
timeout.tv_sec = tv_sec;
timeout.tv_usec = tv_usec;
/* timeout */
ret = curl_multi_timeout(_pndman_curlm, &curl_timeout);
if (ret != CURLM_OK) goto fail;
if (curl_timeout >= 0) {
timeout.tv_sec = curl_timeout / 1000;
timeout.tv_usec = 0;
if (timeout.tv_sec > 60) {
timeout.tv_sec = 60;
} else if (timeout.tv_sec == 0) {
timeout.tv_usec = curl_timeout * 1000;
}
}
/* get file descriptors from the transfers */
ret = curl_multi_fdset(_pndman_curlm, &fdread, &fdwrite, &fdexcep, &maxfd);
if (ret != CURLM_OK || maxfd < -1) goto fail;
select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
}
/* update status of curl handles */
while ((msg = curl_multi_info_read(_pndman_curlm, &msgs_left))) {
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&handle);
if (msg->msg == CURLMSG_DONE) { /* DONE */
_pndman_curl_msg(msg->data.result, handle);
/* free handle if requested */
if (handle && handle->free)
_pndman_curl_handle_free_real(handle);
/* if we got messages and still running == 0
* make it back 1, since callbacks might restart
* curl requests */
if (!still_running) still_running = 1;
}
}
return still_running;
fail:
DEBFAIL("%s", ret);
return -1;
}
/* PUBLIC API */
/* \brief process all internal curl requests */
PNDMANAPI int pndman_curl_process(unsigned long tv_sec, unsigned long tv_usec)
{
int still_running;
/* we are done :) */
if (!_pndman_curlm) return 0;
/* perform sync */
if ((still_running = _pndman_curl_perform(tv_sec, tv_usec)) == -1)
goto fail;
/* destoroy curlm when done,
* and return exit code */
if (!still_running) {
_pndman_curl_cleanup();
/* fake so that we are still running, why?
* this lets user to catch the final completed handles
*
* However, in recent version callbacks
* are the better method, but this doesn't hurt anything */
still_running = 1;
}
return still_running;
fail:
_pndman_curl_cleanup();
return RETURN_FAIL;
}
/* vim: set ts=8 sw=3 tw=0 :*/