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>
42 #include "giognutls.h"
46 #define DEFAULT_BUFFER_SIZE 2048
48 #define SESSION_FLAG_USE_TLS (1 << 0)
73 struct addrinfo *addr;
77 GIOChannel *transport_channel;
78 guint transport_watch;
84 guint8 *receive_buffer;
87 GString *current_header;
91 gboolean request_started;
93 enum chunk_state chunck_state;
100 GWebResultFunc result_func;
101 GWebInputFunc input_func;
122 char *user_agent_profile;
124 gboolean close_connection;
126 GWebDebugFunc debug_func;
130 static inline void debug(GWeb *web, const char *format, ...)
135 if (web->debug_func == NULL)
138 va_start(ap, format);
140 if (vsnprintf(str, sizeof(str), format, ap) > 0)
141 web->debug_func(str, web->debug_data);
146 static void free_session(struct web_session *session)
153 g_free(session->request);
156 if (session->resolv_action > 0)
157 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
159 if (session->transport_watch > 0)
160 g_source_remove(session->transport_watch);
162 if (session->send_watch > 0)
163 g_source_remove(session->send_watch);
165 if (session->transport_channel != NULL)
166 g_io_channel_unref(session->transport_channel);
168 g_free(session->result.last_key);
170 if (session->result.headers != NULL)
171 g_hash_table_destroy(session->result.headers);
173 if (session->send_buffer != NULL)
174 g_string_free(session->send_buffer, TRUE);
176 if (session->current_header != NULL)
177 g_string_free(session->current_header, TRUE);
179 g_free(session->receive_buffer);
181 g_free(session->content_type);
183 g_free(session->host);
184 g_free(session->address);
185 if (session->addr != NULL)
186 freeaddrinfo(session->addr);
191 static void flush_sessions(GWeb *web)
195 for (list = g_list_first(web->session_list);
196 list; list = g_list_next(list))
197 free_session(list->data);
199 g_list_free(web->session_list);
200 web->session_list = NULL;
203 GWeb *g_web_new(int index)
210 web = g_try_new0(GWeb, 1);
216 web->next_query_id = 1;
218 web->family = AF_UNSPEC;
221 web->session_list = NULL;
223 web->resolv = g_resolv_new(index);
224 if (web->resolv == NULL) {
229 web->accept_option = g_strdup("*/*");
230 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
231 web->close_connection = FALSE;
236 GWeb *g_web_ref(GWeb *web)
241 __sync_fetch_and_add(&web->ref_count, 1);
246 void g_web_unref(GWeb *web)
251 if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
256 g_resolv_unref(web->resolv);
260 g_free(web->accept_option);
261 g_free(web->user_agent);
262 g_free(web->user_agent_profile);
263 g_free(web->http_version);
268 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
273 web->debug_func = func;
274 web->debug_data = user_data;
276 g_resolv_set_debug(web->resolv, func, user_data);
279 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
288 debug(web, "clearing proxy");
290 web->proxy = g_strdup(proxy);
291 debug(web, "setting proxy %s", web->proxy);
297 gboolean g_web_set_address_family(GWeb *web, int family)
302 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
305 web->family = family;
307 g_resolv_set_address_family(web->resolv, family);
312 gboolean g_web_add_nameserver(GWeb *web, const char *address)
317 g_resolv_add_nameserver(web->resolv, address, 53, 0);
322 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
324 g_free(web->accept_option);
326 if (format == NULL) {
327 web->accept_option = NULL;
328 debug(web, "clearing accept option");
330 web->accept_option = g_strdup_vprintf(format, args);
331 debug(web, "setting accept %s", web->accept_option);
337 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
345 va_start(args, format);
346 result = set_accept_option(web, format, args);
352 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
354 g_free(web->user_agent);
356 if (format == NULL) {
357 web->user_agent = NULL;
358 debug(web, "clearing user agent");
360 web->user_agent = g_strdup_vprintf(format, args);
361 debug(web, "setting user agent %s", web->user_agent);
367 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
375 va_start(args, format);
376 result = set_user_agent(web, format, args);
382 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
387 g_free(web->user_agent_profile);
389 web->user_agent_profile = g_strdup(profile);
390 debug(web, "setting user agent profile %s", web->user_agent);
395 gboolean g_web_set_http_version(GWeb *web, const char *version)
400 g_free(web->http_version);
402 if (version == NULL) {
403 web->http_version = NULL;
404 debug(web, "clearing HTTP version");
406 web->http_version = g_strdup(version);
407 debug(web, "setting HTTP version %s", web->http_version);
413 void g_web_set_close_connection(GWeb *web, gboolean enabled)
418 web->close_connection = enabled;
421 gboolean g_web_get_close_connection(GWeb *web)
426 return web->close_connection;
429 static inline void call_result_func(struct web_session *session, guint16 status)
433 if (session->result_func == NULL)
437 session->result.status = status;
439 result = session->result_func(&session->result, session->user_data);
441 debug(session->web, "[result function] %s",
442 result == TRUE ? "continue" : "stop");
445 static gboolean process_send_buffer(struct web_session *session)
448 gsize count, bytes_written;
454 buf = session->send_buffer;
458 if (session->request_started == TRUE &&
459 session->more_data == FALSE &&
461 session->body_done = TRUE;
466 status = g_io_channel_write_chars(session->transport_channel,
467 buf->str, count, &bytes_written, NULL);
469 debug(session->web, "status %u bytes to write %zu bytes written %zu",
470 status, count, bytes_written);
472 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
475 g_string_erase(buf, 0, bytes_written);
480 static gboolean process_send_file(struct web_session *session)
486 if (session->fd == -1)
489 if (session->request_started == FALSE || session->more_data == TRUE)
492 sk = g_io_channel_unix_get_fd(session->transport_channel);
496 offset = session->offset;
498 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
500 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
501 errno, session->length, bytes_sent);
503 if (bytes_sent < 0 && errno != EAGAIN)
506 session->offset = offset;
507 session->length -= bytes_sent;
509 if (session->length == 0) {
510 session->body_done = TRUE;
517 static void process_next_chunk(struct web_session *session)
519 GString *buf = session->send_buffer;
523 if (session->input_func == NULL) {
524 session->more_data = FALSE;
528 session->more_data = session->input_func(&body, &length,
532 g_string_append_printf(buf, "%zx\r\n", length);
533 g_string_append_len(buf, (char *) body, length);
534 g_string_append(buf, "\r\n");
537 if (session->more_data == FALSE)
538 g_string_append(buf, "0\r\n\r\n");
541 static void start_request(struct web_session *session)
543 GString *buf = session->send_buffer;
548 debug(session->web, "request %s from %s",
549 session->request, session->host);
551 g_string_truncate(buf, 0);
553 if (session->web->http_version == NULL)
556 version = session->web->http_version;
558 if (session->content_type == NULL)
559 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
560 session->request, version);
562 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
563 session->request, version);
565 g_string_append_printf(buf, "Host: %s\r\n", session->host);
567 if (session->web->user_agent != NULL)
568 g_string_append_printf(buf, "User-Agent: %s\r\n",
569 session->web->user_agent);
571 if (session->web->user_agent_profile != NULL) {
572 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
573 session->web->user_agent_profile);
576 if (session->web->accept_option != NULL)
577 g_string_append_printf(buf, "Accept: %s\r\n",
578 session->web->accept_option);
580 if (session->content_type != NULL) {
581 g_string_append_printf(buf, "Content-Type: %s\r\n",
582 session->content_type);
583 if (session->input_func == NULL) {
584 session->more_data = FALSE;
585 length = session->length;
587 session->more_data = session->input_func(&body, &length,
589 if (session->more_data == FALSE)
590 g_string_append_printf(buf, "Content-Length: %zu\r\n",
593 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
596 if (session->web->close_connection == TRUE)
597 g_string_append(buf, "Connection: close\r\n");
599 g_string_append(buf, "\r\n");
601 if (session->content_type != NULL && length > 0) {
602 if (session->more_data == TRUE) {
603 g_string_append_printf(buf, "%zx\r\n", length);
604 g_string_append_len(buf, (char *) body, length);
605 g_string_append(buf, "\r\n");
606 } else if (session->fd == -1)
607 g_string_append_len(buf, (char *) body, length);
611 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
614 struct web_session *session = user_data;
616 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
617 session->send_watch = 0;
621 if (process_send_buffer(session) == TRUE)
624 if (process_send_file(session) == TRUE)
627 if (session->request_started == FALSE) {
628 session->request_started = TRUE;
629 start_request(session);
630 } else if (session->more_data == TRUE)
631 process_next_chunk(session);
633 process_send_buffer(session);
635 if (session->body_done == TRUE) {
636 session->send_watch = 0;
643 static int decode_chunked(struct web_session *session,
644 const guint8 *buf, gsize len)
646 const guint8 *ptr = buf;
654 switch (session->chunck_state) {
656 pos = memchr(ptr, '\n', len);
658 g_string_append_len(session->current_header,
664 if (count < 1 || ptr[count - 1] != '\r')
667 g_string_append_len(session->current_header,
668 (gchar *) ptr, count);
673 str = session->current_header->str;
675 counter = strtoul(str, NULL, 16);
676 if ((counter == 0 && errno == EINVAL) ||
677 counter == ULONG_MAX)
680 session->chunk_size = counter;
681 session->chunk_left = counter;
683 session->chunck_state = CHUNK_DATA;
690 session->chunck_state = CHUNK_N_BODY;
697 session->chunck_state = CHUNK_SIZE;
700 if (session->chunk_size == 0) {
701 debug(session->web, "Download Done in chunk");
702 g_string_truncate(session->current_header, 0);
706 if (session->chunk_left <= len) {
707 session->result.buffer = ptr;
708 session->result.length = session->chunk_left;
709 call_result_func(session, 0);
711 len -= session->chunk_left;
712 ptr += session->chunk_left;
714 session->total_len += session->chunk_left;
715 session->chunk_left = 0;
717 g_string_truncate(session->current_header, 0);
718 session->chunck_state = CHUNK_R_BODY;
722 session->result.buffer = ptr;
723 session->result.length = len;
724 call_result_func(session, 0);
726 session->chunk_left -= len;
727 session->total_len += len;
738 static int handle_body(struct web_session *session,
739 const guint8 *buf, gsize len)
743 debug(session->web, "[body] length %zu", len);
745 if (session->result.use_chunk == FALSE) {
747 session->result.buffer = buf;
748 session->result.length = len;
749 call_result_func(session, 0);
754 err = decode_chunked(session, buf, len);
756 debug(session->web, "Error in chunk decode %d", err);
758 session->result.buffer = NULL;
759 session->result.length = 0;
760 call_result_func(session, 400);
766 static void handle_multi_line(struct web_session *session)
772 str = session->current_header->str;
774 if (str[0] != ' ' && str[0] != '\t')
777 while (str[0] == ' ' || str[0] == '\t')
780 count = str - session->current_header->str;
782 g_string_erase(session->current_header, 0, count);
783 g_string_insert_c(session->current_header, 0, ' ');
786 value = g_hash_table_lookup(session->result.headers,
787 session->result.last_key);
789 g_string_insert(session->current_header, 0, value);
791 str = session->current_header->str;
793 g_hash_table_replace(session->result.headers,
794 g_strdup(session->result.last_key),
799 static void add_header_field(struct web_session *session)
807 str = session->current_header->str;
809 pos = memchr(str, ':', session->current_header->len);
816 /* remove preceding white spaces */
820 count = (char *) pos - str;
822 g_string_erase(session->current_header, 0, count);
824 value = g_hash_table_lookup(session->result.headers, key);
826 g_string_insert_c(session->current_header, 0, ' ');
827 g_string_insert_c(session->current_header, 0, ';');
829 g_string_insert(session->current_header, 0, value);
832 str = session->current_header->str;
833 g_hash_table_replace(session->result.headers, key,
836 g_free(session->result.last_key);
837 session->result.last_key = g_strdup(key);
841 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
844 struct web_session *session = user_data;
845 guint8 *ptr = session->receive_buffer;
849 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
850 session->transport_watch = 0;
851 session->result.buffer = NULL;
852 session->result.length = 0;
853 call_result_func(session, 400);
857 status = g_io_channel_read_chars(channel,
858 (gchar *) session->receive_buffer,
859 session->receive_space - 1, &bytes_read, NULL);
861 debug(session->web, "bytes read %zu", bytes_read);
863 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
864 session->transport_watch = 0;
865 session->result.buffer = NULL;
866 session->result.length = 0;
867 call_result_func(session, 0);
871 session->receive_buffer[bytes_read] = '\0';
873 if (session->header_done == TRUE) {
874 if (handle_body(session, session->receive_buffer,
876 session->transport_watch = 0;
882 while (bytes_read > 0) {
887 pos = memchr(ptr, '\n', bytes_read);
889 g_string_append_len(session->current_header,
890 (gchar *) ptr, bytes_read);
895 count = strlen((char *) ptr);
896 if (count > 0 && ptr[count - 1] == '\r') {
901 g_string_append_len(session->current_header,
902 (gchar *) ptr, count);
904 bytes_read -= count + 1;
910 if (session->current_header->len == 0) {
913 session->header_done = TRUE;
915 val = g_hash_table_lookup(session->result.headers,
916 "Transfer-Encoding");
918 val = g_strrstr(val, "chunked");
920 session->result.use_chunk = TRUE;
922 session->chunck_state = CHUNK_SIZE;
923 session->chunk_left = 0;
924 session->total_len = 0;
928 if (handle_body(session, ptr, bytes_read) < 0) {
929 session->transport_watch = 0;
935 str = session->current_header->str;
937 if (session->result.status == 0) {
940 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
941 session->result.status = code;
944 debug(session->web, "[header] %s", str);
946 /* handle multi-line header */
947 if (str[0] == ' ' || str[0] == '\t')
948 handle_multi_line(session);
950 add_header_field(session);
952 g_string_truncate(session->current_header, 0);
958 static int bind_to_address(int sk, const char *interface, int family)
960 struct ifaddrs *ifaddr_list, *ifaddr;
963 if (getifaddrs(&ifaddr_list) < 0)
966 for (ifaddr = ifaddr_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
967 if (g_strcmp0(ifaddr->ifa_name, interface) != 0)
970 if (ifaddr->ifa_addr == NULL ||
971 ifaddr->ifa_addr->sa_family != family)
976 size = sizeof(struct sockaddr_in);
979 size = sizeof(struct sockaddr_in6);
985 err = bind(sk, (struct sockaddr *) ifaddr->ifa_addr, size);
989 freeifaddrs(ifaddr_list);
993 static inline int bind_socket(int sk, int index, int family)
995 char interface[IF_NAMESIZE];
998 if (if_indextoname(index, interface) == NULL)
1001 err = setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1002 interface, IF_NAMESIZE);
1004 err = bind_to_address(sk, interface, family);
1009 static int connect_session_transport(struct web_session *session)
1014 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
1019 if (session->web->index > 0) {
1020 if (bind_socket(sk, session->web->index,
1021 session->addr->ai_family) < 0) {
1022 debug(session->web, "bind() %s", strerror(errno));
1028 if (session->flags & SESSION_FLAG_USE_TLS) {
1029 debug(session->web, "using TLS encryption");
1030 session->transport_channel = g_io_channel_gnutls_new(sk);
1032 debug(session->web, "no encryption");
1033 session->transport_channel = g_io_channel_unix_new(sk);
1036 if (session->transport_channel == NULL) {
1037 debug(session->web, "channel missing");
1042 flags = g_io_channel_get_flags(session->transport_channel);
1043 g_io_channel_set_flags(session->transport_channel,
1044 flags | G_IO_FLAG_NONBLOCK, NULL);
1046 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
1047 g_io_channel_set_buffered(session->transport_channel, FALSE);
1049 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1051 if (connect(sk, session->addr->ai_addr,
1052 session->addr->ai_addrlen) < 0) {
1053 if (errno != EINPROGRESS) {
1054 debug(session->web, "connect() %s", strerror(errno));
1060 session->transport_watch = g_io_add_watch(session->transport_channel,
1061 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1062 received_data, session);
1064 session->send_watch = g_io_add_watch(session->transport_channel,
1065 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1066 send_data, session);
1071 static int create_transport(struct web_session *session)
1075 err = connect_session_transport(session);
1079 debug(session->web, "creating session %s:%u",
1080 session->address, session->port);
1085 static int parse_url(struct web_session *session,
1086 const char *url, const char *proxy)
1088 char *scheme, *host, *port, *path;
1090 scheme = g_strdup(url);
1094 host = strstr(scheme, "://");
1099 if (strcasecmp(scheme, "https") == 0) {
1100 session->port = 443;
1101 session->flags |= SESSION_FLAG_USE_TLS;
1102 } else if (strcasecmp(scheme, "http") == 0) {
1113 path = strchr(host, '/');
1118 session->request = g_strdup_printf("/%s", path ? path : "");
1120 session->request = g_strdup(url);
1122 port = strrchr(host, ':');
1125 int tmp = strtol(port + 1, &end, 10);
1129 session->port = tmp;
1133 session->host = g_strdup(host);
1135 session->host = g_strdup_printf("%s:%u", host, tmp);
1137 session->host = g_strdup(host);
1144 scheme = g_strdup(proxy);
1148 host = strstr(proxy, "://");
1153 if (strcasecmp(scheme, "http") != 0) {
1160 path = strchr(host, '/');
1164 port = strrchr(host, ':');
1167 int tmp = strtol(port + 1, &end, 10);
1171 session->port = tmp;
1175 session->address = g_strdup(host);
1182 static void resolv_result(GResolvResultStatus status,
1183 char **results, gpointer user_data)
1185 struct web_session *session = user_data;
1186 struct addrinfo hints;
1190 if (results == NULL || results[0] == NULL) {
1191 call_result_func(session, 404);
1195 debug(session->web, "address %s", results[0]);
1197 memset(&hints, 0, sizeof(struct addrinfo));
1198 hints.ai_flags = AI_NUMERICHOST;
1199 hints.ai_family = session->web->family;
1201 if (session->addr != NULL) {
1202 freeaddrinfo(session->addr);
1203 session->addr = NULL;
1206 port = g_strdup_printf("%u", session->port);
1207 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1209 if (ret != 0 || session->addr == NULL) {
1210 call_result_func(session, 400);
1214 session->address = g_strdup(results[0]);
1216 if (create_transport(session) < 0) {
1217 call_result_func(session, 409);
1222 static guint do_request(GWeb *web, const char *url,
1223 const char *type, GWebInputFunc input,
1224 int fd, gsize length, GWebResultFunc func,
1227 struct web_session *session;
1229 if (web == NULL || url == NULL)
1232 debug(web, "request %s", url);
1234 session = g_try_new0(struct web_session, 1);
1235 if (session == NULL)
1238 if (parse_url(session, url, web->proxy) < 0) {
1239 free_session(session);
1243 debug(web, "address %s", session->address);
1244 debug(web, "port %u", session->port);
1245 debug(web, "host %s", session->host);
1246 debug(web, "flags %lu", session->flags);
1247 debug(web, "request %s", session->request);
1250 session->content_type = g_strdup(type);
1252 debug(web, "content-type %s", session->content_type);
1257 session->result_func = func;
1258 session->input_func = input;
1260 session->length = length;
1261 session->offset = 0;
1262 session->user_data = user_data;
1264 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1265 if (session->receive_buffer == NULL) {
1266 free_session(session);
1270 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1272 if (session->result.headers == NULL) {
1273 free_session(session);
1277 session->receive_space = DEFAULT_BUFFER_SIZE;
1278 session->send_buffer = g_string_sized_new(0);
1279 session->current_header = g_string_sized_new(0);
1280 session->header_done = FALSE;
1281 session->body_done = FALSE;
1283 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1284 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1285 session->host, resolv_result, session);
1286 if (session->resolv_action == 0) {
1287 free_session(session);
1291 struct addrinfo hints;
1295 if (session->address == NULL)
1296 session->address = g_strdup(session->host);
1298 memset(&hints, 0, sizeof(struct addrinfo));
1299 hints.ai_flags = AI_NUMERICHOST;
1300 hints.ai_family = session->web->family;
1302 if (session->addr != NULL) {
1303 freeaddrinfo(session->addr);
1304 session->addr = NULL;
1307 port = g_strdup_printf("%u", session->port);
1308 ret = getaddrinfo(session->address, port, &hints,
1311 if (ret != 0 || session->addr == NULL) {
1312 free_session(session);
1316 if (create_transport(session) < 0) {
1317 free_session(session);
1322 web->session_list = g_list_append(web->session_list, session);
1324 return web->next_query_id++;
1327 guint g_web_request_get(GWeb *web, const char *url,
1328 GWebResultFunc func, gpointer user_data)
1330 return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
1333 guint g_web_request_post(GWeb *web, const char *url,
1334 const char *type, GWebInputFunc input,
1335 GWebResultFunc func, gpointer user_data)
1337 return do_request(web, url, type, input, -1, 0, func, user_data);
1340 guint g_web_request_post_file(GWeb *web, const char *url,
1341 const char *type, const char *file,
1342 GWebResultFunc func, gpointer user_data)
1348 if (stat(file, &st) < 0)
1351 fd = open(file, O_RDONLY);
1355 ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
1362 gboolean g_web_cancel_request(GWeb *web, guint id)
1370 guint16 g_web_result_get_status(GWebResult *result)
1375 return result->status;
1378 gboolean g_web_result_get_chunk(GWebResult *result,
1379 const guint8 **chunk, gsize *length)
1387 *chunk = result->buffer;
1390 *length = result->length;
1395 gboolean g_web_result_get_header(GWebResult *result,
1396 const char *header, const char **value)
1404 *value = g_hash_table_lookup(result->headers, header);
1412 struct _GWebParser {
1416 const char *token_str;
1421 GWebParserFunc func;
1425 GWebParser *g_web_parser_new(const char *begin, const char *end,
1426 GWebParserFunc func, gpointer user_data)
1430 parser = g_try_new0(GWebParser, 1);
1434 parser->ref_count = 1;
1436 parser->begin_token = g_strdup(begin);
1437 parser->end_token = g_strdup(end);
1439 if (parser->begin_token == NULL) {
1444 parser->func = func;
1445 parser->user_data = user_data;
1447 parser->token_str = parser->begin_token;
1448 parser->token_len = strlen(parser->token_str);
1449 parser->token_pos = 0;
1451 parser->intoken = FALSE;
1452 parser->content = g_string_sized_new(0);
1457 GWebParser *g_web_parser_ref(GWebParser *parser)
1462 __sync_fetch_and_add(&parser->ref_count, 1);
1467 void g_web_parser_unref(GWebParser *parser)
1472 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1475 g_string_free(parser->content, TRUE);
1477 g_free(parser->begin_token);
1478 g_free(parser->end_token);
1482 void g_web_parser_feed_data(GWebParser *parser,
1483 const guint8 *data, gsize length)
1485 const guint8 *ptr = data;
1490 while (length > 0) {
1491 guint8 chr = parser->token_str[parser->token_pos];
1493 if (parser->token_pos == 0) {
1496 pos = memchr(ptr, chr, length);
1498 if (parser->intoken == TRUE)
1499 g_string_append_len(parser->content,
1500 (gchar *) ptr, length);
1504 if (parser->intoken == TRUE)
1505 g_string_append_len(parser->content,
1506 (gchar *) ptr, (pos - ptr) + 1);
1508 length -= (pos - ptr) + 1;
1511 parser->token_pos++;
1515 if (parser->intoken == TRUE)
1516 g_string_append_c(parser->content, ptr[0]);
1518 if (ptr[0] != chr) {
1522 parser->token_pos = 0;
1529 parser->token_pos++;
1531 if (parser->token_pos == parser->token_len) {
1532 if (parser->intoken == FALSE) {
1533 g_string_append(parser->content,
1536 parser->intoken = TRUE;
1537 parser->token_str = parser->end_token;
1538 parser->token_len = strlen(parser->end_token);
1539 parser->token_pos = 0;
1542 str = g_string_free(parser->content, FALSE);
1543 parser->content = g_string_sized_new(0);
1545 parser->func(str, parser->user_data);
1548 parser->intoken = FALSE;
1549 parser->token_str = parser->begin_token;
1550 parser->token_len = strlen(parser->begin_token);
1551 parser->token_pos = 0;
1557 void g_web_parser_end_data(GWebParser *parser)