Added a new FETCH_CALLBACK option to fetch data without allocation
[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 <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 METHOD(fetcher_t, fetch, status_t,
64 private_soup_fetcher_t *this, char *uri, void *userdata)
65 {
66 SoupSession *session;
67 SoupMessage *message;
68 status_t status = FAILED;
69 chunk_t *result = userdata;
70
71 message = soup_message_new(this->method, uri);
72 if (!message)
73 {
74 return NOT_SUPPORTED;
75 }
76 if (this->type)
77 {
78 soup_message_set_request(message, this->type, SOUP_MEMORY_STATIC,
79 this->data.ptr, this->data.len);
80 }
81 soup_message_set_http_version(message, this->version);
82 session = soup_session_sync_new();
83 g_object_set(G_OBJECT(session),
84 SOUP_SESSION_TIMEOUT, (guint)this->timeout, NULL);
85
86 DBG2(DBG_LIB, "sending http request to '%s'...", uri);
87 soup_session_send_message(session, message);
88 if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
89 {
90 *result = chunk_clone(chunk_create((u_char*)message->response_body->data,
91 message->response_body->length));
92 status = SUCCESS;
93 }
94 else
95 {
96 DBG1(DBG_LIB, "HTTP request failed, code %d", message->status_code);
97 }
98 g_object_unref(G_OBJECT(message));
99 g_object_unref(G_OBJECT(session));
100 return status;
101 }
102
103 METHOD(fetcher_t, set_option, bool,
104 private_soup_fetcher_t *this, fetcher_option_t option, ...)
105 {
106 bool supported = TRUE;
107 va_list args;
108
109 va_start(args, option);
110 switch (option)
111 {
112 case FETCH_REQUEST_DATA:
113 this->method = SOUP_METHOD_POST;
114 this->data = va_arg(args, chunk_t);
115 break;
116 case FETCH_REQUEST_TYPE:
117 this->type = va_arg(args, char*);
118 break;
119 case FETCH_HTTP_VERSION_1_0:
120 this->version = SOUP_HTTP_1_0;
121 break;
122 case FETCH_TIMEOUT:
123 this->timeout = va_arg(args, u_int);
124 break;
125 default:
126 supported = FALSE;
127 break;
128 }
129 va_end(args);
130 return supported;
131 }
132
133 METHOD(fetcher_t, destroy, void,
134 private_soup_fetcher_t *this)
135 {
136 free(this);
137 }
138
139 /*
140 * Described in header.
141 */
142 soup_fetcher_t *soup_fetcher_create()
143 {
144 private_soup_fetcher_t *this;
145
146 INIT(this,
147 .public = {
148 .interface = {
149 .fetch = _fetch,
150 .set_option = _set_option,
151 .destroy = _destroy,
152 },
153 },
154 .method = SOUP_METHOD_GET,
155 .version = SOUP_HTTP_1_1,
156 .timeout = DEFAULT_TIMEOUT,
157 );
158
159 return &this->public;
160 }