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>
49 GIOChannel *transport_channel;
50 guint transport_watch;
57 GWebResultFunc result_func;
71 GWebDebugFunc debug_func;
75 static inline void debug(GWeb *web, const char *format, ...)
80 if (web->debug_func == NULL)
85 if (vsnprintf(str, sizeof(str), format, ap) > 0)
86 web->debug_func(str, web->debug_data);
91 static void free_session(struct web_session *session)
93 GWeb *web = session->web;
98 g_free(session->request);
100 if (session->resolv_action > 0)
101 g_resolv_cancel_lookup(web->resolv, session->resolv_action);
103 if (session->transport_watch > 0)
104 g_source_remove(session->transport_watch);
106 if (session->transport_channel != NULL)
107 g_io_channel_unref(session->transport_channel);
109 g_free(session->host);
110 g_free(session->address);
114 static void flush_sessions(GWeb *web)
118 for (list = g_list_first(web->session_list);
119 list; list = g_list_next(list))
120 free_session(list->data);
122 g_list_free(web->session_list);
123 web->session_list = NULL;
126 GWeb *g_web_new(int index)
133 web = g_try_new0(GWeb, 1);
139 web->next_query_id = 1;
142 web->session_list = NULL;
144 web->resolv = g_resolv_new(index);
145 if (web->resolv == NULL) {
153 GWeb *g_web_ref(GWeb *web)
158 g_atomic_int_inc(&web->ref_count);
163 void g_web_unref(GWeb *web)
168 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
173 g_resolv_unref(web->resolv);
178 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
183 web->debug_func = func;
184 web->debug_data = user_data;
186 g_resolv_set_debug(web->resolv, func, user_data);
189 gboolean g_web_add_nameserver(GWeb *web, const char *address)
194 g_resolv_add_nameserver(web->resolv, address, 53, 0);
199 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
202 struct web_session *session = user_data;
203 unsigned char buf[4096];
206 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
207 session->transport_watch = 0;
208 if (session->result_func != NULL)
209 session->result_func(400, NULL, session->result_data);
213 sk = g_io_channel_unix_get_fd(session->transport_channel);
215 memset(buf, 0, sizeof(buf));
216 len = recv(sk, buf, sizeof(buf) - 1, 0);
219 session->transport_watch = 0;
220 if (session->result_func != NULL)
221 session->result_func(200, NULL, session->result_data);
229 static int connect_session_transport(struct web_session *session)
231 struct sockaddr_in sin;
234 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
238 memset(&sin, 0, sizeof(sin));
239 sin.sin_family = AF_INET;
240 sin.sin_port = htons(session->port);
241 sin.sin_addr.s_addr = inet_addr(session->address);
243 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
248 session->transport_channel = g_io_channel_unix_new(sk);
249 if (session->transport_channel == NULL) {
254 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
256 session->transport_watch = g_io_add_watch(session->transport_channel,
257 G_IO_IN, received_data,
263 static void start_request(struct web_session *session)
270 debug(session->web, "request %s from %s",
271 session->request, session->host);
273 sk = g_io_channel_unix_get_fd(session->transport_channel);
275 buf = g_string_new(NULL);
276 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
277 g_string_append_printf(buf, "Host: %s\r\n", session->host);
278 g_string_append_printf(buf, "User-Agent: ConnMan/%s\r\n", VERSION);
279 g_string_append(buf, "Accept: */*\r\n");
280 g_string_append(buf, "\r\n");
281 str = g_string_free(buf, FALSE);
283 len = send(sk, str, strlen(str), 0);
290 static int parse_url(struct web_session *session, const char *url)
292 char *scheme, *host, *port, *path;
294 scheme = g_strdup(url);
298 host = strstr(scheme, "://");
303 if (strcasecmp(scheme, "https") == 0)
305 else if (strcasecmp(scheme, "http") == 0)
316 path = strchr(host, '/');
320 session->request = g_strdup_printf("/%s", path ? path : "");
322 port = strrchr(host, ':');
325 int tmp = strtol(port + 1, &end, 10);
333 session->host = g_strdup(host);
340 static void resolv_result(GResolvResultStatus status,
341 char **results, gpointer user_data)
343 struct web_session *session = user_data;
345 if (results == NULL || results[0] == NULL) {
346 if (session->result_func != NULL)
347 session->result_func(404, NULL, session->result_data);
351 debug(session->web, "address %s", results[0]);
353 if (inet_aton(results[0], NULL) == 0) {
354 if (session->result_func != NULL)
355 session->result_func(400, NULL, session->result_data);
359 session->address = g_strdup(results[0]);
361 if (connect_session_transport(session) < 0) {
362 if (session->result_func != NULL)
363 session->result_func(409, NULL, session->result_data);
367 debug(session->web, "creating session %s:%u",
368 session->address, session->port);
370 start_request(session);
373 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
374 GWebResultFunc func, gpointer user_data)
376 struct web_session *session;
378 if (web == NULL || url == NULL)
381 debug(web, "request %s", url);
383 session = g_try_new0(struct web_session, 1);
387 if (parse_url(session, url) < 0) {
388 free_session(session);
392 debug(web, "host %s:%u", session->host, session->port);
396 session->result_func = func;
397 session->result_data = user_data;
399 session->resolv_action = g_resolv_lookup_hostname(web->resolv,
400 session->host, resolv_result, session);
401 if (session->resolv_action == 0) {
402 free_session(session);
406 web->session_list = g_list_append(web->session_list, session);
408 return web->next_query_id++;