unit-tests: Don't assert failures for unreadable settings files as root
[strongswan.git] / src / libstrongswan / plugins / soup / soup_fetcher.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "soup_fetcher.h"
17
18 #include <libsoup/soup.h>
19
20 #include <library.h>
21 #include <utils/debug.h>
22
23 #define DEFAULT_TIMEOUT 10
24
25 typedef struct private_soup_fetcher_t private_soup_fetcher_t;
26
27 /**
28 * private data of a soup_fetcher_t object.
29 */
30 struct private_soup_fetcher_t {
31
32 /**
33 * Public data
34 */
35 soup_fetcher_t public;
36
37 /**
38 * HTTP request method
39 */
40 const char *method;
41
42 /**
43 * Request content type
44 */
45 char *type;
46
47 /**
48 * Request data
49 */
50 chunk_t data;
51
52 /**
53 * Request timeout
54 */
55 u_int timeout;
56
57 /**
58 * HTTP request version
59 */
60 SoupHTTPVersion version;
61
62 /**
63 * Fetcher callback function
64 */
65 fetcher_callback_t cb;
66 };
67
68 /**
69 * Data to pass to soup callback
70 */
71 typedef struct {
72 fetcher_callback_t cb;
73 void *user;
74 SoupSession *session;
75 } cb_data_t;
76
77 /**
78 * Soup callback invoking our callback
79 */
80 static void soup_cb(SoupMessage *message, SoupBuffer *chunk, cb_data_t *data)
81 {
82 if (!data->cb(data->user, chunk_create((u_char*)chunk->data, chunk->length)))
83 {
84 soup_session_cancel_message(data->session, message,
85 SOUP_STATUS_CANCELLED);
86 }
87 }
88
89 METHOD(fetcher_t, fetch, status_t,
90 private_soup_fetcher_t *this, char *uri, void *userdata)
91 {
92 SoupMessage *message;
93 status_t status = FAILED;
94 cb_data_t data = {
95 .cb = this->cb,
96 .user = userdata,
97 };
98
99 message = soup_message_new(this->method, uri);
100 if (!message)
101 {
102 return NOT_SUPPORTED;
103 }
104 if (this->cb == fetcher_default_callback)
105 {
106 *(chunk_t*)userdata = chunk_empty;
107 }
108 if (this->type)
109 {
110 soup_message_set_request(message, this->type, SOUP_MEMORY_STATIC,
111 this->data.ptr, this->data.len);
112 }
113 soup_message_set_http_version(message, this->version);
114 soup_message_body_set_accumulate(message->response_body, FALSE);
115 g_signal_connect(message, "got-chunk", G_CALLBACK(soup_cb), &data);
116 data.session = soup_session_sync_new();
117 g_object_set(G_OBJECT(data.session),
118 SOUP_SESSION_TIMEOUT, (guint)this->timeout, NULL);
119
120 DBG2(DBG_LIB, "sending http request to '%s'...", uri);
121 soup_session_send_message(data.session, message);
122 if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
123 {
124 status = SUCCESS;
125 }
126 else
127 {
128 DBG1(DBG_LIB, "HTTP request failed: %s", message->reason_phrase);
129 }
130 g_object_unref(G_OBJECT(message));
131 g_object_unref(G_OBJECT(data.session));
132 return status;
133 }
134
135 METHOD(fetcher_t, set_option, bool,
136 private_soup_fetcher_t *this, fetcher_option_t option, ...)
137 {
138 bool supported = TRUE;
139 va_list args;
140
141 va_start(args, option);
142 switch (option)
143 {
144 case FETCH_REQUEST_DATA:
145 this->method = SOUP_METHOD_POST;
146 this->data = va_arg(args, chunk_t);
147 break;
148 case FETCH_REQUEST_TYPE:
149 this->type = va_arg(args, char*);
150 break;
151 case FETCH_HTTP_VERSION_1_0:
152 this->version = SOUP_HTTP_1_0;
153 break;
154 case FETCH_TIMEOUT:
155 this->timeout = va_arg(args, u_int);
156 break;
157 case FETCH_CALLBACK:
158 this->cb = va_arg(args, fetcher_callback_t);
159 break;
160 default:
161 supported = FALSE;
162 break;
163 }
164 va_end(args);
165 return supported;
166 }
167
168 METHOD(fetcher_t, destroy, void,
169 private_soup_fetcher_t *this)
170 {
171 free(this);
172 }
173
174 /*
175 * Described in header.
176 */
177 soup_fetcher_t *soup_fetcher_create()
178 {
179 private_soup_fetcher_t *this;
180
181 INIT(this,
182 .public = {
183 .interface = {
184 .fetch = _fetch,
185 .set_option = _set_option,
186 .destroy = _destroy,
187 },
188 },
189 .method = SOUP_METHOD_GET,
190 .version = SOUP_HTTP_1_1,
191 .timeout = DEFAULT_TIMEOUT,
192 .cb = fetcher_default_callback,
193 );
194
195 return &this->public;
196 }