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