3 * Web service library with GLib integration
5 * Copyright (C) 2009-2012 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 GWebRouteFunc route_func;
102 GWebInputFunc input_func;
123 char *user_agent_profile;
125 gboolean close_connection;
127 GWebDebugFunc debug_func;
131 static inline void debug(GWeb *web, const char *format, ...)
136 if (web->debug_func == NULL)
139 va_start(ap, format);
141 if (vsnprintf(str, sizeof(str), format, ap) > 0)
142 web->debug_func(str, web->debug_data);
147 static void free_session(struct web_session *session)
154 g_free(session->request);
157 if (session->resolv_action > 0)
158 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
160 if (session->transport_watch > 0)
161 g_source_remove(session->transport_watch);
163 if (session->send_watch > 0)
164 g_source_remove(session->send_watch);
166 if (session->transport_channel != NULL)
167 g_io_channel_unref(session->transport_channel);
169 g_free(session->result.last_key);
171 if (session->result.headers != NULL)
172 g_hash_table_destroy(session->result.headers);
174 if (session->send_buffer != NULL)
175 g_string_free(session->send_buffer, TRUE);
177 if (session->current_header != NULL)
178 g_string_free(session->current_header, TRUE);
180 g_free(session->receive_buffer);
182 g_free(session->content_type);
184 g_free(session->host);
185 g_free(session->address);
186 if (session->addr != NULL)
187 freeaddrinfo(session->addr);
192 static void flush_sessions(GWeb *web)
196 for (list = g_list_first(web->session_list);
197 list; list = g_list_next(list))
198 free_session(list->data);
200 g_list_free(web->session_list);
201 web->session_list = NULL;
204 GWeb *g_web_new(int index)
211 web = g_try_new0(GWeb, 1);
217 web->next_query_id = 1;
219 web->family = AF_UNSPEC;
222 web->session_list = NULL;
224 web->resolv = g_resolv_new(index);
225 if (web->resolv == NULL) {
230 web->accept_option = g_strdup("*/*");
231 web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
232 web->close_connection = FALSE;
237 GWeb *g_web_ref(GWeb *web)
242 __sync_fetch_and_add(&web->ref_count, 1);
247 void g_web_unref(GWeb *web)
252 if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
257 g_resolv_unref(web->resolv);
261 g_free(web->accept_option);
262 g_free(web->user_agent);
263 g_free(web->user_agent_profile);
264 g_free(web->http_version);
269 gboolean g_web_supports_tls(void)
271 return g_io_channel_supports_tls();
274 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
279 web->debug_func = func;
280 web->debug_data = user_data;
282 g_resolv_set_debug(web->resolv, func, user_data);
285 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
294 debug(web, "clearing proxy");
296 web->proxy = g_strdup(proxy);
297 debug(web, "setting proxy %s", web->proxy);
303 gboolean g_web_set_address_family(GWeb *web, int family)
308 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
311 web->family = family;
313 g_resolv_set_address_family(web->resolv, family);
318 gboolean g_web_add_nameserver(GWeb *web, const char *address)
323 g_resolv_add_nameserver(web->resolv, address, 53, 0);
328 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
330 g_free(web->accept_option);
332 if (format == NULL) {
333 web->accept_option = NULL;
334 debug(web, "clearing accept option");
336 web->accept_option = g_strdup_vprintf(format, args);
337 debug(web, "setting accept %s", web->accept_option);
343 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
351 va_start(args, format);
352 result = set_accept_option(web, format, args);
358 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
360 g_free(web->user_agent);
362 if (format == NULL) {
363 web->user_agent = NULL;
364 debug(web, "clearing user agent");
366 web->user_agent = g_strdup_vprintf(format, args);
367 debug(web, "setting user agent %s", web->user_agent);
373 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
381 va_start(args, format);
382 result = set_user_agent(web, format, args);
388 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
393 g_free(web->user_agent_profile);
395 web->user_agent_profile = g_strdup(profile);
396 debug(web, "setting user agent profile %s", web->user_agent);
401 gboolean g_web_set_http_version(GWeb *web, const char *version)
406 g_free(web->http_version);
408 if (version == NULL) {
409 web->http_version = NULL;
410 debug(web, "clearing HTTP version");
412 web->http_version = g_strdup(version);
413 debug(web, "setting HTTP version %s", web->http_version);
419 void g_web_set_close_connection(GWeb *web, gboolean enabled)
424 web->close_connection = enabled;
427 gboolean g_web_get_close_connection(GWeb *web)
432 return web->close_connection;
435 static inline void call_result_func(struct web_session *session, guint16 status)
438 if (session->result_func == NULL)
442 session->result.status = status;
444 session->result_func(&session->result, session->user_data);
448 static inline void call_route_func(struct web_session *session)
450 if (session->route_func != NULL)
451 session->route_func(session->address, session->addr->ai_family,
452 session->web->index, session->user_data);
455 static gboolean process_send_buffer(struct web_session *session)
458 gsize count, bytes_written;
464 buf = session->send_buffer;
468 if (session->request_started == TRUE &&
469 session->more_data == FALSE &&
471 session->body_done = TRUE;
476 status = g_io_channel_write_chars(session->transport_channel,
477 buf->str, count, &bytes_written, NULL);
479 debug(session->web, "status %u bytes to write %zu bytes written %zu",
480 status, count, bytes_written);
482 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
485 g_string_erase(buf, 0, bytes_written);
490 static gboolean process_send_file(struct web_session *session)
496 if (session->fd == -1)
499 if (session->request_started == FALSE || session->more_data == TRUE)
502 sk = g_io_channel_unix_get_fd(session->transport_channel);
506 offset = session->offset;
508 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
510 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
511 errno, session->length, bytes_sent);
513 if (bytes_sent < 0 && errno != EAGAIN)
516 session->offset = offset;
517 session->length -= bytes_sent;
519 if (session->length == 0) {
520 session->body_done = TRUE;
527 static void process_next_chunk(struct web_session *session)
529 GString *buf = session->send_buffer;
533 if (session->input_func == NULL) {
534 session->more_data = FALSE;
538 session->more_data = session->input_func(&body, &length,
542 g_string_append_printf(buf, "%zx\r\n", length);
543 g_string_append_len(buf, (char *) body, length);
544 g_string_append(buf, "\r\n");
547 if (session->more_data == FALSE)
548 g_string_append(buf, "0\r\n\r\n");
551 static void start_request(struct web_session *session)
553 GString *buf = session->send_buffer;
558 debug(session->web, "request %s from %s",
559 session->request, session->host);
561 g_string_truncate(buf, 0);
563 if (session->web->http_version == NULL)
566 version = session->web->http_version;
568 if (session->content_type == NULL)
569 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
570 session->request, version);
572 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
573 session->request, version);
575 g_string_append_printf(buf, "Host: %s\r\n", session->host);
577 if (session->web->user_agent != NULL)
578 g_string_append_printf(buf, "User-Agent: %s\r\n",
579 session->web->user_agent);
581 if (session->web->user_agent_profile != NULL) {
582 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
583 session->web->user_agent_profile);
586 if (session->web->accept_option != NULL)
587 g_string_append_printf(buf, "Accept: %s\r\n",
588 session->web->accept_option);
590 if (session->content_type != NULL) {
591 g_string_append_printf(buf, "Content-Type: %s\r\n",
592 session->content_type);
593 if (session->input_func == NULL) {
594 session->more_data = FALSE;
595 length = session->length;
597 session->more_data = session->input_func(&body, &length,
599 if (session->more_data == FALSE)
600 g_string_append_printf(buf, "Content-Length: %zu\r\n",
603 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
606 if (session->web->close_connection == TRUE)
607 g_string_append(buf, "Connection: close\r\n");
609 g_string_append(buf, "\r\n");
611 if (session->content_type != NULL && length > 0) {
612 if (session->more_data == TRUE) {
613 g_string_append_printf(buf, "%zx\r\n", length);
614 g_string_append_len(buf, (char *) body, length);
615 g_string_append(buf, "\r\n");
616 } else if (session->fd == -1)
617 g_string_append_len(buf, (char *) body, length);
621 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
624 struct web_session *session = user_data;
626 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
627 session->send_watch = 0;
631 if (process_send_buffer(session) == TRUE)
634 if (process_send_file(session) == TRUE)
637 if (session->request_started == FALSE) {
638 session->request_started = TRUE;
639 start_request(session);
640 } else if (session->more_data == TRUE)
641 process_next_chunk(session);
643 process_send_buffer(session);
645 if (session->body_done == TRUE) {
646 session->send_watch = 0;
653 static int decode_chunked(struct web_session *session,
654 const guint8 *buf, gsize len)
656 const guint8 *ptr = buf;
664 switch (session->chunck_state) {
666 pos = memchr(ptr, '\n', len);
668 g_string_append_len(session->current_header,
674 if (count < 1 || ptr[count - 1] != '\r')
677 g_string_append_len(session->current_header,
678 (gchar *) ptr, count);
683 str = session->current_header->str;
685 counter = strtoul(str, NULL, 16);
686 if ((counter == 0 && errno == EINVAL) ||
687 counter == ULONG_MAX)
690 session->chunk_size = counter;
691 session->chunk_left = counter;
693 session->chunck_state = CHUNK_DATA;
700 session->chunck_state = CHUNK_N_BODY;
707 session->chunck_state = CHUNK_SIZE;
710 if (session->chunk_size == 0) {
711 debug(session->web, "Download Done in chunk");
712 g_string_truncate(session->current_header, 0);
716 if (session->chunk_left <= len) {
717 session->result.buffer = ptr;
718 session->result.length = session->chunk_left;
719 call_result_func(session, 0);
721 len -= session->chunk_left;
722 ptr += session->chunk_left;
724 session->total_len += session->chunk_left;
725 session->chunk_left = 0;
727 g_string_truncate(session->current_header, 0);
728 session->chunck_state = CHUNK_R_BODY;
732 session->result.buffer = ptr;
733 session->result.length = len;
734 call_result_func(session, 0);
736 session->chunk_left -= len;
737 session->total_len += len;
748 static int handle_body(struct web_session *session,
749 const guint8 *buf, gsize len)
753 debug(session->web, "[body] length %zu", len);
755 if (session->result.use_chunk == FALSE) {
757 session->result.buffer = buf;
758 session->result.length = len;
759 call_result_func(session, 0);
764 err = decode_chunked(session, buf, len);
766 debug(session->web, "Error in chunk decode %d", err);
768 session->result.buffer = NULL;
769 session->result.length = 0;
770 call_result_func(session, 400);
776 static void handle_multi_line(struct web_session *session)
782 str = session->current_header->str;
784 if (str[0] != ' ' && str[0] != '\t')
787 while (str[0] == ' ' || str[0] == '\t')
790 count = str - session->current_header->str;
792 g_string_erase(session->current_header, 0, count);
793 g_string_insert_c(session->current_header, 0, ' ');
796 value = g_hash_table_lookup(session->result.headers,
797 session->result.last_key);
799 g_string_insert(session->current_header, 0, value);
801 str = session->current_header->str;
803 g_hash_table_replace(session->result.headers,
804 g_strdup(session->result.last_key),
809 static void add_header_field(struct web_session *session)
817 str = session->current_header->str;
819 pos = memchr(str, ':', session->current_header->len);
826 /* remove preceding white spaces */
830 count = (char *) pos - str;
832 g_string_erase(session->current_header, 0, count);
834 value = g_hash_table_lookup(session->result.headers, key);
836 g_string_insert_c(session->current_header, 0, ' ');
837 g_string_insert_c(session->current_header, 0, ';');
839 g_string_insert(session->current_header, 0, value);
842 str = session->current_header->str;
843 g_hash_table_replace(session->result.headers, key,
846 g_free(session->result.last_key);
847 session->result.last_key = g_strdup(key);
851 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
854 struct web_session *session = user_data;
855 guint8 *ptr = session->receive_buffer;
859 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
860 session->transport_watch = 0;
861 session->result.buffer = NULL;
862 session->result.length = 0;
863 call_result_func(session, 400);
867 status = g_io_channel_read_chars(channel,
868 (gchar *) session->receive_buffer,
869 session->receive_space - 1, &bytes_read, NULL);
871 debug(session->web, "bytes read %zu", bytes_read);
873 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
874 session->transport_watch = 0;
875 session->result.buffer = NULL;
876 session->result.length = 0;
877 call_result_func(session, 0);
881 session->receive_buffer[bytes_read] = '\0';
883 if (session->header_done == TRUE) {
884 if (handle_body(session, session->receive_buffer,
886 session->transport_watch = 0;
892 while (bytes_read > 0) {
897 pos = memchr(ptr, '\n', bytes_read);
899 g_string_append_len(session->current_header,
900 (gchar *) ptr, bytes_read);
905 count = strlen((char *) ptr);
906 if (count > 0 && ptr[count - 1] == '\r') {
911 g_string_append_len(session->current_header,
912 (gchar *) ptr, count);
914 bytes_read -= count + 1;
920 if (session->current_header->len == 0) {
923 session->header_done = TRUE;
925 val = g_hash_table_lookup(session->result.headers,
926 "Transfer-Encoding");
928 val = g_strrstr(val, "chunked");
930 session->result.use_chunk = TRUE;
932 session->chunck_state = CHUNK_SIZE;
933 session->chunk_left = 0;
934 session->total_len = 0;
938 if (handle_body(session, ptr, bytes_read) < 0) {
939 session->transport_watch = 0;
945 str = session->current_header->str;
947 if (session->result.status == 0) {
950 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
951 session->result.status = code;
954 debug(session->web, "[header] %s", str);
956 /* handle multi-line header */
957 if (str[0] == ' ' || str[0] == '\t')
958 handle_multi_line(session);
960 add_header_field(session);
962 g_string_truncate(session->current_header, 0);
968 static int bind_to_address(int sk, const char *interface, int family)
970 struct ifaddrs *ifaddr_list, *ifaddr;
973 if (getifaddrs(&ifaddr_list) < 0)
976 for (ifaddr = ifaddr_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
977 if (g_strcmp0(ifaddr->ifa_name, interface) != 0)
980 if (ifaddr->ifa_addr == NULL ||
981 ifaddr->ifa_addr->sa_family != family)
986 size = sizeof(struct sockaddr_in);
989 size = sizeof(struct sockaddr_in6);
995 err = bind(sk, (struct sockaddr *) ifaddr->ifa_addr, size);
999 freeifaddrs(ifaddr_list);
1003 static inline int bind_socket(int sk, int index, int family)
1005 char interface[IF_NAMESIZE];
1008 if (if_indextoname(index, interface) == NULL)
1011 err = setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1012 interface, IF_NAMESIZE);
1014 err = bind_to_address(sk, interface, family);
1019 static int connect_session_transport(struct web_session *session)
1024 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
1029 if (session->web->index > 0) {
1030 if (bind_socket(sk, session->web->index,
1031 session->addr->ai_family) < 0) {
1032 debug(session->web, "bind() %s", strerror(errno));
1038 if (session->flags & SESSION_FLAG_USE_TLS) {
1039 debug(session->web, "using TLS encryption");
1040 session->transport_channel = g_io_channel_gnutls_new(sk);
1042 debug(session->web, "no encryption");
1043 session->transport_channel = g_io_channel_unix_new(sk);
1046 if (session->transport_channel == NULL) {
1047 debug(session->web, "channel missing");
1052 flags = g_io_channel_get_flags(session->transport_channel);
1053 g_io_channel_set_flags(session->transport_channel,
1054 flags | G_IO_FLAG_NONBLOCK, NULL);
1056 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
1057 g_io_channel_set_buffered(session->transport_channel, FALSE);
1059 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1061 if (connect(sk, session->addr->ai_addr,
1062 session->addr->ai_addrlen) < 0) {
1063 if (errno != EINPROGRESS) {
1064 debug(session->web, "connect() %s", strerror(errno));
1070 session->transport_watch = g_io_add_watch(session->transport_channel,
1071 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1072 received_data, session);
1074 session->send_watch = g_io_add_watch(session->transport_channel,
1075 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1076 send_data, session);
1081 static int create_transport(struct web_session *session)
1085 err = connect_session_transport(session);
1089 debug(session->web, "creating session %s:%u",
1090 session->address, session->port);
1095 static int parse_url(struct web_session *session,
1096 const char *url, const char *proxy)
1098 char *scheme, *host, *port, *path;
1100 scheme = g_strdup(url);
1104 host = strstr(scheme, "://");
1109 if (strcasecmp(scheme, "https") == 0) {
1110 session->port = 443;
1111 session->flags |= SESSION_FLAG_USE_TLS;
1112 } else if (strcasecmp(scheme, "http") == 0) {
1123 path = strchr(host, '/');
1128 session->request = g_strdup_printf("/%s", path ? path : "");
1130 session->request = g_strdup(url);
1132 port = strrchr(host, ':');
1135 int tmp = strtol(port + 1, &end, 10);
1139 session->port = tmp;
1143 session->host = g_strdup(host);
1145 session->host = g_strdup_printf("%s:%u", host, tmp);
1147 session->host = g_strdup(host);
1154 scheme = g_strdup(proxy);
1158 host = strstr(proxy, "://");
1163 if (strcasecmp(scheme, "http") != 0) {
1170 path = strchr(host, '/');
1174 port = strrchr(host, ':');
1177 int tmp = strtol(port + 1, &end, 10);
1181 session->port = tmp;
1185 session->address = g_strdup(host);
1192 static void resolv_result(GResolvResultStatus status,
1193 char **results, gpointer user_data)
1195 struct web_session *session = user_data;
1196 struct addrinfo hints;
1200 if (results == NULL || results[0] == NULL) {
1201 call_result_func(session, 404);
1205 debug(session->web, "address %s", results[0]);
1207 memset(&hints, 0, sizeof(struct addrinfo));
1208 hints.ai_flags = AI_NUMERICHOST;
1209 hints.ai_family = session->web->family;
1211 if (session->addr != NULL) {
1212 freeaddrinfo(session->addr);
1213 session->addr = NULL;
1216 port = g_strdup_printf("%u", session->port);
1217 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1219 if (ret != 0 || session->addr == NULL) {
1220 call_result_func(session, 400);
1224 session->address = g_strdup(results[0]);
1225 call_route_func(session);
1227 if (create_transport(session) < 0) {
1228 call_result_func(session, 409);
1233 static guint do_request(GWeb *web, const char *url,
1234 const char *type, GWebInputFunc input,
1235 int fd, gsize length, GWebResultFunc func,
1236 GWebRouteFunc route, gpointer user_data)
1238 struct web_session *session;
1240 if (web == NULL || url == NULL)
1243 debug(web, "request %s", url);
1245 session = g_try_new0(struct web_session, 1);
1246 if (session == NULL)
1249 if (parse_url(session, url, web->proxy) < 0) {
1250 free_session(session);
1254 debug(web, "address %s", session->address);
1255 debug(web, "port %u", session->port);
1256 debug(web, "host %s", session->host);
1257 debug(web, "flags %lu", session->flags);
1258 debug(web, "request %s", session->request);
1261 session->content_type = g_strdup(type);
1263 debug(web, "content-type %s", session->content_type);
1268 session->result_func = func;
1269 session->route_func = route;
1270 session->input_func = input;
1272 session->length = length;
1273 session->offset = 0;
1274 session->user_data = user_data;
1276 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1277 if (session->receive_buffer == NULL) {
1278 free_session(session);
1282 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1284 if (session->result.headers == NULL) {
1285 free_session(session);
1289 session->receive_space = DEFAULT_BUFFER_SIZE;
1290 session->send_buffer = g_string_sized_new(0);
1291 session->current_header = g_string_sized_new(0);
1292 session->header_done = FALSE;
1293 session->body_done = FALSE;
1295 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1296 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1297 session->host, resolv_result, session);
1298 if (session->resolv_action == 0) {
1299 free_session(session);
1303 struct addrinfo hints;
1307 if (session->address == NULL)
1308 session->address = g_strdup(session->host);
1310 memset(&hints, 0, sizeof(struct addrinfo));
1311 hints.ai_flags = AI_NUMERICHOST;
1312 hints.ai_family = session->web->family;
1314 if (session->addr != NULL) {
1315 freeaddrinfo(session->addr);
1316 session->addr = NULL;
1319 port = g_strdup_printf("%u", session->port);
1320 ret = getaddrinfo(session->address, port, &hints,
1323 if (ret != 0 || session->addr == NULL) {
1324 free_session(session);
1328 if (create_transport(session) < 0) {
1329 free_session(session);
1334 web->session_list = g_list_append(web->session_list, session);
1336 return web->next_query_id++;
1339 guint g_web_request_get(GWeb *web, const char *url, GWebResultFunc func,
1340 GWebRouteFunc route, gpointer user_data)
1342 return do_request(web, url, NULL, NULL, -1, 0, func, route, user_data);
1345 guint g_web_request_post(GWeb *web, const char *url,
1346 const char *type, GWebInputFunc input,
1347 GWebResultFunc func, gpointer user_data)
1349 return do_request(web, url, type, input, -1, 0, func, NULL, user_data);
1352 guint g_web_request_post_file(GWeb *web, const char *url,
1353 const char *type, const char *file,
1354 GWebResultFunc func, gpointer user_data)
1360 if (stat(file, &st) < 0)
1363 fd = open(file, O_RDONLY);
1367 ret = do_request(web, url, type, NULL, fd, st.st_size, func, NULL,
1375 gboolean g_web_cancel_request(GWeb *web, guint id)
1383 guint16 g_web_result_get_status(GWebResult *result)
1388 return result->status;
1391 gboolean g_web_result_get_chunk(GWebResult *result,
1392 const guint8 **chunk, gsize *length)
1400 *chunk = result->buffer;
1403 *length = result->length;
1408 gboolean g_web_result_get_header(GWebResult *result,
1409 const char *header, const char **value)
1417 *value = g_hash_table_lookup(result->headers, header);
1425 struct _GWebParser {
1429 const char *token_str;
1434 GWebParserFunc func;
1438 GWebParser *g_web_parser_new(const char *begin, const char *end,
1439 GWebParserFunc func, gpointer user_data)
1443 parser = g_try_new0(GWebParser, 1);
1447 parser->ref_count = 1;
1449 parser->begin_token = g_strdup(begin);
1450 parser->end_token = g_strdup(end);
1452 if (parser->begin_token == NULL) {
1457 parser->func = func;
1458 parser->user_data = user_data;
1460 parser->token_str = parser->begin_token;
1461 parser->token_len = strlen(parser->token_str);
1462 parser->token_pos = 0;
1464 parser->intoken = FALSE;
1465 parser->content = g_string_sized_new(0);
1470 GWebParser *g_web_parser_ref(GWebParser *parser)
1475 __sync_fetch_and_add(&parser->ref_count, 1);
1480 void g_web_parser_unref(GWebParser *parser)
1485 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1488 g_string_free(parser->content, TRUE);
1490 g_free(parser->begin_token);
1491 g_free(parser->end_token);
1495 void g_web_parser_feed_data(GWebParser *parser,
1496 const guint8 *data, gsize length)
1498 const guint8 *ptr = data;
1503 while (length > 0) {
1504 guint8 chr = parser->token_str[parser->token_pos];
1506 if (parser->token_pos == 0) {
1509 pos = memchr(ptr, chr, length);
1511 if (parser->intoken == TRUE)
1512 g_string_append_len(parser->content,
1513 (gchar *) ptr, length);
1517 if (parser->intoken == TRUE)
1518 g_string_append_len(parser->content,
1519 (gchar *) ptr, (pos - ptr) + 1);
1521 length -= (pos - ptr) + 1;
1524 parser->token_pos++;
1528 if (parser->intoken == TRUE)
1529 g_string_append_c(parser->content, ptr[0]);
1531 if (ptr[0] != chr) {
1535 parser->token_pos = 0;
1542 parser->token_pos++;
1544 if (parser->token_pos == parser->token_len) {
1545 if (parser->intoken == FALSE) {
1546 g_string_append(parser->content,
1549 parser->intoken = TRUE;
1550 parser->token_str = parser->end_token;
1551 parser->token_len = strlen(parser->end_token);
1552 parser->token_pos = 0;
1555 str = g_string_free(parser->content, FALSE);
1556 parser->content = g_string_sized_new(0);
1558 parser->func(str, parser->user_data);
1561 parser->intoken = FALSE;
1562 parser->token_str = parser->begin_token;
1563 parser->token_len = strlen(parser->begin_token);
1564 parser->token_pos = 0;
1570 void g_web_parser_end_data(GWebParser *parser)