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)
57 GIOChannel *transport_channel;
58 guint transport_watch;
65 unsigned int line_space;
71 GWebResultFunc result_func;
86 gboolean close_connection;
88 GWebDebugFunc debug_func;
92 static inline void debug(GWeb *web, const char *format, ...)
97 if (web->debug_func == NULL)
100 va_start(ap, format);
102 if (vsnprintf(str, sizeof(str), format, ap) > 0)
103 web->debug_func(str, web->debug_data);
108 static void free_session(struct web_session *session)
110 GWeb *web = session->web;
115 g_free(session->request);
117 if (session->resolv_action > 0)
118 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
120 if (session->transport_watch > 0)
121 g_source_remove(session->transport_watch);
123 if (session->transport_channel != NULL)
124 g_io_channel_unref(session->transport_channel);
126 g_free(session->line_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 gsize bytes_read, consumed = 0;
317 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
318 session->transport_watch = 0;
319 call_result_func(session, 400);
323 status = g_io_channel_read_chars(channel, session->line_offset,
324 session->line_space, &bytes_read, NULL);
326 debug(session->web, "status %u bytes read %zu", status, bytes_read);
328 if (status != G_IO_STATUS_NORMAL) {
329 session->transport_watch = 0;
330 call_result_func(session, 200);
334 if (session->header_done == TRUE) {
335 session->result.length = bytes_read;
336 call_result_func(session, 100);
340 str = memchr(session->line_offset, '\n', bytes_read);
342 while (str != NULL) {
343 char *start = session->current_line;
347 count = strlen(start);
348 if (count > 0 && start[count - 1] == '\r') {
349 start[--count] = '\0';
353 session->current_line = str + 1;
354 consumed += count + 1;
357 const void *ptr = session->current_line;
358 session->header_done = TRUE;
359 session->result.buffer = ptr;
360 session->result.length = bytes_read - consumed;
361 call_result_func(session, 100);
365 //printf("[ %s ]\n", start);
367 if (session->result.status == 0) {
368 if (sscanf(start, "HTTP/%*s %u %*s", &code) == 1)
369 session->result.status = code;
372 str = memchr(session->current_line, '\n',
373 bytes_read - consumed);
376 if (session->header_done == TRUE) {
377 gsize size = session->line_offset - session->line_buffer;
379 session->line_offset = session->line_buffer;
380 session->line_space += size;
382 session->result.buffer = (const guint8 *) session->line_offset;
386 session->line_offset += bytes_read;
387 session->line_space -= bytes_read;
389 if (session->line_space < 32) {
390 gsize size = session->line_offset - session->line_buffer;
391 gsize pos = session->current_line - session->line_buffer;
394 printf("realloc, space %u size %zu\n",
395 session->line_space, size);
397 buf = g_try_realloc(session->line_buffer,
398 size + LINE_CHUNK_SIZE);
400 session->line_buffer = buf;
401 session->line_offset = buf + size;
402 session->line_space = LINE_CHUNK_SIZE;
403 session->current_line = buf + pos;
410 static int connect_session_transport(struct web_session *session)
412 struct sockaddr_in sin;
415 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
419 memset(&sin, 0, sizeof(sin));
420 sin.sin_family = AF_INET;
421 sin.sin_port = htons(session->port);
422 sin.sin_addr.s_addr = inet_addr(session->address);
424 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
429 if (session->flags & SESSION_FLAG_USE_TLS)
430 session->transport_channel = g_io_channel_gnutls_new(sk);
432 session->transport_channel = g_io_channel_unix_new(sk);
434 if (session->transport_channel == NULL) {
439 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
440 g_io_channel_set_buffered(session->transport_channel, FALSE);
442 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
444 session->transport_watch = g_io_add_watch(session->transport_channel,
445 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
446 received_data, session);
451 static int create_transport(struct web_session *session)
455 err = connect_session_transport(session);
459 debug(session->web, "creating session %s:%u",
460 session->address, session->port);
465 static void start_request(struct web_session *session)
469 gsize count, bytes_written;
472 debug(session->web, "request %s from %s",
473 session->request, session->host);
475 buf = g_string_new(NULL);
476 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
477 g_string_append_printf(buf, "Host: %s\r\n", session->host);
478 if (session->web->user_agent != NULL)
479 g_string_append_printf(buf, "User-Agent: %s\r\n",
480 session->web->user_agent);
481 if (session->web->accept_option != NULL)
482 g_string_append_printf(buf, "Accept: %s\r\n",
483 session->web->accept_option);
484 if (session->web->close_connection == TRUE)
485 g_string_append(buf, "Connection: close\r\n");
486 g_string_append(buf, "\r\n");
487 str = g_string_free(buf, FALSE);
491 debug(session->web, "bytes to write %zu", count);
493 status = g_io_channel_write_chars(session->transport_channel,
494 str, count, &bytes_written, NULL);
496 debug(session->web, "status %u bytes written %zu",
497 status, bytes_written);
504 static int parse_url(struct web_session *session, const char *url)
506 char *scheme, *host, *port, *path;
508 scheme = g_strdup(url);
512 host = strstr(scheme, "://");
517 if (strcasecmp(scheme, "https") == 0) {
519 session->flags |= SESSION_FLAG_USE_TLS;
520 } else if (strcasecmp(scheme, "http") == 0) {
531 path = strchr(host, '/');
535 session->request = g_strdup_printf("/%s", path ? path : "");
537 port = strrchr(host, ':');
540 int tmp = strtol(port + 1, &end, 10);
548 session->host = g_strdup(host);
555 static void resolv_result(GResolvResultStatus status,
556 char **results, gpointer user_data)
558 struct web_session *session = user_data;
560 if (results == NULL || results[0] == NULL) {
561 call_result_func(session, 404);
565 debug(session->web, "address %s", results[0]);
567 if (inet_aton(results[0], NULL) == 0) {
568 call_result_func(session, 400);
572 session->address = g_strdup(results[0]);
574 if (create_transport(session) < 0) {
575 call_result_func(session, 409);
579 start_request(session);
582 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
583 GWebResultFunc func, gpointer user_data)
585 struct web_session *session;
587 if (web == NULL || url == NULL)
590 debug(web, "request %s", url);
592 session = g_try_new0(struct web_session, 1);
596 if (parse_url(session, url) < 0) {
597 free_session(session);
601 debug(web, "host %s:%u", session->host, session->port);
602 debug(web, "flags %lu", session->flags);
606 session->result_func = func;
607 session->result_data = user_data;
609 session->line_buffer = g_try_malloc(LINE_CHUNK_SIZE);
610 session->line_offset = session->line_buffer;
611 session->line_space = LINE_CHUNK_SIZE;
612 session->current_line = session->line_buffer;
613 session->header_done = FALSE;
615 if (inet_aton(session->host, NULL) == 0) {
616 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
617 session->host, resolv_result, session);
618 if (session->resolv_action == 0) {
619 free_session(session);
623 session->address = g_strdup(session->host);
625 if (create_transport(session) < 0) {
626 free_session(session);
630 start_request(session);
633 web->session_list = g_list_append(web->session_list, session);
635 return web->next_query_id++;
638 guint16 g_web_result_get_status(GWebResult *result)
643 return result->status;
646 gboolean g_web_result_get_chunk(GWebResult *result,
647 const guint8 **chunk, gsize *length)
655 *chunk = result->buffer;
658 *length = result->length;