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