curl: Replace spaces in URIs with %20
[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 * Timeout for a transfer
54 */
55 long timeout;
56 };
57
58 /**
59 * Data to pass to curl callback
60 */
61 typedef struct {
62 fetcher_callback_t cb;
63 void *user;
64 } cb_data_t;
65
66 /**
67 * Curl callback function, invokes fetcher_callback_t function
68 */
69 static size_t curl_cb(void *ptr, size_t size, size_t nmemb, cb_data_t *data)
70 {
71 size_t realsize = size * nmemb;
72
73 if (data->cb(data->user, chunk_create(ptr, realsize)))
74 {
75 return realsize;
76 }
77 return 0;
78 }
79
80 METHOD(fetcher_t, fetch, status_t,
81 private_curl_fetcher_t *this, char *uri, void *userdata)
82 {
83 char error[CURL_ERROR_SIZE], *enc_uri;
84 status_t status;
85 cb_data_t data = {
86 .cb = this->cb,
87 .user = userdata,
88 };
89
90 if (this->cb == fetcher_default_callback)
91 {
92 *(chunk_t*)userdata = chunk_empty;
93 }
94
95 /* the URI has to be URL-encoded, we only replace spaces as replacing other
96 * characters (e.g. '/' or ':') would render the URI invalid */
97 enc_uri = strreplace(uri, " ", "%20");
98
99 if (curl_easy_setopt(this->curl, CURLOPT_URL, enc_uri) != CURLE_OK)
100 { /* URL type not supported by curl */
101 status = NOT_SUPPORTED;
102 goto out;
103 }
104 curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, error);
105 curl_easy_setopt(this->curl, CURLOPT_FAILONERROR, TRUE);
106 curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, TRUE);
107 if (this->timeout)
108 {
109 curl_easy_setopt(this->curl, CURLOPT_TIMEOUT, this->timeout);
110 }
111 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
112 curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, (void*)curl_cb);
113 curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &data);
114 if (this->headers)
115 {
116 curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);
117 }
118
119 DBG2(DBG_LIB, " sending http request to '%s'...", uri);
120 switch (curl_easy_perform(this->curl))
121 {
122 case CURLE_UNSUPPORTED_PROTOCOL:
123 status = NOT_SUPPORTED;
124 break;
125 case CURLE_OK:
126 status = SUCCESS;
127 break;
128 default:
129 DBG1(DBG_LIB, "libcurl http request failed: %s", error);
130 status = FAILED;
131 break;
132 }
133
134 out:
135 if (enc_uri != uri)
136 {
137 free(enc_uri);
138 }
139 return status;
140 }
141
142 METHOD(fetcher_t, set_option, bool,
143 private_curl_fetcher_t *this, fetcher_option_t option, ...)
144 {
145 bool supported = TRUE;
146 va_list args;
147
148 va_start(args, option);
149 switch (option)
150 {
151 case FETCH_REQUEST_DATA:
152 {
153 chunk_t data = va_arg(args, chunk_t);
154
155 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
156 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
157 break;
158 }
159 case FETCH_REQUEST_TYPE:
160 {
161 char header[BUF_LEN];
162 char *request_type = va_arg(args, char*);
163
164 snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
165 this->headers = curl_slist_append(this->headers, header);
166 break;
167 }
168 case FETCH_REQUEST_HEADER:
169 {
170 char *header = va_arg(args, char*);
171
172 this->headers = curl_slist_append(this->headers, header);
173 break;
174 }
175 case FETCH_HTTP_VERSION_1_0:
176 {
177 curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
178 CURL_HTTP_VERSION_1_0);
179 break;
180 }
181 case FETCH_TIMEOUT:
182 {
183 this->timeout = va_arg(args, u_int);
184 break;
185 }
186 case FETCH_CALLBACK:
187 {
188 this->cb = va_arg(args, fetcher_callback_t);
189 break;
190 }
191 case FETCH_SOURCEIP:
192 {
193 char buf[64];
194
195 snprintf(buf, sizeof(buf), "%H", va_arg(args, host_t*));
196 supported = curl_easy_setopt(this->curl, CURLOPT_INTERFACE,
197 buf) == CURLE_OK;
198 break;
199 }
200 default:
201 supported = FALSE;
202 break;
203 }
204 va_end(args);
205 return supported;
206 }
207
208 METHOD(fetcher_t, destroy, void,
209 private_curl_fetcher_t *this)
210 {
211 curl_slist_free_all(this->headers);
212 curl_easy_cleanup(this->curl);
213 free(this);
214 }
215
216 /*
217 * Described in header.
218 */
219 curl_fetcher_t *curl_fetcher_create()
220 {
221 private_curl_fetcher_t *this;
222
223 INIT(this,
224 .public = {
225 .interface = {
226 .fetch = _fetch,
227 .set_option = _set_option,
228 .destroy = _destroy,
229 },
230 },
231 .curl = curl_easy_init(),
232 .cb = fetcher_default_callback,
233 );
234
235 if (!this->curl)
236 {
237 free(this);
238 return NULL;
239 }
240 return &this->public;
241 }