Update TODO list regarding WPS support
[framework/connectivity/connman.git] / gweb / gweb.c
1 /*
2  *
3  *  Web service library with GLib integration
4  *
5  *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34
35 #include "giognutls.h"
36 #include "gresolv.h"
37 #include "gweb.h"
38
39 #define DEFAULT_BUFFER_SIZE  2048
40
41 #define SESSION_FLAG_USE_TLS    (1 << 0)
42
43 enum chunk_state {
44         CHUNK_SIZE,
45         CHUNK_R_BODY,
46         CHUNK_N_BODY,
47         CHUNK_DATA,
48 };
49
50 struct _GWebResult {
51         guint16 status;
52         const guint8 *buffer;
53         gsize length;
54         gboolean use_chunk;
55 };
56
57 struct web_session {
58         GWeb *web;
59
60         char *address;
61         char *host;
62         uint16_t port;
63         unsigned long flags;
64
65         char *content_type;
66
67         GIOChannel *transport_channel;
68         guint transport_watch;
69         guint send_watch;
70
71         guint resolv_action;
72         char *request;
73
74         guint8 *receive_buffer;
75         gsize receive_space;
76         GString *send_buffer;
77         GString *current_header;
78         gboolean header_done;
79         gboolean body_done;
80         gboolean more_data;
81         gboolean request_started;
82
83         enum chunk_state chunck_state;
84         gsize chunk_size;
85         gsize chunk_left;
86         gsize total_len;
87
88         GWebResult result;
89
90         GWebResultFunc result_func;
91         GWebInputFunc input_func;
92         gpointer user_data;
93 };
94
95 struct _GWeb {
96         gint ref_count;
97
98         guint next_query_id;
99
100         int index;
101         GList *session_list;
102
103         GResolv *resolv;
104         char *proxy;
105         char *accept_option;
106         char *user_agent;
107         char *http_version;
108         gboolean close_connection;
109
110         GWebDebugFunc debug_func;
111         gpointer debug_data;
112 };
113
114 static inline void debug(GWeb *web, const char *format, ...)
115 {
116         char str[256];
117         va_list ap;
118
119         if (web->debug_func == NULL)
120                 return;
121
122         va_start(ap, format);
123
124         if (vsnprintf(str, sizeof(str), format, ap) > 0)
125                 web->debug_func(str, web->debug_data);
126
127         va_end(ap);
128 }
129
130 static void free_session(struct web_session *session)
131 {
132         GWeb *web = session->web;
133
134         if (session == NULL)
135                 return;
136
137         g_free(session->request);
138
139         if (session->resolv_action > 0)
140                 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
141
142         if (session->transport_watch > 0)
143                 g_source_remove(session->transport_watch);
144
145         if (session->send_watch > 0)
146                 g_source_remove(session->send_watch);
147
148         if (session->transport_channel != NULL)
149                 g_io_channel_unref(session->transport_channel);
150
151         g_string_free(session->send_buffer, TRUE);
152         g_string_free(session->current_header, TRUE);
153         g_free(session->receive_buffer);
154
155         g_free(session->content_type);
156
157         g_free(session->host);
158         g_free(session->address);
159         g_free(session);
160 }
161
162 static void flush_sessions(GWeb *web)
163 {
164         GList *list;
165
166         for (list = g_list_first(web->session_list);
167                                         list; list = g_list_next(list))
168                 free_session(list->data);
169
170         g_list_free(web->session_list);
171         web->session_list = NULL;
172 }
173
174 GWeb *g_web_new(int index)
175 {
176         GWeb *web;
177
178         if (index < 0)
179                 return NULL;
180
181         web = g_try_new0(GWeb, 1);
182         if (web == NULL)
183                 return NULL;
184
185         web->ref_count = 1;
186
187         web->next_query_id = 1;
188
189         web->index = index;
190         web->session_list = NULL;
191
192         web->resolv = g_resolv_new(index);
193         if (web->resolv == NULL) {
194                 g_free(web);
195                 return NULL;
196         }
197
198         web->accept_option = g_strdup("*/*");
199         web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
200         web->close_connection = FALSE;
201
202         return web;
203 }
204
205 GWeb *g_web_ref(GWeb *web)
206 {
207         if (web == NULL)
208                 return NULL;
209
210         g_atomic_int_inc(&web->ref_count);
211
212         return web;
213 }
214
215 void g_web_unref(GWeb *web)
216 {
217         if (web == NULL)
218                 return;
219
220         if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
221                 return;
222
223         flush_sessions(web);
224
225         g_resolv_unref(web->resolv);
226
227         g_free(web->proxy);
228
229         g_free(web->accept_option);
230         g_free(web->user_agent);
231         g_free(web->http_version);
232
233         g_free(web);
234 }
235
236 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
237 {
238         if (web == NULL)
239                 return;
240
241         web->debug_func = func;
242         web->debug_data = user_data;
243
244         g_resolv_set_debug(web->resolv, func, user_data);
245 }
246
247 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
248 {
249         if (web == NULL)
250                 return FALSE;
251
252         g_free(web->proxy);
253         web->proxy = g_strdup(proxy);
254
255         return TRUE;
256 }
257
258 gboolean g_web_add_nameserver(GWeb *web, const char *address)
259 {
260         if (web == NULL)
261                 return FALSE;
262
263         g_resolv_add_nameserver(web->resolv, address, 53, 0);
264
265         return TRUE;
266 }
267
268 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
269 {
270         g_free(web->accept_option);
271
272         if (format == NULL) {
273                 web->accept_option = NULL;
274                 debug(web, "clearing accept option");
275         } else {
276                 web->accept_option = g_strdup_vprintf(format, args);
277                 debug(web, "setting accept %s", web->accept_option);
278         }
279
280         return TRUE;
281 }
282
283 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
284 {
285         va_list args;
286         gboolean result;
287
288         if (web == NULL)
289                 return FALSE;
290
291         va_start(args, format);
292         result = set_accept_option(web, format, args);
293         va_end(args);
294
295         return result;
296 }
297
298 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
299 {
300         g_free(web->user_agent);
301
302         if (format == NULL) {
303                 web->user_agent = NULL;
304                 debug(web, "clearing user agent");
305         } else {
306                 web->user_agent = g_strdup_vprintf(format, args);
307                 debug(web, "setting user agent %s", web->user_agent);
308         }
309
310         return TRUE;
311 }
312
313 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
314 {
315         va_list args;
316         gboolean result;
317
318         if (web == NULL)
319                 return FALSE;
320
321         va_start(args, format);
322         result = set_user_agent(web, format, args);
323         va_end(args);
324
325         return result;
326 }
327
328 gboolean g_web_set_http_version(GWeb *web, const char *version)
329 {
330         if (web == NULL)
331                 return FALSE;
332
333         g_free(web->http_version);
334
335         if (version == NULL) {
336                 web->http_version = NULL;
337                 debug(web, "clearing HTTP version");
338         } else {
339                 web->http_version = g_strdup(version);
340                 debug(web, "setting HTTP version %s", web->http_version);
341         }
342
343         return TRUE;
344 }
345
346 void g_web_set_close_connection(GWeb *web, gboolean enabled)
347 {
348         if (web == NULL)
349                 return;
350
351         web->close_connection = enabled;
352 }
353
354 gboolean g_web_get_close_connection(GWeb *web)
355 {
356         if (web == NULL)
357                 return FALSE;
358
359         return web->close_connection;
360 }
361
362 static inline void call_result_func(struct web_session *session, guint16 status)
363 {
364         gboolean result;
365
366         if (session->result_func == NULL)
367                 return;
368
369         if (status != 0)
370                 session->result.status = status;
371
372         result = session->result_func(&session->result, session->user_data);
373
374         debug(session->web, "[result function] %s",
375                                         result == TRUE ? "continue" : "stop");
376 }
377
378 static gboolean process_send_buffer(struct web_session *session)
379 {
380         GString *buf = session->send_buffer;
381         gsize count, bytes_written;
382         GIOStatus status;
383
384         count = buf->len;
385
386         if (count == 0) {
387                 if (session->request_started == TRUE &&
388                                         session->more_data == FALSE)
389                         session->body_done = TRUE;
390
391                 return FALSE;
392         }
393
394         debug(session->web, "bytes to write %zu", count);
395
396         status = g_io_channel_write_chars(session->transport_channel,
397                                         buf->str, count, &bytes_written, NULL);
398
399         debug(session->web, "status %u bytes written %zu",
400                                         status, bytes_written);
401
402         if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
403                 return FALSE;
404
405         g_string_erase(buf, 0, bytes_written);
406
407         return TRUE;
408 }
409
410 static void process_next_chunk(struct web_session *session)
411 {
412         GString *buf = session->send_buffer;
413         const guint8 *body;
414         gsize length;
415
416         if (session->input_func == NULL) {
417                 session->more_data = FALSE;
418                 return;
419         }
420
421         session->more_data = session->input_func(&body, &length,
422                                                 session->user_data);
423
424         if (length > 0) {
425                 g_string_append_printf(buf, "%zx\r\n", length);
426                 g_string_append_len(buf, (char *) body, length);
427                 g_string_append(buf, "\r\n");
428         }
429
430         if (session->more_data == FALSE)
431                 g_string_append(buf, "0\r\n\r\n");
432 }
433
434 static void start_request(struct web_session *session)
435 {
436         GString *buf = session->send_buffer;
437         const char *version;
438         const guint8 *body;
439         gsize length;
440
441         debug(session->web, "request %s from %s",
442                                         session->request, session->host);
443
444         g_string_truncate(buf, 0);
445
446         if (session->web->http_version == NULL)
447                 version = "1.1";
448         else
449                 version = session->web->http_version;
450
451         if (session->content_type == NULL)
452                 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
453                                                 session->request, version);
454         else
455                 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
456                                                 session->request, version);
457
458         g_string_append_printf(buf, "Host: %s\r\n", session->host);
459
460         if (session->web->user_agent != NULL)
461                 g_string_append_printf(buf, "User-Agent: %s\r\n",
462                                                 session->web->user_agent);
463
464         if (session->web->accept_option != NULL)
465                 g_string_append_printf(buf, "Accept: %s\r\n",
466                                                 session->web->accept_option);
467
468         if (session->content_type != NULL) {
469                 g_string_append_printf(buf, "Content-Type: %s\r\n",
470                                                         session->content_type);
471                 if (session->input_func == NULL) {
472                         session->more_data = FALSE;
473                         length = 0;
474                 } else
475                         session->more_data = session->input_func(&body, &length,
476                                                         session->user_data);
477                 if (session->more_data == FALSE)
478                         g_string_append_printf(buf, "Content-Length: %zu\r\n",
479                                                                         length);
480                 else
481                         g_string_append(buf, "Transfer-Encoding: chunked\r\n");
482         }
483
484         if (session->web->close_connection == TRUE)
485                 g_string_append(buf, "Connection: close\r\n");
486
487         g_string_append(buf, "\r\n");
488
489         if (session->content_type != NULL && length > 0) {
490                 if (session->more_data == TRUE) {
491                         g_string_append_printf(buf, "%zx\r\n", length);
492                         g_string_append_len(buf, (char *) body, length);
493                         g_string_append(buf, "\r\n");
494                 } else
495                         g_string_append_len(buf, (char *) body, length);
496         }
497 }
498
499 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
500                                                 gpointer user_data)
501 {
502         struct web_session *session = user_data;
503
504         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
505                 session->send_watch = 0;
506                 return FALSE;
507         }
508
509         if (process_send_buffer(session) == TRUE)
510                 return TRUE;
511
512         if (session->request_started == FALSE) {
513                 session->request_started = TRUE;
514                 start_request(session);
515         } else if (session->more_data == TRUE)
516                 process_next_chunk(session);
517
518         process_send_buffer(session);
519
520         if (session->body_done == TRUE) {
521                 session->send_watch = 0;
522                 return FALSE;
523         }
524
525         return TRUE;
526 }
527
528 static int decode_chunked(struct web_session *session,
529                                         const guint8 *buf, gsize len)
530 {
531         const guint8 *ptr = buf;
532         gsize counter;
533
534         while (len > 0) {
535                 guint8 *pos;
536                 gsize count;
537                 char *str;
538
539                 switch (session->chunck_state) {
540                 case CHUNK_SIZE:
541                         pos = memchr(ptr, '\n', len);
542                         if (pos == NULL) {
543                                 g_string_append_len(session->current_header,
544                                                 (gchar *) ptr, len);
545                                 return 0;
546                         }
547
548                         count = pos - ptr;
549                         if (count < 1 || ptr[count - 1] != '\r')
550                                 return -EILSEQ;
551
552                         g_string_append_len(session->current_header,
553                                                 (gchar *) ptr, count);
554
555                         len -= count + 1;
556                         ptr = pos + 1;
557
558                         str = session->current_header->str;
559
560                         counter = strtoul(str, NULL, 16);
561                         if ((counter == 0 && errno == EINVAL) ||
562                                                 counter == ULONG_MAX)
563                                 return -EILSEQ;
564
565                         session->chunk_size = counter;
566                         session->chunk_left = counter;
567
568                         session->chunck_state = CHUNK_DATA;
569                         break;
570                 case CHUNK_R_BODY:
571                         if (*ptr != '\r')
572                                 return -EILSEQ;
573                         ptr++;
574                         len--;
575                         session->chunck_state = CHUNK_N_BODY;
576                         break;
577                 case CHUNK_N_BODY:
578                         if (*ptr != '\n')
579                                 return -EILSEQ;
580                         ptr++;
581                         len--;
582                         session->chunck_state = CHUNK_SIZE;
583                         break;
584                 case CHUNK_DATA:
585                         if (session->chunk_size == 0) {
586                                 debug(session->web, "Download Done in chunk");
587                                 g_string_truncate(session->current_header, 0);
588                                 return 0;
589                         }
590
591                         if (session->chunk_left <= len) {
592                                 session->result.buffer = ptr;
593                                 session->result.length = session->chunk_left;
594                                 call_result_func(session, 0);
595
596                                 len -= session->chunk_left;
597                                 ptr += session->chunk_left;
598
599                                 session->total_len += session->chunk_left;
600                                 session->chunk_left = 0;
601
602                                 g_string_truncate(session->current_header, 0);
603                                 session->chunck_state = CHUNK_R_BODY;
604                                 break;
605                         }
606                         /* more data */
607                         session->result.buffer = ptr;
608                         session->result.length = len;
609                         call_result_func(session, 0);
610
611                         session->chunk_left -= len;
612                         session->total_len += len;
613
614                         len -= len;
615                         ptr += len;
616                         break;
617                 }
618         }
619
620         return 0;
621 }
622
623 static int handle_body(struct web_session *session,
624                                 const guint8 *buf, gsize len)
625 {
626         int err;
627
628         debug(session->web, "[body] length %zu", len);
629
630         if (session->result.use_chunk == FALSE) {
631                 if (len > 0) {
632                         session->result.buffer = buf;
633                         session->result.length = len;
634                         call_result_func(session, 0);
635                 }
636                 return 0;
637         }
638
639         err = decode_chunked(session, buf, len);
640         if (err < 0) {
641                 debug(session->web, "Error in chunk decode %d", err);
642
643                 session->result.buffer = NULL;
644                 session->result.length = 0;
645                 call_result_func(session, 400);
646         }
647
648         return err;
649 }
650
651 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
652                                                         gpointer user_data)
653 {
654         struct web_session *session = user_data;
655         guint8 *ptr = session->receive_buffer;
656         gsize bytes_read;
657         GIOStatus status;
658
659         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
660                 session->transport_watch = 0;
661                 session->result.buffer = NULL;
662                 session->result.length = 0;
663                 call_result_func(session, 400);
664                 return FALSE;
665         }
666
667         status = g_io_channel_read_chars(channel,
668                                 (gchar *) session->receive_buffer,
669                                 session->receive_space - 1, &bytes_read, NULL);
670
671         debug(session->web, "bytes read %zu", bytes_read);
672
673         if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
674                 session->transport_watch = 0;
675                 session->result.buffer = NULL;
676                 session->result.length = 0;
677                 call_result_func(session, 0);
678                 return FALSE;
679         }
680
681         session->receive_buffer[bytes_read] = '\0';
682
683         if (session->header_done == TRUE) {
684                 if (handle_body(session, session->receive_buffer,
685                                                         bytes_read) < 0) {
686                         session->transport_watch = 0;
687                         return FALSE;
688                 }
689                 return TRUE;
690         }
691
692         while (bytes_read > 0) {
693                 guint8 *pos;
694                 gsize count;
695                 char *str;
696
697                 pos = memchr(ptr, '\n', bytes_read);
698                 if (pos == NULL) {
699                         g_string_append_len(session->current_header,
700                                                 (gchar *) ptr, bytes_read);
701                         return TRUE;
702                 }
703
704                 *pos = '\0';
705                 count = strlen((char *) ptr);
706                 if (count > 0 && ptr[count - 1] == '\r') {
707                         ptr[--count] = '\0';
708                         bytes_read--;
709                 }
710
711                 g_string_append_len(session->current_header,
712                                                 (gchar *) ptr, count);
713
714                 bytes_read -= count + 1;
715                 if (bytes_read > 0)
716                         ptr = pos + 1;
717                 else
718                         ptr = NULL;
719
720                 if (session->current_header->len == 0) {
721                         session->header_done = TRUE;
722                         if (handle_body(session, ptr, bytes_read) < 0) {
723                                 session->transport_watch = 0;
724                                 return FALSE;
725                         }
726                         break;
727                 }
728
729                 str = session->current_header->str;
730
731                 if (session->result.status == 0) {
732                         unsigned int code;
733
734                         if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
735                                 session->result.status = code;
736                 } else if (session->result.use_chunk == FALSE &&
737                                 g_ascii_strncasecmp("Transfer-Encoding:",
738                                                                 str, 18) == 0) {
739                         char *val;
740
741                         val = g_strrstr(str + 18, "chunked");
742                         if (val != NULL) {
743                                 session->result.use_chunk = TRUE;
744
745                                 session->chunck_state = CHUNK_SIZE;
746                                 session->chunk_left = 0;
747                                 session->chunk_left = 0;
748                                 session->total_len = 0;
749                         }
750                 }
751
752                 debug(session->web, "[header] %s", str);
753
754                 g_string_truncate(session->current_header, 0);
755         }
756
757         return TRUE;
758 }
759
760 static int connect_session_transport(struct web_session *session)
761 {
762         GIOFlags flags;
763         struct sockaddr_in sin;
764         int sk;
765
766         sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
767         if (sk < 0)
768                 return -EIO;
769
770         if (session->flags & SESSION_FLAG_USE_TLS) {
771                 debug(session->web, "using TLS encryption");
772                 session->transport_channel = g_io_channel_gnutls_new(sk);
773         } else {
774                 debug(session->web, "no encryption");
775                 session->transport_channel = g_io_channel_unix_new(sk);
776         }
777
778         if (session->transport_channel == NULL) {
779                 close(sk);
780                 return -ENOMEM;
781         }
782
783         flags = g_io_channel_get_flags(session->transport_channel);
784         g_io_channel_set_flags(session->transport_channel,
785                                         flags | G_IO_FLAG_NONBLOCK, NULL);
786
787         g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
788         g_io_channel_set_buffered(session->transport_channel, FALSE);
789
790         g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
791
792         memset(&sin, 0, sizeof(sin));
793         sin.sin_family = AF_INET;
794         sin.sin_port = htons(session->port);
795         sin.sin_addr.s_addr = inet_addr(session->address);
796
797         if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
798                 if (errno != EINPROGRESS) {
799                         close(sk);
800                         return -EIO;
801                 }
802         }
803
804         session->transport_watch = g_io_add_watch(session->transport_channel,
805                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
806                                                 received_data, session);
807
808         session->send_watch = g_io_add_watch(session->transport_channel,
809                                 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
810                                                 send_data, session);
811
812         return 0;
813 }
814
815 static int create_transport(struct web_session *session)
816 {
817         int err;
818
819         err = connect_session_transport(session);
820         if (err < 0)
821                 return err;
822
823         debug(session->web, "creating session %s:%u",
824                                         session->address, session->port);
825
826         return 0;
827 }
828
829 static int parse_url(struct web_session *session, const char *url)
830 {
831         char *scheme, *host, *port, *path;
832
833         scheme = g_strdup(url);
834         if (scheme == NULL)
835                 return -EINVAL;
836
837         host = strstr(scheme, "://");
838         if (host != NULL) {
839                 *host = '\0';
840                 host += 3;
841
842                 if (strcasecmp(scheme, "https") == 0) {
843                         session->port = 443;
844                         session->flags |= SESSION_FLAG_USE_TLS;
845                 } else if (strcasecmp(scheme, "http") == 0) {
846                         session->port = 80;
847                 } else {
848                         g_free(scheme);
849                         return -EINVAL;
850                 }
851         } else {
852                 host = scheme;
853                 session->port = 80;
854         }
855
856         path = strchr(host, '/');
857         if (path != NULL)
858                 *(path++) = '\0';
859
860         session->request = g_strdup_printf("/%s", path ? path : "");
861
862         port = strrchr(host, ':');
863         if (port != NULL) {
864                 char *end;
865                 int tmp = strtol(port + 1, &end, 10);
866
867                 if (*end == '\0') {
868                         *port = '\0';
869                         session->port = tmp;
870                 }
871         }
872
873         session->host = g_strdup(host);
874
875         g_free(scheme);
876
877         return 0;
878 }
879
880 static void resolv_result(GResolvResultStatus status,
881                                         char **results, gpointer user_data)
882 {
883         struct web_session *session = user_data;
884
885         if (results == NULL || results[0] == NULL) {
886                 call_result_func(session, 404);
887                 return;
888         }
889
890         debug(session->web, "address %s", results[0]);
891
892         if (inet_aton(results[0], NULL) == 0) {
893                 call_result_func(session, 400);
894                 return;
895         }
896
897         session->address = g_strdup(results[0]);
898
899         if (create_transport(session) < 0) {
900                 call_result_func(session, 409);
901                 return;
902         }
903 }
904
905 static guint do_request(GWeb *web, const char *url,
906                                 const char *type, GWebInputFunc input,
907                                 GWebResultFunc func, gpointer user_data)
908 {
909         struct web_session *session;
910
911         if (web == NULL || url == NULL)
912                 return 0;
913
914         debug(web, "request %s", url);
915
916         session = g_try_new0(struct web_session, 1);
917         if (session == NULL)
918                 return 0;
919
920         if (parse_url(session, url) < 0) {
921                 free_session(session);
922                 return 0;
923         }
924
925         debug(web, "host %s:%u", session->host, session->port);
926         debug(web, "flags %lu", session->flags);
927
928         if (type != NULL) {
929                 session->content_type = g_strdup(type);
930
931                 debug(web, "content-type %s", session->content_type);
932         }
933
934         session->web = web;
935
936         session->result_func = func;
937         session->input_func = input;
938         session->user_data = user_data;
939
940         session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
941         if (session->receive_buffer == NULL) {
942                 free_session(session);
943                 return 0;
944         }
945
946         session->receive_space = DEFAULT_BUFFER_SIZE;
947         session->send_buffer = g_string_sized_new(0);
948         session->current_header = g_string_sized_new(0);
949         session->header_done = FALSE;
950         session->body_done = FALSE;
951
952         if (inet_aton(session->host, NULL) == 0) {
953                 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
954                                         session->host, resolv_result, session);
955                 if (session->resolv_action == 0) {
956                         free_session(session);
957                         return 0;
958                 }
959         } else {
960                 session->address = g_strdup(session->host);
961
962                 if (create_transport(session) < 0) {
963                         free_session(session);
964                         return 0;
965                 }
966         }
967
968         web->session_list = g_list_append(web->session_list, session);
969
970         return web->next_query_id++;
971 }
972
973 guint g_web_request_get(GWeb *web, const char *url,
974                                 GWebResultFunc func, gpointer user_data)
975 {
976         return do_request(web, url, NULL, NULL, func, user_data);
977 }
978
979 guint g_web_request_post(GWeb *web, const char *url,
980                                 const char *type, GWebInputFunc input,
981                                 GWebResultFunc func, gpointer user_data)
982 {
983         return do_request(web, url, type, input, func, user_data);
984 }
985
986 gboolean g_web_cancel_request(GWeb *web, guint id)
987 {
988         if (web == NULL)
989                 return FALSE;
990
991         return TRUE;
992 }
993
994 guint16 g_web_result_get_status(GWebResult *result)
995 {
996         if (result == NULL)
997                 return 0;
998
999         return result->status;
1000 }
1001
1002 gboolean g_web_result_get_chunk(GWebResult *result,
1003                                 const guint8 **chunk, gsize *length)
1004 {
1005         if (result == NULL)
1006                 return FALSE;
1007
1008         if (chunk == NULL)
1009                 return FALSE;
1010
1011         *chunk = result->buffer;
1012
1013         if (length != NULL)
1014                 *length = result->length;
1015
1016         return TRUE;
1017 }
1018
1019 struct _GWebParser {
1020         gint ref_count;
1021         char *begin_token;
1022         char *end_token;
1023         const char *token_str;
1024         size_t token_len;
1025         size_t token_pos;
1026         gboolean intoken;
1027         GString *content;
1028         GWebParserFunc func;
1029         gpointer user_data;
1030 };
1031
1032 GWebParser *g_web_parser_new(const char *begin, const char *end,
1033                                 GWebParserFunc func, gpointer user_data)
1034 {
1035         GWebParser *parser;
1036
1037         parser = g_try_new0(GWebParser, 1);
1038         if (parser == NULL)
1039                 return NULL;
1040
1041         parser->ref_count = 1;
1042
1043         parser->begin_token = g_strdup(begin);
1044         parser->end_token = g_strdup(end);
1045
1046         if (parser->begin_token == NULL) {
1047                 g_free(parser);
1048                 return NULL;
1049         }
1050
1051         parser->func = func;
1052         parser->user_data = user_data;
1053
1054         parser->token_str = parser->begin_token;
1055         parser->token_len = strlen(parser->token_str);
1056         parser->token_pos = 0;
1057
1058         parser->intoken = FALSE;
1059         parser->content = g_string_sized_new(0);
1060
1061         return parser;
1062 }
1063
1064 GWebParser *g_web_parser_ref(GWebParser *parser)
1065 {
1066         if (parser == NULL)
1067                 return NULL;
1068
1069         g_atomic_int_inc(&parser->ref_count);
1070
1071         return parser;
1072 }
1073
1074 void g_web_parser_unref(GWebParser *parser)
1075 {
1076         if (parser == NULL)
1077                 return;
1078
1079         if (g_atomic_int_dec_and_test(&parser->ref_count) == FALSE)
1080                 return;
1081
1082         g_string_free(parser->content, TRUE);
1083
1084         g_free(parser->begin_token);
1085         g_free(parser->end_token);
1086         g_free(parser);
1087 }
1088
1089 void g_web_parser_feed_data(GWebParser *parser,
1090                                 const guint8 *data, gsize length)
1091 {
1092         const guint8 *ptr = data;
1093
1094         if (parser == NULL)
1095                 return;
1096
1097         while (length > 0) {
1098                 guint8 chr = parser->token_str[parser->token_pos];
1099
1100                 if (parser->token_pos == 0) {
1101                         guint8 *pos;
1102
1103                         pos = memchr(ptr, chr, length);
1104                         if (pos == NULL) {
1105                                 if (parser->intoken == TRUE)
1106                                         g_string_append_len(parser->content,
1107                                                         (gchar *) ptr, length);
1108                                 break;
1109                         }
1110
1111                         if (parser->intoken == TRUE)
1112                                 g_string_append_len(parser->content,
1113                                                 (gchar *) ptr, (pos - ptr) + 1);
1114
1115                         length -= (pos - ptr) + 1;
1116                         ptr = pos + 1;
1117
1118                         parser->token_pos++;
1119                         continue;
1120                 }
1121
1122                 if (parser->intoken == TRUE)
1123                         g_string_append_c(parser->content, ptr[0]);
1124
1125                 if (ptr[0] != chr) {
1126                         length--;
1127                         ptr++;
1128
1129                         parser->token_pos = 0;
1130                         continue;
1131                 }
1132
1133                 length--;
1134                 ptr++;
1135
1136                 parser->token_pos++;
1137
1138                 if (parser->token_pos == parser->token_len) {
1139                         if (parser->intoken == FALSE) {
1140                                 g_string_append(parser->content,
1141                                                         parser->token_str);
1142
1143                                 parser->intoken = TRUE;
1144                                 parser->token_str = parser->end_token;
1145                                 parser->token_len = strlen(parser->end_token);
1146                                 parser->token_pos = 0;
1147                         } else {
1148                                 char *str;
1149                                 str = g_string_free(parser->content, FALSE);
1150                                 parser->content = g_string_sized_new(0);
1151                                 if (parser->func)
1152                                         parser->func(str, parser->user_data);
1153                                 g_free(str);
1154
1155                                 parser->intoken = FALSE;
1156                                 parser->token_str = parser->begin_token;
1157                                 parser->token_len = strlen(parser->begin_token);
1158                                 parser->token_pos = 0;
1159                         }
1160                 }
1161         }
1162 }
1163
1164 void g_web_parser_end_data(GWebParser *parser)
1165 {
1166         if (parser == NULL)
1167                 return;
1168 }