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
|
/*
* Copyright (C) 2008 Martin Willi
* Copyright (C) 2007 Andreas Steffen
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <curl/curl.h>
#include <library.h>
#include <debug.h>
#include "curl_fetcher.h"
#define DEFAULT_TIMEOUT 10
typedef struct private_curl_fetcher_t private_curl_fetcher_t;
/**
* private data of a curl_fetcher_t object.
*/
struct private_curl_fetcher_t {
/**
* Public data
*/
curl_fetcher_t public;
/**
* CURL handle
*/
CURL* curl;
/**
* Optional HTTP headers
*/
struct curl_slist *headers;
};
/**
* writes data into a dynamically resizeable chunk_t
*/
static size_t append(void *ptr, size_t size, size_t nmemb, chunk_t *data)
{
size_t realsize = size * nmemb;
data->ptr = (u_char*)realloc(data->ptr, data->len + realsize);
if (data->ptr)
{
memcpy(&data->ptr[data->len], ptr, realsize);
data->len += realsize;
}
return realsize;
}
METHOD(fetcher_t, fetch, status_t,
private_curl_fetcher_t *this, char *uri, chunk_t *result)
{
char error[CURL_ERROR_SIZE];
status_t status;
*result = chunk_empty;
if (curl_easy_setopt(this->curl, CURLOPT_URL, uri) != CURLE_OK)
{ /* URL type not supported by curl */
return NOT_SUPPORTED;
}
curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, error);
curl_easy_setopt(this->curl, CURLOPT_FAILONERROR, TRUE);
curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, TRUE);
curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, DEFAULT_TIMEOUT);
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, (void*)append);
curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, (void*)result);
if (this->headers)
{
curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);
}
DBG2(" sending http request to '%s'...", uri);
switch (curl_easy_perform(this->curl))
{
case CURLE_UNSUPPORTED_PROTOCOL:
status = NOT_SUPPORTED;
break;
case CURLE_OK:
status = SUCCESS;
break;
default:
DBG1("libcurl http request failed: %s", error);
status = FAILED;
break;
}
return status;
}
METHOD(fetcher_t, set_option, bool,
private_curl_fetcher_t *this, fetcher_option_t option, ...)
{
va_list args;
va_start(args, option);
switch (option)
{
case FETCH_REQUEST_DATA:
{
chunk_t data = va_arg(args, chunk_t);
curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
return TRUE;
}
case FETCH_REQUEST_TYPE:
{
char header[BUF_LEN];
char *request_type = va_arg(args, char*);
snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
this->headers = curl_slist_append(this->headers, header);
return TRUE;
}
case FETCH_REQUEST_HEADER:
{
char *header = va_arg(args, char*);
this->headers = curl_slist_append(this->headers, header);
return TRUE;
}
case FETCH_HTTP_VERSION_1_0:
{
curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
CURL_HTTP_VERSION_1_0);
return TRUE;
}
case FETCH_TIMEOUT:
{
curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT,
va_arg(args, u_int));
return TRUE;
}
default:
return FALSE;
}
}
METHOD(fetcher_t, destroy, void,
private_curl_fetcher_t *this)
{
curl_slist_free_all(this->headers);
curl_easy_cleanup(this->curl);
free(this);
}
/*
* Described in header.
*/
curl_fetcher_t *curl_fetcher_create()
{
private_curl_fetcher_t *this;
INIT(this,
.public.interface = {
.fetch = _fetch,
.set_option = _set_option,
.destroy = _destroy,
},
.curl = curl_easy_init(),
);
if (!this->curl)
{
free(this);
return NULL;
}
return &this->public;
}
|