gweb: workaround for setsockopt failure
[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                         close(sk);
1023                         return -EIO;
1024                 }
1025         }
1026
1027         if (session->flags & SESSION_FLAG_USE_TLS) {
1028                 debug(session->web, "using TLS encryption");
1029                 session->transport_channel = g_io_channel_gnutls_new(sk);
1030         } else {
1031                 debug(session->web, "no encryption");
1032                 session->transport_channel = g_io_channel_unix_new(sk);
1033         }
1034
1035         if (session->transport_channel == NULL) {
1036                 close(sk);
1037                 return -ENOMEM;
1038         }
1039
1040         flags = g_io_channel_get_flags(session->transport_channel);
1041         g_io_channel_set_flags(session->transport_channel,
1042                                         flags | G_IO_FLAG_NONBLOCK, NULL);
1043
1044         g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
1045         g_io_channel_set_buffered(session->transport_channel, FALSE);
1046
1047         g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
1048
1049         if (connect(sk, session->addr->ai_addr,
1050                         session->addr->ai_addrlen) < 0) {
1051                 if (errno != EINPROGRESS) {
1052                         close(sk);
1053                         return -EIO;
1054                 }
1055         }
1056
1057         session->transport_watch = g_io_add_watch(session->transport_channel,
1058                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1059                                                 received_data, session);
1060
1061         session->send_watch = g_io_add_watch(session->transport_channel,
1062                                 G_IO_OUT | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
1063                                                 send_data, session);
1064
1065         return 0;
1066 }
1067
1068 static int create_transport(struct web_session *session)
1069 {
1070         int err;
1071
1072         err = connect_session_transport(session);
1073         if (err < 0)
1074                 return err;
1075
1076         debug(session->web, "creating session %s:%u",
1077                                         session->address, session->port);
1078
1079         return 0;
1080 }
1081
1082 static int parse_url(struct web_session *session,
1083                                 const char *url, const char *proxy)
1084 {
1085         char *scheme, *host, *port, *path;
1086
1087         scheme = g_strdup(url);
1088         if (scheme == NULL)
1089                 return -EINVAL;
1090
1091         host = strstr(scheme, "://");
1092         if (host != NULL) {
1093                 *host = '\0';
1094                 host += 3;
1095
1096                 if (strcasecmp(scheme, "https") == 0) {
1097                         session->port = 443;
1098                         session->flags |= SESSION_FLAG_USE_TLS;
1099                 } else if (strcasecmp(scheme, "http") == 0) {
1100                         session->port = 80;
1101                 } else {
1102                         g_free(scheme);
1103                         return -EINVAL;
1104                 }
1105         } else {
1106                 host = scheme;
1107                 session->port = 80;
1108         }
1109
1110         path = strchr(host, '/');
1111         if (path != NULL)
1112                 *(path++) = '\0';
1113
1114         if (proxy == NULL)
1115                 session->request = g_strdup_printf("/%s", path ? path : "");
1116         else
1117                 session->request = g_strdup(url);
1118
1119         port = strrchr(host, ':');
1120         if (port != NULL) {
1121                 char *end;
1122                 int tmp = strtol(port + 1, &end, 10);
1123
1124                 if (*end == '\0') {
1125                         *port = '\0';
1126                         session->port = tmp;
1127                 }
1128
1129                 if (proxy == NULL)
1130                         session->host = g_strdup(host);
1131                 else
1132                         session->host = g_strdup_printf("%s:%u", host, tmp);
1133         } else
1134                 session->host = g_strdup(host);
1135
1136         g_free(scheme);
1137
1138         if (proxy == NULL)
1139                 return 0;
1140
1141         scheme = g_strdup(proxy);
1142         if (scheme == NULL)
1143                 return -EINVAL;
1144
1145         host = strstr(proxy, "://");
1146         if (host != NULL) {
1147                 *host = '\0';
1148                 host += 3;
1149
1150                 if (strcasecmp(scheme, "http") != 0) {
1151                         g_free(scheme);
1152                         return -EINVAL;
1153                 }
1154         } else
1155                 host = scheme;
1156
1157         path = strchr(host, '/');
1158         if (path != NULL)
1159                 *(path++) = '\0';
1160
1161         port = strrchr(host, ':');
1162         if (port != NULL) {
1163                 char *end;
1164                 int tmp = strtol(port + 1, &end, 10);
1165
1166                 if (*end == '\0') {
1167                         *port = '\0';
1168                         session->port = tmp;
1169                 }
1170         }
1171
1172         session->address = g_strdup(host);
1173
1174         g_free(scheme);
1175
1176         return 0;
1177 }
1178
1179 static void resolv_result(GResolvResultStatus status,
1180                                         char **results, gpointer user_data)
1181 {
1182         struct web_session *session = user_data;
1183         struct addrinfo hints;
1184         char *port;
1185         int ret;
1186
1187         if (results == NULL || results[0] == NULL) {
1188                 call_result_func(session, 404);
1189                 return;
1190         }
1191
1192         debug(session->web, "address %s", results[0]);
1193
1194         memset(&hints, 0, sizeof(struct addrinfo));
1195         hints.ai_flags = AI_NUMERICHOST;
1196         hints.ai_family = session->web->family;
1197
1198         if (session->addr != NULL) {
1199                 freeaddrinfo(session->addr);
1200                 session->addr = NULL;
1201         }
1202
1203         port = g_strdup_printf("%u", session->port);
1204         ret = getaddrinfo(results[0], port, &hints, &session->addr);
1205         g_free(port);
1206         if (ret != 0 || session->addr == NULL) {
1207                 call_result_func(session, 400);
1208                 return;
1209         }
1210
1211         session->address = g_strdup(results[0]);
1212
1213         if (create_transport(session) < 0) {
1214                 call_result_func(session, 409);
1215                 return;
1216         }
1217 }
1218
1219 static guint do_request(GWeb *web, const char *url,
1220                                 const char *type, GWebInputFunc input,
1221                                 int fd, gsize length, GWebResultFunc func,
1222                                 gpointer user_data)
1223 {
1224         struct web_session *session;
1225
1226         if (web == NULL || url == NULL)
1227                 return 0;
1228
1229         debug(web, "request %s", url);
1230
1231         session = g_try_new0(struct web_session, 1);
1232         if (session == NULL)
1233                 return 0;
1234
1235         if (parse_url(session, url, web->proxy) < 0) {
1236                 free_session(session);
1237                 return 0;
1238         }
1239
1240         debug(web, "address %s", session->address);
1241         debug(web, "port %u", session->port);
1242         debug(web, "host %s", session->host);
1243         debug(web, "flags %lu", session->flags);
1244         debug(web, "request %s", session->request);
1245
1246         if (type != NULL) {
1247                 session->content_type = g_strdup(type);
1248
1249                 debug(web, "content-type %s", session->content_type);
1250         }
1251
1252         session->web = web;
1253
1254         session->result_func = func;
1255         session->input_func = input;
1256         session->fd = fd;
1257         session->length = length;
1258         session->offset = 0;
1259         session->user_data = user_data;
1260
1261         session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
1262         if (session->receive_buffer == NULL) {
1263                 free_session(session);
1264                 return 0;
1265         }
1266
1267         session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
1268                                                         g_free, g_free);
1269         if (session->result.headers == NULL) {
1270                 free_session(session);
1271                 return 0;
1272         }
1273
1274         session->receive_space = DEFAULT_BUFFER_SIZE;
1275         session->send_buffer = g_string_sized_new(0);
1276         session->current_header = g_string_sized_new(0);
1277         session->header_done = FALSE;
1278         session->body_done = FALSE;
1279
1280         if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
1281                 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
1282                                         session->host, resolv_result, session);
1283                 if (session->resolv_action == 0) {
1284                         free_session(session);
1285                         return 0;
1286                 }
1287         } else {
1288                 struct addrinfo hints;
1289                 char *port;
1290                 int ret;
1291
1292                 if (session->address == NULL)
1293                         session->address = g_strdup(session->host);
1294
1295                 memset(&hints, 0, sizeof(struct addrinfo));
1296                 hints.ai_flags = AI_NUMERICHOST;
1297                 hints.ai_family = session->web->family;
1298
1299                 if (session->addr != NULL) {
1300                         freeaddrinfo(session->addr);
1301                         session->addr = NULL;
1302                 }
1303
1304                 port = g_strdup_printf("%u", session->port);
1305                 ret = getaddrinfo(session->address, port, &hints,
1306                                                         &session->addr);
1307                 g_free(port);
1308                 if (ret != 0 || session->addr == NULL) {
1309                         free_session(session);
1310                         return 0;
1311                 }
1312
1313                 if (create_transport(session) < 0) {
1314                         free_session(session);
1315                         return 0;
1316                 }
1317         }
1318
1319         web->session_list = g_list_append(web->session_list, session);
1320
1321         return web->next_query_id++;
1322 }
1323
1324 guint g_web_request_get(GWeb *web, const char *url,
1325                                 GWebResultFunc func, gpointer user_data)
1326 {
1327         return do_request(web, url, NULL, NULL, -1, 0, func, user_data);
1328 }
1329
1330 guint g_web_request_post(GWeb *web, const char *url,
1331                                 const char *type, GWebInputFunc input,
1332                                 GWebResultFunc func, gpointer user_data)
1333 {
1334         return do_request(web, url, type, input, -1, 0, func, user_data);
1335 }
1336
1337 guint g_web_request_post_file(GWeb *web, const char *url,
1338                                 const char *type, const char *file,
1339                                 GWebResultFunc func, gpointer user_data)
1340 {
1341         struct stat st;
1342         int fd;
1343         guint ret;
1344
1345         if (stat(file, &st) < 0)
1346                 return 0;
1347
1348         fd = open(file, O_RDONLY);
1349         if (fd < 0)
1350                 return 0;
1351
1352         ret = do_request(web, url, type, NULL, fd, st.st_size, func, user_data);
1353         if (ret == 0)
1354                 close(fd);
1355
1356         return ret;
1357 }
1358
1359 gboolean g_web_cancel_request(GWeb *web, guint id)
1360 {
1361         if (web == NULL)
1362                 return FALSE;
1363
1364         return TRUE;
1365 }
1366
1367 guint16 g_web_result_get_status(GWebResult *result)
1368 {
1369         if (result == NULL)
1370                 return 0;
1371
1372         return result->status;
1373 }
1374
1375 gboolean g_web_result_get_chunk(GWebResult *result,
1376                                 const guint8 **chunk, gsize *length)
1377 {
1378         if (result == NULL)
1379                 return FALSE;
1380
1381         if (chunk == NULL)
1382                 return FALSE;
1383
1384         *chunk = result->buffer;
1385
1386         if (length != NULL)
1387                 *length = result->length;
1388
1389         return TRUE;
1390 }
1391
1392 gboolean g_web_result_get_header(GWebResult *result,
1393                                 const char *header, const char **value)
1394 {
1395         if (result == NULL)
1396                 return FALSE;
1397
1398         if (value == NULL)
1399                 return FALSE;
1400
1401         *value = g_hash_table_lookup(result->headers, header);
1402
1403         if (*value == NULL)
1404                 return FALSE;
1405
1406         return TRUE;
1407 }
1408
1409 struct _GWebParser {
1410         gint ref_count;
1411         char *begin_token;
1412         char *end_token;
1413         const char *token_str;
1414         size_t token_len;
1415         size_t token_pos;
1416         gboolean intoken;
1417         GString *content;
1418         GWebParserFunc func;
1419         gpointer user_data;
1420 };
1421
1422 GWebParser *g_web_parser_new(const char *begin, const char *end,
1423                                 GWebParserFunc func, gpointer user_data)
1424 {
1425         GWebParser *parser;
1426
1427         parser = g_try_new0(GWebParser, 1);
1428         if (parser == NULL)
1429                 return NULL;
1430
1431         parser->ref_count = 1;
1432
1433         parser->begin_token = g_strdup(begin);
1434         parser->end_token = g_strdup(end);
1435
1436         if (parser->begin_token == NULL) {
1437                 g_free(parser);
1438                 return NULL;
1439         }
1440
1441         parser->func = func;
1442         parser->user_data = user_data;
1443
1444         parser->token_str = parser->begin_token;
1445         parser->token_len = strlen(parser->token_str);
1446         parser->token_pos = 0;
1447
1448         parser->intoken = FALSE;
1449         parser->content = g_string_sized_new(0);
1450
1451         return parser;
1452 }
1453
1454 GWebParser *g_web_parser_ref(GWebParser *parser)
1455 {
1456         if (parser == NULL)
1457                 return NULL;
1458
1459         __sync_fetch_and_add(&parser->ref_count, 1);
1460
1461         return parser;
1462 }
1463
1464 void g_web_parser_unref(GWebParser *parser)
1465 {
1466         if (parser == NULL)
1467                 return;
1468
1469         if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
1470                 return;
1471
1472         g_string_free(parser->content, TRUE);
1473
1474         g_free(parser->begin_token);
1475         g_free(parser->end_token);
1476         g_free(parser);
1477 }
1478
1479 void g_web_parser_feed_data(GWebParser *parser,
1480                                 const guint8 *data, gsize length)
1481 {
1482         const guint8 *ptr = data;
1483
1484         if (parser == NULL)
1485                 return;
1486
1487         while (length > 0) {
1488                 guint8 chr = parser->token_str[parser->token_pos];
1489
1490                 if (parser->token_pos == 0) {
1491                         guint8 *pos;
1492
1493                         pos = memchr(ptr, chr, length);
1494                         if (pos == NULL) {
1495                                 if (parser->intoken == TRUE)
1496                                         g_string_append_len(parser->content,
1497                                                         (gchar *) ptr, length);
1498                                 break;
1499                         }
1500
1501                         if (parser->intoken == TRUE)
1502                                 g_string_append_len(parser->content,
1503                                                 (gchar *) ptr, (pos - ptr) + 1);
1504
1505                         length -= (pos - ptr) + 1;
1506                         ptr = pos + 1;
1507
1508                         parser->token_pos++;
1509                         continue;
1510                 }
1511
1512                 if (parser->intoken == TRUE)
1513                         g_string_append_c(parser->content, ptr[0]);
1514
1515                 if (ptr[0] != chr) {
1516                         length--;
1517                         ptr++;
1518
1519                         parser->token_pos = 0;
1520                         continue;
1521                 }
1522
1523                 length--;
1524                 ptr++;
1525
1526                 parser->token_pos++;
1527
1528                 if (parser->token_pos == parser->token_len) {
1529                         if (parser->intoken == FALSE) {
1530                                 g_string_append(parser->content,
1531                                                         parser->token_str);
1532
1533                                 parser->intoken = TRUE;
1534                                 parser->token_str = parser->end_token;
1535                                 parser->token_len = strlen(parser->end_token);
1536                                 parser->token_pos = 0;
1537                         } else {
1538                                 char *str;
1539                                 str = g_string_free(parser->content, FALSE);
1540                                 parser->content = g_string_sized_new(0);
1541                                 if (parser->func)
1542                                         parser->func(str, parser->user_data);
1543                                 g_free(str);
1544
1545                                 parser->intoken = FALSE;
1546                                 parser->token_str = parser->begin_token;
1547                                 parser->token_len = strlen(parser->begin_token);
1548                                 parser->token_pos = 0;
1549                         }
1550                 }
1551         }
1552 }
1553
1554 void g_web_parser_end_data(GWebParser *parser)
1555 {
1556         if (parser == NULL)
1557                 return;
1558 }