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