Merge branch 'fetcher-response-code'
[strongswan.git] / src / libstrongswan / plugins / curl / curl_fetcher.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2007 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <curl/curl.h>
18
19 #include <library.h>
20 #include <utils/debug.h>
21
22 #include "curl_fetcher.h"
23
24 #define CONNECT_TIMEOUT 10
25
26 typedef struct private_curl_fetcher_t private_curl_fetcher_t;
27
28 /**
29 * private data of a curl_fetcher_t object.
30 */
31 struct private_curl_fetcher_t {
32 /**
33 * Public data
34 */
35 curl_fetcher_t public;
36
37 /**
38 * CURL handle
39 */
40 CURL* curl;
41
42 /**
43 * Optional HTTP headers
44 */
45 struct curl_slist *headers;
46
47 /**
48 * Callback function
49 */
50 fetcher_callback_t cb;
51
52 /**
53 * Variable that receives the response code
54 */
55 u_int *result;
56
57 /**
58 * Timeout for a transfer
59 */
60 long timeout;
61 };
62
63 /**
64 * Data to pass to curl callback
65 */
66 typedef struct {
67 fetcher_callback_t cb;
68 void *user;
69 } cb_data_t;
70
71 /**
72 * Curl callback function, invokes fetcher_callback_t function
73 */
74 static size_t curl_cb(void *ptr, size_t size, size_t nmemb, cb_data_t *data)
75 {
76 size_t realsize = size * nmemb;
77
78 if (data->cb(data->user, chunk_create(ptr, realsize)))
79 {
80 return realsize;
81 }
82 return 0;
83 }
84
85 METHOD(fetcher_t, fetch, status_t,
86 private_curl_fetcher_t *this, char *uri, void *userdata)
87 {
88 char error[CURL_ERROR_SIZE], *enc_uri;
89 status_t status;
90 long result = 0;
91 cb_data_t data = {
92 .cb = this->cb,
93 .user = userdata,
94 };
95
96 if (this->cb == fetcher_default_callback)
97 {
98 *(chunk_t*)userdata = chunk_empty;
99 }
100
101 /* the URI has to be URL-encoded, we only replace spaces as replacing other
102 * characters (e.g. '/' or ':') would render the URI invalid */
103 enc_uri = strreplace(uri, " ", "%20");
104
105 if (curl_easy_setopt(this->curl, CURLOPT_URL, enc_uri) != CURLE_OK)
106 { /* URL type not supported by curl */
107 status = NOT_SUPPORTED;
108 goto out;
109 }
110 curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, error);
111 curl_easy_setopt(this->curl, CURLOPT_FAILONERROR, TRUE);
112 curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, TRUE);
113 if (this->timeout)
114 {
115 curl_easy_setopt(this->curl, CURLOPT_TIMEOUT, this->timeout);
116 }
117 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
118 curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, (void*)curl_cb);
119 curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &data);
120 if (this->headers)
121 {
122 curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);
123 }
124
125 DBG2(DBG_LIB, " sending http request to '%s'...", uri);
126 switch (curl_easy_perform(this->curl))
127 {
128 case CURLE_UNSUPPORTED_PROTOCOL:
129 status = NOT_SUPPORTED;
130 break;
131 case CURLE_OK:
132 if (this->result)
133 {
134 curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE,
135 &result);
136 *this->result = result;
137 }
138 status = SUCCESS;
139 break;
140 default:
141 if (this->result)
142 { /* don't log an error in this case */
143 curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE,
144 &result);
145 *this->result = result;
146 }
147 else
148 {
149 DBG1(DBG_LIB, "libcurl http request failed: %s", error);
150 }
151 status = FAILED;
152 break;
153 }
154
155 out:
156 if (enc_uri != uri)
157 {
158 free(enc_uri);
159 }
160 return status;
161 }
162
163 METHOD(fetcher_t, set_option, bool,
164 private_curl_fetcher_t *this, fetcher_option_t option, ...)
165 {
166 bool supported = TRUE;
167 va_list args;
168
169 va_start(args, option);
170 switch (option)
171 {
172 case FETCH_REQUEST_DATA:
173 {
174 chunk_t data = va_arg(args, chunk_t);
175
176 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
177 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
178 break;
179 }
180 case FETCH_REQUEST_TYPE:
181 {
182 char header[BUF_LEN];
183 char *request_type = va_arg(args, char*);
184
185 snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
186 this->headers = curl_slist_append(this->headers, header);
187 break;
188 }
189 case FETCH_REQUEST_HEADER:
190 {
191 char *header = va_arg(args, char*);
192
193 this->headers = curl_slist_append(this->headers, header);
194 break;
195 }
196 case FETCH_HTTP_VERSION_1_0:
197 {
198 curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
199 CURL_HTTP_VERSION_1_0);
200 break;
201 }
202 case FETCH_TIMEOUT:
203 {
204 this->timeout = va_arg(args, u_int);
205 break;
206 }
207 case FETCH_CALLBACK:
208 {
209 this->cb = va_arg(args, fetcher_callback_t);
210 break;
211 }
212 case FETCH_RESPONSE_CODE:
213 {
214 this->result = va_arg(args, u_int*);
215 break;
216 }
217 case FETCH_SOURCEIP:
218 {
219 char buf[64];
220
221 snprintf(buf, sizeof(buf), "%H", va_arg(args, host_t*));
222 supported = curl_easy_setopt(this->curl, CURLOPT_INTERFACE,
223 buf) == CURLE_OK;
224 break;
225 }
226 default:
227 supported = FALSE;
228 break;
229 }
230 va_end(args);
231 return supported;
232 }
233
234 METHOD(fetcher_t, destroy, void,
235 private_curl_fetcher_t *this)
236 {
237 curl_slist_free_all(this->headers);
238 curl_easy_cleanup(this->curl);
239 free(this);
240 }
241
242 /*
243 * Described in header.
244 */
245 curl_fetcher_t *curl_fetcher_create()
246 {
247 private_curl_fetcher_t *this;
248
249 INIT(this,
250 .public = {
251 .interface = {
252 .fetch = _fetch,
253 .set_option = _set_option,
254 .destroy = _destroy,
255 },
256 },
257 .curl = curl_easy_init(),
258 .cb = fetcher_default_callback,
259 );
260
261 if (!this->curl)
262 {
263 free(this);
264 return NULL;
265 }
266 return &this->public;
267 }