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