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