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
33 #include <sys/socket.h>
34 #include <sys/sendfile.h>
36 #include <arpa/inet.h>
39 #include <netinet/tcp.h>
41 #include "giognutls.h"
45 #define DEFAULT_BUFFER_SIZE 2048
47 #define SESSION_FLAG_USE_TLS (1 << 0)
72 struct addrinfo *addr;
76 GIOChannel *transport_channel;
77 guint transport_watch;
83 guint8 *receive_buffer;
86 GString *current_header;
90 gboolean request_started;
92 enum chunk_state chunck_state;
99 GWebResultFunc result_func;
100 GWebInputFunc input_func;
121 char *user_agent_profile;
123 gboolean close_connection;
125 GWebDebugFunc debug_func;
129 static inline void debug(GWeb *web, const char *format, ...)
134 if (web->debug_func == NULL)
137 va_start(ap, format);
139 if (vsnprintf(str, sizeof(str), format, ap) > 0)
140 web->debug_func(str, web->debug_data);
145 static void free_session(struct web_session *session)
152 g_free(session->request);
155 if (session->resolv_action > 0)
156 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
158 if (session->transport_watch > 0)
159 g_source_remove(session->transport_watch);
161 if (session->send_watch > 0)
162 g_source_remove(session->send_watch);
164 if (session->transport_channel != NULL)
165 g_io_channel_unref(session->transport_channel);
167 g_free(session->result.last_key);
169 if (session->result.headers != NULL)
170 g_hash_table_destroy(session->result.headers);
172 if (session->send_buffer != NULL)
173 g_string_free(session->send_buffer, TRUE);
175 if (session->current_header != NULL)
176 g_string_free(session->current_header, TRUE);
178 g_free(session->receive_buffer);
180 g_free(session->content_type);
182 g_free(session->host);
183 g_free(session->address);
184 if (session->addr != NULL)
185 freeaddrinfo(session->addr);
190 static void flush_sessions(GWeb *web)
194 for (list = g_list_first(web->session_list);
195 list; list = g_list_next(list))
196 free_session(list->data);
198 g_list_free(web->session_list);
199 web->session_list = NULL;
202 GWeb *g_web_new(int index)
209 web = g_try_new0(GWeb, 1);
215 web->next_query_id = 1;
217 web->family = AF_UNSPEC;
220 web->session_list = NULL;
222 web->resolv = g_resolv_new(index);
223 if (web->resolv == NULL) {
228 web->accept_option = g_strdup("*/*");
229 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
230 web->close_connection = FALSE;
235 GWeb *g_web_ref(GWeb *web)
240 __sync_fetch_and_add(&web->ref_count, 1);
245 void g_web_unref(GWeb *web)
250 if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
255 g_resolv_unref(web->resolv);
259 g_free(web->accept_option);
260 g_free(web->user_agent);
261 g_free(web->user_agent_profile);
262 g_free(web->http_version);
267 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
272 web->debug_func = func;
273 web->debug_data = user_data;
275 g_resolv_set_debug(web->resolv, func, user_data);
278 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
287 debug(web, "clearing proxy");
289 web->proxy = g_strdup(proxy);
290 debug(web, "setting proxy %s", web->proxy);
296 gboolean g_web_set_address_family(GWeb *web, int family)
301 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
304 web->family = family;
306 g_resolv_set_address_family(web->resolv, family);
311 gboolean g_web_add_nameserver(GWeb *web, const char *address)
316 g_resolv_add_nameserver(web->resolv, address, 53, 0);
321 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
323 g_free(web->accept_option);
325 if (format == NULL) {
326 web->accept_option = NULL;
327 debug(web, "clearing accept option");
329 web->accept_option = g_strdup_vprintf(format, args);
330 debug(web, "setting accept %s", web->accept_option);
336 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
344 va_start(args, format);
345 result = set_accept_option(web, format, args);
351 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
353 g_free(web->user_agent);
355 if (format == NULL) {
356 web->user_agent = NULL;
357 debug(web, "clearing user agent");
359 web->user_agent = g_strdup_vprintf(format, args);
360 debug(web, "setting user agent %s", web->user_agent);
366 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
374 va_start(args, format);
375 result = set_user_agent(web, format, args);
381 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
386 g_free(web->user_agent_profile);
388 web->user_agent_profile = g_strdup(profile);
389 debug(web, "setting user agent profile %s", web->user_agent);
394 gboolean g_web_set_http_version(GWeb *web, const char *version)
399 g_free(web->http_version);
401 if (version == NULL) {
402 web->http_version = NULL;
403 debug(web, "clearing HTTP version");
405 web->http_version = g_strdup(version);
406 debug(web, "setting HTTP version %s", web->http_version);
412 void g_web_set_close_connection(GWeb *web, gboolean enabled)
417 web->close_connection = enabled;
420 gboolean g_web_get_close_connection(GWeb *web)
425 return web->close_connection;
428 static inline void call_result_func(struct web_session *session, guint16 status)
432 if (session->result_func == NULL)
436 session->result.status = status;
438 result = session->result_func(&session->result, session->user_data);
440 debug(session->web, "[result function] %s",
441 result == TRUE ? "continue" : "stop");
444 static gboolean process_send_buffer(struct web_session *session)
447 gsize count, bytes_written;
453 buf = session->send_buffer;
457 if (session->request_started == TRUE &&
458 session->more_data == FALSE &&
460 session->body_done = TRUE;
465 status = g_io_channel_write_chars(session->transport_channel,
466 buf->str, count, &bytes_written, NULL);
468 debug(session->web, "status %u bytes to write %zu bytes written %zu",
469 status, count, bytes_written);
471 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
474 g_string_erase(buf, 0, bytes_written);
479 static gboolean process_send_file(struct web_session *session)
485 if (session->fd == -1)
488 sk = g_io_channel_unix_get_fd(session->transport_channel);
492 offset = session->offset;
494 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
496 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
497 errno, session->length, bytes_sent);
499 if (bytes_sent < 0 && errno != EAGAIN)
502 session->offset = offset;
503 session->length -= bytes_sent;
505 if (session->length == 0) {
506 session->body_done = TRUE;
513 static void process_next_chunk(struct web_session *session)
515 GString *buf = session->send_buffer;
519 if (session->input_func == NULL) {
520 session->more_data = FALSE;
524 session->more_data = session->input_func(&body, &length,
528 g_string_append_printf(buf, "%zx\r\n", length);
529 g_string_append_len(buf, (char *) body, length);
530 g_string_append(buf, "\r\n");
533 if (session->more_data == FALSE)
534 g_string_append(buf, "0\r\n\r\n");
537 static void start_request(struct web_session *session)
539 GString *buf = session->send_buffer;
544 debug(session->web, "request %s from %s",
545 session->request, session->host);
547 g_string_truncate(buf, 0);
549 if (session->web->http_version == NULL)
552 version = session->web->http_version;
554 if (session->content_type == NULL)
555 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
556 session->request, version);
558 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
559 session->request, version);
561 g_string_append_printf(buf, "Host: %s\r\n", session->host);
563 if (session->web->user_agent != NULL)
564 g_string_append_printf(buf, "User-Agent: %s\r\n",
565 session->web->user_agent);
567 if (session->web->user_agent_profile != NULL) {
568 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
569 session->web->user_agent_profile);
572 if (session->web->accept_option != NULL)
573 g_string_append_printf(buf, "Accept: %s\r\n",
574 session->web->accept_option);
576 if (session->content_type != NULL) {
577 g_string_append_printf(buf, "Content-Type: %s\r\n",
578 session->content_type);
579 if (session->input_func == NULL) {
580 session->more_data = FALSE;
581 length = session->length;
583 session->more_data = session->input_func(&body, &length,
585 if (session->more_data == FALSE)
586 g_string_append_printf(buf, "Content-Length: %zu\r\n",
589 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
592 if (session->web->close_connection == TRUE)
593 g_string_append(buf, "Connection: close\r\n");
595 g_string_append(buf, "\r\n");
597 if (session->content_type != NULL && length > 0) {
598 if (session->more_data == TRUE) {
599 g_string_append_printf(buf, "%zx\r\n", length);
600 g_string_append_len(buf, (char *) body, length);
601 g_string_append(buf, "\r\n");
602 } else if (session->fd == -1)
603 g_string_append_len(buf, (char *) body, length);
607 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
610 struct web_session *session = user_data;
612 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
613 session->send_watch = 0;
617 if (process_send_buffer(session) == TRUE)
620 if (process_send_file(session) == TRUE)
623 if (session->request_started == FALSE) {
624 session->request_started = TRUE;
625 start_request(session);
626 } else if (session->more_data == TRUE)
627 process_next_chunk(session);
629 process_send_buffer(session);
631 if (session->body_done == TRUE) {
632 session->send_watch = 0;
639 static int decode_chunked(struct web_session *session,
640 const guint8 *buf, gsize len)
642 const guint8 *ptr = buf;
650 switch (session->chunck_state) {
652 pos = memchr(ptr, '\n', len);
654 g_string_append_len(session->current_header,
660 if (count < 1 || ptr[count - 1] != '\r')
663 g_string_append_len(session->current_header,
664 (gchar *) ptr, count);
669 str = session->current_header->str;
671 counter = strtoul(str, NULL, 16);
672 if ((counter == 0 && errno == EINVAL) ||
673 counter == ULONG_MAX)
676 session->chunk_size = counter;
677 session->chunk_left = counter;
679 session->chunck_state = CHUNK_DATA;
686 session->chunck_state = CHUNK_N_BODY;
693 session->chunck_state = CHUNK_SIZE;
696 if (session->chunk_size == 0) {
697 debug(session->web, "Download Done in chunk");
698 g_string_truncate(session->current_header, 0);
702 if (session->chunk_left <= len) {
703 session->result.buffer = ptr;
704 session->result.length = session->chunk_left;
705 call_result_func(session, 0);
707 len -= session->chunk_left;
708 ptr += session->chunk_left;
710 session->total_len += session->chunk_left;
711 session->chunk_left = 0;
713 g_string_truncate(session->current_header, 0);
714 session->chunck_state = CHUNK_R_BODY;
718 session->result.buffer = ptr;
719 session->result.length = len;
720 call_result_func(session, 0);
722 session->chunk_left -= len;
723 session->total_len += len;
734 static int handle_body(struct web_session *session,
735 const guint8 *buf, gsize len)
739 debug(session->web, "[body] length %zu", len);
741 if (session->result.use_chunk == FALSE) {
743 session->result.buffer = buf;
744 session->result.length = len;
745 call_result_func(session, 0);
750 err = decode_chunked(session, buf, len);
752 debug(session->web, "Error in chunk decode %d", err);
754 session->result.buffer = NULL;
755 session->result.length = 0;
756 call_result_func(session, 400);
762 static void handle_multi_line(struct web_session *session)
768 str = session->current_header->str;
770 if (str[0] != ' ' && str[0] != '\t')
773 while (str[0] == ' ' || str[0] == '\t')
776 count = str - session->current_header->str;
778 g_string_erase(session->current_header, 0, count);
779 g_string_insert_c(session->current_header, 0, ' ');
782 value = g_hash_table_lookup(session->result.headers,
783 session->result.last_key);
785 g_string_insert(session->current_header, 0, value);
787 str = session->current_header->str;
789 g_hash_table_replace(session->result.headers,
790 g_strdup(session->result.last_key),
795 static void add_header_field(struct web_session *session)
803 str = session->current_header->str;
805 pos = memchr(str, ':', session->current_header->len);
812 /* remove preceding white spaces */
816 count = (char *) pos - str;
818 g_string_erase(session->current_header, 0, count);
820 value = g_hash_table_lookup(session->result.headers, key);
822 g_string_insert_c(session->current_header, 0, ' ');
823 g_string_insert_c(session->current_header, 0, ';');
825 g_string_insert(session->current_header, 0, value);
828 str = session->current_header->str;
829 g_hash_table_replace(session->result.headers, key,
832 g_free(session->result.last_key);
833 session->result.last_key = g_strdup(key);
837 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
840 struct web_session *session = user_data;
841 guint8 *ptr = session->receive_buffer;
845 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
846 session->transport_watch = 0;
847 session->result.buffer = NULL;
848 session->result.length = 0;
849 call_result_func(session, 400);
853 status = g_io_channel_read_chars(channel,
854 (gchar *) session->receive_buffer,
855 session->receive_space - 1, &bytes_read, NULL);
857 debug(session->web, "bytes read %zu", bytes_read);
859 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
860 session->transport_watch = 0;
861 session->result.buffer = NULL;
862 session->result.length = 0;
863 call_result_func(session, 0);
867 session->receive_buffer[bytes_read] = '\0';
869 if (session->header_done == TRUE) {
870 if (handle_body(session, session->receive_buffer,
872 session->transport_watch = 0;
878 while (bytes_read > 0) {
883 pos = memchr(ptr, '\n', bytes_read);
885 g_string_append_len(session->current_header,
886 (gchar *) ptr, bytes_read);
891 count = strlen((char *) ptr);
892 if (count > 0 && ptr[count - 1] == '\r') {
897 g_string_append_len(session->current_header,
898 (gchar *) ptr, count);
900 bytes_read -= count + 1;
906 if (session->current_header->len == 0) {
909 session->header_done = TRUE;
911 val = g_hash_table_lookup(session->result.headers,
912 "Transfer-Encoding");
914 val = g_strrstr(val, "chunked");
916 session->result.use_chunk = TRUE;
918 session->chunck_state = CHUNK_SIZE;
919 session->chunk_left = 0;
920 session->total_len = 0;
924 if (handle_body(session, ptr, bytes_read) < 0) {
925 session->transport_watch = 0;
931 str = session->current_header->str;
933 if (session->result.status == 0) {
936 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
937 session->result.status = code;
940 debug(session->web, "[header] %s", str);
942 /* handle multi-line header */
943 if (str[0] == ' ' || str[0] == '\t')
944 handle_multi_line(session);
946 add_header_field(session);
948 g_string_truncate(session->current_header, 0);
954 static int connect_session_transport(struct web_session *session)
959 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
964 if (session->web->index > 0) {
965 char interface[IF_NAMESIZE];
967 memset(interface, 0, IF_NAMESIZE);
969 if (if_indextoname(session->web->index, interface) != NULL) {
970 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
971 interface, IF_NAMESIZE) < 0) {
976 debug(session->web, "Use interface %s", interface);
980 if (session->flags & SESSION_FLAG_USE_TLS) {
981 debug(session->web, "using TLS encryption");
982 session->transport_channel = g_io_channel_gnutls_new(sk);
984 debug(session->web, "no encryption");
985 session->transport_channel = g_io_channel_unix_new(sk);
988 if (session->transport_channel == NULL) {
993 flags = g_io_channel_get_flags(session->transport_channel);
994 g_io_channel_set_flags(session->transport_channel,
995 flags | G_IO_FLAG_NONBLOCK, NULL);
997 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
998 g_io_channel_set_buffered(session->transport_channel, FALSE);
1000 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1002 if (connect(sk, session->addr->ai_addr,
1003 session->addr->ai_addrlen) < 0) {
1004 if (errno != EINPROGRESS) {
1010 session->transport_watch = g_io_add_watch(session->transport_channel,
1011 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1012 received_data, session);
1014 session->send_watch = g_io_add_watch(session->transport_channel,
1015 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1016 send_data, session);
1021 static int create_transport(struct web_session *session)
1025 err = connect_session_transport(session);
1029 debug(session->web, "creating session %s:%u",
1030 session->address, session->port);
1035 static int parse_url(struct web_session *session,
1036 const char *url, const char *proxy)
1038 char *scheme, *host, *port, *path;
1040 scheme = g_strdup(url);
1044 host = strstr(scheme, "://");
1049 if (strcasecmp(scheme, "https") == 0) {
1050 session->port = 443;
1051 session->flags |= SESSION_FLAG_USE_TLS;
1052 } else if (strcasecmp(scheme, "http") == 0) {
1063 path = strchr(host, '/');
1068 session->request = g_strdup_printf("/%s", path ? path : "");
1070 session->request = g_strdup(url);
1072 port = strrchr(host, ':');
1075 int tmp = strtol(port + 1, &end, 10);
1079 session->port = tmp;
1083 session->host = g_strdup(host);
1085 session->host = g_strdup_printf("%s:%u", host, tmp);
1087 session->host = g_strdup(host);
1094 scheme = g_strdup(proxy);
1098 host = strstr(proxy, "://");
1103 if (strcasecmp(scheme, "http") != 0) {
1110 path = strchr(host, '/');
1114 port = strrchr(host, ':');
1117 int tmp = strtol(port + 1, &end, 10);
1121 session->port = tmp;
1125 session->address = g_strdup(host);
1132 static void resolv_result(GResolvResultStatus status,
1133 char **results, gpointer user_data)
1135 struct web_session *session = user_data;
1136 struct addrinfo hints;
1140 if (results == NULL || results[0] == NULL) {
1141 call_result_func(session, 404);
1145 debug(session->web, "address %s", results[0]);
1147 memset(&hints, 0, sizeof(struct addrinfo));
1148 hints.ai_flags = AI_NUMERICHOST;
1149 hints.ai_family = session->web->family;
1151 if (session->addr != NULL) {
1152 freeaddrinfo(session->addr);
1153 session->addr = NULL;
1156 port = g_strdup_printf("%u", session->port);
1157 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1159 if (ret != 0 || session->addr == NULL) {
1160 call_result_func(session, 400);
1164 session->address = g_strdup(results[0]);
1166 if (create_transport(session) < 0) {
1167 call_result_func(session, 409);
1172 static guint do_request(GWeb *web, const char *url,
1173 const char *type, GWebInputFunc input,
1174 int fd, gsize length, GWebResultFunc func,
1177 struct web_session *session;
1179 if (web == NULL || url == NULL)
1182 debug(web, "request %s", url);
1184 session = g_try_new0(struct web_session, 1);
1185 if (session == NULL)
1188 if (parse_url(session, url, web->proxy) < 0) {
1189 free_session(session);
1193 debug(web, "address %s", session->address);
1194 debug(web, "port %u", session->port);
1195 debug(web, "host %s", session->host);
1196 debug(web, "flags %lu", session->flags);
1197 debug(web, "request %s", session->request);
1200 session->content_type = g_strdup(type);
1202 debug(web, "content-type %s", session->content_type);
1207 session->result_func = func;
1208 session->input_func = input;
1210 session->length = length;
1211 session->offset = 0;
1212 session->user_data = user_data;
1214 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1215 if (session->receive_buffer == NULL) {
1216 free_session(session);
1220 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1222 if (session->result.headers == NULL) {
1223 free_session(session);
1227 session->receive_space = DEFAULT_BUFFER_SIZE;
1228 session->send_buffer = g_string_sized_new(0);
1229 session->current_header = g_string_sized_new(0);
1230 session->header_done = FALSE;
1231 session->body_done = FALSE;
1233 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1234 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1235 session->host, resolv_result, session);
1236 if (session->resolv_action == 0) {
1237 free_session(session);
1241 struct addrinfo hints;
1245 if (session->address == NULL)
1246 session->address = g_strdup(session->host);
1248 memset(&hints, 0, sizeof(struct addrinfo));
1249 hints.ai_flags = AI_NUMERICHOST;
1250 hints.ai_family = session->web->family;
1252 if (session->addr != NULL) {
1253 freeaddrinfo(session->addr);
1254 session->addr = NULL;
1257 port = g_strdup_printf("%u", session->port);
1258 ret = getaddrinfo(session->address, port, &hints,
1261 if (ret != 0 || session->addr == NULL) {
1262 free_session(session);
1266 if (create_transport(session) < 0) {
1267 free_session(session);
1272 web->session_list = g_list_append(web->session_list, session);
1274 return web->next_query_id++;
1277 guint g_web_request_get(GWeb *web, const char *url,
1278 GWebResultFunc func, gpointer user_data)
1280 return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
1283 guint g_web_request_post(GWeb *web, const char *url,
1284 const char *type, GWebInputFunc input,
1285 GWebResultFunc func, gpointer user_data)
1287 return do_request(web, url, type, input, -1, 0, func, user_data);
1290 guint g_web_request_post_file(GWeb *web, const char *url,
1291 const char *type, const char *file,
1292 GWebResultFunc func, gpointer user_data)
1298 if (stat(file, &st) < 0)
1301 fd = open(file, O_RDONLY);
1305 ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
1312 gboolean g_web_cancel_request(GWeb *web, guint id)
1320 guint16 g_web_result_get_status(GWebResult *result)
1325 return result->status;
1328 gboolean g_web_result_get_chunk(GWebResult *result,
1329 const guint8 **chunk, gsize *length)
1337 *chunk = result->buffer;
1340 *length = result->length;
1345 gboolean g_web_result_get_header(GWebResult *result,
1346 const char *header, const char **value)
1354 *value = g_hash_table_lookup(result->headers, header);
1362 struct _GWebParser {
1366 const char *token_str;
1371 GWebParserFunc func;
1375 GWebParser *g_web_parser_new(const char *begin, const char *end,
1376 GWebParserFunc func, gpointer user_data)
1380 parser = g_try_new0(GWebParser, 1);
1384 parser->ref_count = 1;
1386 parser->begin_token = g_strdup(begin);
1387 parser->end_token = g_strdup(end);
1389 if (parser->begin_token == NULL) {
1394 parser->func = func;
1395 parser->user_data = user_data;
1397 parser->token_str = parser->begin_token;
1398 parser->token_len = strlen(parser->token_str);
1399 parser->token_pos = 0;
1401 parser->intoken = FALSE;
1402 parser->content = g_string_sized_new(0);
1407 GWebParser *g_web_parser_ref(GWebParser *parser)
1412 __sync_fetch_and_add(&parser->ref_count, 1);
1417 void g_web_parser_unref(GWebParser *parser)
1422 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1425 g_string_free(parser->content, TRUE);
1427 g_free(parser->begin_token);
1428 g_free(parser->end_token);
1432 void g_web_parser_feed_data(GWebParser *parser,
1433 const guint8 *data, gsize length)
1435 const guint8 *ptr = data;
1440 while (length > 0) {
1441 guint8 chr = parser->token_str[parser->token_pos];
1443 if (parser->token_pos == 0) {
1446 pos = memchr(ptr, chr, length);
1448 if (parser->intoken == TRUE)
1449 g_string_append_len(parser->content,
1450 (gchar *) ptr, length);
1454 if (parser->intoken == TRUE)
1455 g_string_append_len(parser->content,
1456 (gchar *) ptr, (pos - ptr) + 1);
1458 length -= (pos - ptr) + 1;
1461 parser->token_pos++;
1465 if (parser->intoken == TRUE)
1466 g_string_append_c(parser->content, ptr[0]);
1468 if (ptr[0] != chr) {
1472 parser->token_pos = 0;
1479 parser->token_pos++;
1481 if (parser->token_pos == parser->token_len) {
1482 if (parser->intoken == FALSE) {
1483 g_string_append(parser->content,
1486 parser->intoken = TRUE;
1487 parser->token_str = parser->end_token;
1488 parser->token_len = strlen(parser->end_token);
1489 parser->token_pos = 0;
1492 str = g_string_free(parser->content, FALSE);
1493 parser->content = g_string_sized_new(0);
1495 parser->func(str, parser->user_data);
1498 parser->intoken = FALSE;
1499 parser->token_str = parser->begin_token;
1500 parser->token_len = strlen(parser->begin_token);
1501 parser->token_pos = 0;
1507 void g_web_parser_end_data(GWebParser *parser)