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 DEFAULT_BUFFER_SIZE 2048
41 #define SESSION_FLAG_USE_TLS (1 << 0)
57 GIOChannel *transport_channel;
58 guint transport_watch;
63 guint8 *receive_buffer;
65 GString *current_header;
70 GWebResultFunc result_func;
85 gboolean close_connection;
87 GWebDebugFunc debug_func;
91 static inline void debug(GWeb *web, const char *format, ...)
96 if (web->debug_func == NULL)
101 if (vsnprintf(str, sizeof(str), format, ap) > 0)
102 web->debug_func(str, web->debug_data);
107 static void free_session(struct web_session *session)
109 GWeb *web = session->web;
114 g_free(session->request);
116 if (session->resolv_action > 0)
117 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
119 if (session->transport_watch > 0)
120 g_source_remove(session->transport_watch);
122 if (session->transport_channel != NULL)
123 g_io_channel_unref(session->transport_channel);
125 g_string_free(session->current_header, TRUE);
126 g_free(session->receive_buffer);
128 g_free(session->host);
129 g_free(session->address);
133 static void flush_sessions(GWeb *web)
137 for (list = g_list_first(web->session_list);
138 list; list = g_list_next(list))
139 free_session(list->data);
141 g_list_free(web->session_list);
142 web->session_list = NULL;
145 GWeb *g_web_new(int index)
152 web = g_try_new0(GWeb, 1);
158 web->next_query_id = 1;
161 web->session_list = NULL;
163 web->resolv = g_resolv_new(index);
164 if (web->resolv == NULL) {
169 web->accept_option = g_strdup("*/*");
170 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
171 web->close_connection = FALSE;
176 GWeb *g_web_ref(GWeb *web)
181 g_atomic_int_inc(&web->ref_count);
186 void g_web_unref(GWeb *web)
191 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
196 g_resolv_unref(web->resolv);
198 g_free(web->accept_option);
199 g_free(web->user_agent);
203 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
208 web->debug_func = func;
209 web->debug_data = user_data;
211 g_resolv_set_debug(web->resolv, func, user_data);
214 gboolean g_web_add_nameserver(GWeb *web, const char *address)
219 g_resolv_add_nameserver(web->resolv, address, 53, 0);
224 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
226 g_free(web->accept_option);
228 if (format == NULL) {
229 web->accept_option = NULL;
230 debug(web, "clearing accept option");
232 web->accept_option = g_strdup_vprintf(format, args);
233 debug(web, "setting accept %s", web->accept_option);
239 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
247 va_start(args, format);
248 result = set_accept_option(web, format, args);
254 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
256 g_free(web->user_agent);
258 if (format == NULL) {
259 web->user_agent = NULL;
260 debug(web, "clearing user agent");
262 web->user_agent = g_strdup_vprintf(format, args);
263 debug(web, "setting user agent %s", web->user_agent);
269 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
277 va_start(args, format);
278 result = set_user_agent(web, format, args);
284 void g_web_set_close_connection(GWeb *web, gboolean enabled)
289 web->close_connection = enabled;
292 gboolean g_web_get_close_connection(GWeb *web)
297 return web->close_connection;
300 static inline void call_result_func(struct web_session *session, guint status)
302 if (session->result_func == NULL)
305 session->result_func(status, &session->result, session->result_data);
308 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
311 struct web_session *session = user_data;
312 guint8 *ptr = session->receive_buffer;
316 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
317 session->transport_watch = 0;
318 session->result.buffer = NULL;
319 session->result.length = 0;
320 call_result_func(session, 400);
324 status = g_io_channel_read_chars(channel,
325 (gchar *) session->receive_buffer,
326 session->receive_space - 1, &bytes_read, NULL);
328 debug(session->web, "bytes read %zu", bytes_read);
330 if (status != G_IO_STATUS_NORMAL) {
331 session->transport_watch = 0;
332 session->result.buffer = NULL;
333 session->result.length = 0;
334 call_result_func(session, 200);
338 session->receive_buffer[bytes_read] = '\0';
340 if (session->header_done == TRUE) {
341 session->result.buffer = session->receive_buffer;
342 session->result.length = bytes_read;
343 call_result_func(session, 100);
347 while (bytes_read > 0) {
352 pos = memchr(ptr, '\n', bytes_read);
354 g_string_append_len(session->current_header,
355 (gchar *) ptr, bytes_read);
360 count = strlen((char *) ptr);
361 if (count > 0 && ptr[count - 1] == '\r') {
366 g_string_append_len(session->current_header,
367 (gchar *) ptr, count);
369 bytes_read -= count + 1;
372 if (session->current_header->len == 0) {
373 session->header_done = TRUE;
374 session->result.buffer = pos + 1;
375 session->result.length = bytes_read;
376 call_result_func(session, 100);
380 str = session->current_header->str;
382 if (session->result.status == 0) {
385 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
386 session->result.status = code;
389 debug(session->web, "[header] %s", str);
391 g_string_truncate(session->current_header, 0);
397 static int connect_session_transport(struct web_session *session)
399 struct sockaddr_in sin;
402 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
406 memset(&sin, 0, sizeof(sin));
407 sin.sin_family = AF_INET;
408 sin.sin_port = htons(session->port);
409 sin.sin_addr.s_addr = inet_addr(session->address);
411 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
416 if (session->flags & SESSION_FLAG_USE_TLS)
417 session->transport_channel = g_io_channel_gnutls_new(sk);
419 session->transport_channel = g_io_channel_unix_new(sk);
421 if (session->transport_channel == NULL) {
426 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
427 g_io_channel_set_buffered(session->transport_channel, FALSE);
429 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
431 session->transport_watch = g_io_add_watch(session->transport_channel,
432 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
433 received_data, session);
438 static int create_transport(struct web_session *session)
442 err = connect_session_transport(session);
446 debug(session->web, "creating session %s:%u",
447 session->address, session->port);
452 static void start_request(struct web_session *session)
456 gsize count, bytes_written;
459 debug(session->web, "request %s from %s",
460 session->request, session->host);
462 buf = g_string_new(NULL);
463 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
464 g_string_append_printf(buf, "Host: %s\r\n", session->host);
465 if (session->web->user_agent != NULL)
466 g_string_append_printf(buf, "User-Agent: %s\r\n",
467 session->web->user_agent);
468 if (session->web->accept_option != NULL)
469 g_string_append_printf(buf, "Accept: %s\r\n",
470 session->web->accept_option);
471 if (session->web->close_connection == TRUE)
472 g_string_append(buf, "Connection: close\r\n");
473 g_string_append(buf, "\r\n");
474 str = g_string_free(buf, FALSE);
478 debug(session->web, "bytes to write %zu", count);
480 status = g_io_channel_write_chars(session->transport_channel,
481 str, count, &bytes_written, NULL);
483 debug(session->web, "status %u bytes written %zu",
484 status, bytes_written);
491 static int parse_url(struct web_session *session, const char *url)
493 char *scheme, *host, *port, *path;
495 scheme = g_strdup(url);
499 host = strstr(scheme, "://");
504 if (strcasecmp(scheme, "https") == 0) {
506 session->flags |= SESSION_FLAG_USE_TLS;
507 } else if (strcasecmp(scheme, "http") == 0) {
518 path = strchr(host, '/');
522 session->request = g_strdup_printf("/%s", path ? path : "");
524 port = strrchr(host, ':');
527 int tmp = strtol(port + 1, &end, 10);
535 session->host = g_strdup(host);
542 static void resolv_result(GResolvResultStatus status,
543 char **results, gpointer user_data)
545 struct web_session *session = user_data;
547 if (results == NULL || results[0] == NULL) {
548 call_result_func(session, 404);
552 debug(session->web, "address %s", results[0]);
554 if (inet_aton(results[0], NULL) == 0) {
555 call_result_func(session, 400);
559 session->address = g_strdup(results[0]);
561 if (create_transport(session) < 0) {
562 call_result_func(session, 409);
566 start_request(session);
569 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
570 GWebResultFunc func, gpointer user_data)
572 struct web_session *session;
574 if (web == NULL || url == NULL)
577 debug(web, "request %s", url);
579 session = g_try_new0(struct web_session, 1);
583 if (parse_url(session, url) < 0) {
584 free_session(session);
588 debug(web, "host %s:%u", session->host, session->port);
589 debug(web, "flags %lu", session->flags);
593 session->result_func = func;
594 session->result_data = user_data;
596 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
597 if (session->receive_buffer == NULL) {
598 free_session(session);
602 session->receive_space = DEFAULT_BUFFER_SIZE;
603 session->current_header = g_string_sized_new(0);
604 session->header_done = FALSE;
606 if (inet_aton(session->host, NULL) == 0) {
607 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
608 session->host, resolv_result, session);
609 if (session->resolv_action == 0) {
610 free_session(session);
614 session->address = g_strdup(session->host);
616 if (create_transport(session) < 0) {
617 free_session(session);
621 start_request(session);
624 web->session_list = g_list_append(web->session_list, session);
626 return web->next_query_id++;
629 guint16 g_web_result_get_status(GWebResult *result)
634 return result->status;
637 gboolean g_web_result_get_chunk(GWebResult *result,
638 const guint8 **chunk, gsize *length)
646 *chunk = result->buffer;
649 *length = result->length;