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 if (session->flags & SESSION_FLAG_USE_TLS)
340 session->transport_channel = g_io_channel_gnutls_new(sk);
342 session->transport_channel = g_io_channel_unix_new(sk);
344 if (session->transport_channel == NULL) {
349 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
350 g_io_channel_set_buffered(session->transport_channel, FALSE);
352 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
354 session->transport_watch = g_io_add_watch(session->transport_channel,
355 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
356 received_data, session);
361 static int create_transport(struct web_session *session)
365 err = connect_session_transport(session);
369 debug(session->web, "creating session %s:%u",
370 session->address, session->port);
375 static void start_request(struct web_session *session)
379 gsize count, bytes_written;
382 debug(session->web, "request %s from %s",
383 session->request, session->host);
385 buf = g_string_new(NULL);
386 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
387 g_string_append_printf(buf, "Host: %s\r\n", session->host);
388 if (session->web->user_agent != NULL)
389 g_string_append_printf(buf, "User-Agent: %s\r\n",
390 session->web->user_agent);
391 if (session->web->accept_option != NULL)
392 g_string_append_printf(buf, "Accept: %s\r\n",
393 session->web->accept_option);
394 if (session->web->close_connection == TRUE)
395 g_string_append(buf, "Connection: close\r\n");
396 g_string_append(buf, "\r\n");
397 str = g_string_free(buf, FALSE);
401 debug(session->web, "bytes to write %zu", count);
403 status = g_io_channel_write_chars(session->transport_channel,
404 str, count, &bytes_written, NULL);
406 debug(session->web, "status %u bytes written %zu",
407 status, bytes_written);
414 static int parse_url(struct web_session *session, const char *url)
416 char *scheme, *host, *port, *path;
418 scheme = g_strdup(url);
422 host = strstr(scheme, "://");
427 if (strcasecmp(scheme, "https") == 0) {
429 session->flags |= SESSION_FLAG_USE_TLS;
430 } else if (strcasecmp(scheme, "http") == 0) {
441 path = strchr(host, '/');
445 session->request = g_strdup_printf("/%s", path ? path : "");
447 port = strrchr(host, ':');
450 int tmp = strtol(port + 1, &end, 10);
458 session->host = g_strdup(host);
465 static void resolv_result(GResolvResultStatus status,
466 char **results, gpointer user_data)
468 struct web_session *session = user_data;
470 if (results == NULL || results[0] == NULL) {
471 if (session->result_func != NULL)
472 session->result_func(404, NULL, session->result_data);
476 debug(session->web, "address %s", results[0]);
478 if (inet_aton(results[0], NULL) == 0) {
479 if (session->result_func != NULL)
480 session->result_func(400, NULL, session->result_data);
484 session->address = g_strdup(results[0]);
486 if (create_transport(session) < 0) {
487 if (session->result_func != NULL)
488 session->result_func(409, NULL, session->result_data);
492 start_request(session);
495 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
496 GWebResultFunc func, gpointer user_data)
498 struct web_session *session;
500 if (web == NULL || url == NULL)
503 debug(web, "request %s", url);
505 session = g_try_new0(struct web_session, 1);
509 if (parse_url(session, url) < 0) {
510 free_session(session);
514 debug(web, "host %s:%u", session->host, session->port);
515 debug(web, "flags %lu", session->flags);
519 session->result_func = func;
520 session->result_data = user_data;
522 if (inet_aton(session->host, NULL) == 0) {
523 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
524 session->host, resolv_result, session);
525 if (session->resolv_action == 0) {
526 free_session(session);
530 session->address = g_strdup(session->host);
532 if (create_transport(session) < 0) {
533 free_session(session);
537 start_request(session);
540 web->session_list = g_list_append(web->session_list, session);
542 return web->next_query_id++;