Added a new FETCH_CALLBACK option to fetch data without allocation
[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 <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 /**
49 * writes data into a dynamically resizeable chunk_t
50 */
51 static size_t append(void *ptr, size_t size, size_t nmemb, chunk_t *data)
52 {
53 size_t realsize = size * nmemb;
54
55 data->ptr = (u_char*)realloc(data->ptr, data->len + realsize);
56 if (data->ptr)
57 {
58 memcpy(&data->ptr[data->len], ptr, realsize);
59 data->len += realsize;
60 }
61 return realsize;
62 }
63
64 METHOD(fetcher_t, fetch, status_t,
65 private_curl_fetcher_t *this, char *uri, void *userdata)
66 {
67 char error[CURL_ERROR_SIZE];
68 status_t status;
69 chunk_t *result = userdata;
70
71 *result = chunk_empty;
72
73 if (curl_easy_setopt(this->curl, CURLOPT_URL, uri) != CURLE_OK)
74 { /* URL type not supported by curl */
75 return NOT_SUPPORTED;
76 }
77 curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, error);
78 curl_easy_setopt(this->curl, CURLOPT_FAILONERROR, TRUE);
79 curl_easy_setopt(this->curl, CURLOPT_NOSIGNAL, TRUE);
80 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT, DEFAULT_TIMEOUT);
81 curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, (void*)append);
82 curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, (void*)result);
83 if (this->headers)
84 {
85 curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);
86 }
87
88 DBG2(DBG_LIB, " sending http request to '%s'...", uri);
89 switch (curl_easy_perform(this->curl))
90 {
91 case CURLE_UNSUPPORTED_PROTOCOL:
92 status = NOT_SUPPORTED;
93 break;
94 case CURLE_OK:
95 status = SUCCESS;
96 break;
97 default:
98 DBG1(DBG_LIB, "libcurl http request failed: %s", error);
99 status = FAILED;
100 break;
101 }
102 return status;
103 }
104
105 METHOD(fetcher_t, set_option, bool,
106 private_curl_fetcher_t *this, fetcher_option_t option, ...)
107 {
108 bool supported = TRUE;
109 va_list args;
110
111 va_start(args, option);
112 switch (option)
113 {
114 case FETCH_REQUEST_DATA:
115 {
116 chunk_t data = va_arg(args, chunk_t);
117
118 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
119 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
120 break;
121 }
122 case FETCH_REQUEST_TYPE:
123 {
124 char header[BUF_LEN];
125 char *request_type = va_arg(args, char*);
126
127 snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
128 this->headers = curl_slist_append(this->headers, header);
129 break;
130 }
131 case FETCH_REQUEST_HEADER:
132 {
133 char *header = va_arg(args, char*);
134
135 this->headers = curl_slist_append(this->headers, header);
136 break;
137 }
138 case FETCH_HTTP_VERSION_1_0:
139 {
140 curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
141 CURL_HTTP_VERSION_1_0);
142 break;
143 }
144 case FETCH_TIMEOUT:
145 {
146 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT,
147 va_arg(args, u_int));
148 break;
149 }
150 default:
151 supported = FALSE;
152 break;
153 }
154 va_end(args);
155 return supported;
156 }
157
158 METHOD(fetcher_t, destroy, void,
159 private_curl_fetcher_t *this)
160 {
161 curl_slist_free_all(this->headers);
162 curl_easy_cleanup(this->curl);
163 free(this);
164 }
165
166 /*
167 * Described in header.
168 */
169 curl_fetcher_t *curl_fetcher_create()
170 {
171 private_curl_fetcher_t *this;
172
173 INIT(this,
174 .public = {
175 .interface = {
176 .fetch = _fetch,
177 .set_option = _set_option,
178 .destroy = _destroy,
179 },
180 },
181 .curl = curl_easy_init(),
182 );
183
184 if (!this->curl)
185 {
186 free(this);
187 return NULL;
188 }
189 return &this->public;
190 }
191