Fix GWeb header parsing and make it more robust
[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 <unistd.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34
35 #include "giognutls.h"
36 #include "gresolv.h"
37 #include "gweb.h"
38
39 #define DEFAULT_BUFFER_SIZE  2048
40
41 #define SESSION_FLAG_USE_TLS    (1 << 0)
42
43 struct _GWebResult {
44         guint status;
45         const guint8 *buffer;
46         gsize length;
47 };
48
49 struct web_session {
50         GWeb *web;
51
52         char *address;
53         char *host;
54         uint16_t port;
55         unsigned long flags;
56
57         GIOChannel *transport_channel;
58         guint transport_watch;
59
60         guint resolv_action;
61         char *request;
62
63         guint8 *receive_buffer;
64         gsize receive_space;
65         GString *current_header;
66         gboolean header_done;
67
68         GWebResult result;
69
70         GWebResultFunc result_func;
71         gpointer result_data;
72 };
73
74 struct _GWeb {
75         gint ref_count;
76
77         guint next_query_id;
78
79         int index;
80         GList *session_list;
81
82         GResolv *resolv;
83         char *accept_option;
84         char *user_agent;
85         gboolean close_connection;
86
87         GWebDebugFunc debug_func;
88         gpointer debug_data;
89 };
90
91 static inline void debug(GWeb *web, const char *format, ...)
92 {
93         char str[256];
94         va_list ap;
95
96         if (web->debug_func == NULL)
97                 return;
98
99         va_start(ap, format);
100
101         if (vsnprintf(str, sizeof(str), format, ap) > 0)
102                 web->debug_func(str, web->debug_data);
103
104         va_end(ap);
105 }
106
107 static void free_session(struct web_session *session)
108 {
109         GWeb *web = session->web;
110
111         if (session == NULL)
112                 return;
113
114         g_free(session->request);
115
116         if (session->resolv_action > 0)
117                 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
118
119         if (session->transport_watch > 0)
120                 g_source_remove(session->transport_watch);
121
122         if (session->transport_channel != NULL)
123                 g_io_channel_unref(session->transport_channel);
124
125         g_string_free(session->current_header, TRUE);
126         g_free(session->receive_buffer);
127
128         g_free(session->host);
129         g_free(session->address);
130         g_free(session);
131 }
132
133 static void flush_sessions(GWeb *web)
134 {
135         GList *list;
136
137         for (list = g_list_first(web->session_list);
138                                         list; list = g_list_next(list))
139                 free_session(list->data);
140
141         g_list_free(web->session_list);
142         web->session_list = NULL;
143 }
144
145 GWeb *g_web_new(int index)
146 {
147         GWeb *web;
148
149         if (index < 0)
150                 return NULL;
151
152         web = g_try_new0(GWeb, 1);
153         if (web == NULL)
154                 return NULL;
155
156         web->ref_count = 1;
157
158         web->next_query_id = 1;
159
160         web->index = index;
161         web->session_list = NULL;
162
163         web->resolv = g_resolv_new(index);
164         if (web->resolv == NULL) {
165                 g_free(web);
166                 return NULL;
167         }
168
169         web->accept_option = g_strdup("*/*");
170         web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
171         web->close_connection = FALSE;
172
173         return web;
174 }
175
176 GWeb *g_web_ref(GWeb *web)
177 {
178         if (web == NULL)
179                 return NULL;
180
181         g_atomic_int_inc(&web->ref_count);
182
183         return web;
184 }
185
186 void g_web_unref(GWeb *web)
187 {
188         if (web == NULL)
189                 return;
190
191         if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
192                 return;
193
194         flush_sessions(web);
195
196         g_resolv_unref(web->resolv);
197
198         g_free(web->accept_option);
199         g_free(web->user_agent);
200         g_free(web);
201 }
202
203 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
204 {
205         if (web == NULL)
206                 return;
207
208         web->debug_func = func;
209         web->debug_data = user_data;
210
211         g_resolv_set_debug(web->resolv, func, user_data);
212 }
213
214 gboolean g_web_add_nameserver(GWeb *web, const char *address)
215 {
216         if (web == NULL)
217                 return FALSE;
218
219         g_resolv_add_nameserver(web->resolv, address, 53, 0);
220
221         return TRUE;
222 }
223
224 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
225 {
226         g_free(web->accept_option);
227
228         if (format == NULL) {
229                 web->accept_option = NULL;
230                 debug(web, "clearing accept option");
231         } else {
232                 web->accept_option = g_strdup_vprintf(format, args);
233                 debug(web, "setting accept %s", web->accept_option);
234         }
235
236         return TRUE;
237 }
238
239 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
240 {
241         va_list args;
242         gboolean result;
243
244         if (web == NULL)
245                 return FALSE;
246
247         va_start(args, format);
248         result = set_accept_option(web, format, args);
249         va_end(args);
250
251         return result;
252 }
253
254 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
255 {
256         g_free(web->user_agent);
257
258         if (format == NULL) {
259                 web->user_agent = NULL;
260                 debug(web, "clearing user agent");
261         } else {
262                 web->user_agent = g_strdup_vprintf(format, args);
263                 debug(web, "setting user agent %s", web->user_agent);
264         }
265
266         return TRUE;
267 }
268
269 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
270 {
271         va_list args;
272         gboolean result;
273
274         if (web == NULL)
275                 return FALSE;
276
277         va_start(args, format);
278         result = set_user_agent(web, format, args);
279         va_end(args);
280
281         return result;
282 }
283
284 void g_web_set_close_connection(GWeb *web, gboolean enabled)
285 {
286         if (web == NULL)
287                 return;
288
289         web->close_connection = enabled;
290 }
291
292 gboolean g_web_get_close_connection(GWeb *web)
293 {
294         if (web == NULL)
295                 return FALSE;
296
297         return web->close_connection;
298 }
299
300 static inline void call_result_func(struct web_session *session, guint status)
301 {
302         if (session->result_func == NULL)
303                 return;
304
305         session->result_func(status, &session->result, session->result_data);
306 }
307
308 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
309                                                         gpointer user_data)
310 {
311         struct web_session *session = user_data;
312         guint8 *ptr = session->receive_buffer;
313         gsize bytes_read;
314         GIOStatus status;
315
316         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
317                 session->transport_watch = 0;
318                 session->result.buffer = NULL;
319                 session->result.length = 0;
320                 call_result_func(session, 400);
321                 return FALSE;
322         }
323
324         status = g_io_channel_read_chars(channel,
325                                 (gchar *) session->receive_buffer,
326                                 session->receive_space - 1, &bytes_read, NULL);
327
328         debug(session->web, "bytes read %zu", bytes_read);
329
330         if (status != G_IO_STATUS_NORMAL) {
331                 session->transport_watch = 0;
332                 session->result.buffer = NULL;
333                 session->result.length = 0;
334                 call_result_func(session, 200);
335                 return FALSE;
336         }
337
338         session->receive_buffer[bytes_read] = '\0';
339
340         if (session->header_done == TRUE) {
341                 session->result.buffer = session->receive_buffer;
342                 session->result.length = bytes_read;
343                 call_result_func(session, 100);
344                 return TRUE;
345         }
346
347         while (bytes_read > 0) {
348                 guint8 *pos;
349                 gsize count;
350                 char *str;
351
352                 pos = memchr(ptr, '\n', bytes_read);
353                 if (pos == NULL) {
354                         g_string_append_len(session->current_header,
355                                                 (gchar *) ptr, bytes_read);
356                         return TRUE;
357                 }
358
359                 *pos = '\0';
360                 count = strlen((char *) ptr);
361                 if (count > 0 && ptr[count - 1] == '\r') {
362                         ptr[--count] = '\0';
363                         bytes_read--;
364                 }
365
366                 g_string_append_len(session->current_header,
367                                                 (gchar *) ptr, count);
368
369                 bytes_read -= count + 1;
370                 ptr = pos + 1;
371
372                 if (session->current_header->len == 0) {
373                         session->header_done = TRUE;
374                         session->result.buffer = pos + 1;
375                         session->result.length = bytes_read;
376                         call_result_func(session, 100);
377                         break;
378                 }
379
380                 str = session->current_header->str;
381
382                 if (session->result.status == 0) {
383                         unsigned int code;
384
385                         if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
386                                 session->result.status = code;
387                 }
388
389                 debug(session->web, "[header] %s", str);
390
391                 g_string_truncate(session->current_header, 0);
392         }
393
394         return TRUE;
395 }
396
397 static int connect_session_transport(struct web_session *session)
398 {
399         struct sockaddr_in sin;
400         int sk;
401
402         sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
403         if (sk < 0)
404                 return -EIO;
405
406         memset(&sin, 0, sizeof(sin));
407         sin.sin_family = AF_INET;
408         sin.sin_port = htons(session->port);
409         sin.sin_addr.s_addr = inet_addr(session->address);
410
411         if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
412                 close(sk);
413                 return -EIO;
414         }
415
416         if (session->flags & SESSION_FLAG_USE_TLS)
417                 session->transport_channel = g_io_channel_gnutls_new(sk);
418         else
419                 session->transport_channel = g_io_channel_unix_new(sk);
420
421         if (session->transport_channel == NULL) {
422                 close(sk);
423                 return -ENOMEM;
424         }
425
426         g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
427         g_io_channel_set_buffered(session->transport_channel, FALSE);
428
429         g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
430
431         session->transport_watch = g_io_add_watch(session->transport_channel,
432                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
433                                                 received_data, session);
434
435         return 0;
436 }
437
438 static int create_transport(struct web_session *session)
439 {
440         int err;
441
442         err = connect_session_transport(session);
443         if (err < 0)
444                 return err;
445
446         debug(session->web, "creating session %s:%u",
447                                         session->address, session->port);
448
449         return 0;
450 }
451
452 static void start_request(struct web_session *session)
453 {
454         GString *buf;
455         gchar *str;
456         gsize count, bytes_written;
457         GIOStatus status;
458
459         debug(session->web, "request %s from %s",
460                                         session->request, session->host);
461
462         buf = g_string_new(NULL);
463         g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
464         g_string_append_printf(buf, "Host: %s\r\n", session->host);
465         if (session->web->user_agent != NULL)
466                 g_string_append_printf(buf, "User-Agent: %s\r\n",
467                                                 session->web->user_agent);
468         if (session->web->accept_option != NULL)
469                 g_string_append_printf(buf, "Accept: %s\r\n",
470                                                 session->web->accept_option);
471         if (session->web->close_connection == TRUE)
472                 g_string_append(buf, "Connection: close\r\n");
473         g_string_append(buf, "\r\n");
474         str = g_string_free(buf, FALSE);
475
476         count = strlen(str);
477
478         debug(session->web, "bytes to write %zu", count);
479
480         status = g_io_channel_write_chars(session->transport_channel,
481                                         str, count, &bytes_written, NULL);
482
483         debug(session->web, "status %u bytes written %zu",
484                                                 status, bytes_written);
485
486         //printf("%s", str);
487
488         g_free(str);
489 }
490
491 static int parse_url(struct web_session *session, const char *url)
492 {
493         char *scheme, *host, *port, *path;
494
495         scheme = g_strdup(url);
496         if (scheme == NULL)
497                 return -EINVAL;
498
499         host = strstr(scheme, "://");
500         if (host != NULL) {
501                 *host = '\0';
502                 host += 3;
503
504                 if (strcasecmp(scheme, "https") == 0) {
505                         session->port = 443;
506                         session->flags |= SESSION_FLAG_USE_TLS;
507                 } else if (strcasecmp(scheme, "http") == 0) {
508                         session->port = 80;
509                 } else {
510                         g_free(scheme);
511                         return -EINVAL;
512                 }
513         } else {
514                 host = scheme;
515                 session->port = 80;
516         }
517
518         path = strchr(host, '/');
519         if (path != NULL)
520                 *(path++) = '\0';
521
522         session->request = g_strdup_printf("/%s", path ? path : "");
523
524         port = strrchr(host, ':');
525         if (port != NULL) {
526                 char *end;
527                 int tmp = strtol(port + 1, &end, 10);
528
529                 if (*end == '\0') {
530                         *port = '\0';
531                         session->port = tmp;
532                 }
533         }
534
535         session->host = g_strdup(host);
536
537         g_free(scheme);
538
539         return 0;
540 }
541
542 static void resolv_result(GResolvResultStatus status,
543                                         char **results, gpointer user_data)
544 {
545         struct web_session *session = user_data;
546
547         if (results == NULL || results[0] == NULL) {
548                 call_result_func(session, 404);
549                 return;
550         }
551
552         debug(session->web, "address %s", results[0]);
553
554         if (inet_aton(results[0], NULL) == 0) {
555                 call_result_func(session, 400);
556                 return;
557         }
558
559         session->address = g_strdup(results[0]);
560
561         if (create_transport(session) < 0) {
562                 call_result_func(session, 409);
563                 return;
564         }
565
566         start_request(session);
567 }
568
569 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
570                                 GWebResultFunc func, gpointer user_data)
571 {
572         struct web_session *session;
573
574         if (web == NULL || url == NULL)
575                 return 0;
576
577         debug(web, "request %s", url);
578
579         session = g_try_new0(struct web_session, 1);
580         if (session == NULL)
581                 return 0;
582
583         if (parse_url(session, url) < 0) {
584                 free_session(session);
585                 return 0;
586         }
587
588         debug(web, "host %s:%u", session->host, session->port);
589         debug(web, "flags %lu", session->flags);
590
591         session->web = web;
592
593         session->result_func = func;
594         session->result_data = user_data;
595
596         session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
597         if (session->receive_buffer == NULL) {
598                 free_session(session);
599                 return 0;
600         }
601
602         session->receive_space = DEFAULT_BUFFER_SIZE;
603         session->current_header = g_string_sized_new(0);
604         session->header_done = FALSE;
605
606         if (inet_aton(session->host, NULL) == 0) {
607                 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
608                                         session->host, resolv_result, session);
609                 if (session->resolv_action == 0) {
610                         free_session(session);
611                         return 0;
612                 }
613         } else {
614                 session->address = g_strdup(session->host);
615
616                 if (create_transport(session) < 0) {
617                         free_session(session);
618                         return 0;
619                 }
620
621                 start_request(session);
622         }
623
624         web->session_list = g_list_append(web->session_list, session);
625
626         return web->next_query_id++;
627 }
628
629 guint16 g_web_result_get_status(GWebResult *result)
630 {
631         if (result == NULL)
632                 return 0;
633
634         return result->status;
635 }
636
637 gboolean g_web_result_get_chunk(GWebResult *result,
638                                 const guint8 **chunk, gsize *length)
639 {
640         if (result == NULL)
641                 return FALSE;
642
643         if (chunk == NULL)
644                 return FALSE;
645
646         *chunk = result->buffer;
647
648         if (length != NULL)
649                 *length = result->length;
650
651         return TRUE;
652 }