#include <sys/socket.h>
#include <arpa/inet.h>
+#include <gresolv/gresolv.h>
+
#include "gweb.h"
struct web_session {
GIOChannel *transport_channel;
guint transport_watch;
+
+ guint resolv_action;
+ char *request;
+
+ GWebResultFunc result_func;
+ gpointer result_data;
};
struct _GWeb {
int index;
GList *session_list;
+ GResolv *resolv;
+
GWebDebugFunc debug_func;
gpointer debug_data;
};
static void free_session(struct web_session *session)
{
+ GWeb *web = session->web;
+
if (session == NULL)
return;
+ g_free(session->request);
+
+ if (session->resolv_action > 0)
+ g_resolv_cancel_lookup(web->resolv, session->resolv_action);
+
if (session->transport_watch > 0)
g_source_remove(session->transport_watch);
web->index = index;
web->session_list = NULL;
+ web->resolv = g_resolv_new(index);
+ if (web->resolv == NULL) {
+ g_free(web);
+ return NULL;
+ }
+
return web;
}
flush_sessions(web);
+ g_resolv_unref(web->resolv);
+
g_free(web);
}
web->debug_func = func;
web->debug_data = user_data;
+
+ g_resolv_set_debug(web->resolv, func, user_data);
+}
+
+gboolean g_web_add_nameserver(GWeb *web, const char *address)
+{
+ if (web == NULL)
+ return FALSE;
+
+ g_resolv_add_nameserver(web->resolv, address, 53, 0);
+
+ return TRUE;
}
static gboolean received_data(GIOChannel *channel, GIOCondition cond,
if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
session->transport_watch = 0;
+ if (session->result_func != NULL)
+ session->result_func(400, session->result_data);
return FALSE;
}
memset(buf, 0, sizeof(buf));
len = recv(sk, buf, sizeof(buf) - 1, 0);
+ if (len == 0) {
+ session->transport_watch = 0;
+ if (session->result_func != NULL)
+ session->result_func(200, session->result_data);
+ return FALSE;
+ }
printf("%s", buf);
return TRUE;
return 0;
}
-static void start_request(struct web_session *session, const char *request)
+static void start_request(struct web_session *session)
{
GString *buf;
char *str;
ssize_t len;
int sk;
- debug(session->web, "request %s from %s", request, session->host);
+ debug(session->web, "request %s from %s",
+ session->request, session->host);
sk = g_io_channel_unix_get_fd(session->transport_channel);
buf = g_string_new(NULL);
- g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", request);
+ g_string_append_printf(buf, "GET %s HTTP/1.1\r\n", session->request);
g_string_append_printf(buf, "Host: %s\r\n", session->host);
g_string_append_printf(buf, "User-Agent: ConnMan/%s\r\n", VERSION);
g_string_append(buf, "Accept: */*\r\n");
g_free(str);
}
-static char *parse_url(struct web_session *session, const char *url)
+static int parse_url(struct web_session *session, const char *url)
{
- char *scheme, *host, *port, *path, *request;
+ char *scheme, *host, *port, *path;
scheme = g_strdup(url);
if (scheme == NULL)
- return NULL;
+ return -EINVAL;
host = strstr(scheme, "://");
if (host != NULL) {
session->port = 80;
else {
g_free(scheme);
- return NULL;
+ return -EINVAL;
}
} else {
host = scheme;
if (path != NULL)
*(path++) = '\0';
- request = g_strdup_printf("/%s", path ? path : "");
+ session->request = g_strdup_printf("/%s", path ? path : "");
port = strrchr(host, ':');
if (port != NULL) {
g_free(scheme);
- return request;
+ return 0;
+}
+
+static void resolv_result(GResolvResultStatus status,
+ char **results, gpointer user_data)
+{
+ struct web_session *session = user_data;
+
+ if (results == NULL || results[0] == NULL) {
+ if (session->result_func != NULL)
+ session->result_func(404, session->result_data);
+ return;
+ }
+
+ debug(session->web, "address %s", results[0]);
+
+ if (inet_aton(results[0], NULL) == 0) {
+ if (session->result_func != NULL)
+ session->result_func(400, session->result_data);
+ return;
+ }
+
+ session->address = g_strdup(results[0]);
+
+ if (connect_session_transport(session) < 0) {
+ if (session->result_func != NULL)
+ session->result_func(409, session->result_data);
+ return;
+ }
+
+ debug(session->web, "creating session %s:%u",
+ session->address, session->port);
+
+ start_request(session);
}
guint g_web_request(GWeb *web, GWebMethod method, const char *url,
GWebResultFunc func, gpointer user_data)
{
struct web_session *session;
- char *request;
if (web == NULL || url == NULL)
return 0;
if (session == NULL)
return 0;
- request = parse_url(session, url);
- if (request == NULL) {
- g_free(session);
+ if (parse_url(session, url) < 0) {
+ free_session(session);
return 0;
}
- if (inet_aton(session->host, NULL) == 0) {
- g_free(session);
- return 0;
- }
+ debug(web, "host %s:%u", session->host, session->port);
- session->address = g_strdup(session->host);
+ session->web = web;
- debug(web, "host %s:%u", session->host, session->port);
+ session->result_func = func;
+ session->result_data = user_data;
- if (connect_session_transport(session) < 0) {
- g_free(request);
+ session->resolv_action = g_resolv_lookup_hostname(web->resolv,
+ session->host, resolv_result, session);
+ if (session->resolv_action == 0) {
free_session(session);
- return FALSE;
+ return 0;
}
- session->web = web;
-
web->session_list = g_list_append(web->session_list, session);
- debug(web, "creating session %s:%u", session->address, session->port);
-
- start_request(session, request);
-
- g_free(request);
-
return web->next_query_id++;
}