3 * Web service library with GLib integration
5 * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #include "giognutls.h"
39 #define SESSION_FLAG_USE_TLS (1 << 0)
52 GIOChannel *transport_channel;
53 guint transport_watch;
60 GWebResultFunc result_func;
74 GWebDebugFunc debug_func;
78 static inline void debug(GWeb *web, const char *format, ...)
83 if (web->debug_func == NULL)
88 if (vsnprintf(str, sizeof(str), format, ap) > 0)
89 web->debug_func(str, web->debug_data);
94 static void free_session(struct web_session *session)
96 GWeb *web = session->web;
101 g_free(session->request);
103 if (session->resolv_action > 0)
104 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
106 if (session->transport_watch > 0)
107 g_source_remove(session->transport_watch);
109 if (session->transport_channel != NULL)
110 g_io_channel_unref(session->transport_channel);
112 g_free(session->host);
113 g_free(session->address);
117 static void flush_sessions(GWeb *web)
121 for (list = g_list_first(web->session_list);
122 list; list = g_list_next(list))
123 free_session(list->data);
125 g_list_free(web->session_list);
126 web->session_list = NULL;
129 GWeb *g_web_new(int index)
136 web = g_try_new0(GWeb, 1);
142 web->next_query_id = 1;
145 web->session_list = NULL;
147 web->resolv = g_resolv_new(index);
148 if (web->resolv == NULL) {
156 GWeb *g_web_ref(GWeb *web)
161 g_atomic_int_inc(&web->ref_count);
166 void g_web_unref(GWeb *web)
171 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
176 g_resolv_unref(web->resolv);
181 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
186 web->debug_func = func;
187 web->debug_data = user_data;
189 g_resolv_set_debug(web->resolv, func, user_data);
192 gboolean g_web_add_nameserver(GWeb *web, const char *address)
197 g_resolv_add_nameserver(web->resolv, address, 53, 0);
202 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
205 struct web_session *session = user_data;
210 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
211 session->transport_watch = 0;
212 if (session->result_func != NULL)
213 session->result_func(400, NULL, session->result_data);
217 memset(buf, 0, sizeof(buf));
218 status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
221 debug(session->web, "status %u bytes read %zu", status, bytes_read);
223 if (status != G_IO_STATUS_NORMAL) {
224 session->transport_watch = 0;
225 if (session->result_func != NULL)
226 session->result_func(200, NULL, session->result_data);
235 static int connect_session_transport(struct web_session *session)
237 struct sockaddr_in sin;
240 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
244 memset(&sin, 0, sizeof(sin));
245 sin.sin_family = AF_INET;
246 sin.sin_port = htons(session->port);
247 sin.sin_addr.s_addr = inet_addr(session->address);
249 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
254 debug(session->web, "flags %lu", session->flags);
256 if (session->flags & SESSION_FLAG_USE_TLS)
257 session->transport_channel = g_io_channel_gnutls_new(sk);
259 session->transport_channel = g_io_channel_unix_new(sk);
261 if (session->transport_channel == NULL) {
266 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
268 session->transport_watch = g_io_add_watch(session->transport_channel,
269 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
270 received_data, session);
275 static void start_request(struct web_session *session)
282 debug(session->web, "request %s from %s",
283 session->request, session->host);
285 buf = g_string_new(NULL);
286 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
287 g_string_append_printf(buf, "Host: %s\r\n", session->host);
288 g_string_append_printf(buf, "User-Agent: ConnMan/%s\r\n", VERSION);
289 g_string_append(buf, "Accept: */*\r\n");
290 g_string_append(buf, "\r\n");
291 str = g_string_free(buf, FALSE);
293 status = g_io_channel_write_chars(session->transport_channel,
294 str, strlen(str), &bytes_written, NULL);
296 debug(session->web, "status %u bytes written %zu",
297 status, bytes_written);
304 static int parse_url(struct web_session *session, const char *url)
306 char *scheme, *host, *port, *path;
308 scheme = g_strdup(url);
312 host = strstr(scheme, "://");
317 if (strcasecmp(scheme, "https") == 0) {
319 session->flags |= SESSION_FLAG_USE_TLS;
320 } else if (strcasecmp(scheme, "http") == 0) {
331 path = strchr(host, '/');
335 session->request = g_strdup_printf("/%s", path ? path : "");
337 port = strrchr(host, ':');
340 int tmp = strtol(port + 1, &end, 10);
348 session->host = g_strdup(host);
355 static void resolv_result(GResolvResultStatus status,
356 char **results, gpointer user_data)
358 struct web_session *session = user_data;
360 if (results == NULL || results[0] == NULL) {
361 if (session->result_func != NULL)
362 session->result_func(404, NULL, session->result_data);
366 debug(session->web, "address %s", results[0]);
368 if (inet_aton(results[0], NULL) == 0) {
369 if (session->result_func != NULL)
370 session->result_func(400, NULL, session->result_data);
374 session->address = g_strdup(results[0]);
376 if (connect_session_transport(session) < 0) {
377 if (session->result_func != NULL)
378 session->result_func(409, NULL, session->result_data);
382 debug(session->web, "creating session %s:%u",
383 session->address, session->port);
385 start_request(session);
388 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
389 GWebResultFunc func, gpointer user_data)
391 struct web_session *session;
393 if (web == NULL || url == NULL)
396 debug(web, "request %s", url);
398 session = g_try_new0(struct web_session, 1);
402 if (parse_url(session, url) < 0) {
403 free_session(session);
407 debug(web, "host %s:%u", session->host, session->port);
408 debug(web, "flags %lu", session->flags);
412 session->result_func = func;
413 session->result_data = user_data;
415 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
416 session->host, resolv_result, session);
417 if (session->resolv_action == 0) {
418 free_session(session);
422 web->session_list = g_list_append(web->session_list, session);
424 return web->next_query_id++;