Add support for setting accept option for GWeb
[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 SESSION_FLAG_USE_TLS    (1 << 0)
40
41 struct _GWebResult {
42 };
43
44 struct web_session {
45         GWeb *web;
46
47         char *address;
48         char *host;
49         uint16_t port;
50         unsigned long flags;
51
52         GIOChannel *transport_channel;
53         guint transport_watch;
54
55         guint resolv_action;
56         char *request;
57
58         GWebResult *result;
59
60         GWebResultFunc result_func;
61         gpointer result_data;
62 };
63
64 struct _GWeb {
65         gint ref_count;
66
67         guint next_query_id;
68
69         int index;
70         GList *session_list;
71
72         GResolv *resolv;
73         char *accept_option;
74         char *user_agent;
75
76         GWebDebugFunc debug_func;
77         gpointer debug_data;
78 };
79
80 static inline void debug(GWeb *web, const char *format, ...)
81 {
82         char str[256];
83         va_list ap;
84
85         if (web->debug_func == NULL)
86                 return;
87
88         va_start(ap, format);
89
90         if (vsnprintf(str, sizeof(str), format, ap) > 0)
91                 web->debug_func(str, web->debug_data);
92
93         va_end(ap);
94 }
95
96 static void free_session(struct web_session *session)
97 {
98         GWeb *web = session->web;
99
100         if (session == NULL)
101                 return;
102
103         g_free(session->request);
104
105         if (session->resolv_action > 0)
106                 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
107
108         if (session->transport_watch > 0)
109                 g_source_remove(session->transport_watch);
110
111         if (session->transport_channel != NULL)
112                 g_io_channel_unref(session->transport_channel);
113
114         g_free(session->host);
115         g_free(session->address);
116         g_free(session);
117 }
118
119 static void flush_sessions(GWeb *web)
120 {
121         GList *list;
122
123         for (list = g_list_first(web->session_list);
124                                         list; list = g_list_next(list))
125                 free_session(list->data);
126
127         g_list_free(web->session_list);
128         web->session_list = NULL;
129 }
130
131 GWeb *g_web_new(int index)
132 {
133         GWeb *web;
134
135         if (index < 0)
136                 return NULL;
137
138         web = g_try_new0(GWeb, 1);
139         if (web == NULL)
140                 return NULL;
141
142         web->ref_count = 1;
143
144         web->next_query_id = 1;
145
146         web->index = index;
147         web->session_list = NULL;
148
149         web->resolv = g_resolv_new(index);
150         if (web->resolv == NULL) {
151                 g_free(web);
152                 return NULL;
153         }
154
155         web->accept_option = g_strdup("*/*");
156         web->user_agent = g_strdup_printf("GWeb/%s", VERSION);
157
158         return web;
159 }
160
161 GWeb *g_web_ref(GWeb *web)
162 {
163         if (web == NULL)
164                 return NULL;
165
166         g_atomic_int_inc(&web->ref_count);
167
168         return web;
169 }
170
171 void g_web_unref(GWeb *web)
172 {
173         if (web == NULL)
174                 return;
175
176         if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
177                 return;
178
179         flush_sessions(web);
180
181         g_resolv_unref(web->resolv);
182
183         g_free(web->accept_option);
184         g_free(web->user_agent);
185         g_free(web);
186 }
187
188 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
189 {
190         if (web == NULL)
191                 return;
192
193         web->debug_func = func;
194         web->debug_data = user_data;
195
196         g_resolv_set_debug(web->resolv, func, user_data);
197 }
198
199 gboolean g_web_add_nameserver(GWeb *web, const char *address)
200 {
201         if (web == NULL)
202                 return FALSE;
203
204         g_resolv_add_nameserver(web->resolv, address, 53, 0);
205
206         return TRUE;
207 }
208
209 static gboolean set_accept_option(GWeb *web, const char *format, va_list args)
210 {
211         g_free(web->accept_option);
212
213         if (format == NULL) {
214                 web->accept_option = NULL;
215                 debug(web, "clearing accept option");
216         } else {
217                 web->accept_option = g_strdup_vprintf(format, args);
218                 debug(web, "setting accept %s", web->accept_option);
219         }
220
221         return TRUE;
222 }
223
224 gboolean g_web_set_accept(GWeb *web, const char *format, ...)
225 {
226         va_list args;
227         gboolean result;
228
229         if (web == NULL)
230                 return FALSE;
231
232         va_start(args, format);
233         result = set_accept_option(web, format, args);
234         va_end(args);
235
236         return result;
237 }
238
239 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
240 {
241         g_free(web->user_agent);
242
243         if (format == NULL) {
244                 web->user_agent = NULL;
245                 debug(web, "clearing user agent");
246         } else {
247                 web->user_agent = g_strdup_vprintf(format, args);
248                 debug(web, "setting user agent %s", web->user_agent);
249         }
250
251         return TRUE;
252 }
253
254 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
255 {
256         va_list args;
257         gboolean result;
258
259         if (web == NULL)
260                 return FALSE;
261
262         va_start(args, format);
263         result = set_user_agent(web, format, args);
264         va_end(args);
265
266         return result;
267 }
268
269 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
270                                                         gpointer user_data)
271 {
272         struct web_session *session = user_data;
273         gchar buf[4096];
274         gsize bytes_read;
275         GIOStatus status;
276
277         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
278                 session->transport_watch = 0;
279                 if (session->result_func != NULL)
280                         session->result_func(400, NULL, session->result_data);
281                 return FALSE;
282         }
283
284         memset(buf, 0, sizeof(buf));
285         status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
286                                                 &bytes_read, NULL);
287
288         debug(session->web, "status %u bytes read %zu", status, bytes_read);
289
290         if (status != G_IO_STATUS_NORMAL) {
291                 session->transport_watch = 0;
292                 if (session->result_func != NULL)
293                         session->result_func(200, NULL, session->result_data);
294                 return FALSE;
295         }
296
297         printf("%s", buf);
298
299         return TRUE;
300 }
301
302 static int connect_session_transport(struct web_session *session)
303 {
304         struct sockaddr_in sin;
305         int sk;
306
307         sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
308         if (sk < 0)
309                 return -EIO;
310
311         memset(&sin, 0, sizeof(sin));
312         sin.sin_family = AF_INET;
313         sin.sin_port = htons(session->port);
314         sin.sin_addr.s_addr = inet_addr(session->address);
315
316         if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
317                 close(sk);
318                 return -EIO;
319         }
320
321         debug(session->web, "flags %lu", session->flags);
322
323         if (session->flags & SESSION_FLAG_USE_TLS)
324                 session->transport_channel = g_io_channel_gnutls_new(sk);
325         else
326                 session->transport_channel = g_io_channel_unix_new(sk);
327
328         if (session->transport_channel == NULL) {
329                 close(sk);
330                 return -ENOMEM;
331         }
332
333         g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
334         g_io_channel_set_buffered(session->transport_channel, FALSE);
335
336         g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
337
338         session->transport_watch = g_io_add_watch(session->transport_channel,
339                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
340                                                 received_data, session);
341
342         return 0;
343 }
344
345 static void start_request(struct web_session *session)
346 {
347         GString *buf;
348         gchar *str;
349         gsize count, bytes_written;
350         GIOStatus status;
351
352         debug(session->web, "request %s from %s",
353                                         session->request, session->host);
354
355         buf = g_string_new(NULL);
356         g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
357         g_string_append_printf(buf, "Host: %s\r\n", session->host);
358         if (session->web->user_agent != NULL)
359                 g_string_append_printf(buf, "User-Agent: %s\r\n",
360                                                 session->web->user_agent);
361         if (session->web->accept_option != NULL)
362                 g_string_append_printf(buf, "Accept: %s\r\n",
363                                                 session->web->accept_option);
364         g_string_append(buf, "\r\n");
365         str = g_string_free(buf, FALSE);
366
367         count = strlen(str);
368
369         debug(session->web, "bytes to write %zu", count);
370
371         status = g_io_channel_write_chars(session->transport_channel,
372                                         str, count, &bytes_written, NULL);
373
374         debug(session->web, "status %u bytes written %zu",
375                                                 status, bytes_written);
376
377         printf("%s", str);
378
379         g_free(str);
380 }
381
382 static int parse_url(struct web_session *session, const char *url)
383 {
384         char *scheme, *host, *port, *path;
385
386         scheme = g_strdup(url);
387         if (scheme == NULL)
388                 return -EINVAL;
389
390         host = strstr(scheme, "://");
391         if (host != NULL) {
392                 *host = '\0';
393                 host += 3;
394
395                 if (strcasecmp(scheme, "https") == 0) {
396                         session->port = 443;
397                         session->flags |= SESSION_FLAG_USE_TLS;
398                 } else if (strcasecmp(scheme, "http") == 0) {
399                         session->port = 80;
400                 } else {
401                         g_free(scheme);
402                         return -EINVAL;
403                 }
404         } else {
405                 host = scheme;
406                 session->port = 80;
407         }
408
409         path = strchr(host, '/');
410         if (path != NULL)
411                 *(path++) = '\0';
412
413         session->request = g_strdup_printf("/%s", path ? path : "");
414
415         port = strrchr(host, ':');
416         if (port != NULL) {
417                 char *end;
418                 int tmp = strtol(port + 1, &end, 10);
419
420                 if (*end == '\0') {
421                         *port = '\0';
422                         session->port = tmp;
423                 }
424         }
425
426         session->host = g_strdup(host);
427
428         g_free(scheme);
429
430         return 0;
431 }
432
433 static void resolv_result(GResolvResultStatus status,
434                                         char **results, gpointer user_data)
435 {
436         struct web_session *session = user_data;
437
438         if (results == NULL || results[0] == NULL) {
439                 if (session->result_func != NULL)
440                         session->result_func(404, NULL, session->result_data);
441                 return;
442         }
443
444         debug(session->web, "address %s", results[0]);
445
446         if (inet_aton(results[0], NULL) == 0) {
447                 if (session->result_func != NULL)
448                         session->result_func(400, NULL, session->result_data);
449                 return;
450         }
451
452         session->address = g_strdup(results[0]);
453
454         if (connect_session_transport(session) < 0) {
455                 if (session->result_func != NULL)
456                         session->result_func(409, NULL, session->result_data);
457                 return;
458         }
459
460         debug(session->web, "creating session %s:%u",
461                                         session->address, session->port);
462
463         start_request(session);
464 }
465
466 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
467                                 GWebResultFunc func, gpointer user_data)
468 {
469         struct web_session *session;
470
471         if (web == NULL || url == NULL)
472                 return 0;
473
474         debug(web, "request %s", url);
475
476         session = g_try_new0(struct web_session, 1);
477         if (session == NULL)
478                 return 0;
479
480         if (parse_url(session, url) < 0) {
481                 free_session(session);
482                 return 0;
483         }
484
485         debug(web, "host %s:%u", session->host, session->port);
486         debug(web, "flags %lu", session->flags);
487
488         session->web = web;
489
490         session->result_func = func;
491         session->result_data = user_data;
492
493         session->resolv_action = g_resolv_lookup_hostname(web->resolv,
494                                         session->host, resolv_result, session);
495         if (session->resolv_action == 0) {
496                 free_session(session);
497                 return 0;
498         }
499
500         web->session_list = g_list_append(web->session_list, session);
501
502         return web->next_query_id++;
503 }