3 * Web service library with GLib integration
5 * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #include "giognutls.h"
39 #define SESSION_FLAG_USE_TLS (1 << 0)
52 GIOChannel *transport_channel;
53 guint transport_watch;
60 GWebResultFunc result_func;
75 GWebDebugFunc debug_func;
79 static inline void debug(GWeb *web, const char *format, ...)
84 if (web->debug_func == NULL)
89 if (vsnprintf(str, sizeof(str), format, ap) > 0)
90 web->debug_func(str, web->debug_data);
95 static void free_session(struct web_session *session)
97 GWeb *web = session->web;
102 g_free(session->request);
104 if (session->resolv_action > 0)
105 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
107 if (session->transport_watch > 0)
108 g_source_remove(session->transport_watch);
110 if (session->transport_channel != NULL)
111 g_io_channel_unref(session->transport_channel);
113 g_free(session->host);
114 g_free(session->address);
118 static void flush_sessions(GWeb *web)
122 for (list = g_list_first(web->session_list);
123 list; list = g_list_next(list))
124 free_session(list->data);
126 g_list_free(web->session_list);
127 web->session_list = NULL;
130 GWeb *g_web_new(int index)
137 web = g_try_new0(GWeb, 1);
143 web->next_query_id = 1;
146 web->session_list = NULL;
148 web->resolv = g_resolv_new(index);
149 if (web->resolv == NULL) {
157 GWeb *g_web_ref(GWeb *web)
162 g_atomic_int_inc(&web->ref_count);
167 void g_web_unref(GWeb *web)
172 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
177 g_resolv_unref(web->resolv);
179 g_free(web->user_agent);
183 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
188 web->debug_func = func;
189 web->debug_data = user_data;
191 g_resolv_set_debug(web->resolv, func, user_data);
194 gboolean g_web_add_nameserver(GWeb *web, const char *address)
199 g_resolv_add_nameserver(web->resolv, address, 53, 0);
204 static gboolean set_user_agent(GWeb *web, const char *format, va_list args)
206 g_free(web->user_agent);
207 web->user_agent = g_strdup_vprintf(format, args);
209 debug(web, "user agent %s", web->user_agent);
214 gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
222 va_start(args, format);
223 result = set_user_agent(web, format, args);
229 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
232 struct web_session *session = user_data;
237 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
238 session->transport_watch = 0;
239 if (session->result_func != NULL)
240 session->result_func(400, NULL, session->result_data);
244 memset(buf, 0, sizeof(buf));
245 status = g_io_channel_read_chars(channel, buf, sizeof(buf) - 1,
248 debug(session->web, "status %u bytes read %zu", status, bytes_read);
250 if (status != G_IO_STATUS_NORMAL) {
251 session->transport_watch = 0;
252 if (session->result_func != NULL)
253 session->result_func(200, NULL, session->result_data);
262 static int connect_session_transport(struct web_session *session)
264 struct sockaddr_in sin;
267 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
271 memset(&sin, 0, sizeof(sin));
272 sin.sin_family = AF_INET;
273 sin.sin_port = htons(session->port);
274 sin.sin_addr.s_addr = inet_addr(session->address);
276 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
281 debug(session->web, "flags %lu", session->flags);
283 if (session->flags & SESSION_FLAG_USE_TLS)
284 session->transport_channel = g_io_channel_gnutls_new(sk);
286 session->transport_channel = g_io_channel_unix_new(sk);
288 if (session->transport_channel == NULL) {
293 g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
294 g_io_channel_set_buffered(session->transport_channel, FALSE);
296 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
298 session->transport_watch = g_io_add_watch(session->transport_channel,
299 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
300 received_data, session);
305 static void start_request(struct web_session *session)
309 gsize count, bytes_written;
312 debug(session->web, "request %s from %s",
313 session->request, session->host);
315 buf = g_string_new(NULL);
316 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
317 g_string_append_printf(buf, "Host: %s\r\n", session->host);
318 if (session->web->user_agent == NULL)
319 g_string_append_printf(buf, "User-Agent: GWeb/%s\r\n", VERSION);
321 g_string_append_printf(buf, "User-Agent: %s\r\n",
322 session->web->user_agent);
323 g_string_append(buf, "Accept: */*\r\n");
324 g_string_append(buf, "\r\n");
325 str = g_string_free(buf, FALSE);
329 debug(session->web, "bytes to write %zu", count);
331 status = g_io_channel_write_chars(session->transport_channel,
332 str, count, &bytes_written, NULL);
334 debug(session->web, "status %u bytes written %zu",
335 status, bytes_written);
342 static int parse_url(struct web_session *session, const char *url)
344 char *scheme, *host, *port, *path;
346 scheme = g_strdup(url);
350 host = strstr(scheme, "://");
355 if (strcasecmp(scheme, "https") == 0) {
357 session->flags |= SESSION_FLAG_USE_TLS;
358 } else if (strcasecmp(scheme, "http") == 0) {
369 path = strchr(host, '/');
373 session->request = g_strdup_printf("/%s", path ? path : "");
375 port = strrchr(host, ':');
378 int tmp = strtol(port + 1, &end, 10);
386 session->host = g_strdup(host);
393 static void resolv_result(GResolvResultStatus status,
394 char **results, gpointer user_data)
396 struct web_session *session = user_data;
398 if (results == NULL || results[0] == NULL) {
399 if (session->result_func != NULL)
400 session->result_func(404, NULL, session->result_data);
404 debug(session->web, "address %s", results[0]);
406 if (inet_aton(results[0], NULL) == 0) {
407 if (session->result_func != NULL)
408 session->result_func(400, NULL, session->result_data);
412 session->address = g_strdup(results[0]);
414 if (connect_session_transport(session) < 0) {
415 if (session->result_func != NULL)
416 session->result_func(409, NULL, session->result_data);
420 debug(session->web, "creating session %s:%u",
421 session->address, session->port);
423 start_request(session);
426 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
427 GWebResultFunc func, gpointer user_data)
429 struct web_session *session;
431 if (web == NULL || url == NULL)
434 debug(web, "request %s", url);
436 session = g_try_new0(struct web_session, 1);
440 if (parse_url(session, url) < 0) {
441 free_session(session);
445 debug(web, "host %s:%u", session->host, session->port);
446 debug(web, "flags %lu", session->flags);
450 session->result_func = func;
451 session->result_data = user_data;
453 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
454 session->host, resolv_result, session);
455 if (session->resolv_action == 0) {
456 free_session(session);
460 web->session_list = g_list_append(web->session_list, session);
462 return web->next_query_id++;