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 LINE_CHUNK_SIZE 2048
41 #define SESSION_FLAG_USE_TLS (1 << 0)
56 GIOChannel *transport_channel;
57 guint transport_watch;
64 unsigned int line_space;
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_free(session->line_buffer);
127 g_free(session->host);
128 g_free(session->address);
132 static void flush_sessions(GWeb *web)
136 for (list = g_list_first(web->session_list);
137 list; list = g_list_next(list))
138 free_session(list->data);
140 g_list_free(web->session_list);
141 web->session_list = NULL;
144 GWeb *g_web_new(int index)
151 web = g_try_new0(GWeb, 1);
157 web->next_query_id = 1;
160 web->session_list = NULL;
162 web->resolv = g_resolv_new(index);
163 if (web->resolv == NULL) {
168 web->accept_option = g_strdup("*/*");
169 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
170 web->close_connection = FALSE;
175 GWeb *g_web_ref(GWeb *web)
180 g_atomic_int_inc(&web->ref_count);
185 void g_web_unref(GWeb *web)
190 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
195 g_resolv_unref(web->resolv);
197 g_free(web->accept_option);
198 g_free(web->user_agent);
202 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
207 web->debug_func = func;
208 web->debug_data = user_data;
210 g_resolv_set_debug(web->resolv, func, user_data);
213 gboolean g_web_add_nameserver(GWeb *web, const char *address)
218 g_resolv_add_nameserver(web->resolv, address, 53, 0);
223 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
225 g_free(web->accept_option);
227 if (format == NULL) {
228 web->accept_option = NULL;
229 debug(web, "clearing accept option");
231 web->accept_option = g_strdup_vprintf(format, args);
232 debug(web, "setting accept %s", web->accept_option);
238 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
246 va_start(args, format);
247 result = set_accept_option(web, format, args);
253 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
255 g_free(web->user_agent);
257 if (format == NULL) {
258 web->user_agent = NULL;
259 debug(web, "clearing user agent");
261 web->user_agent = g_strdup_vprintf(format, args);
262 debug(web, "setting user agent %s", web->user_agent);
268 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
276 va_start(args, format);
277 result = set_user_agent(web, format, args);
283 void g_web_set_close_connection(GWeb *web, gboolean enabled)
288 web->close_connection = enabled;
291 gboolean g_web_get_close_connection(GWeb *web)
296 return web->close_connection;
299 static inline void call_result_func(struct web_session *session, guint status)
301 if (session->result_func == NULL)
304 session->result_func(status, &session->result, session->result_data);
307 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
310 struct web_session *session = user_data;
311 gsize bytes_read, consumed = 0;
316 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
317 session->transport_watch = 0;
318 call_result_func(session, 400);
322 status = g_io_channel_read_chars(channel, session->line_offset,
323 session->line_space, &bytes_read, NULL);
325 debug(session->web, "status %u bytes read %zu", status, bytes_read);
327 if (status != G_IO_STATUS_NORMAL) {
328 session->transport_watch = 0;
329 call_result_func(session, 200);
333 if (session->header_done == TRUE) {
334 session->result.length = bytes_read;
335 call_result_func(session, 100);
339 str = memchr(session->line_offset, '\n', bytes_read);
341 while (str != NULL) {
342 char *start = session->current_line;
345 count = strlen(start);
346 if (count > 0 && start[count - 1] == '\r') {
347 start[--count] = '\0';
351 session->current_line = str + 1;
352 consumed += count + 1;
355 const void *ptr = session->current_line;
356 session->header_done = TRUE;
357 session->result.buffer = ptr;
358 session->result.length = bytes_read - consumed;
359 call_result_func(session, 100);
363 //printf("[ %s ]\n", start);
365 str = memchr(session->current_line, '\n',
366 bytes_read - consumed);
369 if (session->header_done == TRUE) {
370 gsize size = session->line_offset - session->line_buffer;
372 session->line_offset = session->line_buffer;
373 session->line_space += size;
375 session->result.buffer = (const guint8 *) session->line_offset;
379 session->line_offset += bytes_read;
380 session->line_space -= bytes_read;
382 if (session->line_space < 32) {
383 gsize size = session->line_offset - session->line_buffer;
384 gsize pos = session->current_line - session->line_buffer;
387 printf("realloc, space %u size %zu\n",
388 session->line_space, size);
390 buf = g_try_realloc(session->line_buffer,
391 size + LINE_CHUNK_SIZE);
393 session->line_buffer = buf;
394 session->line_offset = buf + size;
395 session->line_space = LINE_CHUNK_SIZE;
396 session->current_line = buf + pos;
403 static int connect_session_transport(struct web_session *session)
405 struct sockaddr_in sin;
408 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
412 memset(&sin, 0, sizeof(sin));
413 sin.sin_family = AF_INET;
414 sin.sin_port = htons(session->port);
415 sin.sin_addr.s_addr = inet_addr(session->address);
417 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
422 if (session->flags & SESSION_FLAG_USE_TLS)
423 session->transport_channel = g_io_channel_gnutls_new(sk);
425 session->transport_channel = g_io_channel_unix_new(sk);
427 if (session->transport_channel == NULL) {
432 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
433 g_io_channel_set_buffered(session->transport_channel, FALSE);
435 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
437 session->transport_watch = g_io_add_watch(session->transport_channel,
438 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
439 received_data, session);
444 static int create_transport(struct web_session *session)
448 err = connect_session_transport(session);
452 debug(session->web, "creating session %s:%u",
453 session->address, session->port);
458 static void start_request(struct web_session *session)
462 gsize count, bytes_written;
465 debug(session->web, "request %s from %s",
466 session->request, session->host);
468 buf = g_string_new(NULL);
469 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
470 g_string_append_printf(buf, "Host: %s\r\n", session->host);
471 if (session->web->user_agent != NULL)
472 g_string_append_printf(buf, "User-Agent: %s\r\n",
473 session->web->user_agent);
474 if (session->web->accept_option != NULL)
475 g_string_append_printf(buf, "Accept: %s\r\n",
476 session->web->accept_option);
477 if (session->web->close_connection == TRUE)
478 g_string_append(buf, "Connection: close\r\n");
479 g_string_append(buf, "\r\n");
480 str = g_string_free(buf, FALSE);
484 debug(session->web, "bytes to write %zu", count);
486 status = g_io_channel_write_chars(session->transport_channel,
487 str, count, &bytes_written, NULL);
489 debug(session->web, "status %u bytes written %zu",
490 status, bytes_written);
497 static int parse_url(struct web_session *session, const char *url)
499 char *scheme, *host, *port, *path;
501 scheme = g_strdup(url);
505 host = strstr(scheme, "://");
510 if (strcasecmp(scheme, "https") == 0) {
512 session->flags |= SESSION_FLAG_USE_TLS;
513 } else if (strcasecmp(scheme, "http") == 0) {
524 path = strchr(host, '/');
528 session->request = g_strdup_printf("/%s", path ? path : "");
530 port = strrchr(host, ':');
533 int tmp = strtol(port + 1, &end, 10);
541 session->host = g_strdup(host);
548 static void resolv_result(GResolvResultStatus status,
549 char **results, gpointer user_data)
551 struct web_session *session = user_data;
553 if (results == NULL || results[0] == NULL) {
554 call_result_func(session, 404);
558 debug(session->web, "address %s", results[0]);
560 if (inet_aton(results[0], NULL) == 0) {
561 call_result_func(session, 400);
565 session->address = g_strdup(results[0]);
567 if (create_transport(session) < 0) {
568 call_result_func(session, 409);
572 start_request(session);
575 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
576 GWebResultFunc func, gpointer user_data)
578 struct web_session *session;
580 if (web == NULL || url == NULL)
583 debug(web, "request %s", url);
585 session = g_try_new0(struct web_session, 1);
589 if (parse_url(session, url) < 0) {
590 free_session(session);
594 debug(web, "host %s:%u", session->host, session->port);
595 debug(web, "flags %lu", session->flags);
599 session->result_func = func;
600 session->result_data = user_data;
602 session->line_buffer = g_try_malloc(LINE_CHUNK_SIZE);
603 session->line_offset = session->line_buffer;
604 session->line_space = LINE_CHUNK_SIZE;
605 session->current_line = session->line_buffer;
606 session->header_done = FALSE;
608 if (inet_aton(session->host, NULL) == 0) {
609 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
610 session->host, resolv_result, session);
611 if (session->resolv_action == 0) {
612 free_session(session);
616 session->address = g_strdup(session->host);
618 if (create_transport(session) < 0) {
619 free_session(session);
623 start_request(session);
626 web->session_list = g_list_append(web->session_list, session);
628 return web->next_query_id++;
631 gboolean g_web_result_get_chunk(GWebResult *result,
632 const guint8 **chunk, gsize *length)
640 *chunk = result->buffer;
643 *length = result->length;