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;
76 GWebDebugFunc debug_func;
80 static inline void debug(GWeb *web, const char *format, ...)
85 if (web->debug_func == NULL)
90 if (vsnprintf(str, sizeof(str), format, ap) > 0)
91 web->debug_func(str, web->debug_data);
96 static void free_session(struct web_session *session)
98 GWeb *web = session->web;
103 g_free(session->request);
105 if (session->resolv_action > 0)
106 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
108 if (session->transport_watch > 0)
109 g_source_remove(session->transport_watch);
111 if (session->transport_channel != NULL)
112 g_io_channel_unref(session->transport_channel);
114 g_free(session->host);
115 g_free(session->address);
119 static void flush_sessions(GWeb *web)
123 for (list = g_list_first(web->session_list);
124 list; list = g_list_next(list))
125 free_session(list->data);
127 g_list_free(web->session_list);
128 web->session_list = NULL;
131 GWeb *g_web_new(int index)
138 web = g_try_new0(GWeb, 1);
144 web->next_query_id = 1;
147 web->session_list = NULL;
149 web->resolv = g_resolv_new(index);
150 if (web->resolv == NULL) {
155 web->accept_option = g_strdup("*/*");
156 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
161 GWeb *g_web_ref(GWeb *web)
166 g_atomic_int_inc(&web->ref_count);
171 void g_web_unref(GWeb *web)
176 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
181 g_resolv_unref(web->resolv);
183 g_free(web->accept_option);
184 g_free(web->user_agent);
188 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
193 web->debug_func = func;
194 web->debug_data = user_data;
196 g_resolv_set_debug(web->resolv, func, user_data);
199 gboolean g_web_add_nameserver(GWeb *web, const char *address)
204 g_resolv_add_nameserver(web->resolv, address, 53, 0);
209 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
211 g_free(web->accept_option);
213 if (format == NULL) {
214 web->accept_option = NULL;
215 debug(web, "clearing accept option");
217 web->accept_option = g_strdup_vprintf(format, args);
218 debug(web, "setting accept %s", web->accept_option);
224 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
232 va_start(args, format);
233 result = set_accept_option(web, format, args);
239 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
241 g_free(web->user_agent);
243 if (format == NULL) {
244 web->user_agent = NULL;
245 debug(web, "clearing user agent");
247 web->user_agent = g_strdup_vprintf(format, args);
248 debug(web, "setting user agent %s", web->user_agent);
254 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
262 va_start(args, format);
263 result = set_user_agent(web, format, args);
269 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
272 struct web_session *session = user_data;
277 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
278 session->transport_watch = 0;
279 if (session->result_func != NULL)
280 session->result_func(400, NULL, session->result_data);
284 memset(buf, 0, sizeof(buf));
285 status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
288 debug(session->web, "status %u bytes read %zu", status, bytes_read);
290 if (status != G_IO_STATUS_NORMAL) {
291 session->transport_watch = 0;
292 if (session->result_func != NULL)
293 session->result_func(200, NULL, session->result_data);
302 static int connect_session_transport(struct web_session *session)
304 struct sockaddr_in sin;
307 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
311 memset(&sin, 0, sizeof(sin));
312 sin.sin_family = AF_INET;
313 sin.sin_port = htons(session->port);
314 sin.sin_addr.s_addr = inet_addr(session->address);
316 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
321 debug(session->web, "flags %lu", session->flags);
323 if (session->flags & SESSION_FLAG_USE_TLS)
324 session->transport_channel = g_io_channel_gnutls_new(sk);
326 session->transport_channel = g_io_channel_unix_new(sk);
328 if (session->transport_channel == NULL) {
333 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
334 g_io_channel_set_buffered(session->transport_channel, FALSE);
336 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
338 session->transport_watch = g_io_add_watch(session->transport_channel,
339 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
340 received_data, session);
345 static int create_transport(struct web_session *session)
349 err = connect_session_transport(session);
353 debug(session->web, "creating session %s:%u",
354 session->address, session->port);
359 static void start_request(struct web_session *session)
363 gsize count, bytes_written;
366 debug(session->web, "request %s from %s",
367 session->request, session->host);
369 buf = g_string_new(NULL);
370 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
371 g_string_append_printf(buf, "Host: %s\r\n", session->host);
372 if (session->web->user_agent != NULL)
373 g_string_append_printf(buf, "User-Agent: %s\r\n",
374 session->web->user_agent);
375 if (session->web->accept_option != NULL)
376 g_string_append_printf(buf, "Accept: %s\r\n",
377 session->web->accept_option);
378 g_string_append(buf, "Connection: close\r\n");
379 g_string_append(buf, "\r\n");
380 str = g_string_free(buf, FALSE);
384 debug(session->web, "bytes to write %zu", count);
386 status = g_io_channel_write_chars(session->transport_channel,
387 str, count, &bytes_written, NULL);
389 debug(session->web, "status %u bytes written %zu",
390 status, bytes_written);
397 static int parse_url(struct web_session *session, const char *url)
399 char *scheme, *host, *port, *path;
401 scheme = g_strdup(url);
405 host = strstr(scheme, "://");
410 if (strcasecmp(scheme, "https") == 0) {
412 session->flags |= SESSION_FLAG_USE_TLS;
413 } else if (strcasecmp(scheme, "http") == 0) {
424 path = strchr(host, '/');
428 session->request = g_strdup_printf("/%s", path ? path : "");
430 port = strrchr(host, ':');
433 int tmp = strtol(port + 1, &end, 10);
441 session->host = g_strdup(host);
448 static void resolv_result(GResolvResultStatus status,
449 char **results, gpointer user_data)
451 struct web_session *session = user_data;
453 if (results == NULL || results[0] == NULL) {
454 if (session->result_func != NULL)
455 session->result_func(404, NULL, session->result_data);
459 debug(session->web, "address %s", results[0]);
461 if (inet_aton(results[0], NULL) == 0) {
462 if (session->result_func != NULL)
463 session->result_func(400, NULL, session->result_data);
467 session->address = g_strdup(results[0]);
469 if (create_transport(session) < 0) {
470 if (session->result_func != NULL)
471 session->result_func(409, NULL, session->result_data);
475 start_request(session);
478 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
479 GWebResultFunc func, gpointer user_data)
481 struct web_session *session;
483 if (web == NULL || url == NULL)
486 debug(web, "request %s", url);
488 session = g_try_new0(struct web_session, 1);
492 if (parse_url(session, url) < 0) {
493 free_session(session);
497 debug(web, "host %s:%u", session->host, session->port);
498 debug(web, "flags %lu", session->flags);
502 session->result_func = func;
503 session->result_data = user_data;
505 if (inet_aton(session->host, NULL) == 0) {
506 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
507 session->host, resolv_result, session);
508 if (session->resolv_action == 0) {
509 free_session(session);
513 session->address = g_strdup(session->host);
515 if (create_transport(session) < 0) {
516 free_session(session);
520 start_request(session);
523 web->session_list = g_list_append(web->session_list, session);
525 return web->next_query_id++;