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