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 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 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
274 web->debug_func = func;
275 web->debug_data = user_data;
277 g_resolv_set_debug(web->resolv, func, user_data);
280 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
289 debug(web, "clearing proxy");
291 web->proxy = g_strdup(proxy);
292 debug(web, "setting proxy %s", web->proxy);
298 gboolean g_web_set_address_family(GWeb *web, int family)
303 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
306 web->family = family;
308 g_resolv_set_address_family(web->resolv, family);
313 gboolean g_web_add_nameserver(GWeb *web, const char *address)
318 g_resolv_add_nameserver(web->resolv, address, 53, 0);
323 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
325 g_free(web->accept_option);
327 if (format == NULL) {
328 web->accept_option = NULL;
329 debug(web, "clearing accept option");
331 web->accept_option = g_strdup_vprintf(format, args);
332 debug(web, "setting accept %s", web->accept_option);
338 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
346 va_start(args, format);
347 result = set_accept_option(web, format, args);
353 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
355 g_free(web->user_agent);
357 if (format == NULL) {
358 web->user_agent = NULL;
359 debug(web, "clearing user agent");
361 web->user_agent = g_strdup_vprintf(format, args);
362 debug(web, "setting user agent %s", web->user_agent);
368 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
376 va_start(args, format);
377 result = set_user_agent(web, format, args);
383 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
388 g_free(web->user_agent_profile);
390 web->user_agent_profile = g_strdup(profile);
391 debug(web, "setting user agent profile %s", web->user_agent);
396 gboolean g_web_set_http_version(GWeb *web, const char *version)
401 g_free(web->http_version);
403 if (version == NULL) {
404 web->http_version = NULL;
405 debug(web, "clearing HTTP version");
407 web->http_version = g_strdup(version);
408 debug(web, "setting HTTP version %s", web->http_version);
414 void g_web_set_close_connection(GWeb *web, gboolean enabled)
419 web->close_connection = enabled;
422 gboolean g_web_get_close_connection(GWeb *web)
427 return web->close_connection;
430 static inline void call_result_func(struct web_session *session, guint16 status)
434 if (session->result_func == NULL)
438 session->result.status = status;
440 result = session->result_func(&session->result, session->user_data);
442 debug(session->web, "[result function] %s",
443 result == TRUE ? "continue" : "stop");
446 static inline void call_route_func(struct web_session *session)
448 if (session->route_func != NULL)
449 session->route_func(session->address, session->addr->ai_family,
450 session->web->index, session->user_data);
453 static gboolean process_send_buffer(struct web_session *session)
456 gsize count, bytes_written;
462 buf = session->send_buffer;
466 if (session->request_started == TRUE &&
467 session->more_data == FALSE &&
469 session->body_done = TRUE;
474 status = g_io_channel_write_chars(session->transport_channel,
475 buf->str, count, &bytes_written, NULL);
477 debug(session->web, "status %u bytes to write %zu bytes written %zu",
478 status, count, bytes_written);
480 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
483 g_string_erase(buf, 0, bytes_written);
488 static gboolean process_send_file(struct web_session *session)
494 if (session->fd == -1)
497 if (session->request_started == FALSE || session->more_data == TRUE)
500 sk = g_io_channel_unix_get_fd(session->transport_channel);
504 offset = session->offset;
506 bytes_sent = sendfile(sk, session->fd, &offset, session->length);
508 debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
509 errno, session->length, bytes_sent);
511 if (bytes_sent < 0 && errno != EAGAIN)
514 session->offset = offset;
515 session->length -= bytes_sent;
517 if (session->length == 0) {
518 session->body_done = TRUE;
525 static void process_next_chunk(struct web_session *session)
527 GString *buf = session->send_buffer;
531 if (session->input_func == NULL) {
532 session->more_data = FALSE;
536 session->more_data = session->input_func(&body, &length,
540 g_string_append_printf(buf, "%zx\r\n", length);
541 g_string_append_len(buf, (char *) body, length);
542 g_string_append(buf, "\r\n");
545 if (session->more_data == FALSE)
546 g_string_append(buf, "0\r\n\r\n");
549 static void start_request(struct web_session *session)
551 GString *buf = session->send_buffer;
556 debug(session->web, "request %s from %s",
557 session->request, session->host);
559 g_string_truncate(buf, 0);
561 if (session->web->http_version == NULL)
564 version = session->web->http_version;
566 if (session->content_type == NULL)
567 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
568 session->request, version);
570 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
571 session->request, version);
573 g_string_append_printf(buf, "Host: %s\r\n", session->host);
575 if (session->web->user_agent != NULL)
576 g_string_append_printf(buf, "User-Agent: %s\r\n",
577 session->web->user_agent);
579 if (session->web->user_agent_profile != NULL) {
580 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
581 session->web->user_agent_profile);
584 if (session->web->accept_option != NULL)
585 g_string_append_printf(buf, "Accept: %s\r\n",
586 session->web->accept_option);
588 if (session->content_type != NULL) {
589 g_string_append_printf(buf, "Content-Type: %s\r\n",
590 session->content_type);
591 if (session->input_func == NULL) {
592 session->more_data = FALSE;
593 length = session->length;
595 session->more_data = session->input_func(&body, &length,
597 if (session->more_data == FALSE)
598 g_string_append_printf(buf, "Content-Length: %zu\r\n",
601 g_string_append(buf, "Transfer-Encoding: chunked\r\n");
604 if (session->web->close_connection == TRUE)
605 g_string_append(buf, "Connection: close\r\n");
607 g_string_append(buf, "\r\n");
609 if (session->content_type != NULL && length > 0) {
610 if (session->more_data == TRUE) {
611 g_string_append_printf(buf, "%zx\r\n", length);
612 g_string_append_len(buf, (char *) body, length);
613 g_string_append(buf, "\r\n");
614 } else if (session->fd == -1)
615 g_string_append_len(buf, (char *) body, length);
619 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
622 struct web_session *session = user_data;
624 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
625 session->send_watch = 0;
629 if (process_send_buffer(session) == TRUE)
632 if (process_send_file(session) == TRUE)
635 if (session->request_started == FALSE) {
636 session->request_started = TRUE;
637 start_request(session);
638 } else if (session->more_data == TRUE)
639 process_next_chunk(session);
641 process_send_buffer(session);
643 if (session->body_done == TRUE) {
644 session->send_watch = 0;
651 static int decode_chunked(struct web_session *session,
652 const guint8 *buf, gsize len)
654 const guint8 *ptr = buf;
662 switch (session->chunck_state) {
664 pos = memchr(ptr, '\n', len);
666 g_string_append_len(session->current_header,
672 if (count < 1 || ptr[count - 1] != '\r')
675 g_string_append_len(session->current_header,
676 (gchar *) ptr, count);
681 str = session->current_header->str;
683 counter = strtoul(str, NULL, 16);
684 if ((counter == 0 && errno == EINVAL) ||
685 counter == ULONG_MAX)
688 session->chunk_size = counter;
689 session->chunk_left = counter;
691 session->chunck_state = CHUNK_DATA;
698 session->chunck_state = CHUNK_N_BODY;
705 session->chunck_state = CHUNK_SIZE;
708 if (session->chunk_size == 0) {
709 debug(session->web, "Download Done in chunk");
710 g_string_truncate(session->current_header, 0);
714 if (session->chunk_left <= len) {
715 session->result.buffer = ptr;
716 session->result.length = session->chunk_left;
717 call_result_func(session, 0);
719 len -= session->chunk_left;
720 ptr += session->chunk_left;
722 session->total_len += session->chunk_left;
723 session->chunk_left = 0;
725 g_string_truncate(session->current_header, 0);
726 session->chunck_state = CHUNK_R_BODY;
730 session->result.buffer = ptr;
731 session->result.length = len;
732 call_result_func(session, 0);
734 session->chunk_left -= len;
735 session->total_len += len;
746 static int handle_body(struct web_session *session,
747 const guint8 *buf, gsize len)
751 debug(session->web, "[body] length %zu", len);
753 if (session->result.use_chunk == FALSE) {
755 session->result.buffer = buf;
756 session->result.length = len;
757 call_result_func(session, 0);
762 err = decode_chunked(session, buf, len);
764 debug(session->web, "Error in chunk decode %d", err);
766 session->result.buffer = NULL;
767 session->result.length = 0;
768 call_result_func(session, 400);
774 static void handle_multi_line(struct web_session *session)
780 str = session->current_header->str;
782 if (str[0] != ' ' && str[0] != '\t')
785 while (str[0] == ' ' || str[0] == '\t')
788 count = str - session->current_header->str;
790 g_string_erase(session->current_header, 0, count);
791 g_string_insert_c(session->current_header, 0, ' ');
794 value = g_hash_table_lookup(session->result.headers,
795 session->result.last_key);
797 g_string_insert(session->current_header, 0, value);
799 str = session->current_header->str;
801 g_hash_table_replace(session->result.headers,
802 g_strdup(session->result.last_key),
807 static void add_header_field(struct web_session *session)
815 str = session->current_header->str;
817 pos = memchr(str, ':', session->current_header->len);
824 /* remove preceding white spaces */
828 count = (char *) pos - str;
830 g_string_erase(session->current_header, 0, count);
832 value = g_hash_table_lookup(session->result.headers, key);
834 g_string_insert_c(session->current_header, 0, ' ');
835 g_string_insert_c(session->current_header, 0, ';');
837 g_string_insert(session->current_header, 0, value);
840 str = session->current_header->str;
841 g_hash_table_replace(session->result.headers, key,
844 g_free(session->result.last_key);
845 session->result.last_key = g_strdup(key);
849 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
852 struct web_session *session = user_data;
853 guint8 *ptr = session->receive_buffer;
857 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
858 session->transport_watch = 0;
859 session->result.buffer = NULL;
860 session->result.length = 0;
861 call_result_func(session, 400);
865 status = g_io_channel_read_chars(channel,
866 (gchar *) session->receive_buffer,
867 session->receive_space - 1, &bytes_read, NULL);
869 debug(session->web, "bytes read %zu", bytes_read);
871 if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
872 session->transport_watch = 0;
873 session->result.buffer = NULL;
874 session->result.length = 0;
875 call_result_func(session, 0);
879 session->receive_buffer[bytes_read] = '\0';
881 if (session->header_done == TRUE) {
882 if (handle_body(session, session->receive_buffer,
884 session->transport_watch = 0;
890 while (bytes_read > 0) {
895 pos = memchr(ptr, '\n', bytes_read);
897 g_string_append_len(session->current_header,
898 (gchar *) ptr, bytes_read);
903 count = strlen((char *) ptr);
904 if (count > 0 && ptr[count - 1] == '\r') {
909 g_string_append_len(session->current_header,
910 (gchar *) ptr, count);
912 bytes_read -= count + 1;
918 if (session->current_header->len == 0) {
921 session->header_done = TRUE;
923 val = g_hash_table_lookup(session->result.headers,
924 "Transfer-Encoding");
926 val = g_strrstr(val, "chunked");
928 session->result.use_chunk = TRUE;
930 session->chunck_state = CHUNK_SIZE;
931 session->chunk_left = 0;
932 session->total_len = 0;
936 if (handle_body(session, ptr, bytes_read) < 0) {
937 session->transport_watch = 0;
943 str = session->current_header->str;
945 if (session->result.status == 0) {
948 if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
949 session->result.status = code;
952 debug(session->web, "[header] %s", str);
954 /* handle multi-line header */
955 if (str[0] == ' ' || str[0] == '\t')
956 handle_multi_line(session);
958 add_header_field(session);
960 g_string_truncate(session->current_header, 0);
966 static int bind_to_address(int sk, const char *interface, int family)
968 struct ifaddrs *ifaddr_list, *ifaddr;
971 if (getifaddrs(&ifaddr_list) < 0)
974 for (ifaddr = ifaddr_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
975 if (g_strcmp0(ifaddr->ifa_name, interface) != 0)
978 if (ifaddr->ifa_addr == NULL ||
979 ifaddr->ifa_addr->sa_family != family)
984 size = sizeof(struct sockaddr_in);
987 size = sizeof(struct sockaddr_in6);
993 err = bind(sk, (struct sockaddr *) ifaddr->ifa_addr, size);
997 freeifaddrs(ifaddr_list);
1001 static inline int bind_socket(int sk, int index, int family)
1003 char interface[IF_NAMESIZE];
1006 if (if_indextoname(index, interface) == NULL)
1009 err = setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
1010 interface, IF_NAMESIZE);
1012 err = bind_to_address(sk, interface, family);
1017 static int connect_session_transport(struct web_session *session)
1022 sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
1027 if (session->web->index > 0) {
1028 if (bind_socket(sk, session->web->index,
1029 session->addr->ai_family) < 0) {
1030 debug(session->web, "bind() %s", strerror(errno));
1036 if (session->flags & SESSION_FLAG_USE_TLS) {
1037 debug(session->web, "using TLS encryption");
1038 session->transport_channel = g_io_channel_gnutls_new(sk);
1040 debug(session->web, "no encryption");
1041 session->transport_channel = g_io_channel_unix_new(sk);
1044 if (session->transport_channel == NULL) {
1045 debug(session->web, "channel missing");
1050 flags = g_io_channel_get_flags(session->transport_channel);
1051 g_io_channel_set_flags(session->transport_channel,
1052 flags | G_IO_FLAG_NONBLOCK, NULL);
1054 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
1055 g_io_channel_set_buffered(session->transport_channel, FALSE);
1057 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1059 if (connect(sk, session->addr->ai_addr,
1060 session->addr->ai_addrlen) < 0) {
1061 if (errno != EINPROGRESS) {
1062 debug(session->web, "connect() %s", strerror(errno));
1068 session->transport_watch = g_io_add_watch(session->transport_channel,
1069 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1070 received_data, session);
1072 session->send_watch = g_io_add_watch(session->transport_channel,
1073 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1074 send_data, session);
1079 static int create_transport(struct web_session *session)
1083 err = connect_session_transport(session);
1087 debug(session->web, "creating session %s:%u",
1088 session->address, session->port);
1093 static int parse_url(struct web_session *session,
1094 const char *url, const char *proxy)
1096 char *scheme, *host, *port, *path;
1098 scheme = g_strdup(url);
1102 host = strstr(scheme, "://");
1107 if (strcasecmp(scheme, "https") == 0) {
1108 session->port = 443;
1109 session->flags |= SESSION_FLAG_USE_TLS;
1110 } else if (strcasecmp(scheme, "http") == 0) {
1121 path = strchr(host, '/');
1126 session->request = g_strdup_printf("/%s", path ? path : "");
1128 session->request = g_strdup(url);
1130 port = strrchr(host, ':');
1133 int tmp = strtol(port + 1, &end, 10);
1137 session->port = tmp;
1141 session->host = g_strdup(host);
1143 session->host = g_strdup_printf("%s:%u", host, tmp);
1145 session->host = g_strdup(host);
1152 scheme = g_strdup(proxy);
1156 host = strstr(proxy, "://");
1161 if (strcasecmp(scheme, "http") != 0) {
1168 path = strchr(host, '/');
1172 port = strrchr(host, ':');
1175 int tmp = strtol(port + 1, &end, 10);
1179 session->port = tmp;
1183 session->address = g_strdup(host);
1190 static void resolv_result(GResolvResultStatus status,
1191 char **results, gpointer user_data)
1193 struct web_session *session = user_data;
1194 struct addrinfo hints;
1198 if (results == NULL || results[0] == NULL) {
1199 call_result_func(session, 404);
1203 debug(session->web, "address %s", results[0]);
1205 memset(&hints, 0, sizeof(struct addrinfo));
1206 hints.ai_flags = AI_NUMERICHOST;
1207 hints.ai_family = session->web->family;
1209 if (session->addr != NULL) {
1210 freeaddrinfo(session->addr);
1211 session->addr = NULL;
1214 port = g_strdup_printf("%u", session->port);
1215 ret = getaddrinfo(results[0], port, &hints, &session->addr);
1217 if (ret != 0 || session->addr == NULL) {
1218 call_result_func(session, 400);
1222 session->address = g_strdup(results[0]);
1223 call_route_func(session);
1225 if (create_transport(session) < 0) {
1226 call_result_func(session, 409);
1231 static guint do_request(GWeb *web, const char *url,
1232 const char *type, GWebInputFunc input,
1233 int fd, gsize length, GWebResultFunc func,
1234 GWebRouteFunc route, gpointer user_data)
1236 struct web_session *session;
1238 if (web == NULL || url == NULL)
1241 debug(web, "request %s", url);
1243 session = g_try_new0(struct web_session, 1);
1244 if (session == NULL)
1247 if (parse_url(session, url, web->proxy) < 0) {
1248 free_session(session);
1252 debug(web, "address %s", session->address);
1253 debug(web, "port %u", session->port);
1254 debug(web, "host %s", session->host);
1255 debug(web, "flags %lu", session->flags);
1256 debug(web, "request %s", session->request);
1259 session->content_type = g_strdup(type);
1261 debug(web, "content-type %s", session->content_type);
1266 session->result_func = func;
1267 session->route_func = route;
1268 session->input_func = input;
1270 session->length = length;
1271 session->offset = 0;
1272 session->user_data = user_data;
1274 session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1275 if (session->receive_buffer == NULL) {
1276 free_session(session);
1280 session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1282 if (session->result.headers == NULL) {
1283 free_session(session);
1287 session->receive_space = DEFAULT_BUFFER_SIZE;
1288 session->send_buffer = g_string_sized_new(0);
1289 session->current_header = g_string_sized_new(0);
1290 session->header_done = FALSE;
1291 session->body_done = FALSE;
1293 if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1294 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1295 session->host, resolv_result, session);
1296 if (session->resolv_action == 0) {
1297 free_session(session);
1301 struct addrinfo hints;
1305 if (session->address == NULL)
1306 session->address = g_strdup(session->host);
1308 memset(&hints, 0, sizeof(struct addrinfo));
1309 hints.ai_flags = AI_NUMERICHOST;
1310 hints.ai_family = session->web->family;
1312 if (session->addr != NULL) {
1313 freeaddrinfo(session->addr);
1314 session->addr = NULL;
1317 port = g_strdup_printf("%u", session->port);
1318 ret = getaddrinfo(session->address, port, &hints,
1321 if (ret != 0 || session->addr == NULL) {
1322 free_session(session);
1326 if (create_transport(session) < 0) {
1327 free_session(session);
1332 web->session_list = g_list_append(web->session_list, session);
1334 return web->next_query_id++;
1337 guint g_web_request_get(GWeb *web, const char *url, GWebResultFunc func,
1338 GWebRouteFunc route, gpointer user_data)
1340 return do_request(web, url, NULL, NULL, -1, 0, func, route, user_data);
1343 guint g_web_request_post(GWeb *web, const char *url,
1344 const char *type, GWebInputFunc input,
1345 GWebResultFunc func, gpointer user_data)
1347 return do_request(web, url, type, input, -1, 0, func, NULL, user_data);
1350 guint g_web_request_post_file(GWeb *web, const char *url,
1351 const char *type, const char *file,
1352 GWebResultFunc func, gpointer user_data)
1358 if (stat(file, &st) < 0)
1361 fd = open(file, O_RDONLY);
1365 ret = do_request(web, url, type, NULL, fd, st.st_size, func, NULL,
1373 gboolean g_web_cancel_request(GWeb *web, guint id)
1381 guint16 g_web_result_get_status(GWebResult *result)
1386 return result->status;
1389 gboolean g_web_result_get_chunk(GWebResult *result,
1390 const guint8 **chunk, gsize *length)
1398 *chunk = result->buffer;
1401 *length = result->length;
1406 gboolean g_web_result_get_header(GWebResult *result,
1407 const char *header, const char **value)
1415 *value = g_hash_table_lookup(result->headers, header);
1423 struct _GWebParser {
1427 const char *token_str;
1432 GWebParserFunc func;
1436 GWebParser *g_web_parser_new(const char *begin, const char *end,
1437 GWebParserFunc func, gpointer user_data)
1441 parser = g_try_new0(GWebParser, 1);
1445 parser->ref_count = 1;
1447 parser->begin_token = g_strdup(begin);
1448 parser->end_token = g_strdup(end);
1450 if (parser->begin_token == NULL) {
1455 parser->func = func;
1456 parser->user_data = user_data;
1458 parser->token_str = parser->begin_token;
1459 parser->token_len = strlen(parser->token_str);
1460 parser->token_pos = 0;
1462 parser->intoken = FALSE;
1463 parser->content = g_string_sized_new(0);
1468 GWebParser *g_web_parser_ref(GWebParser *parser)
1473 __sync_fetch_and_add(&parser->ref_count, 1);
1478 void g_web_parser_unref(GWebParser *parser)
1483 if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1486 g_string_free(parser->content, TRUE);
1488 g_free(parser->begin_token);
1489 g_free(parser->end_token);
1493 void g_web_parser_feed_data(GWebParser *parser,
1494 const guint8 *data, gsize length)
1496 const guint8 *ptr = data;
1501 while (length > 0) {
1502 guint8 chr = parser->token_str[parser->token_pos];
1504 if (parser->token_pos == 0) {
1507 pos = memchr(ptr, chr, length);
1509 if (parser->intoken == TRUE)
1510 g_string_append_len(parser->content,
1511 (gchar *) ptr, length);
1515 if (parser->intoken == TRUE)
1516 g_string_append_len(parser->content,
1517 (gchar *) ptr, (pos - ptr) + 1);
1519 length -= (pos - ptr) + 1;
1522 parser->token_pos++;
1526 if (parser->intoken == TRUE)
1527 g_string_append_c(parser->content, ptr[0]);
1529 if (ptr[0] != chr) {
1533 parser->token_pos = 0;
1540 parser->token_pos++;
1542 if (parser->token_pos == parser->token_len) {
1543 if (parser->intoken == FALSE) {
1544 g_string_append(parser->content,
1547 parser->intoken = TRUE;
1548 parser->token_str = parser->end_token;
1549 parser->token_len = strlen(parser->end_token);
1550 parser->token_pos = 0;
1553 str = g_string_free(parser->content, FALSE);
1554 parser->content = g_string_sized_new(0);
1556 parser->func(str, parser->user_data);
1559 parser->intoken = FALSE;
1560 parser->token_str = parser->begin_token;
1561 parser->token_len = strlen(parser->begin_token);
1562 parser->token_pos = 0;
1568 void g_web_parser_end_data(GWebParser *parser)