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, guint16 status)
302 if (session->result_func == NULL)
306 session->result.status = status;
308 session->result_func(&session->result, session->result_data);
311 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
314 struct web_session *session = user_data;
315 guint8 *ptr = session->receive_buffer;
319 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
320 session->transport_watch = 0;
321 session->result.buffer = NULL;
322 session->result.length = 0;
323 call_result_func(session, 400);
327 status = g_io_channel_read_chars(channel,
328 (gchar *) session->receive_buffer,
329 session->receive_space - 1, &bytes_read, NULL);
331 debug(session->web, "bytes read %zu", bytes_read);
333 if (status != G_IO_STATUS_NORMAL) {
334 session->transport_watch = 0;
335 session->result.buffer = NULL;
336 session->result.length = 0;
337 call_result_func(session, 0);
341 session->receive_buffer[bytes_read] = '\0';
343 if (session->header_done == TRUE) {
344 session->result.buffer = session->receive_buffer;
345 session->result.length = bytes_read;
346 call_result_func(session, 0);
350 while (bytes_read > 0) {
355 pos = memchr(ptr, '\n', bytes_read);
357 g_string_append_len(session->current_header,
358 (gchar *) ptr, bytes_read);
363 count = strlen((char *) ptr);
364 if (count > 0 && ptr[count - 1] == '\r') {
369 g_string_append_len(session->current_header,
370 (gchar *) ptr, count);
372 bytes_read -= count + 1;
375 if (session->current_header->len == 0) {
376 session->header_done = TRUE;
377 session->result.buffer = pos + 1;
378 session->result.length = bytes_read;
379 call_result_func(session, 0);
383 str = session->current_header->str;
385 if (session->result.status == 0) {
388 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
389 session->result.status = code;
392 debug(session->web, "[header] %s", str);
394 g_string_truncate(session->current_header, 0);
400 static int connect_session_transport(struct web_session *session)
402 struct sockaddr_in sin;
405 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
409 memset(&sin, 0, sizeof(sin));
410 sin.sin_family = AF_INET;
411 sin.sin_port = htons(session->port);
412 sin.sin_addr.s_addr = inet_addr(session->address);
414 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
419 if (session->flags & SESSION_FLAG_USE_TLS)
420 session->transport_channel = g_io_channel_gnutls_new(sk);
422 session->transport_channel = g_io_channel_unix_new(sk);
424 if (session->transport_channel == NULL) {
429 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
430 g_io_channel_set_buffered(session->transport_channel, FALSE);
432 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
434 session->transport_watch = g_io_add_watch(session->transport_channel,
435 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
436 received_data, session);
441 static int create_transport(struct web_session *session)
445 err = connect_session_transport(session);
449 debug(session->web, "creating session %s:%u",
450 session->address, session->port);
455 static void start_request(struct web_session *session)
459 gsize count, bytes_written;
462 debug(session->web, "request %s from %s",
463 session->request, session->host);
465 buf = g_string_new(NULL);
466 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
467 g_string_append_printf(buf, "Host: %s\r\n", session->host);
468 if (session->web->user_agent != NULL)
469 g_string_append_printf(buf, "User-Agent: %s\r\n",
470 session->web->user_agent);
471 if (session->web->accept_option != NULL)
472 g_string_append_printf(buf, "Accept: %s\r\n",
473 session->web->accept_option);
474 if (session->web->close_connection == TRUE)
475 g_string_append(buf, "Connection: close\r\n");
476 g_string_append(buf, "\r\n");
477 str = g_string_free(buf, FALSE);
481 debug(session->web, "bytes to write %zu", count);
483 status = g_io_channel_write_chars(session->transport_channel,
484 str, count, &bytes_written, NULL);
486 debug(session->web, "status %u bytes written %zu",
487 status, bytes_written);
494 static int parse_url(struct web_session *session, const char *url)
496 char *scheme, *host, *port, *path;
498 scheme = g_strdup(url);
502 host = strstr(scheme, "://");
507 if (strcasecmp(scheme, "https") == 0) {
509 session->flags |= SESSION_FLAG_USE_TLS;
510 } else if (strcasecmp(scheme, "http") == 0) {
521 path = strchr(host, '/');
525 session->request = g_strdup_printf("/%s", path ? path : "");
527 port = strrchr(host, ':');
530 int tmp = strtol(port + 1, &end, 10);
538 session->host = g_strdup(host);
545 static void resolv_result(GResolvResultStatus status,
546 char **results, gpointer user_data)
548 struct web_session *session = user_data;
550 if (results == NULL || results[0] == NULL) {
551 call_result_func(session, 404);
555 debug(session->web, "address %s", results[0]);
557 if (inet_aton(results[0], NULL) == 0) {
558 call_result_func(session, 400);
562 session->address = g_strdup(results[0]);
564 if (create_transport(session) < 0) {
565 call_result_func(session, 409);
569 start_request(session);
572 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
573 GWebResultFunc func, gpointer user_data)
575 struct web_session *session;
577 if (web == NULL || url == NULL)
580 debug(web, "request %s", url);
582 session = g_try_new0(struct web_session, 1);
586 if (parse_url(session, url) < 0) {
587 free_session(session);
591 debug(web, "host %s:%u", session->host, session->port);
592 debug(web, "flags %lu", session->flags);
596 session->result_func = func;
597 session->result_data = user_data;
599 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
600 if (session->receive_buffer == NULL) {
601 free_session(session);
605 session->receive_space = DEFAULT_BUFFER_SIZE;
606 session->current_header = g_string_sized_new(0);
607 session->header_done = FALSE;
609 if (inet_aton(session->host, NULL) == 0) {
610 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
611 session->host, resolv_result, session);
612 if (session->resolv_action == 0) {
613 free_session(session);
617 session->address = g_strdup(session->host);
619 if (create_transport(session) < 0) {
620 free_session(session);
624 start_request(session);
627 web->session_list = g_list_append(web->session_list, session);
629 return web->next_query_id++;
632 guint16 g_web_result_get_status(GWebResult *result)
637 return result->status;
640 gboolean g_web_result_get_chunk(GWebResult *result,
641 const guint8 **chunk, gsize *length)
649 *chunk = result->buffer;
652 *length = result->length;
661 const char *token_str;
670 GWebParser *g_web_parser_new(const char *begin, const char *end,
671 GWebParserFunc func, gpointer user_data)
675 parser = g_try_new0(GWebParser, 1);
679 parser->ref_count = 1;
681 parser->begin_token = g_strdup(begin);
682 parser->end_token = g_strdup(end);
684 if (parser->begin_token == NULL) {
690 parser->user_data = user_data;
692 parser->token_str = parser->begin_token;
693 parser->token_len = strlen(parser->token_str);
694 parser->token_pos = 0;
696 parser->intoken = FALSE;
697 parser->content = g_string_sized_new(0);
702 GWebParser *g_web_parser_ref(GWebParser *parser)
707 g_atomic_int_inc(&parser->ref_count);
712 void g_web_parser_unref(GWebParser *parser)
717 if (g_atomic_int_dec_and_test(&parser->ref_count) == FALSE)
720 g_string_free(parser->content, TRUE);
722 g_free(parser->begin_token);
723 g_free(parser->end_token);
727 void g_web_parser_feed_data(GWebParser *parser,
728 const guint8 *data, gsize length)
730 const guint8 *ptr = data;
736 guint8 chr = parser->token_str[parser->token_pos];
738 if (parser->token_pos == 0) {
741 pos = memchr(ptr, chr, length);
743 if (parser->intoken == TRUE)
744 g_string_append_len(parser->content,
745 (gchar *) ptr, length);
749 if (parser->intoken == TRUE)
750 g_string_append_len(parser->content,
751 (gchar *) ptr, (pos - ptr) + 1);
753 length -= (pos - ptr) + 1;
760 if (parser->intoken == TRUE)
761 g_string_append_c(parser->content, ptr[0]);
767 parser->token_pos = 0;
776 if (parser->token_pos == parser->token_len) {
777 if (parser->intoken == FALSE) {
778 g_string_append(parser->content,
781 parser->intoken = TRUE;
782 parser->token_str = parser->end_token;
783 parser->token_len = strlen(parser->end_token);
784 parser->token_pos = 0;
787 str = g_string_free(parser->content, FALSE);
788 parser->content = g_string_sized_new(0);
790 parser->func(str, parser->user_data);
793 parser->intoken = FALSE;
794 parser->token_str = parser->begin_token;
795 parser->token_len = strlen(parser->begin_token);
796 parser->token_pos = 0;
802 void g_web_parser_end_data(GWebParser *parser)