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)
69 GIOChannel *transport_channel;
70 guint transport_watch;
76 guint8 *receive_buffer;
79 GString *current_header;
83 gboolean request_started;
85 enum chunk_state chunck_state;
92 GWebResultFunc result_func;
93 GWebInputFunc input_func;
109 char *user_agent_profile;
111 gboolean close_connection;
113 GWebDebugFunc debug_func;
117 static inline void debug(GWeb *web, const char *format, ...)
122 if (web->debug_func == NULL)
125 va_start(ap, format);
127 if (vsnprintf(str, sizeof(str), format, ap) > 0)
128 web->debug_func(str, web->debug_data);
133 static void free_session(struct web_session *session)
135 GWeb *web = session->web;
140 g_free(session->request);
142 if (session->resolv_action > 0)
143 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
145 if (session->transport_watch > 0)
146 g_source_remove(session->transport_watch);
148 if (session->send_watch > 0)
149 g_source_remove(session->send_watch);
151 if (session->transport_channel != NULL)
152 g_io_channel_unref(session->transport_channel);
154 g_free(session->result.last_key);
156 if (session->result.headers != NULL)
157 g_hash_table_destroy(session->result.headers);
159 if (session->send_buffer != NULL)
160 g_string_free(session->send_buffer, TRUE);
162 if (session->current_header != NULL)
163 g_string_free(session->current_header, TRUE);
165 g_free(session->receive_buffer);
167 g_free(session->content_type);
169 g_free(session->host);
170 g_free(session->address);
174 static void flush_sessions(GWeb *web)
178 for (list = g_list_first(web->session_list);
179 list; list = g_list_next(list))
180 free_session(list->data);
182 g_list_free(web->session_list);
183 web->session_list = NULL;
186 GWeb *g_web_new(int index)
193 web = g_try_new0(GWeb, 1);
199 web->next_query_id = 1;
202 web->session_list = NULL;
204 web->resolv = g_resolv_new(index);
205 if (web->resolv == NULL) {
210 web->accept_option = g_strdup("*/*");
211 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
212 web->close_connection = FALSE;
217 GWeb *g_web_ref(GWeb *web)
222 g_atomic_int_inc(&web->ref_count);
227 void g_web_unref(GWeb *web)
232 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
237 g_resolv_unref(web->resolv);
241 g_free(web->accept_option);
242 g_free(web->user_agent);
243 g_free(web->user_agent_profile);
244 g_free(web->http_version);
249 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
254 web->debug_func = func;
255 web->debug_data = user_data;
257 g_resolv_set_debug(web->resolv, func, user_data);
260 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
269 debug(web, "clearing proxy");
271 web->proxy = g_strdup(proxy);
272 debug(web, "setting proxy %s", web->proxy);
278 gboolean g_web_add_nameserver(GWeb *web, const char *address)
283 g_resolv_add_nameserver(web->resolv, address, 53, 0);
288 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
290 g_free(web->accept_option);
292 if (format == NULL) {
293 web->accept_option = NULL;
294 debug(web, "clearing accept option");
296 web->accept_option = g_strdup_vprintf(format, args);
297 debug(web, "setting accept %s", web->accept_option);
303 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
311 va_start(args, format);
312 result = set_accept_option(web, format, args);
318 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
320 g_free(web->user_agent);
322 if (format == NULL) {
323 web->user_agent = NULL;
324 debug(web, "clearing user agent");
326 web->user_agent = g_strdup_vprintf(format, args);
327 debug(web, "setting user agent %s", web->user_agent);
333 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
341 va_start(args, format);
342 result = set_user_agent(web, format, args);
348 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
353 g_free(web->user_agent_profile);
355 web->user_agent_profile = g_strdup(profile);
356 debug(web, "setting user agent profile %s", web->user_agent);
361 gboolean g_web_set_http_version(GWeb *web, const char *version)
366 g_free(web->http_version);
368 if (version == NULL) {
369 web->http_version = NULL;
370 debug(web, "clearing HTTP version");
372 web->http_version = g_strdup(version);
373 debug(web, "setting HTTP version %s", web->http_version);
379 void g_web_set_close_connection(GWeb *web, gboolean enabled)
384 web->close_connection = enabled;
387 gboolean g_web_get_close_connection(GWeb *web)
392 return web->close_connection;
395 static inline void call_result_func(struct web_session *session, guint16 status)
399 if (session->result_func == NULL)
403 session->result.status = status;
405 result = session->result_func(&session->result, session->user_data);
407 debug(session->web, "[result function] %s",
408 result == TRUE ? "continue" : "stop");
411 static gboolean process_send_buffer(struct web_session *session)
413 GString *buf = session->send_buffer;
414 gsize count, bytes_written;
420 if (session->request_started == TRUE &&
421 session->more_data == FALSE)
422 session->body_done = TRUE;
427 debug(session->web, "bytes to write %zu", count);
429 status = g_io_channel_write_chars(session->transport_channel,
430 buf->str, count, &bytes_written, NULL);
432 debug(session->web, "status %u bytes written %zu",
433 status, bytes_written);
435 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
438 g_string_erase(buf, 0, bytes_written);
443 static void process_next_chunk(struct web_session *session)
445 GString *buf = session->send_buffer;
449 if (session->input_func == NULL) {
450 session->more_data = FALSE;
454 session->more_data = session->input_func(&body, &length,
458 g_string_append_printf(buf, "%zx\r\n", length);
459 g_string_append_len(buf, (char *) body, length);
460 g_string_append(buf, "\r\n");
463 if (session->more_data == FALSE)
464 g_string_append(buf, "0\r\n\r\n");
467 static void start_request(struct web_session *session)
469 GString *buf = session->send_buffer;
474 debug(session->web, "request %s from %s",
475 session->request, session->host);
477 g_string_truncate(buf, 0);
479 if (session->web->http_version == NULL)
482 version = session->web->http_version;
484 if (session->content_type == NULL)
485 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
486 session->request, version);
488 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
489 session->request, version);
491 g_string_append_printf(buf, "Host: %s\r\n", session->host);
493 if (session->web->user_agent != NULL)
494 g_string_append_printf(buf, "User-Agent: %s\r\n",
495 session->web->user_agent);
497 if (session->web->user_agent_profile != NULL) {
498 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
499 session->web->user_agent_profile);
502 if (session->web->accept_option != NULL)
503 g_string_append_printf(buf, "Accept: %s\r\n",
504 session->web->accept_option);
506 if (session->content_type != NULL) {
507 g_string_append_printf(buf, "Content-Type: %s\r\n",
508 session->content_type);
509 if (session->input_func == NULL) {
510 session->more_data = FALSE;
513 session->more_data = session->input_func(&body, &length,
515 if (session->more_data == FALSE)
516 g_string_append_printf(buf, "Content-Length: %zu\r\n",
519 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
522 if (session->web->close_connection == TRUE)
523 g_string_append(buf, "Connection: close\r\n");
525 g_string_append(buf, "\r\n");
527 if (session->content_type != NULL && length > 0) {
528 if (session->more_data == TRUE) {
529 g_string_append_printf(buf, "%zx\r\n", length);
530 g_string_append_len(buf, (char *) body, length);
531 g_string_append(buf, "\r\n");
533 g_string_append_len(buf, (char *) body, length);
537 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
540 struct web_session *session = user_data;
542 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
543 session->send_watch = 0;
547 if (process_send_buffer(session) == TRUE)
550 if (session->request_started == FALSE) {
551 session->request_started = TRUE;
552 start_request(session);
553 } else if (session->more_data == TRUE)
554 process_next_chunk(session);
556 process_send_buffer(session);
558 if (session->body_done == TRUE) {
559 session->send_watch = 0;
566 static int decode_chunked(struct web_session *session,
567 const guint8 *buf, gsize len)
569 const guint8 *ptr = buf;
577 switch (session->chunck_state) {
579 pos = memchr(ptr, '\n', len);
581 g_string_append_len(session->current_header,
587 if (count < 1 || ptr[count - 1] != '\r')
590 g_string_append_len(session->current_header,
591 (gchar *) ptr, count);
596 str = session->current_header->str;
598 counter = strtoul(str, NULL, 16);
599 if ((counter == 0 && errno == EINVAL) ||
600 counter == ULONG_MAX)
603 session->chunk_size = counter;
604 session->chunk_left = counter;
606 session->chunck_state = CHUNK_DATA;
613 session->chunck_state = CHUNK_N_BODY;
620 session->chunck_state = CHUNK_SIZE;
623 if (session->chunk_size == 0) {
624 debug(session->web, "Download Done in chunk");
625 g_string_truncate(session->current_header, 0);
629 if (session->chunk_left <= len) {
630 session->result.buffer = ptr;
631 session->result.length = session->chunk_left;
632 call_result_func(session, 0);
634 len -= session->chunk_left;
635 ptr += session->chunk_left;
637 session->total_len += session->chunk_left;
638 session->chunk_left = 0;
640 g_string_truncate(session->current_header, 0);
641 session->chunck_state = CHUNK_R_BODY;
645 session->result.buffer = ptr;
646 session->result.length = len;
647 call_result_func(session, 0);
649 session->chunk_left -= len;
650 session->total_len += len;
661 static int handle_body(struct web_session *session,
662 const guint8 *buf, gsize len)
666 debug(session->web, "[body] length %zu", len);
668 if (session->result.use_chunk == FALSE) {
670 session->result.buffer = buf;
671 session->result.length = len;
672 call_result_func(session, 0);
677 err = decode_chunked(session, buf, len);
679 debug(session->web, "Error in chunk decode %d", err);
681 session->result.buffer = NULL;
682 session->result.length = 0;
683 call_result_func(session, 400);
689 static void handle_multi_line(struct web_session *session)
695 str = session->current_header->str;
697 if (str[0] != ' ' && str[0] != '\t')
700 while (str[0] == ' ' || str[0] == '\t')
703 count = str - session->current_header->str;
705 g_string_erase(session->current_header, 0, count);
706 g_string_insert_c(session->current_header, 0, ' ');
709 value = g_hash_table_lookup(session->result.headers,
710 session->result.last_key);
712 g_string_insert(session->current_header, 0, value);
714 str = session->current_header->str;
716 g_hash_table_replace(session->result.headers,
717 g_strdup(session->result.last_key),
722 static void add_header_field(struct web_session *session)
730 str = session->current_header->str;
732 pos = memchr(str, ':', session->current_header->len);
739 /* remove preceding white spaces */
743 count = (char *) pos - str;
745 g_string_erase(session->current_header, 0, count);
747 value = g_hash_table_lookup(session->result.headers, key);
749 g_string_insert_c(session->current_header, 0, ' ');
750 g_string_insert_c(session->current_header, 0, ';');
752 g_string_insert(session->current_header, 0, value);
755 str = session->current_header->str;
756 g_hash_table_replace(session->result.headers, key,
759 g_free(session->result.last_key);
760 session->result.last_key = g_strdup(key);
764 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
767 struct web_session *session = user_data;
768 guint8 *ptr = session->receive_buffer;
772 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
773 session->transport_watch = 0;
774 session->result.buffer = NULL;
775 session->result.length = 0;
776 call_result_func(session, 400);
780 status = g_io_channel_read_chars(channel,
781 (gchar *) session->receive_buffer,
782 session->receive_space - 1, &bytes_read, NULL);
784 debug(session->web, "bytes read %zu", bytes_read);
786 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
787 session->transport_watch = 0;
788 session->result.buffer = NULL;
789 session->result.length = 0;
790 call_result_func(session, 0);
794 session->receive_buffer[bytes_read] = '\0';
796 if (session->header_done == TRUE) {
797 if (handle_body(session, session->receive_buffer,
799 session->transport_watch = 0;
805 while (bytes_read > 0) {
810 pos = memchr(ptr, '\n', bytes_read);
812 g_string_append_len(session->current_header,
813 (gchar *) ptr, bytes_read);
818 count = strlen((char *) ptr);
819 if (count > 0 && ptr[count - 1] == '\r') {
824 g_string_append_len(session->current_header,
825 (gchar *) ptr, count);
827 bytes_read -= count + 1;
833 if (session->current_header->len == 0) {
836 session->header_done = TRUE;
838 val = g_hash_table_lookup(session->result.headers,
839 "Transfer-Encoding");
841 val = g_strrstr(val, "chunked");
843 session->result.use_chunk = TRUE;
845 session->chunck_state = CHUNK_SIZE;
846 session->chunk_left = 0;
847 session->total_len = 0;
851 if (handle_body(session, ptr, bytes_read) < 0) {
852 session->transport_watch = 0;
858 str = session->current_header->str;
860 if (session->result.status == 0) {
863 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
864 session->result.status = code;
867 debug(session->web, "[header] %s", str);
869 /* handle multi-line header */
870 if (str[0] == ' ' || str[0] == '\t')
871 handle_multi_line(session);
873 add_header_field(session);
875 g_string_truncate(session->current_header, 0);
881 static int connect_session_transport(struct web_session *session)
884 struct sockaddr_in sin;
887 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
891 if (session->flags & SESSION_FLAG_USE_TLS) {
892 debug(session->web, "using TLS encryption");
893 session->transport_channel = g_io_channel_gnutls_new(sk);
895 debug(session->web, "no encryption");
896 session->transport_channel = g_io_channel_unix_new(sk);
899 if (session->transport_channel == NULL) {
904 flags = g_io_channel_get_flags(session->transport_channel);
905 g_io_channel_set_flags(session->transport_channel,
906 flags | G_IO_FLAG_NONBLOCK, NULL);
908 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
909 g_io_channel_set_buffered(session->transport_channel, FALSE);
911 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
913 memset(&sin, 0, sizeof(sin));
914 sin.sin_family = AF_INET;
915 sin.sin_port = htons(session->port);
916 sin.sin_addr.s_addr = inet_addr(session->address);
918 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
919 if (errno != EINPROGRESS) {
925 session->transport_watch = g_io_add_watch(session->transport_channel,
926 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
927 received_data, session);
929 session->send_watch = g_io_add_watch(session->transport_channel,
930 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
936 static int create_transport(struct web_session *session)
940 err = connect_session_transport(session);
944 debug(session->web, "creating session %s:%u",
945 session->address, session->port);
950 static int parse_url(struct web_session *session,
951 const char *url, const char *proxy)
953 char *scheme, *host, *port, *path;
955 scheme = g_strdup(url);
959 host = strstr(scheme, "://");
964 if (strcasecmp(scheme, "https") == 0) {
966 session->flags |= SESSION_FLAG_USE_TLS;
967 } else if (strcasecmp(scheme, "http") == 0) {
978 path = strchr(host, '/');
983 session->request = g_strdup_printf("/%s", path ? path : "");
985 session->request = g_strdup(url);
987 port = strrchr(host, ':');
990 int tmp = strtol(port + 1, &end, 10);
998 session->host = g_strdup(host);
1000 session->host = g_strdup_printf("%s:%u", host, tmp);
1002 session->host = g_strdup(host);
1009 scheme = g_strdup(proxy);
1013 host = strstr(proxy, "://");
1018 if (strcasecmp(scheme, "http") != 0) {
1025 path = strchr(host, '/');
1029 port = strrchr(host, ':');
1032 int tmp = strtol(port + 1, &end, 10);
1036 session->port = tmp;
1040 session->address = g_strdup(host);
1047 static void resolv_result(GResolvResultStatus status,
1048 char **results, gpointer user_data)
1050 struct web_session *session = user_data;
1052 if (results == NULL || results[0] == NULL) {
1053 call_result_func(session, 404);
1057 debug(session->web, "address %s", results[0]);
1059 if (inet_aton(results[0], NULL) == 0) {
1060 call_result_func(session, 400);
1064 session->address = g_strdup(results[0]);
1066 if (create_transport(session) < 0) {
1067 call_result_func(session, 409);
1072 static guint do_request(GWeb *web, const char *url,
1073 const char *type, GWebInputFunc input,
1074 GWebResultFunc func, gpointer user_data)
1076 struct web_session *session;
1078 if (web == NULL || url == NULL)
1081 debug(web, "request %s", url);
1083 session = g_try_new0(struct web_session, 1);
1084 if (session == NULL)
1087 if (parse_url(session, url, web->proxy) < 0) {
1088 free_session(session);
1092 debug(web, "address %s", session->address);
1093 debug(web, "port %u", session->port);
1094 debug(web, "host %s", session->host);
1095 debug(web, "flags %lu", session->flags);
1096 debug(web, "request %s", session->request);
1099 session->content_type = g_strdup(type);
1101 debug(web, "content-type %s", session->content_type);
1106 session->result_func = func;
1107 session->input_func = input;
1108 session->user_data = user_data;
1110 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1111 if (session->receive_buffer == NULL) {
1112 free_session(session);
1116 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1118 if (session->result.headers == NULL) {
1119 free_session(session);
1123 session->receive_space = DEFAULT_BUFFER_SIZE;
1124 session->send_buffer = g_string_sized_new(0);
1125 session->current_header = g_string_sized_new(0);
1126 session->header_done = FALSE;
1127 session->body_done = FALSE;
1129 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1130 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1131 session->host, resolv_result, session);
1132 if (session->resolv_action == 0) {
1133 free_session(session);
1137 if (session->address == NULL)
1138 session->address = g_strdup(session->host);
1140 if (create_transport(session) < 0) {
1141 free_session(session);
1146 web->session_list = g_list_append(web->session_list, session);
1148 return web->next_query_id++;
1151 guint g_web_request_get(GWeb *web, const char *url,
1152 GWebResultFunc func, gpointer user_data)
1154 return do_request(web, url, NULL, NULL, func, user_data);
1157 guint g_web_request_post(GWeb *web, const char *url,
1158 const char *type, GWebInputFunc input,
1159 GWebResultFunc func, gpointer user_data)
1161 return do_request(web, url, type, input, func, user_data);
1164 gboolean g_web_cancel_request(GWeb *web, guint id)
1172 guint16 g_web_result_get_status(GWebResult *result)
1177 return result->status;
1180 gboolean g_web_result_get_chunk(GWebResult *result,
1181 const guint8 **chunk, gsize *length)
1189 *chunk = result->buffer;
1192 *length = result->length;
1197 gboolean g_web_result_get_header(GWebResult *result,
1198 const char *header, const char **value)
1206 *value = g_hash_table_lookup(result->headers, header);
1214 struct _GWebParser {
1218 const char *token_str;
1223 GWebParserFunc func;
1227 GWebParser *g_web_parser_new(const char *begin, const char *end,
1228 GWebParserFunc func, gpointer user_data)
1232 parser = g_try_new0(GWebParser, 1);
1236 parser->ref_count = 1;
1238 parser->begin_token = g_strdup(begin);
1239 parser->end_token = g_strdup(end);
1241 if (parser->begin_token == NULL) {
1246 parser->func = func;
1247 parser->user_data = user_data;
1249 parser->token_str = parser->begin_token;
1250 parser->token_len = strlen(parser->token_str);
1251 parser->token_pos = 0;
1253 parser->intoken = FALSE;
1254 parser->content = g_string_sized_new(0);
1259 GWebParser *g_web_parser_ref(GWebParser *parser)
1264 g_atomic_int_inc(&parser->ref_count);
1269 void g_web_parser_unref(GWebParser *parser)
1274 if (g_atomic_int_dec_and_test(&parser->ref_count) == FALSE)
1277 g_string_free(parser->content, TRUE);
1279 g_free(parser->begin_token);
1280 g_free(parser->end_token);
1284 void g_web_parser_feed_data(GWebParser *parser,
1285 const guint8 *data, gsize length)
1287 const guint8 *ptr = data;
1292 while (length > 0) {
1293 guint8 chr = parser->token_str[parser->token_pos];
1295 if (parser->token_pos == 0) {
1298 pos = memchr(ptr, chr, length);
1300 if (parser->intoken == TRUE)
1301 g_string_append_len(parser->content,
1302 (gchar *) ptr, length);
1306 if (parser->intoken == TRUE)
1307 g_string_append_len(parser->content,
1308 (gchar *) ptr, (pos - ptr) + 1);
1310 length -= (pos - ptr) + 1;
1313 parser->token_pos++;
1317 if (parser->intoken == TRUE)
1318 g_string_append_c(parser->content, ptr[0]);
1320 if (ptr[0] != chr) {
1324 parser->token_pos = 0;
1331 parser->token_pos++;
1333 if (parser->token_pos == parser->token_len) {
1334 if (parser->intoken == FALSE) {
1335 g_string_append(parser->content,
1338 parser->intoken = TRUE;
1339 parser->token_str = parser->end_token;
1340 parser->token_len = strlen(parser->end_token);
1341 parser->token_pos = 0;
1344 str = g_string_free(parser->content, FALSE);
1345 parser->content = g_string_sized_new(0);
1347 parser->func(str, parser->user_data);
1350 parser->intoken = FALSE;
1351 parser->token_str = parser->begin_token;
1352 parser->token_len = strlen(parser->begin_token);
1353 parser->token_pos = 0;
1359 void g_web_parser_end_data(GWebParser *parser)