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>
37 #include "giognutls.h"
41 #define DEFAULT_BUFFER_SIZE 2048
43 #define SESSION_FLAG_USE_TLS (1 << 0)
68 struct addrinfo *addr;
72 GIOChannel *transport_channel;
73 guint transport_watch;
79 guint8 *receive_buffer;
82 GString *current_header;
86 gboolean request_started;
88 enum chunk_state chunck_state;
95 GWebResultFunc result_func;
96 GWebInputFunc input_func;
114 char *user_agent_profile;
116 gboolean close_connection;
118 GWebDebugFunc debug_func;
122 static inline void debug(GWeb *web, const char *format, ...)
127 if (web->debug_func == NULL)
130 va_start(ap, format);
132 if (vsnprintf(str, sizeof(str), format, ap) > 0)
133 web->debug_func(str, web->debug_data);
138 static void free_session(struct web_session *session)
140 GWeb *web = session->web;
145 g_free(session->request);
147 if (session->resolv_action > 0)
148 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
150 if (session->transport_watch > 0)
151 g_source_remove(session->transport_watch);
153 if (session->send_watch > 0)
154 g_source_remove(session->send_watch);
156 if (session->transport_channel != NULL)
157 g_io_channel_unref(session->transport_channel);
159 g_free(session->result.last_key);
161 if (session->result.headers != NULL)
162 g_hash_table_destroy(session->result.headers);
164 if (session->send_buffer != NULL)
165 g_string_free(session->send_buffer, TRUE);
167 if (session->current_header != NULL)
168 g_string_free(session->current_header, TRUE);
170 g_free(session->receive_buffer);
172 g_free(session->content_type);
174 g_free(session->host);
175 g_free(session->address);
176 if (session->addr != NULL)
177 freeaddrinfo(session->addr);
182 static void flush_sessions(GWeb *web)
186 for (list = g_list_first(web->session_list);
187 list; list = g_list_next(list))
188 free_session(list->data);
190 g_list_free(web->session_list);
191 web->session_list = NULL;
194 GWeb *g_web_new(int index)
201 web = g_try_new0(GWeb, 1);
207 web->next_query_id = 1;
209 web->family = AF_UNSPEC;
212 web->session_list = NULL;
214 web->resolv = g_resolv_new(index);
215 if (web->resolv == NULL) {
220 web->accept_option = g_strdup("*/*");
221 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
222 web->close_connection = FALSE;
227 GWeb *g_web_ref(GWeb *web)
232 __sync_fetch_and_add(&web->ref_count, 1);
237 void g_web_unref(GWeb *web)
242 if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
247 g_resolv_unref(web->resolv);
251 g_free(web->accept_option);
252 g_free(web->user_agent);
253 g_free(web->user_agent_profile);
254 g_free(web->http_version);
259 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
264 web->debug_func = func;
265 web->debug_data = user_data;
267 g_resolv_set_debug(web->resolv, func, user_data);
270 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
279 debug(web, "clearing proxy");
281 web->proxy = g_strdup(proxy);
282 debug(web, "setting proxy %s", web->proxy);
288 gboolean g_web_set_address_family(GWeb *web, int family)
293 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
296 web->family = family;
298 g_resolv_set_address_family(web->resolv, family);
303 gboolean g_web_add_nameserver(GWeb *web, const char *address)
308 g_resolv_add_nameserver(web->resolv, address, 53, 0);
313 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
315 g_free(web->accept_option);
317 if (format == NULL) {
318 web->accept_option = NULL;
319 debug(web, "clearing accept option");
321 web->accept_option = g_strdup_vprintf(format, args);
322 debug(web, "setting accept %s", web->accept_option);
328 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
336 va_start(args, format);
337 result = set_accept_option(web, format, args);
343 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
345 g_free(web->user_agent);
347 if (format == NULL) {
348 web->user_agent = NULL;
349 debug(web, "clearing user agent");
351 web->user_agent = g_strdup_vprintf(format, args);
352 debug(web, "setting user agent %s", web->user_agent);
358 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
366 va_start(args, format);
367 result = set_user_agent(web, format, args);
373 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
378 g_free(web->user_agent_profile);
380 web->user_agent_profile = g_strdup(profile);
381 debug(web, "setting user agent profile %s", web->user_agent);
386 gboolean g_web_set_http_version(GWeb *web, const char *version)
391 g_free(web->http_version);
393 if (version == NULL) {
394 web->http_version = NULL;
395 debug(web, "clearing HTTP version");
397 web->http_version = g_strdup(version);
398 debug(web, "setting HTTP version %s", web->http_version);
404 void g_web_set_close_connection(GWeb *web, gboolean enabled)
409 web->close_connection = enabled;
412 gboolean g_web_get_close_connection(GWeb *web)
417 return web->close_connection;
420 static inline void call_result_func(struct web_session *session, guint16 status)
424 if (session->result_func == NULL)
428 session->result.status = status;
430 result = session->result_func(&session->result, session->user_data);
432 debug(session->web, "[result function] %s",
433 result == TRUE ? "continue" : "stop");
436 static gboolean process_send_buffer(struct web_session *session)
438 GString *buf = session->send_buffer;
439 gsize count, bytes_written;
445 if (session->request_started == TRUE &&
446 session->more_data == FALSE)
447 session->body_done = TRUE;
452 status = g_io_channel_write_chars(session->transport_channel,
453 buf->str, count, &bytes_written, NULL);
455 debug(session->web, "status %u bytes to write %zu bytes written %zu",
456 status, count, bytes_written);
458 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
461 g_string_erase(buf, 0, bytes_written);
466 static void process_next_chunk(struct web_session *session)
468 GString *buf = session->send_buffer;
472 if (session->input_func == NULL) {
473 session->more_data = FALSE;
477 session->more_data = session->input_func(&body, &length,
481 g_string_append_printf(buf, "%zx\r\n", length);
482 g_string_append_len(buf, (char *) body, length);
483 g_string_append(buf, "\r\n");
486 if (session->more_data == FALSE)
487 g_string_append(buf, "0\r\n\r\n");
490 static void start_request(struct web_session *session)
492 GString *buf = session->send_buffer;
497 debug(session->web, "request %s from %s",
498 session->request, session->host);
500 g_string_truncate(buf, 0);
502 if (session->web->http_version == NULL)
505 version = session->web->http_version;
507 if (session->content_type == NULL)
508 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
509 session->request, version);
511 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
512 session->request, version);
514 g_string_append_printf(buf, "Host: %s\r\n", session->host);
516 if (session->web->user_agent != NULL)
517 g_string_append_printf(buf, "User-Agent: %s\r\n",
518 session->web->user_agent);
520 if (session->web->user_agent_profile != NULL) {
521 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
522 session->web->user_agent_profile);
525 if (session->web->accept_option != NULL)
526 g_string_append_printf(buf, "Accept: %s\r\n",
527 session->web->accept_option);
529 if (session->content_type != NULL) {
530 g_string_append_printf(buf, "Content-Type: %s\r\n",
531 session->content_type);
532 if (session->input_func == NULL) {
533 session->more_data = FALSE;
536 session->more_data = session->input_func(&body, &length,
538 if (session->more_data == FALSE)
539 g_string_append_printf(buf, "Content-Length: %zu\r\n",
542 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
545 if (session->web->close_connection == TRUE)
546 g_string_append(buf, "Connection: close\r\n");
548 g_string_append(buf, "\r\n");
550 if (session->content_type != NULL && length > 0) {
551 if (session->more_data == TRUE) {
552 g_string_append_printf(buf, "%zx\r\n", length);
553 g_string_append_len(buf, (char *) body, length);
554 g_string_append(buf, "\r\n");
556 g_string_append_len(buf, (char *) body, length);
560 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
563 struct web_session *session = user_data;
565 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
566 session->send_watch = 0;
570 if (process_send_buffer(session) == TRUE)
573 if (session->request_started == FALSE) {
574 session->request_started = TRUE;
575 start_request(session);
576 } else if (session->more_data == TRUE)
577 process_next_chunk(session);
579 process_send_buffer(session);
581 if (session->body_done == TRUE) {
582 session->send_watch = 0;
589 static int decode_chunked(struct web_session *session,
590 const guint8 *buf, gsize len)
592 const guint8 *ptr = buf;
600 switch (session->chunck_state) {
602 pos = memchr(ptr, '\n', len);
604 g_string_append_len(session->current_header,
610 if (count < 1 || ptr[count - 1] != '\r')
613 g_string_append_len(session->current_header,
614 (gchar *) ptr, count);
619 str = session->current_header->str;
621 counter = strtoul(str, NULL, 16);
622 if ((counter == 0 && errno == EINVAL) ||
623 counter == ULONG_MAX)
626 session->chunk_size = counter;
627 session->chunk_left = counter;
629 session->chunck_state = CHUNK_DATA;
636 session->chunck_state = CHUNK_N_BODY;
643 session->chunck_state = CHUNK_SIZE;
646 if (session->chunk_size == 0) {
647 debug(session->web, "Download Done in chunk");
648 g_string_truncate(session->current_header, 0);
652 if (session->chunk_left <= len) {
653 session->result.buffer = ptr;
654 session->result.length = session->chunk_left;
655 call_result_func(session, 0);
657 len -= session->chunk_left;
658 ptr += session->chunk_left;
660 session->total_len += session->chunk_left;
661 session->chunk_left = 0;
663 g_string_truncate(session->current_header, 0);
664 session->chunck_state = CHUNK_R_BODY;
668 session->result.buffer = ptr;
669 session->result.length = len;
670 call_result_func(session, 0);
672 session->chunk_left -= len;
673 session->total_len += len;
684 static int handle_body(struct web_session *session,
685 const guint8 *buf, gsize len)
689 debug(session->web, "[body] length %zu", len);
691 if (session->result.use_chunk == FALSE) {
693 session->result.buffer = buf;
694 session->result.length = len;
695 call_result_func(session, 0);
700 err = decode_chunked(session, buf, len);
702 debug(session->web, "Error in chunk decode %d", err);
704 session->result.buffer = NULL;
705 session->result.length = 0;
706 call_result_func(session, 400);
712 static void handle_multi_line(struct web_session *session)
718 str = session->current_header->str;
720 if (str[0] != ' ' && str[0] != '\t')
723 while (str[0] == ' ' || str[0] == '\t')
726 count = str - session->current_header->str;
728 g_string_erase(session->current_header, 0, count);
729 g_string_insert_c(session->current_header, 0, ' ');
732 value = g_hash_table_lookup(session->result.headers,
733 session->result.last_key);
735 g_string_insert(session->current_header, 0, value);
737 str = session->current_header->str;
739 g_hash_table_replace(session->result.headers,
740 g_strdup(session->result.last_key),
745 static void add_header_field(struct web_session *session)
753 str = session->current_header->str;
755 pos = memchr(str, ':', session->current_header->len);
762 /* remove preceding white spaces */
766 count = (char *) pos - str;
768 g_string_erase(session->current_header, 0, count);
770 value = g_hash_table_lookup(session->result.headers, key);
772 g_string_insert_c(session->current_header, 0, ' ');
773 g_string_insert_c(session->current_header, 0, ';');
775 g_string_insert(session->current_header, 0, value);
778 str = session->current_header->str;
779 g_hash_table_replace(session->result.headers, key,
782 g_free(session->result.last_key);
783 session->result.last_key = g_strdup(key);
787 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
790 struct web_session *session = user_data;
791 guint8 *ptr = session->receive_buffer;
795 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
796 session->transport_watch = 0;
797 session->result.buffer = NULL;
798 session->result.length = 0;
799 call_result_func(session, 400);
803 status = g_io_channel_read_chars(channel,
804 (gchar *) session->receive_buffer,
805 session->receive_space - 1, &bytes_read, NULL);
807 debug(session->web, "bytes read %zu", bytes_read);
809 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
810 session->transport_watch = 0;
811 session->result.buffer = NULL;
812 session->result.length = 0;
813 call_result_func(session, 0);
817 session->receive_buffer[bytes_read] = '\0';
819 if (session->header_done == TRUE) {
820 if (handle_body(session, session->receive_buffer,
822 session->transport_watch = 0;
828 while (bytes_read > 0) {
833 pos = memchr(ptr, '\n', bytes_read);
835 g_string_append_len(session->current_header,
836 (gchar *) ptr, bytes_read);
841 count = strlen((char *) ptr);
842 if (count > 0 && ptr[count - 1] == '\r') {
847 g_string_append_len(session->current_header,
848 (gchar *) ptr, count);
850 bytes_read -= count + 1;
856 if (session->current_header->len == 0) {
859 session->header_done = TRUE;
861 val = g_hash_table_lookup(session->result.headers,
862 "Transfer-Encoding");
864 val = g_strrstr(val, "chunked");
866 session->result.use_chunk = TRUE;
868 session->chunck_state = CHUNK_SIZE;
869 session->chunk_left = 0;
870 session->total_len = 0;
874 if (handle_body(session, ptr, bytes_read) < 0) {
875 session->transport_watch = 0;
881 str = session->current_header->str;
883 if (session->result.status == 0) {
886 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
887 session->result.status = code;
890 debug(session->web, "[header] %s", str);
892 /* handle multi-line header */
893 if (str[0] == ' ' || str[0] == '\t')
894 handle_multi_line(session);
896 add_header_field(session);
898 g_string_truncate(session->current_header, 0);
904 static int connect_session_transport(struct web_session *session)
909 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
914 if (session->web->index > 0) {
915 char interface[IF_NAMESIZE];
917 memset(interface, 0, IF_NAMESIZE);
919 if (if_indextoname(session->web->index, interface) != NULL) {
920 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
921 interface, IF_NAMESIZE) < 0) {
926 debug(session->web, "Use interface %s", interface);
930 if (session->flags & SESSION_FLAG_USE_TLS) {
931 debug(session->web, "using TLS encryption");
932 session->transport_channel = g_io_channel_gnutls_new(sk);
934 debug(session->web, "no encryption");
935 session->transport_channel = g_io_channel_unix_new(sk);
938 if (session->transport_channel == NULL) {
943 flags = g_io_channel_get_flags(session->transport_channel);
944 g_io_channel_set_flags(session->transport_channel,
945 flags | G_IO_FLAG_NONBLOCK, NULL);
947 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
948 g_io_channel_set_buffered(session->transport_channel, FALSE);
950 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
952 if (connect(sk, session->addr->ai_addr,
953 session->addr->ai_addrlen) < 0) {
954 if (errno != EINPROGRESS) {
960 session->transport_watch = g_io_add_watch(session->transport_channel,
961 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
962 received_data, session);
964 session->send_watch = g_io_add_watch(session->transport_channel,
965 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
971 static int create_transport(struct web_session *session)
975 err = connect_session_transport(session);
979 debug(session->web, "creating session %s:%u",
980 session->address, session->port);
985 static int parse_url(struct web_session *session,
986 const char *url, const char *proxy)
988 char *scheme, *host, *port, *path;
990 scheme = g_strdup(url);
994 host = strstr(scheme, "://");
999 if (strcasecmp(scheme, "https") == 0) {
1000 session->port = 443;
1001 session->flags |= SESSION_FLAG_USE_TLS;
1002 } else if (strcasecmp(scheme, "http") == 0) {
1013 path = strchr(host, '/');
1018 session->request = g_strdup_printf("/%s", path ? path : "");
1020 session->request = g_strdup(url);
1022 port = strrchr(host, ':');
1025 int tmp = strtol(port + 1, &end, 10);
1029 session->port = tmp;
1033 session->host = g_strdup(host);
1035 session->host = g_strdup_printf("%s:%u", host, tmp);
1037 session->host = g_strdup(host);
1044 scheme = g_strdup(proxy);
1048 host = strstr(proxy, "://");
1053 if (strcasecmp(scheme, "http") != 0) {
1060 path = strchr(host, '/');
1064 port = strrchr(host, ':');
1067 int tmp = strtol(port + 1, &end, 10);
1071 session->port = tmp;
1075 session->address = g_strdup(host);
1082 static void resolv_result(GResolvResultStatus status,
1083 char **results, gpointer user_data)
1085 struct web_session *session = user_data;
1086 struct addrinfo hints;
1090 if (results == NULL || results[0] == NULL) {
1091 call_result_func(session, 404);
1095 debug(session->web, "address %s", results[0]);
1097 memset(&hints, 0, sizeof(struct addrinfo));
1098 hints.ai_flags = AI_NUMERICHOST;
1099 hints.ai_family = session->web->family;
1101 if (session->addr != NULL) {
1102 freeaddrinfo(session->addr);
1103 session->addr = NULL;
1106 port = g_strdup_printf("%u", session->port);
1107 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1109 if (ret != 0 || session->addr == NULL) {
1110 call_result_func(session, 400);
1114 session->address = g_strdup(results[0]);
1116 if (create_transport(session) < 0) {
1117 call_result_func(session, 409);
1122 static guint do_request(GWeb *web, const char *url,
1123 const char *type, GWebInputFunc input,
1124 GWebResultFunc func, gpointer user_data)
1126 struct web_session *session;
1128 if (web == NULL || url == NULL)
1131 debug(web, "request %s", url);
1133 session = g_try_new0(struct web_session, 1);
1134 if (session == NULL)
1137 if (parse_url(session, url, web->proxy) < 0) {
1138 free_session(session);
1142 debug(web, "address %s", session->address);
1143 debug(web, "port %u", session->port);
1144 debug(web, "host %s", session->host);
1145 debug(web, "flags %lu", session->flags);
1146 debug(web, "request %s", session->request);
1149 session->content_type = g_strdup(type);
1151 debug(web, "content-type %s", session->content_type);
1156 session->result_func = func;
1157 session->input_func = input;
1158 session->user_data = user_data;
1160 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1161 if (session->receive_buffer == NULL) {
1162 free_session(session);
1166 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1168 if (session->result.headers == NULL) {
1169 free_session(session);
1173 session->receive_space = DEFAULT_BUFFER_SIZE;
1174 session->send_buffer = g_string_sized_new(0);
1175 session->current_header = g_string_sized_new(0);
1176 session->header_done = FALSE;
1177 session->body_done = FALSE;
1179 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1180 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1181 session->host, resolv_result, session);
1182 if (session->resolv_action == 0) {
1183 free_session(session);
1187 struct addrinfo hints;
1191 if (session->address == NULL)
1192 session->address = g_strdup(session->host);
1194 memset(&hints, 0, sizeof(struct addrinfo));
1195 hints.ai_flags = AI_NUMERICHOST;
1196 hints.ai_family = session->web->family;
1198 if (session->addr != NULL) {
1199 freeaddrinfo(session->addr);
1200 session->addr = NULL;
1203 port = g_strdup_printf("%u", session->port);
1204 ret = getaddrinfo(session->address, port, &hints,
1207 if (ret != 0 || session->addr == NULL) {
1208 free_session(session);
1212 if (create_transport(session) < 0) {
1213 free_session(session);
1218 web->session_list = g_list_append(web->session_list, session);
1220 return web->next_query_id++;
1223 guint g_web_request_get(GWeb *web, const char *url,
1224 GWebResultFunc func, gpointer user_data)
1226 return do_request(web, url, NULL, NULL, func, user_data);
1229 guint g_web_request_post(GWeb *web, const char *url,
1230 const char *type, GWebInputFunc input,
1231 GWebResultFunc func, gpointer user_data)
1233 return do_request(web, url, type, input, func, user_data);
1236 gboolean g_web_cancel_request(GWeb *web, guint id)
1244 guint16 g_web_result_get_status(GWebResult *result)
1249 return result->status;
1252 gboolean g_web_result_get_chunk(GWebResult *result,
1253 const guint8 **chunk, gsize *length)
1261 *chunk = result->buffer;
1264 *length = result->length;
1269 gboolean g_web_result_get_header(GWebResult *result,
1270 const char *header, const char **value)
1278 *value = g_hash_table_lookup(result->headers, header);
1286 struct _GWebParser {
1290 const char *token_str;
1295 GWebParserFunc func;
1299 GWebParser *g_web_parser_new(const char *begin, const char *end,
1300 GWebParserFunc func, gpointer user_data)
1304 parser = g_try_new0(GWebParser, 1);
1308 parser->ref_count = 1;
1310 parser->begin_token = g_strdup(begin);
1311 parser->end_token = g_strdup(end);
1313 if (parser->begin_token == NULL) {
1318 parser->func = func;
1319 parser->user_data = user_data;
1321 parser->token_str = parser->begin_token;
1322 parser->token_len = strlen(parser->token_str);
1323 parser->token_pos = 0;
1325 parser->intoken = FALSE;
1326 parser->content = g_string_sized_new(0);
1331 GWebParser *g_web_parser_ref(GWebParser *parser)
1336 __sync_fetch_and_add(&parser->ref_count, 1);
1341 void g_web_parser_unref(GWebParser *parser)
1346 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1349 g_string_free(parser->content, TRUE);
1351 g_free(parser->begin_token);
1352 g_free(parser->end_token);
1356 void g_web_parser_feed_data(GWebParser *parser,
1357 const guint8 *data, gsize length)
1359 const guint8 *ptr = data;
1364 while (length > 0) {
1365 guint8 chr = parser->token_str[parser->token_pos];
1367 if (parser->token_pos == 0) {
1370 pos = memchr(ptr, chr, length);
1372 if (parser->intoken == TRUE)
1373 g_string_append_len(parser->content,
1374 (gchar *) ptr, length);
1378 if (parser->intoken == TRUE)
1379 g_string_append_len(parser->content,
1380 (gchar *) ptr, (pos - ptr) + 1);
1382 length -= (pos - ptr) + 1;
1385 parser->token_pos++;
1389 if (parser->intoken == TRUE)
1390 g_string_append_c(parser->content, ptr[0]);
1392 if (ptr[0] != chr) {
1396 parser->token_pos = 0;
1403 parser->token_pos++;
1405 if (parser->token_pos == parser->token_len) {
1406 if (parser->intoken == FALSE) {
1407 g_string_append(parser->content,
1410 parser->intoken = TRUE;
1411 parser->token_str = parser->end_token;
1412 parser->token_len = strlen(parser->end_token);
1413 parser->token_pos = 0;
1416 str = g_string_free(parser->content, FALSE);
1417 parser->content = g_string_sized_new(0);
1419 parser->func(str, parser->user_data);
1422 parser->intoken = FALSE;
1423 parser->token_str = parser->begin_token;
1424 parser->token_len = strlen(parser->begin_token);
1425 parser->token_pos = 0;
1431 void g_web_parser_end_data(GWebParser *parser)