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>
45 GIOChannel *transport_channel;
46 guint transport_watch;
57 GWebDebugFunc debug_func;
61 static inline void debug(GWeb *web, const char *format, ...)
66 if (web->debug_func == NULL)
71 if (vsnprintf(str, sizeof(str), format, ap) > 0)
72 web->debug_func(str, web->debug_data);
77 static void free_session(struct web_session *session)
82 if (session->transport_watch > 0)
83 g_source_remove(session->transport_watch);
85 if (session->transport_channel != NULL)
86 g_io_channel_unref(session->transport_channel);
88 g_free(session->host);
89 g_free(session->address);
93 static void flush_sessions(GWeb *web)
97 for (list = g_list_first(web->session_list);
98 list; list = g_list_next(list))
99 free_session(list->data);
101 g_list_free(web->session_list);
102 web->session_list = NULL;
105 GWeb *g_web_new(int index)
112 web = g_try_new0(GWeb, 1);
118 web->next_query_id = 1;
121 web->session_list = NULL;
126 GWeb *g_web_ref(GWeb *web)
131 g_atomic_int_inc(&web->ref_count);
136 void g_web_unref(GWeb *web)
141 if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
149 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
154 web->debug_func = func;
155 web->debug_data = user_data;
158 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
161 struct web_session *session = user_data;
162 unsigned char buf[4096];
165 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
166 session->transport_watch = 0;
170 sk = g_io_channel_unix_get_fd(session->transport_channel);
172 memset(buf, 0, sizeof(buf));
173 len = recv(sk, buf, sizeof(buf) - 1, 0);
180 static int connect_session_transport(struct web_session *session)
182 struct sockaddr_in sin;
185 sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
189 memset(&sin, 0, sizeof(sin));
190 sin.sin_family = AF_INET;
191 sin.sin_port = htons(session->port);
192 sin.sin_addr.s_addr = inet_addr(session->address);
194 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
199 session->transport_channel = g_io_channel_unix_new(sk);
200 if (session->transport_channel == NULL) {
205 g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
207 session->transport_watch = g_io_add_watch(session->transport_channel,
208 G_IO_IN, received_data,
214 static void start_request(struct web_session *session, const char *request)
221 debug(session->web, "request %s from %s", request, session->host);
223 sk = g_io_channel_unix_get_fd(session->transport_channel);
225 buf = g_string_new(NULL);
226 g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", request);
227 g_string_append_printf(buf, "Host: %s\r\n", session->host);
228 g_string_append_printf(buf, "User-Agent: ConnMan/%s\r\n", VERSION);
229 g_string_append(buf, "Accept: */*\r\n");
230 g_string_append(buf, "\r\n");
231 str = g_string_free(buf, FALSE);
233 len = send(sk, str, strlen(str), 0);
240 static char *parse_url(struct web_session *session, const char *url)
242 char *scheme, *host, *port, *path, *request;
244 scheme = g_strdup(url);
248 host = strstr(scheme, "://");
253 if (strcasecmp(scheme, "https") == 0)
255 else if (strcasecmp(scheme, "http") == 0)
266 path = strchr(host, '/');
270 request = g_strdup_printf("/%s", path ? path : "");
272 port = strrchr(host, ':');
275 int tmp = strtol(port + 1, &end, 10);
283 session->host = g_strdup(host);
290 guint g_web_request(GWeb *web, GWebMethod method, const char *url,
291 GWebResultFunc func, gpointer user_data)
293 struct web_session *session;
296 if (web == NULL || url == NULL)
299 debug(web, "request %s", url);
301 session = g_try_new0(struct web_session, 1);
305 request = parse_url(session, url);
306 if (request == NULL) {
311 if (inet_aton(session->host, NULL) == 0) {
316 session->address = g_strdup(session->host);
318 debug(web, "host %s:%u", session->host, session->port);
320 if (connect_session_transport(session) < 0) {
322 free_session(session);
328 web->session_list = g_list_append(web->session_list, session);
330 debug(web, "creating session %s:%u", session->address, session->port);
332 start_request(session, request);
336 return web->next_query_id++;