Migrated curl_fetcher to INIT/METHOD macros
[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(" 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("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 va_list args;
108
109 va_start(args, option);
110 switch (option)
111 {
112 case FETCH_REQUEST_DATA:
113 {
114 chunk_t data = va_arg(args, chunk_t);
115
116 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, (char*)data.ptr);
117 curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, data.len);
118 return TRUE;
119 }
120 case FETCH_REQUEST_TYPE:
121 {
122 char header[BUF_LEN];
123 char *request_type = va_arg(args, char*);
124
125 snprintf(header, BUF_LEN, "Content-Type: %s", request_type);
126 this->headers = curl_slist_append(this->headers, header);
127 return TRUE;
128 }
129 case FETCH_REQUEST_HEADER:
130 {
131 char *header = va_arg(args, char*);
132
133 this->headers = curl_slist_append(this->headers, header);
134 return TRUE;
135 }
136 case FETCH_HTTP_VERSION_1_0:
137 {
138 curl_easy_setopt(this->curl, CURLOPT_HTTP_VERSION,
139 CURL_HTTP_VERSION_1_0);
140 return TRUE;
141 }
142 case FETCH_TIMEOUT:
143 {
144 curl_easy_setopt(this->curl, CURLOPT_CONNECTTIMEOUT,
145 va_arg(args, u_int));
146 return TRUE;
147 }
148 default:
149 return FALSE;
150 }
151 }
152
153 METHOD(fetcher_t, destroy, void,
154 private_curl_fetcher_t *this)
155 {
156 curl_slist_free_all(this->headers);
157 curl_easy_cleanup(this->curl);
158 free(this);
159 }
160
161 /*
162 * Described in header.
163 */
164 curl_fetcher_t *curl_fetcher_create()
165 {
166 private_curl_fetcher_t *this;
167
168 INIT(this,
169 .public.interface = {
170 .fetch = _fetch,
171 .set_option = _set_option,
172 .destroy = _destroy,
173 },
174 .curl = curl_easy_init(),
175 );
176
177 if (!this->curl)
178 {
179 free(this);
180 return NULL;
181 }
182 return &this->public;
183 }
184