File: curl.c

package info (click to toggle)
fbi 2.07-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,240 kB
  • ctags: 2,357
  • sloc: ansic: 17,896; sh: 59; makefile: 33; perl: 12
file content (348 lines) | stat: -rw-r--r-- 7,627 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/select.h>

#include <curl/curl.h>
#include <curl/easy.h>

#include "curl.h"

/* curl globals */
static CURLM *curlm;
static fd_set rd, wr, ex;

/* my globals */
static int url_debug   = 0;
static int url_timeout = 30;

/* my structs */
struct iobuf {
    off_t   start;
    size_t  size;
    char    *data;
};

struct url_state {
    char           *path;
    CURL           *curl;
    char           errmsg[CURL_ERROR_SIZE];
    off_t          curl_pos;
    off_t          buf_pos;
    struct iobuf   buf;
    int            eof;
};

/* ---------------------------------------------------------------------- */
/* curl stuff                                                             */

static void __attribute__ ((constructor)) curl_init(void)
{
    curl_global_init(CURL_GLOBAL_ALL);
    curlm = curl_multi_init();
}

static void __attribute__ ((destructor)) curl_fini(void)
{
    curl_multi_cleanup(curlm);
    curl_global_cleanup();
}

static void curl_free_buffer(struct iobuf *buf)
{
    if (buf->data) {
	free(buf->data);
	memset(buf,0,sizeof(*buf));
    }
}

/* CURLOPT_WRITEFUNCTION */
static int curl_write(void *data,  size_t  size, size_t nmemb, void *handle)
{
    struct url_state *h = handle;

    curl_free_buffer(&h->buf);
    h->buf.start = h->curl_pos;
    h->buf.size  = size * nmemb;
    h->buf.data  = malloc(h->buf.size);
    memcpy(h->buf.data, data, h->buf.size);
    if (url_debug)
	fprintf(stderr,"  put %5d @ %5d\n",
		(int)h->buf.size, (int)h->buf.start);
    
    h->curl_pos += h->buf.size;
    return h->buf.size;
}

/* do transfers */
static int curl_xfer(struct url_state *h)
{
    CURLMcode rc;
    struct timeval tv;
    int count, maxfd;

    FD_ZERO(&rd);
    FD_ZERO(&wr);
    FD_ZERO(&ex);
    maxfd = -1;
    rc = curl_multi_fdset(curlm, &rd, &wr, &ex, &maxfd);
    if (CURLM_OK != rc) {
	fprintf(stderr,"curl_multi_fdset: %d %s\n",rc,h->errmsg);
	return -1;
    }
    if (-1 == maxfd) {
	/* wait 0.1 sec */
	if (url_debug)
	    fprintf(stderr,"wait 0.01 sec\n");
	tv.tv_sec  = 0;
	tv.tv_usec = 100000;
    } else {
	/* wait for data */
	if (url_debug)
	    fprintf(stderr,"select for data [maxfd=%d]\n",maxfd);
	tv.tv_sec  = url_timeout;
	tv.tv_usec = 0;
    }
    switch (select(maxfd+1, &rd, &wr, &ex, &tv)) {
    case -1:
	/* Huh? */
	perror("select");
	exit(1);
    case 0:
	/* timeout */
	return -1;
    }
    for (;;) {
	rc = curl_multi_perform(curlm,&count);
	if (CURLM_CALL_MULTI_PERFORM == rc)
	    continue;
	if (CURLM_OK != rc) {
	    fprintf(stderr,"curl_multi_perform: %d %s\n",rc,h->errmsg);
	    return -1;
	}
	if (0 == count)
	    h->eof = 1;
	break;
    }
    return 0;
}

/* curl setup */
static int curl_setup(struct url_state *h)
{
    if (h->curl) {
	curl_multi_remove_handle(curlm,h->curl);
	curl_easy_cleanup(h->curl);
    }

    h->curl = curl_easy_init();
    curl_easy_setopt(h->curl, CURLOPT_URL,           h->path);
    curl_easy_setopt(h->curl, CURLOPT_ERRORBUFFER,   h->errmsg);
    curl_easy_setopt(h->curl, CURLOPT_WRITEFUNCTION, curl_write);
    curl_easy_setopt(h->curl, CURLOPT_WRITEDATA,     h);
    curl_multi_add_handle(curlm, h->curl);

    h->buf_pos  = 0;
    h->curl_pos = 0;
    h->eof      = 0;
    return 0;
}

/* ---------------------------------------------------------------------- */
/* GNU glibc custom stream interface                                      */

