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;
75 gboolean close_connection;
77 GWebDebugFunc debug_func;
81 static inline void debug(GWeb *web, const char *format, ...)
86 if (web->debug_func == NULL)
91 if (vsnprintf(str, sizeof(str), format, ap) > 0)
92 web->debug_func(str, web->debug_data);
97 static void free_session(struct web_session *session)
99 GWeb *web = session->web;
104 g_free(session->request);
106 if (session->resolv_action > 0)
107 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
109 if (session->transport_watch > 0)
110 g_source_remove(session->transport_watch);
112 if (session->transport_channel != NULL)
113 g_io_channel_unref(session->transport_channel);
115 g_free(session->host);
116 g_free(session->address);
120 static void flush_sessions(GWeb *web)
124 for (list = g_list_first(web->session_list);
125 list; list = g_list_next(list))
126 free_session(list->data);
128 g_list_free(web->session_list);
129 web->session_list = NULL;
132 GWeb *g_web_new(int index)
139 web = g_try_new0(GWeb, 1);
145 web->next_query_id = 1;
148 web->session_list = NULL;
150 web->resolv = g_resolv_new(index);
151 if (web->resolv == NULL) {
156 web->accept_option = g_strdup("*/*");
157 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
158 web->close_connection = FALSE;
163 GWeb *g_web_ref(GWeb *web)
168 g_atomic_int_inc(&web->ref_count);
173 void g_web_unref(GWeb *web)
178 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
183 g_resolv_unref(web->resolv);
185 g_free(web->accept_option);
186 g_free(web->user_agent);
190 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
195 web->debug_func = func;
196 web->debug_data = user_data;
198 g_resolv_set_debug(web->resolv, func, user_data);
201 gboolean g_web_add_nameserver(GWeb *web, const char *address)
206 g_resolv_add_nameserver(web->resolv, address, 53, 0);
211 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
213 g_free(web->accept_option);
215 if (format == NULL) {
216 web->accept_option = NULL;
217 debug(web, "clearing accept option");
219 web->accept_option = g_strdup_vprintf(format, args);
220 debug(web, "setting accept %s", web->accept_option);
226 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
234 va_start(args, format);
235 result = set_accept_option(web, format, args);
241 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
243 g_free(web->user_agent);
245 if (format == NULL) {
246 web->user_agent = NULL;
247 debug(web, "clearing user agent");
249 web->user_agent = g_strdup_vprintf(format, args);
250 debug(web, "setting user agent %s", web->user_agent);
256 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
264 va_start(args, format);
265 result = set_user_agent(web, format, args);
271 void g_web_set_close_connection(GWeb *web, gboolean enabled)
276 web->close_connection = enabled;
279 gboolean g_web_get_close_connection(GWeb *web)
284 return web->close_connection;
287 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
290 struct web_session *session = user_data;
295 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
296 session->transport_watch = 0;
297 if (session->result_func != NULL)
298 session->result_func(400, NULL, session->result_data);
302 memset(buf, 0, sizeof(buf));
303 status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
306 debug(session->web, "status %u bytes read %zu", status, bytes_read);
308 if (status != G_IO_STATUS_NORMAL) {
309 session->transport_watch = 0;
310 if (session->result_func != NULL)
311 session->result_func(200, NULL, session->result_data);
320 static int connect_session_transport(struct web_session *session)
322 struct sockaddr_in sin;
325 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
329 memset(&sin, 0, sizeof(sin));
330 sin.sin_family = AF_INET;
331 sin.sin_port = htons(session->port);
332 sin.sin_addr.s_addr = inet_addr(session->address);
334 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
339 debug(session->web, "flags %lu", session->flags);
341 if (session->flags & SESSION_FLAG_USE_TLS)
342 session->transport_channel = g_io_channel_gnutls_new(sk);
344 session->transport_channel = g_io_channel_unix_new(sk);
346 if (session->transport_channel == NULL) {
351 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
352 g_io_channel_set_buffered(session->transport_channel, FALSE);
354 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
356 session->transport_watch = g_io_add_watch(session->transport_channel,
357 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
358 received_data, session);
363 static int create_transport(struct web_session *session)
367 err = connect_session_transport(session);
371 debug(session->web, "creating session %s:%u",
372 session->address, session->port);
377 static void start_request(struct web_session *session)
381 gsize count, bytes_written;
384 debug(session->web, "request %s from %s",
385 session->request, session->host);
387 buf = g_string_new(NULL);
388 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
389 g_string_append_printf(buf, "Host: %s\r\n", session->host);
390 if (session->web->user_agent != NULL)
391 g_string_append_printf(buf, "User-Agent: %s\r\n",
392 session->web->user_agent);
393 if (session->web->accept_option != NULL)
394 g_string_append_printf(buf, "Accept: %s\r\n",
395 session->web->accept_option);
396 if (session->web->close_connection == TRUE)
397 g_string_append(buf, "Connection: close\r\n");
398 g_string_append(buf, "\r\n");
399 str = g_string_free(buf, FALSE);
403 debug(session->web, "bytes to write %zu", count);
405 status = g_io_channel_write_chars(session->transport_channel,
406 str, count, &bytes_written, NULL);
408 debug(session->web, "status %u bytes written %zu",
409 status, bytes_written);
416 static int parse_url(struct web_session *session, const char *url)
418 char *scheme, *host, *port, *path;
420 scheme = g_strdup(url);
424 host = strstr(scheme, "://");
429 if (strcasecmp(scheme, "https") == 0) {
431 session->flags |= SESSION_FLAG_USE_TLS;
432 } else if (strcasecmp(scheme, "http") == 0) {
443 path = strchr(host, '/');
447 session->request = g_strdup_printf("/%s", path ? path : "");
449 port = strrchr(host, ':');
452 int tmp = strtol(port + 1, &end, 10);
460 session->host = g_strdup(host);
467 static void resolv_result(GResolvResultStatus status,
468 char **results, gpointer user_data)
470 struct web_session *session = user_data;
472 if (results == NULL || results[0] == NULL) {
473 if (session->result_func != NULL)
474 session->result_func(404, NULL, session->result_data);
478 debug(session->web, "address %s", results[0]);
480 if (inet_aton(results[0], NULL) == 0) {
481 if (session->result_func != NULL)
482 session->result_func(400, NULL, session->result_data);
486 session->address = g_strdup(results[0]);
488 if (create_transport(session) < 0) {
489 if (session->result_func != NULL)
490 session->result_func(409, NULL, session->result_data);
494 start_request(session);
497 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
498 GWebResultFunc func, gpointer user_data)
500 struct web_session *session;
502 if (web == NULL || url == NULL)
505 debug(web, "request %s", url);
507 session = g_try_new0(struct web_session, 1);
511 if (parse_url(session, url) < 0) {
512 free_session(session);
516 debug(web, "host %s:%u", session->host, session->port);
517 debug(web, "flags %lu", session->flags);
521 session->result_func = func;
522 session->result_data = user_data;
524 if (inet_aton(session->host, NULL) == 0) {
525 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
526 session->host, resolv_result, session);
527 if (session->resolv_action == 0) {
528 free_session(session);
532 session->address = g_strdup(session->host);
534 if (create_transport(session) < 0) {
535 free_session(session);
539 start_request(session);
542 web->session_list = g_list_append(web->session_list, session);
544 return web->next_query_id++;