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)
147 GWeb *web = session->web;
152 g_free(session->request);
154 if (session->resolv_action > 0)
155 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
157 if (session->transport_watch > 0)
158 g_source_remove(session->transport_watch);
160 if (session->send_watch > 0)
161 g_source_remove(session->send_watch);
163 if (session->transport_channel != NULL)
164 g_io_channel_unref(session->transport_channel);
166 g_free(session->result.last_key);
168 if (session->result.headers != NULL)
169 g_hash_table_destroy(session->result.headers);
171 if (session->send_buffer != NULL)
172 g_string_free(session->send_buffer, TRUE);
174 if (session->current_header != NULL)
175 g_string_free(session->current_header, TRUE);
177 g_free(session->receive_buffer);
179 g_free(session->content_type);
181 g_free(session->host);
182 g_free(session->address);
183 if (session->addr != NULL)
184 freeaddrinfo(session->addr);
189 static void flush_sessions(GWeb *web)
193 for (list = g_list_first(web->session_list);
194 list; list = g_list_next(list))
195 free_session(list->data);
197 g_list_free(web->session_list);
198 web->session_list = NULL;
201 GWeb *g_web_new(int index)
208 web = g_try_new0(GWeb, 1);
214 web->next_query_id = 1;
216 web->family = AF_UNSPEC;
219 web->session_list = NULL;
221 web->resolv = g_resolv_new(index);
222 if (web->resolv == NULL) {
227 web->accept_option = g_strdup("*/*");
228 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
229 web->close_connection = FALSE;
234 GWeb *g_web_ref(GWeb *web)
239 __sync_fetch_and_add(&web->ref_count, 1);
244 void g_web_unref(GWeb *web)
249 if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
254 g_resolv_unref(web->resolv);
258 g_free(web->accept_option);
259 g_free(web->user_agent);
260 g_free(web->user_agent_profile);
261 g_free(web->http_version);
266 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
271 web->debug_func = func;
272 web->debug_data = user_data;
274 g_resolv_set_debug(web->resolv, func, user_data);
277 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
286 debug(web, "clearing proxy");
288 web->proxy = g_strdup(proxy);
289 debug(web, "setting proxy %s", web->proxy);
295 gboolean g_web_set_address_family(GWeb *web, int family)
300 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
303 web->family = family;
305 g_resolv_set_address_family(web->resolv, family);
310 gboolean g_web_add_nameserver(GWeb *web, const char *address)
315 g_resolv_add_nameserver(web->resolv, address, 53, 0);
320 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
322 g_free(web->accept_option);
324 if (format == NULL) {
325 web->accept_option = NULL;
326 debug(web, "clearing accept option");
328 web->accept_option = g_strdup_vprintf(format, args);
329 debug(web, "setting accept %s", web->accept_option);
335 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
343 va_start(args, format);
344 result = set_accept_option(web, format, args);
350 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
352 g_free(web->user_agent);
354 if (format == NULL) {
355 web->user_agent = NULL;
356 debug(web, "clearing user agent");
358 web->user_agent = g_strdup_vprintf(format, args);
359 debug(web, "setting user agent %s", web->user_agent);
365 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
373 va_start(args, format);
374 result = set_user_agent(web, format, args);
380 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
385 g_free(web->user_agent_profile);
387 web->user_agent_profile = g_strdup(profile);
388 debug(web, "setting user agent profile %s", web->user_agent);
393 gboolean g_web_set_http_version(GWeb *web, const char *version)
398 g_free(web->http_version);
400 if (version == NULL) {
401 web->http_version = NULL;
402 debug(web, "clearing HTTP version");
404 web->http_version = g_strdup(version);
405 debug(web, "setting HTTP version %s", web->http_version);
411 void g_web_set_close_connection(GWeb *web, gboolean enabled)
416 web->close_connection = enabled;
419 gboolean g_web_get_close_connection(GWeb *web)
424 return web->close_connection;
427 static inline void call_result_func(struct web_session *session, guint16 status)
431 if (session->result_func == NULL)
435 session->result.status = status;
437 result = session->result_func(&session->result, session->user_data);
439 debug(session->web, "[result function] %s",
440 result == TRUE ? "continue" : "stop");
443 static gboolean process_send_buffer(struct web_session *session)
445 GString *buf = session->send_buffer;
446 gsize count, bytes_written;
452 if (session->request_started == TRUE &&
453 session->more_data == FALSE &&
455 session->body_done = TRUE;
460 status = g_io_channel_write_chars(session->transport_channel,
461 buf->str, count, &bytes_written, NULL);
463 debug(session->web, "status %u bytes to write %zu bytes written %zu",
464 status, count, bytes_written);
466 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
469 g_string_erase(buf, 0, bytes_written);
474 static gboolean process_send_file(struct web_session *session)
480 if (session->fd == -1)
483 sk = g_io_channel_unix_get_fd(session->transport_channel);
487 offset = session->offset;
489 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
491 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
492 errno, session->length, bytes_sent);
494 if (bytes_sent < 0 && errno != EAGAIN)
497 session->offset = offset;
498 session->length -= bytes_sent;
500 if (session->length == 0) {
501 session->body_done = TRUE;
508 static void process_next_chunk(struct web_session *session)
510 GString *buf = session->send_buffer;
514 if (session->input_func == NULL) {
515 session->more_data = FALSE;
519 session->more_data = session->input_func(&body, &length,
523 g_string_append_printf(buf, "%zx\r\n", length);
524 g_string_append_len(buf, (char *) body, length);
525 g_string_append(buf, "\r\n");
528 if (session->more_data == FALSE)
529 g_string_append(buf, "0\r\n\r\n");
532 static void start_request(struct web_session *session)
534 GString *buf = session->send_buffer;
539 debug(session->web, "request %s from %s",
540 session->request, session->host);
542 g_string_truncate(buf, 0);
544 if (session->web->http_version == NULL)
547 version = session->web->http_version;
549 if (session->content_type == NULL)
550 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
551 session->request, version);
553 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
554 session->request, version);
556 g_string_append_printf(buf, "Host: %s\r\n", session->host);
558 if (session->web->user_agent != NULL)
559 g_string_append_printf(buf, "User-Agent: %s\r\n",
560 session->web->user_agent);
562 if (session->web->user_agent_profile != NULL) {
563 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
564 session->web->user_agent_profile);
567 if (session->web->accept_option != NULL)
568 g_string_append_printf(buf, "Accept: %s\r\n",
569 session->web->accept_option);
571 if (session->content_type != NULL) {
572 g_string_append_printf(buf, "Content-Type: %s\r\n",
573 session->content_type);
574 if (session->input_func == NULL) {
575 session->more_data = FALSE;
576 length = session->length;
578 session->more_data = session->input_func(&body, &length,
580 if (session->more_data == FALSE)
581 g_string_append_printf(buf, "Content-Length: %zu\r\n",
584 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
587 if (session->web->close_connection == TRUE)
588 g_string_append(buf, "Connection: close\r\n");
590 g_string_append(buf, "\r\n");
592 if (session->content_type != NULL && length > 0) {
593 if (session->more_data == TRUE) {
594 g_string_append_printf(buf, "%zx\r\n", length);
595 g_string_append_len(buf, (char *) body, length);
596 g_string_append(buf, "\r\n");
597 } else if (session->fd == -1)
598 g_string_append_len(buf, (char *) body, length);
602 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
605 struct web_session *session = user_data;
607 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
608 session->send_watch = 0;
612 if (process_send_buffer(session) == TRUE)
615 if (process_send_file(session) == TRUE)
618 if (session->request_started == FALSE) {
619 session->request_started = TRUE;
620 start_request(session);
621 } else if (session->more_data == TRUE)
622 process_next_chunk(session);
624 process_send_buffer(session);
626 if (session->body_done == TRUE) {
627 session->send_watch = 0;
634 static int decode_chunked(struct web_session *session,
635 const guint8 *buf, gsize len)
637 const guint8 *ptr = buf;
645 switch (session->chunck_state) {
647 pos = memchr(ptr, '\n', len);
649 g_string_append_len(session->current_header,
655 if (count < 1 || ptr[count - 1] != '\r')
658 g_string_append_len(session->current_header,
659 (gchar *) ptr, count);
664 str = session->current_header->str;
666 counter = strtoul(str, NULL, 16);
667 if ((counter == 0 && errno == EINVAL) ||
668 counter == ULONG_MAX)
671 session->chunk_size = counter;
672 session->chunk_left = counter;
674 session->chunck_state = CHUNK_DATA;
681 session->chunck_state = CHUNK_N_BODY;
688 session->chunck_state = CHUNK_SIZE;
691 if (session->chunk_size == 0) {
692 debug(session->web, "Download Done in chunk");
693 g_string_truncate(session->current_header, 0);
697 if (session->chunk_left <= len) {
698 session->result.buffer = ptr;
699 session->result.length = session->chunk_left;
700 call_result_func(session, 0);
702 len -= session->chunk_left;
703 ptr += session->chunk_left;
705 session->total_len += session->chunk_left;
706 session->chunk_left = 0;
708 g_string_truncate(session->current_header, 0);
709 session->chunck_state = CHUNK_R_BODY;
713 session->result.buffer = ptr;
714 session->result.length = len;
715 call_result_func(session, 0);
717 session->chunk_left -= len;
718 session->total_len += len;
729 static int handle_body(struct web_session *session,
730 const guint8 *buf, gsize len)
734 debug(session->web, "[body] length %zu", len);
736 if (session->result.use_chunk == FALSE) {
738 session->result.buffer = buf;
739 session->result.length = len;
740 call_result_func(session, 0);
745 err = decode_chunked(session, buf, len);
747 debug(session->web, "Error in chunk decode %d", err);
749 session->result.buffer = NULL;
750 session->result.length = 0;
751 call_result_func(session, 400);
757 static void handle_multi_line(struct web_session *session)
763 str = session->current_header->str;
765 if (str[0] != ' ' && str[0] != '\t')
768 while (str[0] == ' ' || str[0] == '\t')
771 count = str - session->current_header->str;
773 g_string_erase(session->current_header, 0, count);
774 g_string_insert_c(session->current_header, 0, ' ');
777 value = g_hash_table_lookup(session->result.headers,
778 session->result.last_key);
780 g_string_insert(session->current_header, 0, value);
782 str = session->current_header->str;
784 g_hash_table_replace(session->result.headers,
785 g_strdup(session->result.last_key),
790 static void add_header_field(struct web_session *session)
798 str = session->current_header->str;
800 pos = memchr(str, ':', session->current_header->len);
807 /* remove preceding white spaces */
811 count = (char *) pos - str;
813 g_string_erase(session->current_header, 0, count);
815 value = g_hash_table_lookup(session->result.headers, key);
817 g_string_insert_c(session->current_header, 0, ' ');
818 g_string_insert_c(session->current_header, 0, ';');
820 g_string_insert(session->current_header, 0, value);
823 str = session->current_header->str;
824 g_hash_table_replace(session->result.headers, key,
827 g_free(session->result.last_key);
828 session->result.last_key = g_strdup(key);
832 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
835 struct web_session *session = user_data;
836 guint8 *ptr = session->receive_buffer;
840 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
841 session->transport_watch = 0;
842 session->result.buffer = NULL;
843 session->result.length = 0;
844 call_result_func(session, 400);
848 status = g_io_channel_read_chars(channel,
849 (gchar *) session->receive_buffer,
850 session->receive_space - 1, &bytes_read, NULL);
852 debug(session->web, "bytes read %zu", bytes_read);
854 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
855 session->transport_watch = 0;
856 session->result.buffer = NULL;
857 session->result.length = 0;
858 call_result_func(session, 0);
862 session->receive_buffer[bytes_read] = '\0';
864 if (session->header_done == TRUE) {
865 if (handle_body(session, session->receive_buffer,
867 session->transport_watch = 0;
873 while (bytes_read > 0) {
878 pos = memchr(ptr, '\n', bytes_read);
880 g_string_append_len(session->current_header,
881 (gchar *) ptr, bytes_read);
886 count = strlen((char *) ptr);
887 if (count > 0 && ptr[count - 1] == '\r') {
892 g_string_append_len(session->current_header,
893 (gchar *) ptr, count);
895 bytes_read -= count + 1;
901 if (session->current_header->len == 0) {
904 session->header_done = TRUE;
906 val = g_hash_table_lookup(session->result.headers,
907 "Transfer-Encoding");
909 val = g_strrstr(val, "chunked");
911 session->result.use_chunk = TRUE;
913 session->chunck_state = CHUNK_SIZE;
914 session->chunk_left = 0;
915 session->total_len = 0;
919 if (handle_body(session, ptr, bytes_read) < 0) {
920 session->transport_watch = 0;
926 str = session->current_header->str;
928 if (session->result.status == 0) {
931 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
932 session->result.status = code;
935 debug(session->web, "[header] %s", str);
937 /* handle multi-line header */
938 if (str[0] == ' ' || str[0] == '\t')
939 handle_multi_line(session);
941 add_header_field(session);
943 g_string_truncate(session->current_header, 0);
949 static int connect_session_transport(struct web_session *session)
954 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
959 if (session->web->index > 0) {
960 char interface[IF_NAMESIZE];
962 memset(interface, 0, IF_NAMESIZE);
964 if (if_indextoname(session->web->index, interface) != NULL) {
965 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
966 interface, IF_NAMESIZE) < 0) {
971 debug(session->web, "Use interface %s", interface);
975 if (session->flags & SESSION_FLAG_USE_TLS) {
976 debug(session->web, "using TLS encryption");
977 session->transport_channel = g_io_channel_gnutls_new(sk);
979 debug(session->web, "no encryption");
980 session->transport_channel = g_io_channel_unix_new(sk);
983 if (session->transport_channel == NULL) {
988 flags = g_io_channel_get_flags(session->transport_channel);
989 g_io_channel_set_flags(session->transport_channel,
990 flags | G_IO_FLAG_NONBLOCK, NULL);
992 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
993 g_io_channel_set_buffered(session->transport_channel, FALSE);
995 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
997 if (connect(sk, session->addr->ai_addr,
998 session->addr->ai_addrlen) < 0) {
999 if (errno != EINPROGRESS) {
1005 session->transport_watch = g_io_add_watch(session->transport_channel,
1006 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1007 received_data, session);
1009 session->send_watch = g_io_add_watch(session->transport_channel,
1010 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1011 send_data, session);
1016 static int create_transport(struct web_session *session)
1020 err = connect_session_transport(session);
1024 debug(session->web, "creating session %s:%u",
1025 session->address, session->port);
1030 static int parse_url(struct web_session *session,
1031 const char *url, const char *proxy)
1033 char *scheme, *host, *port, *path;
1035 scheme = g_strdup(url);
1039 host = strstr(scheme, "://");
1044 if (strcasecmp(scheme, "https") == 0) {
1045 session->port = 443;
1046 session->flags |= SESSION_FLAG_USE_TLS;
1047 } else if (strcasecmp(scheme, "http") == 0) {
1058 path = strchr(host, '/');
1063 session->request = g_strdup_printf("/%s", path ? path : "");
1065 session->request = g_strdup(url);
1067 port = strrchr(host, ':');
1070 int tmp = strtol(port + 1, &end, 10);
1074 session->port = tmp;
1078 session->host = g_strdup(host);
1080 session->host = g_strdup_printf("%s:%u", host, tmp);
1082 session->host = g_strdup(host);
1089 scheme = g_strdup(proxy);
1093 host = strstr(proxy, "://");
1098 if (strcasecmp(scheme, "http") != 0) {
1105 path = strchr(host, '/');
1109 port = strrchr(host, ':');
1112 int tmp = strtol(port + 1, &end, 10);
1116 session->port = tmp;
1120 session->address = g_strdup(host);
1127 static void resolv_result(GResolvResultStatus status,
1128 char **results, gpointer user_data)
1130 struct web_session *session = user_data;
1131 struct addrinfo hints;
1135 if (results == NULL || results[0] == NULL) {
1136 call_result_func(session, 404);
1140 debug(session->web, "address %s", results[0]);
1142 memset(&hints, 0, sizeof(struct addrinfo));
1143 hints.ai_flags = AI_NUMERICHOST;
1144 hints.ai_family = session->web->family;
1146 if (session->addr != NULL) {
1147 freeaddrinfo(session->addr);
1148 session->addr = NULL;
1151 port = g_strdup_printf("%u", session->port);
1152 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1154 if (ret != 0 || session->addr == NULL) {
1155 call_result_func(session, 400);
1159 session->address = g_strdup(results[0]);
1161 if (create_transport(session) < 0) {
1162 call_result_func(session, 409);
1167 static guint do_request(GWeb *web, const char *url,
1168 const char *type, GWebInputFunc input,
1169 int fd, gsize length, GWebResultFunc func,
1172 struct web_session *session;
1174 if (web == NULL || url == NULL)
1177 debug(web, "request %s", url);
1179 session = g_try_new0(struct web_session, 1);
1180 if (session == NULL)
1183 if (parse_url(session, url, web->proxy) < 0) {
1184 free_session(session);
1188 debug(web, "address %s", session->address);
1189 debug(web, "port %u", session->port);
1190 debug(web, "host %s", session->host);
1191 debug(web, "flags %lu", session->flags);
1192 debug(web, "request %s", session->request);
1195 session->content_type = g_strdup(type);
1197 debug(web, "content-type %s", session->content_type);
1202 session->result_func = func;
1203 session->input_func = input;
1205 session->length = length;
1206 session->offset = 0;
1207 session->user_data = user_data;
1209 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1210 if (session->receive_buffer == NULL) {
1211 free_session(session);
1215 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1217 if (session->result.headers == NULL) {
1218 free_session(session);
1222 session->receive_space = DEFAULT_BUFFER_SIZE;
1223 session->send_buffer = g_string_sized_new(0);
1224 session->current_header = g_string_sized_new(0);
1225 session->header_done = FALSE;
1226 session->body_done = FALSE;
1228 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1229 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1230 session->host, resolv_result, session);
1231 if (session->resolv_action == 0) {
1232 free_session(session);
1236 struct addrinfo hints;
1240 if (session->address == NULL)
1241 session->address = g_strdup(session->host);
1243 memset(&hints, 0, sizeof(struct addrinfo));
1244 hints.ai_flags = AI_NUMERICHOST;
1245 hints.ai_family = session->web->family;
1247 if (session->addr != NULL) {
1248 freeaddrinfo(session->addr);
1249 session->addr = NULL;
1252 port = g_strdup_printf("%u", session->port);
1253 ret = getaddrinfo(session->address, port, &hints,
1256 if (ret != 0 || session->addr == NULL) {
1257 free_session(session);
1261 if (create_transport(session) < 0) {
1262 free_session(session);
1267 web->session_list = g_list_append(web->session_list, session);
1269 return web->next_query_id++;
1272 guint g_web_request_get(GWeb *web, const char *url,
1273 GWebResultFunc func, gpointer user_data)
1275 return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
1278 guint g_web_request_post(GWeb *web, const char *url,
1279 const char *type, GWebInputFunc input,
1280 GWebResultFunc func, gpointer user_data)
1282 return do_request(web, url, type, input, -1, 0, func, user_data);
1285 guint g_web_request_post_file(GWeb *web, const char *url,
1286 const char *type, const char *file,
1287 GWebResultFunc func, gpointer user_data)
1293 if (stat(file, &st) < 0)
1296 fd = open(file, O_RDONLY);
1300 ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
1307 gboolean g_web_cancel_request(GWeb *web, guint id)
1315 guint16 g_web_result_get_status(GWebResult *result)
1320 return result->status;
1323 gboolean g_web_result_get_chunk(GWebResult *result,
1324 const guint8 **chunk, gsize *length)
1332 *chunk = result->buffer;
1335 *length = result->length;
1340 gboolean g_web_result_get_header(GWebResult *result,
1341 const char *header, const char **value)
1349 *value = g_hash_table_lookup(result->headers, header);
1357 struct _GWebParser {
1361 const char *token_str;
1366 GWebParserFunc func;
1370 GWebParser *g_web_parser_new(const char *begin, const char *end,
1371 GWebParserFunc func, gpointer user_data)
1375 parser = g_try_new0(GWebParser, 1);
1379 parser->ref_count = 1;
1381 parser->begin_token = g_strdup(begin);
1382 parser->end_token = g_strdup(end);
1384 if (parser->begin_token == NULL) {
1389 parser->func = func;
1390 parser->user_data = user_data;
1392 parser->token_str = parser->begin_token;
1393 parser->token_len = strlen(parser->token_str);
1394 parser->token_pos = 0;
1396 parser->intoken = FALSE;
1397 parser->content = g_string_sized_new(0);
1402 GWebParser *g_web_parser_ref(GWebParser *parser)
1407 __sync_fetch_and_add(&parser->ref_count, 1);
1412 void g_web_parser_unref(GWebParser *parser)
1417 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1420 g_string_free(parser->content, TRUE);
1422 g_free(parser->begin_token);
1423 g_free(parser->end_token);
1427 void g_web_parser_feed_data(GWebParser *parser,
1428 const guint8 *data, gsize length)
1430 const guint8 *ptr = data;
1435 while (length > 0) {
1436 guint8 chr = parser->token_str[parser->token_pos];
1438 if (parser->token_pos == 0) {
1441 pos = memchr(ptr, chr, length);
1443 if (parser->intoken == TRUE)
1444 g_string_append_len(parser->content,
1445 (gchar *) ptr, length);
1449 if (parser->intoken == TRUE)
1450 g_string_append_len(parser->content,
1451 (gchar *) ptr, (pos - ptr) + 1);
1453 length -= (pos - ptr) + 1;
1456 parser->token_pos++;
1460 if (parser->intoken == TRUE)
1461 g_string_append_c(parser->content, ptr[0]);
1463 if (ptr[0] != chr) {
1467 parser->token_pos = 0;
1474 parser->token_pos++;
1476 if (parser->token_pos == parser->token_len) {
1477 if (parser->intoken == FALSE) {
1478 g_string_append(parser->content,
1481 parser->intoken = TRUE;
1482 parser->token_str = parser->end_token;
1483 parser->token_len = strlen(parser->end_token);
1484 parser->token_pos = 0;
1487 str = g_string_free(parser->content, FALSE);
1488 parser->content = g_string_sized_new(0);
1490 parser->func(str, parser->user_data);
1493 parser->intoken = FALSE;
1494 parser->token_str = parser->begin_token;
1495 parser->token_len = strlen(parser->begin_token);
1496 parser->token_pos = 0;
1502 void g_web_parser_end_data(GWebParser *parser)