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 <gresolv/gresolv.h>
47 GIOChannel *transport_channel;
48 guint transport_watch;
53 GWebResultFunc result_func;
67 GWebDebugFunc debug_func;
71 static inline void debug(GWeb *web, const char *format, ...)
76 if (web->debug_func == NULL)
81 if (vsnprintf(str, sizeof(str), format, ap) > 0)
82 web->debug_func(str, web->debug_data);
87 static void free_session(struct web_session *session)
89 GWeb *web = session->web;
94 g_free(session->request);
96 if (session->resolv_action > 0)
97 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
99 if (session->transport_watch > 0)
100 g_source_remove(session->transport_watch);
102 if (session->transport_channel != NULL)
103 g_io_channel_unref(session->transport_channel);
105 g_free(session->host);
106 g_free(session->address);
110 static void flush_sessions(GWeb *web)
114 for (list = g_list_first(web->session_list);
115 list; list = g_list_next(list))
116 free_session(list->data);
118 g_list_free(web->session_list);
119 web->session_list = NULL;
122 GWeb *g_web_new(int index)
129 web = g_try_new0(GWeb, 1);
135 web->next_query_id = 1;
138 web->session_list = NULL;
140 web->resolv = g_resolv_new(index);
141 if (web->resolv == NULL) {
149 GWeb *g_web_ref(GWeb *web)
154 g_atomic_int_inc(&web->ref_count);
159 void g_web_unref(GWeb *web)
164 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
169 g_resolv_unref(web->resolv);
174 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
179 web->debug_func = func;
180 web->debug_data = user_data;
182 g_resolv_set_debug(web->resolv, func, user_data);
185 gboolean g_web_add_nameserver(GWeb *web, const char *address)
190 g_resolv_add_nameserver(web->resolv, address, 53, 0);
195 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
198 struct web_session *session = user_data;
199 unsigned char buf[4096];
202 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
203 session->transport_watch = 0;
204 if (session->result_func != NULL)
205 session->result_func(400, session->result_data);
209 sk = g_io_channel_unix_get_fd(session->transport_channel);
211 memset(buf, 0, sizeof(buf));
212 len = recv(sk, buf, sizeof(buf) - 1, 0);
215 session->transport_watch = 0;
216 if (session->result_func != NULL)
217 session->result_func(200, session->result_data);
225 static int connect_session_transport(struct web_session *session)
227 struct sockaddr_in sin;
230 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
234 memset(&sin, 0, sizeof(sin));
235 sin.sin_family = AF_INET;
236 sin.sin_port = htons(session->port);
237 sin.sin_addr.s_addr = inet_addr(session->address);
239 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
244 session->transport_channel = g_io_channel_unix_new(sk);
245 if (session->transport_channel == NULL) {
250 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
252 session->transport_watch = g_io_add_watch(session->transport_channel,
253 G_IO_IN, received_data,
259 static void start_request(struct web_session *session)
266 debug(session->web, "request %s from %s",
267 session->request, session->host);
269 sk = g_io_channel_unix_get_fd(session->transport_channel);
271 buf = g_string_new(NULL);
272 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
273 g_string_append_printf(buf, "Host: %s\r\n", session->host);
274 g_string_append_printf(buf, "User-Agent: ConnMan/%s\r\n", VERSION);
275 g_string_append(buf, "Accept: */*\r\n");
276 g_string_append(buf, "\r\n");
277 str = g_string_free(buf, FALSE);
279 len = send(sk, str, strlen(str), 0);
286 static int parse_url(struct web_session *session, const char *url)
288 char *scheme, *host, *port, *path;
290 scheme = g_strdup(url);
294 host = strstr(scheme, "://");
299 if (strcasecmp(scheme, "https") == 0)
301 else if (strcasecmp(scheme, "http") == 0)
312 path = strchr(host, '/');
316 session->request = g_strdup_printf("/%s", path ? path : "");
318 port = strrchr(host, ':');
321 int tmp = strtol(port + 1, &end, 10);
329 session->host = g_strdup(host);
336 static void resolv_result(GResolvResultStatus status,
337 char **results, gpointer user_data)
339 struct web_session *session = user_data;
341 if (results == NULL || results[0] == NULL) {
342 if (session->result_func != NULL)
343 session->result_func(404, session->result_data);
347 debug(session->web, "address %s", results[0]);
349 if (inet_aton(results[0], NULL) == 0) {
350 if (session->result_func != NULL)
351 session->result_func(400, session->result_data);
355 session->address = g_strdup(results[0]);
357 if (connect_session_transport(session) < 0) {
358 if (session->result_func != NULL)
359 session->result_func(409, session->result_data);
363 debug(session->web, "creating session %s:%u",
364 session->address, session->port);
366 start_request(session);
369 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
370 GWebResultFunc func, gpointer user_data)
372 struct web_session *session;
374 if (web == NULL || url == NULL)
377 debug(web, "request %s", url);
379 session = g_try_new0(struct web_session, 1);
383 if (parse_url(session, url) < 0) {
384 free_session(session);
388 debug(web, "host %s:%u", session->host, session->port);
392 session->result_func = func;
393 session->result_data = user_data;
395 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
396 session->host, resolv_result, session);
397 if (session->resolv_action == 0) {
398 free_session(session);
402 web->session_list = g_list_append(web->session_list, session);
404 return web->next_query_id++;