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