#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
+#include <netdb.h>
#include "giognutls.h"
#include "gresolv.h"
char *host;
uint16_t port;
unsigned long flags;
+ struct addrinfo *addr;
char *content_type;
char *proxy;
char *accept_option;
char *user_agent;
+ char *user_agent_profile;
char *http_version;
gboolean close_connection;
g_io_channel_unref(session->transport_channel);
g_free(session->result.last_key);
- g_hash_table_destroy(session->result.headers);
- g_string_free(session->send_buffer, TRUE);
- g_string_free(session->current_header, TRUE);
+ if (session->result.headers != NULL)
+ g_hash_table_destroy(session->result.headers);
+
+ if (session->send_buffer != NULL)
+ g_string_free(session->send_buffer, TRUE);
+
+ if (session->current_header != NULL)
+ g_string_free(session->current_header, TRUE);
+
g_free(session->receive_buffer);
g_free(session->content_type);
g_free(session->host);
g_free(session->address);
+ if (session->addr != NULL)
+ freeaddrinfo(session->addr);
+
g_free(session);
}
g_free(web->accept_option);
g_free(web->user_agent);
+ g_free(web->user_agent_profile);
g_free(web->http_version);
g_free(web);
return FALSE;
g_free(web->proxy);
- web->proxy = g_strdup(proxy);
+
+ if (proxy == NULL) {
+ web->proxy = NULL;
+ debug(web, "clearing proxy");
+ } else {
+ web->proxy = g_strdup(proxy);
+ debug(web, "setting proxy %s", web->proxy);
+ }
return TRUE;
}
return result;
}
+gboolean g_web_set_ua_profile(GWeb *web, const char *profile)
+{
+ if (web == NULL)
+ return FALSE;
+
+ g_free(web->user_agent_profile);
+
+ web->user_agent_profile = g_strdup(profile);
+ debug(web, "setting user agent profile %s", web->user_agent);
+
+ return TRUE;
+}
+
gboolean g_web_set_http_version(GWeb *web, const char *version)
{
if (web == NULL)
debug(web, "clearing HTTP version");
} else {
web->http_version = g_strdup(version);
- debug(web, "setting HTTP version %s", web->http_version);
+ debug(web, "setting HTTP version %s", web->http_version);
}
return TRUE;
g_string_append_printf(buf, "User-Agent: %s\r\n",
session->web->user_agent);
+ if (session->web->user_agent_profile != NULL) {
+ g_string_append_printf(buf, "x-wap-profile: %s\r\n",
+ session->web->user_agent_profile);
+ }
+
if (session->web->accept_option != NULL)
g_string_append_printf(buf, "Accept: %s\r\n",
session->web->accept_option);
static int connect_session_transport(struct web_session *session)
{
GIOFlags flags;
- struct sockaddr_in sin;
int sk;
- sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ sk = socket(session->addr->ai_family, SOCK_STREAM, IPPROTO_TCP);
if (sk < 0)
return -EIO;
g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(session->port);
- sin.sin_addr.s_addr = inet_addr(session->address);
-
- if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+ if (connect(sk, session->addr->ai_addr,
+ session->addr->ai_addrlen) < 0) {
if (errno != EINPROGRESS) {
close(sk);
return -EIO;
return 0;
}
-static int parse_url(struct web_session *session, const char *url)
+static int parse_url(struct web_session *session,
+ const char *url, const char *proxy)
{
char *scheme, *host, *port, *path;
if (path != NULL)
*(path++) = '\0';
- session->request = g_strdup_printf("/%s", path ? path : "");
+ if (proxy == NULL)
+ session->request = g_strdup_printf("/%s", path ? path : "");
+ else
+ session->request = g_strdup(url);
+
+ port = strrchr(host, ':');
+ if (port != NULL) {
+ char *end;
+ int tmp = strtol(port + 1, &end, 10);
+
+ if (*end == '\0') {
+ *port = '\0';
+ session->port = tmp;
+ }
+
+ if (proxy == NULL)
+ session->host = g_strdup(host);
+ else
+ session->host = g_strdup_printf("%s:%u", host, tmp);
+ } else
+ session->host = g_strdup(host);
+
+ g_free(scheme);
+
+ if (proxy == NULL)
+ return 0;
+
+ scheme = g_strdup(proxy);
+ if (scheme == NULL)
+ return -EINVAL;
+
+ host = strstr(proxy, "://");
+ if (host != NULL) {
+ *host = '\0';
+ host += 3;
+
+ if (strcasecmp(scheme, "http") != 0) {
+ g_free(scheme);
+ return -EINVAL;
+ }
+ } else
+ host = scheme;
+
+ path = strchr(host, '/');
+ if (path != NULL)
+ *(path++) = '\0';
port = strrchr(host, ':');
if (port != NULL) {
}
}
- session->host = g_strdup(host);
+ session->address = g_strdup(host);
g_free(scheme);
char **results, gpointer user_data)
{
struct web_session *session = user_data;
+ struct addrinfo hints;
+ char *port;
+ int ret;
if (results == NULL || results[0] == NULL) {
call_result_func(session, 404);
debug(session->web, "address %s", results[0]);
- if (inet_aton(results[0], NULL) == 0) {
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_flags = AI_NUMERICHOST;
+
+ if (session->addr != NULL) {
+ freeaddrinfo(session->addr);
+ session->addr = NULL;
+ }
+
+ port = g_strdup_printf("%u", session->port);
+ ret = getaddrinfo(results[0], port, &hints, &session->addr);
+ g_free(port);
+ if (ret != 0 || session->addr == NULL) {
call_result_func(session, 400);
return;
}
if (session == NULL)
return 0;
- if (parse_url(session, url) < 0) {
+ if (parse_url(session, url, web->proxy) < 0) {
free_session(session);
return 0;
}
- debug(web, "host %s:%u", session->host, session->port);
+ debug(web, "address %s", session->address);
+ debug(web, "port %u", session->port);
+ debug(web, "host %s", session->host);
debug(web, "flags %lu", session->flags);
+ debug(web, "request %s", session->request);
if (type != NULL) {
session->content_type = g_strdup(type);
session->header_done = FALSE;
session->body_done = FALSE;
- if (inet_aton(session->host, NULL) == 0) {
+ if (session->address == NULL && inet_aton(session->host, NULL) == 0) {
session->resolv_action = g_resolv_lookup_hostname(web->resolv,
session->host, resolv_result, session);
if (session->resolv_action == 0) {
return 0;
}
} else {
- session->address = g_strdup(session->host);
+ struct addrinfo hints;
+ char *port;
+ int ret;
+
+ if (session->address == NULL)
+ session->address = g_strdup(session->host);
+
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_flags = AI_NUMERICHOST;
+
+ if (session->addr != NULL) {
+ freeaddrinfo(session->addr);
+ session->addr = NULL;
+ }
+
+ port = g_strdup_printf("%u", session->port);
+ ret = getaddrinfo(session->address, port, &hints,
+ &session->addr);
+ g_free(port);
+ if (ret != 0 || session->addr == NULL) {
+ free_session(session);
+ return 0;
+ }
if (create_transport(session) < 0) {
free_session(session);