]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/plugins/curl/curl_fetcher.c
Moved debug.[ch] to utils folder
[thirdparty/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 DEFAULT_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 /**
54 * Data to pass to curl callback
55 */
56 typedef struct {
57 fetcher_callback_t cb;
58 void *user;
59 } cb_data_t;
60
61 /**
62 * Curl callback function, invokes fetcher_callback_t function
63 */
64 static size_t curl_cb(void *ptr, size_t size, size_t nmemb, cb_data_t *data)
65 {
66 size_t realsize = size * nmemb;
67
68 if (data->cb(data->user, chunk_create(ptr, realsize)))
69 {
70 return realsize;
71 }
72 return 0;
73 }
74
75 METHOD(fetcher_t, fetch, status_t,
76 private_curl_fetcher_t *this, char *uri, void *userdata)
77 {
78 char error[CURL_ERROR_SIZE];
79 status_t status;
80 cb_data_t data = {
81 .cb = this->cb,
82 .user = userdata,
83 };
84
85 if (this->cb == fetcher_default_callback)
86 {
87 *(chunk_t*)userdata = chunk_empty;
88 }
89
90 if (curl_easy_setopt(this->curl, CURLOPT_URL, uri) != CURLE_OK)
91 { /* URL type not supported by curl */
92 return NOT_SUPPORTED;
93 }
94 curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, error);
95 curl_easy_setopt(this->curl, CURLOPT_FAILONERROR, TRUE);
96 curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, TRUE);
97 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, DEFAULT_TIMEOUT);
98 curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, (void*)curl_cb);
99 curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &data);
100 if (this->headers)
101 {
102 curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);
103 }
104
105 DBG2(DBG_LIB, " sending http request to '%s'...", uri);
106 switch (curl_easy_perform(this->curl))
107 {
108 case CURLE_UNSUPPORTED_PROTOCOL:
109 status = NOT_SUPPORTED;
110 break;
111 case CURLE_OK:
112 status = SUCCESS;
113 break;
114 default:
115 DBG1(DBG_LIB, "libcurl http request failed: %s", error);
116 status = FAILED;
117 break;
118 }
119 return status;
120 }
121
122 METHOD(fetcher_t, set_option, bool,
123 private_curl_fetcher_t *this, fetcher_option_t option, ...)
124 {
125 bool supported = TRUE;
126 va_list args;
127
128 va_start(args, option);
129 switch (option)
130 {
131 case FETCH_REQUEST_DATA:
132 {
133 chunk_t data = va_arg(args, chunk_t);
134
135 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
136 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
137 break;
138 }
139 case FETCH_REQUEST_TYPE:
140 {
141 char header[BUF_LEN];
142 char *request_type = va_arg(args, char*);
143
144 snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
145 this->headers = curl_slist_append(this->headers, header);
146 break;
147 }
148 case FETCH_REQUEST_HEADER:
149 {
150 char *header = va_arg(args, char*);
151
152 this->headers = curl_slist_append(this->headers, header);
153 break;
154 }
155 case FETCH_HTTP_VERSION_1_0:
156 {
157 curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
158 CURL_HTTP_VERSION_1_0);
159 break;
160 }
161 case FETCH_TIMEOUT:
162 {
163 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT,
164 va_arg(args, u_int));
165 break;
166 }
167 case FETCH_CALLBACK:
168 {
169 this->cb = va_arg(args, fetcher_callback_t);
170 break;
171 }
172 default:
173 supported = FALSE;
174 break;
175 }
176 va_end(args);
177 return supported;
178 }
179
180 METHOD(fetcher_t, destroy, void,
181 private_curl_fetcher_t *this)
182 {
183 curl_slist_free_all(this->headers);
184 curl_easy_cleanup(this->curl);
185 free(this);
186 }
187
188 /*
189 * Described in header.
190 */
191 curl_fetcher_t *curl_fetcher_create()
192 {
193 private_curl_fetcher_t *this;
194
195 INIT(this,
196 .public = {
197 .interface = {
198 .fetch = _fetch,
199 .set_option = _set_option,
200 .destroy = _destroy,
201 },
202 },
203 .curl = curl_easy_init(),
204 .cb = fetcher_default_callback,
205 );
206
207 if (!this->curl)
208 {
209 free(this);
210 return NULL;
211 }
212 return &this->public;
213 }
214