static ssize_t url_read(void *handle, char *buf, size_t size)
{
    struct url_state *h = handle;
    size_t bytes, total;
    off_t off;
    int count;
    
    if (url_debug)
	fprintf(stderr,"url_read(size=%d)\n",(int)size);
    for (total = 0; size > 0;) {
	if (h->buf.start                <= h->buf_pos &&
	    h->buf.start + h->buf.size  >  h->buf_pos) {
	    /* can satisfy from current buffer */
	    bytes = h->buf.start + h->buf.size - h->buf_pos;
	    off   = h->buf_pos - h->buf.start;
	    if (bytes > size)
		bytes = size;
	    memcpy(buf+total, h->buf.data + off, bytes);
	    if (url_debug)
		fprintf(stderr,"  get %5d @ %5d [%5d]\n",
			(int)bytes, (int)h->buf_pos, (int)off);
	    size       -= bytes;
	    total      += bytes;
	    h->buf_pos += bytes;
	    continue;
	}
	if (h->buf_pos < h->buf.start) {
	    /* seeking backwards -- restart transfer */
	    if (url_debug)
		fprintf(stderr,"  rewind\n");
	    curl_free_buffer(&h->buf);
	    curl_setup(h);
	    while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curlm,&count))
		/* nothing */;
	}
	if (h->eof)
	    /* stop on eof */
	    break;
	/* fetch more data */
	if (-1 == curl_xfer(h)) {
	    if (0 == total)
		return -1;
	    break;
	}
    }
    return total;
}

#if 0
static ssize_t url_write(void *handle, const char *buf, size_t size)
{
    //struct url_state *h = handle;

    if (url_debug)
	fprintf(stderr,"url_write(size=%d)\n",(int)size);
    return -1;
}
#endif

static int url_seek(void *handle, off64_t *pos, int whence)
{
    struct url_state *h = handle;
    int rc = 0;

    if (url_debug)
	fprintf(stderr,"url_seek(pos=%d,whence=%d)\n", (int)(*pos), whence);
    switch (whence) {
    case SEEK_SET:
	h->buf_pos = *pos;
	break;
    case SEEK_CUR:
	h->buf_pos += *pos;
	break;
    case SEEK_END: 
	rc = -1;
    }
    *pos = h->buf_pos;
    return rc;
}

static int url_close(void *handle)
{
    struct url_state *h = handle;

    if (url_debug)
	fprintf(stderr,"url_close()\n");
    curl_multi_remove_handle(curlm,h->curl);
    curl_easy_cleanup(h->curl);
    if (h->buf.data)
	free(h->buf.data);
    free(h->path);
    free(h);
    return 0;
}

static cookie_io_functions_t url_hooks = {
    .read  = url_read,
#if 0
    .write = url_write,
#endif
    .seek  = url_seek,
    .close = url_close,
};

static FILE *url_open(const char *path, const char *mode)
{
    FILE *fp;
    struct url_state *h;
    int count;

    if (url_debug)
	fprintf(stderr,"url_open(%s,%s)\n",path,mode);

    h = malloc(sizeof(*h));
    if (NULL == h)
	goto err;
    memset(h,0,sizeof(*h));

    h->path = strdup(path);
    if (NULL == h->path)
	goto err;
    
    /* setup */
    curl_setup(h);
    fp = fopencookie(h, mode, url_hooks);
    if (NULL == fp)
	goto err;

    /* connect + start fetching */
    while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curlm,&count))
	/* nothing */;

    /* check for errors */
    if (0 == count  &&  NULL == h->buf.data) {
	errno = ENOENT;
	goto fetch_err;
    }
	
    /* all done */
    return fp;


 fetch_err:
    curl_multi_remove_handle(curlm,h->curl);
 err:
    if (h->curl)
	curl_easy_cleanup(h->curl);
    if (h->path)
	free(h->path);
    if (h)
	free(h);
    return NULL;
}

/* ---------------------------------------------------------------------- */
/* hook into fopen using GNU ld's --wrap                                  */

int curl_is_url(const char *url)
{
    static char *protocols[] = {
	"ftp://",
	"http://",
	NULL,
    };
    int i;

    for (i = 0; protocols[i] != NULL; i++)
	if (0 == strncasecmp(url, protocols[i], strlen(protocols[i])))
	    return 1;
    return 0;
}

FILE *__wrap_fopen(const char *path, const char *mode);
FILE *__real_fopen(const char *path, const char *mode);

FILE *__wrap_fopen(const char *path, const char *mode)
{
    if (url_debug)
	fprintf(stderr,"fopen(%s,%s)\n",path,mode);

    /* catch URLs */
    if (curl_is_url(path)) {
	if (strchr(mode,'w')) {
	    fprintf(stderr,"write access over ftp/http is not supported, sorry\n");
	    return NULL;
	}
	return url_open(path,mode);
    }
    
    /* files passed to the real fopen */
    return __real_fopen(path,mode);
}