gweb: Bind socket to the right interface
[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 #include <netdb.h>
35 #include <net/if.h>
36
37 #include "giognutls.h"
38 #include "gresolv.h"
39 #include "gweb.h"
40
41 #define DEFAULT_BUFFER_SIZE  2048
42
43 #define SESSION_FLAG_USE_TLS    (1 << 0)
44
45 enum chunk_state {
46         CHUNK_SIZE,
47         CHUNK_R_BODY,
48         CHUNK_N_BODY,
49         CHUNK_DATA,
50 };
51
52 struct _GWebResult {
53         guint16 status;
54         const guint8 *buffer;
55         gsize length;
56         gboolean use_chunk;
57         gchar *last_key;
58         GHashTable *headers;
59 };
60
61 struct web_session {
62         GWeb *web;
63
64         char *address;
65         char *host;
66         uint16_t port;
67         unsigned long flags;
68         struct addrinfo *addr;
69
70         char *content_type;
71
72         GIOChannel *transport_channel;
73         guint transport_watch;
74         guint send_watch;
75
76         guint resolv_action;
77         char *request;
78
79         guint8 *receive_buffer;
80         gsize receive_space;
81         GString *send_buffer;
82         GString *current_header;
83         gboolean header_done;
84         gboolean body_done;
85         gboolean more_data;
86         gboolean request_started;
87
88         enum chunk_state chunck_state;
89         gsize chunk_size;
90         gsize chunk_left;
91         gsize total_len;
92
93         GWebResult result;
94
95         GWebResultFunc result_func;
96         GWebInputFunc input_func;
97         gpointer user_data;
98 };
99
100 struct _GWeb {
101         int ref_count;
102
103         guint next_query_id;
104
105         int family;
106
107         int index;
108         GList *session_list;
109
110         GResolv *resolv;
111         char *proxy;
112         char *accept_option;
113         char *user_agent;
114         char *user_agent_profile;
115         char *http_version;
116         gboolean close_connection;
117
118         GWebDebugFunc debug_func;
119         gpointer debug_data;
120 };
121
122 static inline void debug(GWeb *web, const char *format, ...)
123 {
124         char str[256];
125         va_list ap;
126
127         if (web->debug_func == NULL)
128                 return;
129
130         va_start(ap, format);
131
132         if (vsnprintf(str, sizeof(str), format, ap) > 0)
133                 web->debug_func(str, web->debug_data);
134
135         va_end(ap);
136 }
137
138 static void free_session(struct web_session *session)
139 {
140         GWeb *web = session->web;
141
142         if (session == NULL)
143                 return;
144
145         g_free(session->request);
146
147         if (session->resolv_action > 0)
148                 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
149
150         if (session->transport_watch > 0)
151                 g_source_remove(session->transport_watch);
152
153         if (session->send_watch > 0)
154                 g_source_remove(session->send_watch);
155
156         if (session->transport_channel != NULL)
157                 g_io_channel_unref(session->transport_channel);
158
159         g_free(session->result.last_key);
160
161         if (session->result.headers != NULL)
162                 g_hash_table_destroy(session->result.headers);
163
164         if (session->send_buffer != NULL)
165                 g_string_free(session->send_buffer, TRUE);
166
167         if (session->current_header != NULL)
168                 g_string_free(session->current_header, TRUE);
169
170         g_free(session->receive_buffer);
171
172         g_free(session->content_type);
173
174         g_free(session->host);
175         g_free(session->address);
176         if (session->addr != NULL)
177                 freeaddrinfo(session->addr);
178
179         g_free(session);
180 }
181
182 static void flush_sessions(GWeb *web)
183 {
184         GList *list;
185
186         for (list = g_list_first(web->session_list);
187                                         list; list = g_list_next(list))
188                 free_session(list->data);
189
190         g_list_free(web->session_list);
191         web->session_list = NULL;
192 }
193
194 GWeb *g_web_new(int index)
195 {
196         GWeb *web;
197
198         if (index < 0)
199                 return NULL;
200
201         web = g_try_new0(GWeb, 1);
202         if (web == NULL)
203                 return NULL;
204
205         web->ref_count = 1;
206
207         web->next_query_id = 1;
208
209         web->family = AF_UNSPEC;
210
211         web->index = index;
212         web->session_list = NULL;
213
214         web->resolv = g_resolv_new(index);
215         if (web->resolv == NULL) {
216                 g_free(web);
217                 return NULL;
218         }
219
220         web->accept_option = g_strdup("*/*");
221         web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
222         web->close_connection = FALSE;
223
224         return web;
225 }
226
227 GWeb *g_web_ref(GWeb *web)
228 {
229         if (web == NULL)
230                 return NULL;
231
232         __sync_fetch_and_add(&web->ref_count, 1);
233
234         return web;
235 }
236
237 void g_web_unref(GWeb *web)
238 {
239         if (web == NULL)
240                 return;
241
242         if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
243                 return;
244
245         flush_sessions(web);
246
247         g_resolv_unref(web->resolv);
248
249         g_free(web->proxy);
250
251         g_free(web->accept_option);
252         g_free(web->user_agent);
253         g_free(web->user_agent_profile);
254         g_free(web->http_version);
255
256         g_free(web);
257 }
258
259 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
260 {
261         if (web == NULL)
262                 return;
263
264         web->debug_func = func;
265         web->debug_data = user_data;
266
267         g_resolv_set_debug(web->resolv, func, user_data);
268 }
269
270 gboolean g_web_set_proxy(GWeb *web, const char *proxy)
271 {
272         if (web == NULL)
273                 return FALSE;
274
275         g_free(web->proxy);
276
277         if (proxy == NULL) {
278                 web->proxy = NULL;
279                 debug(web, "clearing proxy");
280         } else {
281                 web->proxy = g_strdup(proxy);
282                 debug(web, "setting proxy %s", web->proxy);
283         }
284
285         return TRUE;
286 }
287
288 gboolean g_web_set_address_family(GWeb *web, int family)
289 {
290         if (web == NULL)
291                 return FALSE;
292
293         if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
294                 return FALSE;
295
296         web->family = family;
297
298         g_resolv_set_address_family(web->resolv, family);
299
300         return TRUE;
301 }
302
303 gboolean g_web_add_nameserver(GWeb *web, const char *address)
304 {
305         if (web == NULL)
306                 return FALSE;
307
308         g_resolv_add_nameserver(web->resolv, address, 53, 0);
309
310         return TRUE;
311 }
312
313 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
314 {
315         g_free(web->accept_option);
316
317         if (format == NULL) {
318                 web->accept_option = NULL;
319                 debug(web, "clearing accept option");
320         } else {
321                 web->accept_option = g_strdup_vprintf(format, args);
322                 debug(web, "setting accept %s", web->accept_option);
323         }
324
325         return TRUE;
326 }
327
328 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
329 {
330         va_list args;
331         gboolean result;
332
333         if (web == NULL)
334                 return FALSE;
335
336         va_start(args, format);
337         result = set_accept_option(web, format, args);
338         va_end(args);
339
340         return result;
341 }
342
343 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
344 {
345         g_free(web->user_agent);
346
347         if (format == NULL) {
348                 web->user_agent = NULL;
349                 debug(web, "clearing user agent");
350         } else {
351                 web->user_agent = g_strdup_vprintf(format, args);
352                 debug(web, "setting user agent %s", web->user_agent);
353         }
354
355         return TRUE;
356 }
357
358 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
359 {
360         va_list args;
361         gboolean result;
362
363         if (web == NULL)
364                 return FALSE;
365
366         va_start(args, format);
367         result = set_user_agent(web, format, args);
368         va_end(args);
369
370         return result;
371 }
372
373 gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
374 {
375         if (web == NULL)
376                 return FALSE;
377
378         g_free(web->user_agent_profile);
379
380         web->user_agent_profile = g_strdup(profile);
381         debug(web, "setting user agent profile %s", web->user_agent);
382
383         return TRUE;
384 }
385
386 gboolean g_web_set_http_version(GWeb *web, const char *version)
387 {
388         if (web == NULL)
389                 return FALSE;
390
391         g_free(web->http_version);
392
393         if (version == NULL) {
394                 web->http_version = NULL;
395                 debug(web, "clearing HTTP version");
396         } else {
397                 web->http_version = g_strdup(version);
398                 debug(web, "setting HTTP version %s", web->http_version);
399         }
400
401         return TRUE;
402 }
403
404 void g_web_set_close_connection(GWeb *web, gboolean enabled)
405 {
406         if (web == NULL)
407                 return;
408
409         web->close_connection = enabled;
410 }
411
412 gboolean g_web_get_close_connection(GWeb *web)
413 {
414         if (web == NULL)
415                 return FALSE;
416
417         return web->close_connection;
418 }
419
420 static inline void call_result_func(struct web_session *session, guint16 status)
421 {
422         gboolean result;
423
424         if (session->result_func == NULL)
425                 return;
426
427         if (status != 0)
428                 session->result.status = status;
429
430         result = session->result_func(&session->result, session->user_data);
431
432         debug(session->web, "[result function] %s",
433                                         result == TRUE ? "continue" : "stop");
434 }
435
436 static gboolean process_send_buffer(struct web_session *session)
437 {
438         GString *buf = session->send_buffer;
439         gsize count, bytes_written;
440         GIOStatus status;
441
442         count = buf->len;
443
444         if (count == 0) {
445                 if (session->request_started == TRUE &&
446                                         session->more_data == FALSE)
447                         session->body_done = TRUE;
448
449                 return FALSE;
450         }
451
452         status = g_io_channel_write_chars(session->transport_channel,
453                                         buf->str, count, &bytes_written, NULL);
454
455         debug(session->web, "status %u bytes to write %zu bytes written %zu",
456                                         status, count, bytes_written);
457
458         if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
459                 return FALSE;
460
461         g_string_erase(buf, 0, bytes_written);
462
463         return TRUE;
464 }
465
466 static void process_next_chunk(struct web_session *session)
467 {
468         GString *buf = session->send_buffer;
469         const guint8 *body;
470         gsize length;
471
472         if (session->input_func == NULL) {
473                 session->more_data = FALSE;
474                 return;
475         }
476
477         session->more_data = session->input_func(&body, &length,
478                                                 session->user_data);
479
480         if (length > 0) {
481                 g_string_append_printf(buf, "%zx\r\n", length);
482                 g_string_append_len(buf, (char *) body, length);
483                 g_string_append(buf, "\r\n");
484         }
485
486         if (session->more_data == FALSE)
487                 g_string_append(buf, "0\r\n\r\n");
488 }
489
490 static void start_request(struct web_session *session)
491 {
492         GString *buf = session->send_buffer;
493         const char *version;
494         const guint8 *body;
495         gsize length;
496
497         debug(session->web, "request %s from %s",
498                                         session->request, session->host);
499
500         g_string_truncate(buf, 0);
501
502         if (session->web->http_version == NULL)
503                 version = "1.1";
504         else
505                 version = session->web->http_version;
506
507         if (session->content_type == NULL)
508                 g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
509                                                 session->request, version);
510         else
511                 g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
512                                                 session->request, version);
513
514         g_string_append_printf(buf, "Host: %s\r\n", session->host);
515
516         if (session->web->user_agent != NULL)
517                 g_string_append_printf(buf, "User-Agent: %s\r\n",
518                                                 session->web->user_agent);
519
520         if (session->web->user_agent_profile != NULL) {
521                 g_string_append_printf(buf, "x-wap-profile: %s\r\n",
522                                        session->web->user_agent_profile);
523         }
524
525         if (session->web->accept_option != NULL)
526                 g_string_append_printf(buf, "Accept: %s\r\n",
527                                                 session->web->accept_option);
528
529         if (session->content_type != NULL) {
530                 g_string_append_printf(buf, "Content-Type: %s\r\n",
531                                                         session->content_type);
532                 if (session->input_func == NULL) {
533                         session->more_data = FALSE;
534                         length = 0;
535                 } else
536                         session->more_data = session->input_func(&body, &length,
537                                                         session->user_data);
538                 if (session->more_data == FALSE)
539                         g_string_append_printf(buf, "Content-Length: %zu\r\n",
540                                                                         length);
541                 else
542                         g_string_append(buf, "Transfer-Encoding: chunked\r\n");
543         }
544
545         if (session->web->close_connection == TRUE)
546                 g_string_append(buf, "Connection: close\r\n");
547
548         g_string_append(buf, "\r\n");
549
550         if (session->content_type != NULL && length > 0) {
551                 if (session->more_data == TRUE) {
552                         g_string_append_printf(buf, "%zx\r\n", length);
553                         g_string_append_len(buf, (char *) body, length);
554                         g_string_append(buf, "\r\n");
555                 } else
556                         g_string_append_len(buf, (char *) body, length);
557         }
558 }
559
560 static gboolean send_data(GIOChannel *channel, GIOCondition cond,
561                                                 gpointer user_data)
562 {
563         struct web_session *session = user_data;
564
565         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
566                 session->send_watch = 0;
567                 return FALSE;
568         }
569
570         if (process_send_buffer(session) == TRUE)
571                 return TRUE;
572
573         if (session->request_started == FALSE) {
574                 session->request_started = TRUE;
575                 start_request(session);
576         } else if (session->more_data == TRUE)
577                 process_next_chunk(session);
578
579         process_send_buffer(session);
580
581         if (session->body_done == TRUE) {
582                 session->send_watch = 0;
583                 return FALSE;
584         }
585
586         return TRUE;
587 }
588
589 static int decode_chunked(struct web_session *session,
590                                         const guint8 *buf, gsize len)
591 {
592         const guint8 *ptr = buf;
593         gsize counter;
594
595         while (len > 0) {
596                 guint8 *pos;
597                 gsize count;
598                 char *str;
599
600                 switch (session->chunck_state) {
601                 case CHUNK_SIZE:
602                         pos = memchr(ptr, '\n', len);
603                         if (pos == NULL) {
604                                 g_string_append_len(session->current_header,
605                                                 (gchar *) ptr, len);
606                                 return 0;
607                         }
608
609                         count = pos - ptr;
610                         if (count < 1 || ptr[count - 1] != '\r')
611                                 return -EILSEQ;
612
613                         g_string_append_len(session->current_header,
614                                                 (gchar *) ptr, count);
615
616                         len -= count + 1;
617                         ptr = pos + 1;
618
619                         str = session->current_header->str;
620
621                         counter = strtoul(str, NULL, 16);
622                         if ((counter == 0 && errno == EINVAL) ||
623                                                 counter == ULONG_MAX)
624                                 return -EILSEQ;
625
626                         session->chunk_size = counter;
627                         session->chunk_left = counter;
628
629                         session->chunck_state = CHUNK_DATA;
630                         break;
631                 case CHUNK_R_BODY:
632                         if (*ptr != '\r')
633                                 return -EILSEQ;
634                         ptr++;
635                         len--;
636                         session->chunck_state = CHUNK_N_BODY;
637                         break;
638                 case CHUNK_N_BODY:
639                         if (*ptr != '\n')
640                                 return -EILSEQ;
641                         ptr++;
642                         len--;
643                         session->chunck_state = CHUNK_SIZE;
644                         break;
645                 case CHUNK_DATA:
646                         if (session->chunk_size == 0) {
647                                 debug(session->web, "Download Done in chunk");
648                                 g_string_truncate(session->current_header, 0);
649                                 return 0;
650                         }
651
652                         if (session->chunk_left <= len) {
653                                 session->result.buffer = ptr;
654                                 session->result.length = session->chunk_left;
655                                 call_result_func(session, 0);
656
657                                 len -= session->chunk_left;
658                                 ptr += session->chunk_left;
659
660                                 session->total_len += session->chunk_left;
661                                 session->chunk_left = 0;
662
663                                 g_string_truncate(session->current_header, 0);
664                                 session->chunck_state = CHUNK_R_BODY;
665                                 break;
666                         }
667                         /* more data */
668                         session->result.buffer = ptr;
669                         session->result.length = len;
670                         call_result_func(session, 0);
671
672                         session->chunk_left -= len;
673                         session->total_len += len;
674
675                         len -= len;
676                         ptr += len;
677                         break;
678                 }
679         }
680
681         return 0;
682 }
683
684 static int handle_body(struct web_session *session,
685                                 const guint8 *buf, gsize len)
686 {
687         int err;
688
689         debug(session->web, "[body] length %zu", len);
690
691         if (session->result.use_chunk == FALSE) {
692                 if (len > 0) {
693                         session->result.buffer = buf;
694                         session->result.length = len;
695                         call_result_func(session, 0);
696                 }
697                 return 0;
698         }
699
700         err = decode_chunked(session, buf, len);
701         if (err < 0) {
702                 debug(session->web, "Error in chunk decode %d", err);
703
704                 session->result.buffer = NULL;
705                 session->result.length = 0;
706                 call_result_func(session, 400);
707         }
708
709         return err;
710 }
711
712 static void handle_multi_line(struct web_session *session)
713 {
714         gsize count;
715         char *str;
716         gchar *value;
717
718         str = session->current_header->str;
719
720         if (str[0] != ' ' && str[0] != '\t')
721                 return;
722
723         while (str[0] == ' ' || str[0] == '\t')
724                 str++;
725
726         count = str - session->current_header->str;
727         if (count > 0) {
728                 g_string_erase(session->current_header, 0, count);
729                 g_string_insert_c(session->current_header, 0, ' ');
730         }
731
732         value = g_hash_table_lookup(session->result.headers,
733                                         session->result.last_key);
734         if (value != NULL) {
735                 g_string_insert(session->current_header, 0, value);
736
737                 str = session->current_header->str;
738
739                 g_hash_table_replace(session->result.headers,
740                                         g_strdup(session->result.last_key),
741                                         g_strdup(str));
742         }
743 }
744
745 static void add_header_field(struct web_session *session)
746 {
747         gsize count;
748         guint8 *pos;
749         char *str;
750         gchar *value;
751         gchar *key;
752
753         str = session->current_header->str;
754
755         pos = memchr(str, ':', session->current_header->len);
756         if (pos != NULL) {
757                 *pos = '\0';
758                 pos++;
759
760                 key = g_strdup(str);
761
762                 /* remove preceding white spaces */
763                 while (*pos == ' ')
764                         pos++;
765
766                 count = (char *) pos - str;
767
768                 g_string_erase(session->current_header, 0, count);
769
770                 value = g_hash_table_lookup(session->result.headers, key);
771                 if (value != NULL) {
772                         g_string_insert_c(session->current_header, 0, ' ');
773                         g_string_insert_c(session->current_header, 0, ';');
774
775                         g_string_insert(session->current_header, 0, value);
776                 }
777
778                 str = session->current_header->str;
779                 g_hash_table_replace(session->result.headers, key,
780                                                         g_strdup(str));
781
782                 g_free(session->result.last_key);
783                 session->result.last_key = g_strdup(key);
784         }
785 }
786
787 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
788                                                         gpointer user_data)
789 {
790         struct web_session *session = user_data;
791         guint8 *ptr = session->receive_buffer;
792         gsize bytes_read;
793         GIOStatus status;
794
795         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
796                 session->transport_watch = 0;
797                 session->result.buffer = NULL;
798                 session->result.length = 0;
799                 call_result_func(session, 400);
800                 return FALSE;
801         }
802
803         status = g_io_channel_read_chars(channel,
804                                 (gchar *) session->receive_buffer,
805                                 session->receive_space - 1, &bytes_read, NULL);
806
807         debug(session->web, "bytes read %zu", bytes_read);
808
809         if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
810                 session->transport_watch = 0;
811                 session->result.buffer = NULL;
812                 session->result.length = 0;
813                 call_result_func(session, 0);
814                 return FALSE;
815         }
816
817         session->receive_buffer[bytes_read] = '\0';
818
819         if (session->header_done == TRUE) {
820                 if (handle_body(session, session->receive_buffer,
821                                                         bytes_read) < 0) {
822                         session->transport_watch = 0;
823                         return FALSE;
824                 }
825                 return TRUE;
826         }
827
828         while (bytes_read > 0) {
829                 guint8 *pos;
830                 gsize count;
831                 char *str;
832
833                 pos = memchr(ptr, '\n', bytes_read);
834                 if (pos == NULL) {
835                         g_string_append_len(session->current_header,
836                                                 (gchar *) ptr, bytes_read);
837                         return TRUE;
838                 }
839
840                 *pos = '\0';
841                 count = strlen((char *) ptr);
842                 if (count > 0 && ptr[count - 1] == '\r') {
843                         ptr[--count] = '\0';
844                         bytes_read--;
845                 }
846
847                 g_string_append_len(session->current_header,
848                                                 (gchar *) ptr, count);
849
850                 bytes_read -= count + 1;
851                 if (bytes_read > 0)
852                         ptr = pos + 1;
853                 else
854                         ptr = NULL;
855
856                 if (session->current_header->len == 0) {
857                         char *val;
858
859                         session->header_done = TRUE;
860
861                         val = g_hash_table_lookup(session->result.headers,
862                                                         "Transfer-Encoding");
863                         if (val != NULL) {
864                                 val = g_strrstr(val, "chunked");
865                                 if (val != NULL) {
866                                         session->result.use_chunk = TRUE;
867
868                                         session->chunck_state = CHUNK_SIZE;
869                                         session->chunk_left = 0;
870                                         session->total_len = 0;
871                                 }
872                         }
873
874                         if (handle_body(session, ptr, bytes_read) < 0) {
875                                 session->transport_watch = 0;
876                                 return FALSE;
877                         }
878                         break;
879                 }
880
881                 str = session->current_header->str;
882
883                 if (session->result.status == 0) {
884                         unsigned int code;
885
886                         if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
887                                 session->result.status = code;
888                 }
889
890                 debug(session->web, "[header] %s", str);
891
892                 /* handle multi-line header */
893                 if (str[0] == ' ' || str[0] == '\t')
894                         handle_multi_line(session);
895                 else
896                         add_header_field(session);
897
898                 g_string_truncate(session->current_header, 0);
899         }
900
901         return TRUE;
902 }
903
904 static int connect_session_transport(struct web_session *session)
905 {
906         GIOFlags flags;
907         int sk;
908
909         sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
910                         IPPROTO_TCP);
911         if (sk < 0)
912                 return -EIO;
913
914         if (session->web->index > 0) {
915                 char interface[IF_NAMESIZE];
916
917                 memset(interface, 0, IF_NAMESIZE);
918
919                 if (if_indextoname(session->web->index, interface) != NULL) {
920                         if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
921                                                 interface, IF_NAMESIZE) < 0) {
922                                 close(sk);
923                                 return -EIO;
924                         }
925
926                         debug(session->web, "Use interface %s", interface);
927                 }
928         }
929
930         if (session->flags & SESSION_FLAG_USE_TLS) {
931                 debug(session->web, "using TLS encryption");
932                 session->transport_channel = g_io_channel_gnutls_new(sk);
933         } else {
934                 debug(session->web, "no encryption");
935                 session->transport_channel = g_io_channel_unix_new(sk);
936         }
937
938         if (session->transport_channel == NULL) {
939                 close(sk);
940                 return -ENOMEM;
941         }
942
943         flags = g_io_channel_get_flags(session->transport_channel);
944         g_io_channel_set_flags(session->transport_channel,
945                                         flags | G_IO_FLAG_NONBLOCK, NULL);
946
947         g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
948         g_io_channel_set_buffered(session->transport_channel, FALSE);
949
950         g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
951
952         if (connect(sk, session->addr->ai_addr,
953                         session->addr->ai_addrlen) < 0) {
954                 if (errno != EINPROGRESS) {
955                         close(sk);
956                         return -EIO;
957                 }
958         }
959
960         session->transport_watch = g_io_add_watch(session->transport_channel,
961                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
962                                                 received_data, session);
963
964         session->send_watch = g_io_add_watch(session->transport_channel,
965                                 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
966                                                 send_data, session);
967
968         return 0;
969 }
970
971 static int create_transport(struct web_session *session)
972 {
973         int err;
974
975         err = connect_session_transport(session);
976         if (err < 0)
977                 return err;
978
979         debug(session->web, "creating session %s:%u",
980                                         session->address, session->port);
981
982         return 0;
983 }
984
985 static int parse_url(struct web_session *session,
986                                 const char *url, const char *proxy)
987 {
988         char *scheme, *host, *port, *path;
989
990         scheme = g_strdup(url);
991         if (scheme == NULL)
992                 return -EINVAL;
993
994         host = strstr(scheme, "://");
995         if (host != NULL) {
996                 *host = '\0';
997                 host += 3;
998
999                 if (strcasecmp(scheme, "https") == 0) {
1000                         session->port = 443;
1001                         session->flags |= SESSION_FLAG_USE_TLS;
1002                 } else if (strcasecmp(scheme, "http") == 0) {
1003                         session->port = 80;
1004                 } else {
1005                         g_free(scheme);
1006                         return -EINVAL;
1007                 }
1008         } else {
1009                 host = scheme;
1010                 session->port = 80;
1011         }
1012
1013         path = strchr(host, '/');
1014         if (path != NULL)
1015                 *(path++) = '\0';
1016
1017         if (proxy == NULL)
1018                 session->request = g_strdup_printf("/%s", path ? path : "");
1019         else
1020                 session->request = g_strdup(url);
1021
1022         port = strrchr(host, ':');
1023         if (port != NULL) {
1024                 char *end;
1025                 int tmp = strtol(port + 1, &end, 10);
1026
1027                 if (*end == '\0') {
1028                         *port = '\0';
1029                         session->port = tmp;
1030                 }
1031
1032                 if (proxy == NULL)
1033                         session->host = g_strdup(host);
1034                 else
1035                         session->host = g_strdup_printf("%s:%u", host, tmp);
1036         } else
1037                 session->host = g_strdup(host);
1038
1039         g_free(scheme);
1040
1041         if (proxy == NULL)
1042                 return 0;
1043
1044         scheme = g_strdup(proxy);
1045         if (scheme == NULL)
1046                 return -EINVAL;
1047
1048         host = strstr(proxy, "://");
1049         if (host != NULL) {
1050                 *host = '\0';
1051                 host += 3;
1052
1053                 if (strcasecmp(scheme, "http") != 0) {
1054                         g_free(scheme);
1055                         return -EINVAL;
1056                 }
1057         } else
1058                 host = scheme;
1059
1060         path = strchr(host, '/');
1061         if (path != NULL)
1062                 *(path++) = '\0';
1063
1064         port = strrchr(host, ':');
1065         if (port != NULL) {
1066                 char *end;
1067                 int tmp = strtol(port + 1, &end, 10);
1068
1069                 if (*end == '\0') {
1070                         *port = '\0';
1071                         session->port = tmp;
1072                 }
1073         }
1074
1075         session->address = g_strdup(host);
1076
1077         g_free(scheme);
1078
1079         return 0;
1080 }
1081
1082 static void resolv_result(GResolvResultStatus status,
1083                                         char **results, gpointer user_data)
1084 {
1085         struct web_session *session = user_data;
1086         struct addrinfo hints;
1087         char *port;
1088         int ret;
1089
1090         if (results == NULL || results[0] == NULL) {
1091                 call_result_func(session, 404);
1092                 return;
1093         }
1094
1095         debug(session->web, "address %s", results[0]);
1096
1097         memset(&hints, 0, sizeof(struct addrinfo));
1098         hints.ai_flags = AI_NUMERICHOST;
1099         hints.ai_family = session->web->family;
1100
1101         if (session->addr != NULL) {
1102                 freeaddrinfo(session->addr);
1103                 session->addr = NULL;
1104         }
1105
1106         port = g_strdup_printf("%u", session->port);
1107         ret = getaddrinfo(results[0], port, &hints, &session->addr);
1108         g_free(port);
1109         if (ret != 0 || session->addr == NULL) {
1110                 call_result_func(session, 400);
1111                 return;
1112         }
1113
1114         session->address = g_strdup(results[0]);
1115
1116         if (create_transport(session) < 0) {
1117                 call_result_func(session, 409);
1118                 return;
1119         }
1120 }
1121
1122 static guint do_request(GWeb *web, const char *url,
1123                                 const char *type, GWebInputFunc input,
1124                                 GWebResultFunc func, gpointer user_data)
1125 {
1126         struct web_session *session;
1127
1128         if (web == NULL || url == NULL)
1129                 return 0;
1130
1131         debug(web, "request %s", url);
1132
1133         session = g_try_new0(struct web_session, 1);
1134         if (session == NULL)
1135                 return 0;
1136
1137         if (parse_url(session, url, web->proxy) < 0) {
1138                 free_session(session);
1139                 return 0;
1140         }
1141
1142         debug(web, "address %s", session->address);
1143         debug(web, "port %u", session->port);
1144         debug(web, "host %s", session->host);
1145         debug(web, "flags %lu", session->flags);
1146         debug(web, "request %s", session->request);
1147
1148         if (type != NULL) {
1149                 session->content_type = g_strdup(type);
1150
1151                 debug(web, "content-type %s", session->content_type);
1152         }
1153
1154         session->web = web;
1155
1156         session->result_func = func;
1157         session->input_func = input;
1158         session->user_data = user_data;
1159
1160         session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1161         if (session->receive_buffer == NULL) {
1162                 free_session(session);
1163                 return 0;
1164         }
1165
1166         session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1167                                                         g_free, g_free);
1168         if (session->result.headers == NULL) {
1169                 free_session(session);
1170                 return 0;
1171         }
1172
1173         session->receive_space = DEFAULT_BUFFER_SIZE;
1174         session->send_buffer = g_string_sized_new(0);
1175         session->current_header = g_string_sized_new(0);
1176         session->header_done = FALSE;
1177         session->body_done = FALSE;
1178
1179         if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1180                 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1181                                         session->host, resolv_result, session);
1182                 if (session->resolv_action == 0) {
1183                         free_session(session);
1184                         return 0;
1185                 }
1186         } else {
1187                 struct addrinfo hints;
1188                 char *port;
1189                 int ret;
1190
1191                 if (session->address == NULL)
1192                         session->address = g_strdup(session->host);
1193
1194                 memset(&hints, 0, sizeof(struct addrinfo));
1195                 hints.ai_flags = AI_NUMERICHOST;
1196                 hints.ai_family = session->web->family;
1197
1198                 if (session->addr != NULL) {
1199                         freeaddrinfo(session->addr);
1200                         session->addr = NULL;
1201                 }
1202
1203                 port = g_strdup_printf("%u", session->port);
1204                 ret = getaddrinfo(session->address, port, &hints,
1205                                                         &session->addr);
1206                 g_free(port);
1207                 if (ret != 0 || session->addr == NULL) {
1208                         free_session(session);
1209                         return 0;
1210                 }
1211
1212                 if (create_transport(session) < 0) {
1213                         free_session(session);
1214                         return 0;
1215                 }
1216         }
1217
1218         web->session_list = g_list_append(web->session_list, session);
1219
1220         return web->next_query_id++;
1221 }
1222
1223 guint g_web_request_get(GWeb *web, const char *url,
1224                                 GWebResultFunc func, gpointer user_data)
1225 {
1226         return do_request(web, url, NULL, NULL, func, user_data);
1227 }
1228
1229 guint g_web_request_post(GWeb *web, const char *url,
1230                                 const char *type, GWebInputFunc input,
1231                                 GWebResultFunc func, gpointer user_data)
1232 {
1233         return do_request(web, url, type, input, func, user_data);
1234 }
1235
1236 gboolean g_web_cancel_request(GWeb *web, guint id)
1237 {
1238         if (web == NULL)
1239                 return FALSE;
1240
1241         return TRUE;
1242 }
1243
1244 guint16 g_web_result_get_status(GWebResult *result)
1245 {
1246         if (result == NULL)
1247                 return 0;
1248
1249         return result->status;
1250 }
1251
1252 gboolean g_web_result_get_chunk(GWebResult *result,
1253                                 const guint8 **chunk, gsize *length)
1254 {
1255         if (result == NULL)
1256                 return FALSE;
1257
1258         if (chunk == NULL)
1259                 return FALSE;
1260
1261         *chunk = result->buffer;
1262
1263         if (length != NULL)
1264                 *length = result->length;
1265
1266         return TRUE;
1267 }
1268
1269 gboolean g_web_result_get_header(GWebResult *result,
1270                                 const char *header, const char **value)
1271 {
1272         if (result == NULL)
1273                 return FALSE;
1274
1275         if (value == NULL)
1276                 return FALSE;
1277
1278         *value = g_hash_table_lookup(result->headers, header);
1279
1280         if (*value == NULL)
1281                 return FALSE;
1282
1283         return TRUE;
1284 }
1285
1286 struct _GWebParser {
1287         gint ref_count;
1288         char *begin_token;
1289         char *end_token;
1290         const char *token_str;
1291         size_t token_len;
1292         size_t token_pos;
1293         gboolean intoken;
1294         GString *content;
1295         GWebParserFunc func;
1296         gpointer user_data;
1297 };
1298
1299 GWebParser *g_web_parser_new(const char *begin, const char *end,
1300                                 GWebParserFunc func, gpointer user_data)
1301 {
1302         GWebParser *parser;
1303
1304         parser = g_try_new0(GWebParser, 1);
1305         if (parser == NULL)
1306                 return NULL;
1307
1308         parser->ref_count = 1;
1309
1310         parser->begin_token = g_strdup(begin);
1311         parser->end_token = g_strdup(end);
1312
1313         if (parser->begin_token == NULL) {
1314                 g_free(parser);
1315                 return NULL;
1316         }
1317
1318         parser->func = func;
1319         parser->user_data = user_data;
1320
1321         parser->token_str = parser->begin_token;
1322         parser->token_len = strlen(parser->token_str);
1323         parser->token_pos = 0;
1324
1325         parser->intoken = FALSE;
1326         parser->content = g_string_sized_new(0);
1327
1328         return parser;
1329 }
1330
1331 GWebParser *g_web_parser_ref(GWebParser *parser)
1332 {
1333         if (parser == NULL)
1334                 return NULL;
1335
1336         __sync_fetch_and_add(&parser->ref_count, 1);
1337
1338         return parser;
1339 }
1340
1341 void g_web_parser_unref(GWebParser *parser)
1342 {
1343         if (parser == NULL)
1344                 return;
1345
1346         if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1347                 return;
1348
1349         g_string_free(parser->content, TRUE);
1350
1351         g_free(parser->begin_token);
1352         g_free(parser->end_token);
1353         g_free(parser);
1354 }
1355
1356 void g_web_parser_feed_data(GWebParser *parser,
1357                                 const guint8 *data, gsize length)
1358 {
1359         const guint8 *ptr = data;
1360
1361         if (parser == NULL)
1362                 return;
1363
1364         while (length > 0) {
1365                 guint8 chr = parser->token_str[parser->token_pos];
1366
1367                 if (parser->token_pos == 0) {
1368                         guint8 *pos;
1369
1370                         pos = memchr(ptr, chr, length);
1371                         if (pos == NULL) {
1372                                 if (parser->intoken == TRUE)
1373                                         g_string_append_len(parser->content,
1374                                                         (gchar *) ptr, length);
1375                                 break;
1376                         }
1377
1378                         if (parser->intoken == TRUE)
1379                                 g_string_append_len(parser->content,
1380                                                 (gchar *) ptr, (pos - ptr) + 1);
1381
1382                         length -= (pos - ptr) + 1;
1383                         ptr = pos + 1;
1384
1385                         parser->token_pos++;
1386                         continue;
1387                 }
1388
1389                 if (parser->intoken == TRUE)
1390                         g_string_append_c(parser->content, ptr[0]);
1391
1392                 if (ptr[0] != chr) {
1393                         length--;
1394                         ptr++;
1395
1396                         parser->token_pos = 0;
1397                         continue;
1398                 }
1399
1400                 length--;
1401                 ptr++;
1402
1403                 parser->token_pos++;
1404
1405                 if (parser->token_pos == parser->token_len) {
1406                         if (parser->intoken == FALSE) {
1407                                 g_string_append(parser->content,
1408                                                         parser->token_str);
1409
1410                                 parser->intoken = TRUE;
1411                                 parser->token_str = parser->end_token;
1412                                 parser->token_len = strlen(parser->end_token);
1413                                 parser->token_pos = 0;
1414                         } else {
1415                                 char *str;
1416                                 str = g_string_free(parser->content, FALSE);
1417                                 parser->content = g_string_sized_new(0);
1418                                 if (parser->func)
1419                                         parser->func(str, parser->user_data);
1420                                 g_free(str);
1421
1422                                 parser->intoken = FALSE;
1423                                 parser->token_str = parser->begin_token;
1424                                 parser->token_len = strlen(parser->begin_token);
1425                                 parser->token_pos = 0;
1426                         }
1427                 }
1428         }
1429 }
1430
1431 void g_web_parser_end_data(GWebParser *parser)
1432 {
1433         if (parser == NULL)
1434                 return;
1435 }