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 if (session->request_started == FALSE || session->more_data == TRUE)
491 sk = g_io_channel_unix_get_fd(session->transport_channel);
495 offset = session->offset;
497 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
499 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
500 errno, session->length, bytes_sent);
502 if (bytes_sent < 0 && errno != EAGAIN)
505 session->offset = offset;
506 session->length -= bytes_sent;
508 if (session->length == 0) {
509 session->body_done = TRUE;
516 static void process_next_chunk(struct web_session *session)
518 GString *buf = session->send_buffer;
522 if (session->input_func == NULL) {
523 session->more_data = FALSE;
527 session->more_data = session->input_func(&body, &length,
531 g_string_append_printf(buf, "%zx\r\n", length);
532 g_string_append_len(buf, (char *) body, length);
533 g_string_append(buf, "\r\n");
536 if (session->more_data == FALSE)
537 g_string_append(buf, "0\r\n\r\n");
540 static void start_request(struct web_session *session)
542 GString *buf = session->send_buffer;
547 debug(session->web, "request %s from %s",
548 session->request, session->host);
550 g_string_truncate(buf, 0);
552 if (session->web->http_version == NULL)
555 version = session->web->http_version;
557 if (session->content_type == NULL)
558 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
559 session->request, version);
561 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
562 session->request, version);
564 g_string_append_printf(buf, "Host: %s\r\n", session->host);
566 if (session->web->user_agent != NULL)
567 g_string_append_printf(buf, "User-Agent: %s\r\n",
568 session->web->user_agent);
570 if (session->web->user_agent_profile != NULL) {
571 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
572 session->web->user_agent_profile);
575 if (session->web->accept_option != NULL)
576 g_string_append_printf(buf, "Accept: %s\r\n",
577 session->web->accept_option);
579 if (session->content_type != NULL) {
580 g_string_append_printf(buf, "Content-Type: %s\r\n",
581 session->content_type);
582 if (session->input_func == NULL) {
583 session->more_data = FALSE;
584 length = session->length;
586 session->more_data = session->input_func(&body, &length,
588 if (session->more_data == FALSE)
589 g_string_append_printf(buf, "Content-Length: %zu\r\n",
592 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
595 if (session->web->close_connection == TRUE)
596 g_string_append(buf, "Connection: close\r\n");
598 g_string_append(buf, "\r\n");
600 if (session->content_type != NULL && length > 0) {
601 if (session->more_data == TRUE) {
602 g_string_append_printf(buf, "%zx\r\n", length);
603 g_string_append_len(buf, (char *) body, length);
604 g_string_append(buf, "\r\n");
605 } else if (session->fd == -1)
606 g_string_append_len(buf, (char *) body, length);
610 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
613 struct web_session *session = user_data;
615 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
616 session->send_watch = 0;
620 if (process_send_buffer(session) == TRUE)
623 if (process_send_file(session) == TRUE)
626 if (session->request_started == FALSE) {
627 session->request_started = TRUE;
628 start_request(session);
629 } else if (session->more_data == TRUE)
630 process_next_chunk(session);
632 process_send_buffer(session);
634 if (session->body_done == TRUE) {
635 session->send_watch = 0;
642 static int decode_chunked(struct web_session *session,
643 const guint8 *buf, gsize len)
645 const guint8 *ptr = buf;
653 switch (session->chunck_state) {
655 pos = memchr(ptr, '\n', len);
657 g_string_append_len(session->current_header,
663 if (count < 1 || ptr[count - 1] != '\r')
666 g_string_append_len(session->current_header,
667 (gchar *) ptr, count);
672 str = session->current_header->str;
674 counter = strtoul(str, NULL, 16);
675 if ((counter == 0 && errno == EINVAL) ||
676 counter == ULONG_MAX)
679 session->chunk_size = counter;
680 session->chunk_left = counter;
682 session->chunck_state = CHUNK_DATA;
689 session->chunck_state = CHUNK_N_BODY;
696 session->chunck_state = CHUNK_SIZE;
699 if (session->chunk_size == 0) {
700 debug(session->web, "Download Done in chunk");
701 g_string_truncate(session->current_header, 0);
705 if (session->chunk_left <= len) {
706 session->result.buffer = ptr;
707 session->result.length = session->chunk_left;
708 call_result_func(session, 0);
710 len -= session->chunk_left;
711 ptr += session->chunk_left;
713 session->total_len += session->chunk_left;
714 session->chunk_left = 0;
716 g_string_truncate(session->current_header, 0);
717 session->chunck_state = CHUNK_R_BODY;
721 session->result.buffer = ptr;
722 session->result.length = len;
723 call_result_func(session, 0);
725 session->chunk_left -= len;
726 session->total_len += len;
737 static int handle_body(struct web_session *session,
738 const guint8 *buf, gsize len)
742 debug(session->web, "[body] length %zu", len);
744 if (session->result.use_chunk == FALSE) {
746 session->result.buffer = buf;
747 session->result.length = len;
748 call_result_func(session, 0);
753 err = decode_chunked(session, buf, len);
755 debug(session->web, "Error in chunk decode %d", err);
757 session->result.buffer = NULL;
758 session->result.length = 0;
759 call_result_func(session, 400);
765 static void handle_multi_line(struct web_session *session)
771 str = session->current_header->str;
773 if (str[0] != ' ' && str[0] != '\t')
776 while (str[0] == ' ' || str[0] == '\t')
779 count = str - session->current_header->str;
781 g_string_erase(session->current_header, 0, count);
782 g_string_insert_c(session->current_header, 0, ' ');
785 value = g_hash_table_lookup(session->result.headers,
786 session->result.last_key);
788 g_string_insert(session->current_header, 0, value);
790 str = session->current_header->str;
792 g_hash_table_replace(session->result.headers,
793 g_strdup(session->result.last_key),
798 static void add_header_field(struct web_session *session)
806 str = session->current_header->str;
808 pos = memchr(str, ':', session->current_header->len);
815 /* remove preceding white spaces */
819 count = (char *) pos - str;
821 g_string_erase(session->current_header, 0, count);
823 value = g_hash_table_lookup(session->result.headers, key);
825 g_string_insert_c(session->current_header, 0, ' ');
826 g_string_insert_c(session->current_header, 0, ';');
828 g_string_insert(session->current_header, 0, value);
831 str = session->current_header->str;
832 g_hash_table_replace(session->result.headers, key,
835 g_free(session->result.last_key);
836 session->result.last_key = g_strdup(key);
840 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
843 struct web_session *session = user_data;
844 guint8 *ptr = session->receive_buffer;
848 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
849 session->transport_watch = 0;
850 session->result.buffer = NULL;
851 session->result.length = 0;
852 call_result_func(session, 400);
856 status = g_io_channel_read_chars(channel,
857 (gchar *) session->receive_buffer,
858 session->receive_space - 1, &bytes_read, NULL);
860 debug(session->web, "bytes read %zu", bytes_read);
862 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
863 session->transport_watch = 0;
864 session->result.buffer = NULL;
865 session->result.length = 0;
866 call_result_func(session, 0);
870 session->receive_buffer[bytes_read] = '\0';
872 if (session->header_done == TRUE) {
873 if (handle_body(session, session->receive_buffer,
875 session->transport_watch = 0;
881 while (bytes_read > 0) {
886 pos = memchr(ptr, '\n', bytes_read);
888 g_string_append_len(session->current_header,
889 (gchar *) ptr, bytes_read);
894 count = strlen((char *) ptr);
895 if (count > 0 && ptr[count - 1] == '\r') {
900 g_string_append_len(session->current_header,
901 (gchar *) ptr, count);
903 bytes_read -= count + 1;
909 if (session->current_header->len == 0) {
912 session->header_done = TRUE;
914 val = g_hash_table_lookup(session->result.headers,
915 "Transfer-Encoding");
917 val = g_strrstr(val, "chunked");
919 session->result.use_chunk = TRUE;
921 session->chunck_state = CHUNK_SIZE;
922 session->chunk_left = 0;
923 session->total_len = 0;
927 if (handle_body(session, ptr, bytes_read) < 0) {
928 session->transport_watch = 0;
934 str = session->current_header->str;
936 if (session->result.status == 0) {
939 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
940 session->result.status = code;
943 debug(session->web, "[header] %s", str);
945 /* handle multi-line header */
946 if (str[0] == ' ' || str[0] == '\t')
947 handle_multi_line(session);
949 add_header_field(session);
951 g_string_truncate(session->current_header, 0);
957 static int connect_session_transport(struct web_session *session)
962 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
967 if (session->web->index > 0) {
968 char interface[IF_NAMESIZE];
970 memset(interface, 0, IF_NAMESIZE);
972 if (if_indextoname(session->web->index, interface) != NULL) {
973 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
974 interface, IF_NAMESIZE) < 0) {
979 debug(session->web, "Use interface %s", interface);
983 if (session->flags & SESSION_FLAG_USE_TLS) {
984 debug(session->web, "using TLS encryption");
985 session->transport_channel = g_io_channel_gnutls_new(sk);
987 debug(session->web, "no encryption");
988 session->transport_channel = g_io_channel_unix_new(sk);
991 if (session->transport_channel == NULL) {
996 flags = g_io_channel_get_flags(session->transport_channel);
997 g_io_channel_set_flags(session->transport_channel,
998 flags | G_IO_FLAG_NONBLOCK, NULL);
1000 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
1001 g_io_channel_set_buffered(session->transport_channel, FALSE);
1003 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1005 if (connect(sk, session->addr->ai_addr,
1006 session->addr->ai_addrlen) < 0) {
1007 if (errno != EINPROGRESS) {
1013 session->transport_watch = g_io_add_watch(session->transport_channel,
1014 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1015 received_data, session);
1017 session->send_watch = g_io_add_watch(session->transport_channel,
1018 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1019 send_data, session);
1024 static int create_transport(struct web_session *session)
1028 err = connect_session_transport(session);
1032 debug(session->web, "creating session %s:%u",
1033 session->address, session->port);
1038 static int parse_url(struct web_session *session,
1039 const char *url, const char *proxy)
1041 char *scheme, *host, *port, *path;
1043 scheme = g_strdup(url);
1047 host = strstr(scheme, "://");
1052 if (strcasecmp(scheme, "https") == 0) {
1053 session->port = 443;
1054 session->flags |= SESSION_FLAG_USE_TLS;
1055 } else if (strcasecmp(scheme, "http") == 0) {
1066 path = strchr(host, '/');
1071 session->request = g_strdup_printf("/%s", path ? path : "");
1073 session->request = g_strdup(url);
1075 port = strrchr(host, ':');
1078 int tmp = strtol(port + 1, &end, 10);
1082 session->port = tmp;
1086 session->host = g_strdup(host);
1088 session->host = g_strdup_printf("%s:%u", host, tmp);
1090 session->host = g_strdup(host);
1097 scheme = g_strdup(proxy);
1101 host = strstr(proxy, "://");
1106 if (strcasecmp(scheme, "http") != 0) {
1113 path = strchr(host, '/');
1117 port = strrchr(host, ':');
1120 int tmp = strtol(port + 1, &end, 10);
1124 session->port = tmp;
1128 session->address = g_strdup(host);
1135 static void resolv_result(GResolvResultStatus status,
1136 char **results, gpointer user_data)
1138 struct web_session *session = user_data;
1139 struct addrinfo hints;
1143 if (results == NULL || results[0] == NULL) {
1144 call_result_func(session, 404);
1148 debug(session->web, "address %s", results[0]);
1150 memset(&hints, 0, sizeof(struct addrinfo));
1151 hints.ai_flags = AI_NUMERICHOST;
1152 hints.ai_family = session->web->family;
1154 if (session->addr != NULL) {
1155 freeaddrinfo(session->addr);
1156 session->addr = NULL;
1159 port = g_strdup_printf("%u", session->port);
1160 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1162 if (ret != 0 || session->addr == NULL) {
1163 call_result_func(session, 400);
1167 session->address = g_strdup(results[0]);
1169 if (create_transport(session) < 0) {
1170 call_result_func(session, 409);
1175 static guint do_request(GWeb *web, const char *url,
1176 const char *type, GWebInputFunc input,
1177 int fd, gsize length, GWebResultFunc func,
1180 struct web_session *session;
1182 if (web == NULL || url == NULL)
1185 debug(web, "request %s", url);
1187 session = g_try_new0(struct web_session, 1);
1188 if (session == NULL)
1191 if (parse_url(session, url, web->proxy) < 0) {
1192 free_session(session);
1196 debug(web, "address %s", session->address);
1197 debug(web, "port %u", session->port);
1198 debug(web, "host %s", session->host);
1199 debug(web, "flags %lu", session->flags);
1200 debug(web, "request %s", session->request);
1203 session->content_type = g_strdup(type);
1205 debug(web, "content-type %s", session->content_type);
1210 session->result_func = func;
1211 session->input_func = input;
1213 session->length = length;
1214 session->offset = 0;
1215 session->user_data = user_data;
1217 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1218 if (session->receive_buffer == NULL) {
1219 free_session(session);
1223 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1225 if (session->result.headers == NULL) {
1226 free_session(session);
1230 session->receive_space = DEFAULT_BUFFER_SIZE;
1231 session->send_buffer = g_string_sized_new(0);
1232 session->current_header = g_string_sized_new(0);
1233 session->header_done = FALSE;
1234 session->body_done = FALSE;
1236 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1237 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1238 session->host, resolv_result, session);
1239 if (session->resolv_action == 0) {
1240 free_session(session);
1244 struct addrinfo hints;
1248 if (session->address == NULL)
1249 session->address = g_strdup(session->host);
1251 memset(&hints, 0, sizeof(struct addrinfo));
1252 hints.ai_flags = AI_NUMERICHOST;
1253 hints.ai_family = session->web->family;
1255 if (session->addr != NULL) {
1256 freeaddrinfo(session->addr);
1257 session->addr = NULL;
1260 port = g_strdup_printf("%u", session->port);
1261 ret = getaddrinfo(session->address, port, &hints,
1264 if (ret != 0 || session->addr == NULL) {
1265 free_session(session);
1269 if (create_transport(session) < 0) {
1270 free_session(session);
1275 web->session_list = g_list_append(web->session_list, session);
1277 return web->next_query_id++;
1280 guint g_web_request_get(GWeb *web, const char *url,
1281 GWebResultFunc func, gpointer user_data)
1283 return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
1286 guint g_web_request_post(GWeb *web, const char *url,
1287 const char *type, GWebInputFunc input,
1288 GWebResultFunc func, gpointer user_data)
1290 return do_request(web, url, type, input, -1, 0, func, user_data);
1293 guint g_web_request_post_file(GWeb *web, const char *url,
1294 const char *type, const char *file,
1295 GWebResultFunc func, gpointer user_data)
1301 if (stat(file, &st) < 0)
1304 fd = open(file, O_RDONLY);
1308 ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
1315 gboolean g_web_cancel_request(GWeb *web, guint id)
1323 guint16 g_web_result_get_status(GWebResult *result)
1328 return result->status;
1331 gboolean g_web_result_get_chunk(GWebResult *result,
1332 const guint8 **chunk, gsize *length)
1340 *chunk = result->buffer;
1343 *length = result->length;
1348 gboolean g_web_result_get_header(GWebResult *result,
1349 const char *header, const char **value)
1357 *value = g_hash_table_lookup(result->headers, header);
1365 struct _GWebParser {
1369 const char *token_str;
1374 GWebParserFunc func;
1378 GWebParser *g_web_parser_new(const char *begin, const char *end,
1379 GWebParserFunc func, gpointer user_data)
1383 parser = g_try_new0(GWebParser, 1);
1387 parser->ref_count = 1;
1389 parser->begin_token = g_strdup(begin);
1390 parser->end_token = g_strdup(end);
1392 if (parser->begin_token == NULL) {
1397 parser->func = func;
1398 parser->user_data = user_data;
1400 parser->token_str = parser->begin_token;
1401 parser->token_len = strlen(parser->token_str);
1402 parser->token_pos = 0;
1404 parser->intoken = FALSE;
1405 parser->content = g_string_sized_new(0);
1410 GWebParser *g_web_parser_ref(GWebParser *parser)
1415 __sync_fetch_and_add(&parser->ref_count, 1);
1420 void g_web_parser_unref(GWebParser *parser)
1425 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1428 g_string_free(parser->content, TRUE);
1430 g_free(parser->begin_token);
1431 g_free(parser->end_token);
1435 void g_web_parser_feed_data(GWebParser *parser,
1436 const guint8 *data, gsize length)
1438 const guint8 *ptr = data;
1443 while (length > 0) {
1444 guint8 chr = parser->token_str[parser->token_pos];
1446 if (parser->token_pos == 0) {
1449 pos = memchr(ptr, chr, length);
1451 if (parser->intoken == TRUE)
1452 g_string_append_len(parser->content,
1453 (gchar *) ptr, length);
1457 if (parser->intoken == TRUE)
1458 g_string_append_len(parser->content,
1459 (gchar *) ptr, (pos - ptr) + 1);
1461 length -= (pos - ptr) + 1;
1464 parser->token_pos++;
1468 if (parser->intoken == TRUE)
1469 g_string_append_c(parser->content, ptr[0]);
1471 if (ptr[0] != chr) {
1475 parser->token_pos = 0;
1482 parser->token_pos++;
1484 if (parser->token_pos == parser->token_len) {
1485 if (parser->intoken == FALSE) {
1486 g_string_append(parser->content,
1489 parser->intoken = TRUE;
1490 parser->token_str = parser->end_token;
1491 parser->token_len = strlen(parser->end_token);
1492 parser->token_pos = 0;
1495 str = g_string_free(parser->content, FALSE);
1496 parser->content = g_string_sized_new(0);
1498 parser->func(str, parser->user_data);
1501 parser->intoken = FALSE;
1502 parser->token_str = parser->begin_token;
1503 parser->token_len = strlen(parser->begin_token);
1504 parser->token_pos = 0;
1510 void g_web_parser_end_data(GWebParser *parser)