gweb: Make sure to destroy the lookup before calling any result function
[platform/upstream/connman.git] / gweb / gweb.c
index 04bc9bd..d35179a 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Web service library with GLib integration
  *
- *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2009-2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 
 #include <stdio.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <sys/sendfile.h>
+#include <sys/stat.h>
 #include <arpa/inet.h>
+#include <netdb.h>
+#include <net/if.h>
+#include <netinet/tcp.h>
+#include <ifaddrs.h>
 
 #include "giognutls.h"
 #include "gresolv.h"
@@ -52,6 +59,8 @@ struct _GWebResult {
        const guint8 *buffer;
        gsize length;
        gboolean use_chunk;
+       gchar *last_key;
+       GHashTable *headers;
 };
 
 struct web_session {
@@ -61,6 +70,7 @@ struct web_session {
        char *host;
        uint16_t port;
        unsigned long flags;
+       struct addrinfo *addr;
 
        char *content_type;
 
@@ -88,15 +98,21 @@ struct web_session {
        GWebResult result;
 
        GWebResultFunc result_func;
+       GWebRouteFunc route_func;
        GWebInputFunc input_func;
+       int fd;
+       gsize length;
+       gsize offset;
        gpointer user_data;
 };
 
 struct _GWeb {
-       gint ref_count;
+       int ref_count;
 
        guint next_query_id;
 
+       int family;
+
        int index;
        GList *session_list;
 
@@ -104,37 +120,48 @@ struct _GWeb {
        char *proxy;
        char *accept_option;
        char *user_agent;
+       char *user_agent_profile;
+       char *http_version;
        gboolean close_connection;
 
        GWebDebugFunc debug_func;
        gpointer debug_data;
 };
 
-static inline void debug(GWeb *web, const char *format, ...)
+#define debug(web, format, arg...)                             \
+       _debug(web, __FILE__, __func__, format, ## arg)
+
+static void _debug(GWeb *web, const char *file, const char *caller,
+                                               const char *format, ...)
 {
        char str[256];
        va_list ap;
+       int len;
 
        if (web->debug_func == NULL)
                return;
 
        va_start(ap, format);
 
-       if (vsnprintf(str, sizeof(str), format, ap) > 0)
-               web->debug_func(str, web->debug_data);
+       if ((len = snprintf(str, sizeof(str), "%s:%s() web %p ",
+                                               file, caller, web)) > 0) {
+               if (vsnprintf(str + len, sizeof(str) - len, format, ap) > 0)
+                       web->debug_func(str, web->debug_data);
+       }
 
        va_end(ap);
 }
 
 static void free_session(struct web_session *session)
 {
-       GWeb *web = session->web;
+       GWeb *web;
 
        if (session == NULL)
                return;
 
        g_free(session->request);
 
+       web = session->web;
        if (session->resolv_action > 0)
                g_resolv_cancel_lookup(web->resolv, session->resolv_action);
 
@@ -147,14 +174,26 @@ static void free_session(struct web_session *session)
        if (session->transport_channel != NULL)
                g_io_channel_unref(session->transport_channel);
 
-       g_string_free(session->send_buffer, TRUE);
-       g_string_free(session->current_header, TRUE);
+       g_free(session->result.last_key);
+
+       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);
 }
 
@@ -185,6 +224,8 @@ GWeb *g_web_new(int index)
 
        web->next_query_id = 1;
 
+       web->family = AF_UNSPEC;
+
        web->index = index;
        web->session_list = NULL;
 
@@ -206,7 +247,7 @@ GWeb *g_web_ref(GWeb *web)
        if (web == NULL)
                return NULL;
 
-       g_atomic_int_inc(&web->ref_count);
+       __sync_fetch_and_add(&web->ref_count, 1);
 
        return web;
 }
@@ -216,7 +257,7 @@ void g_web_unref(GWeb *web)
        if (web == NULL)
                return;
 
-       if (g_atomic_int_dec_and_test(&web->ref_count) == FALSE)
+       if (__sync_fetch_and_sub(&web->ref_count, 1) != 1)
                return;
 
        flush_sessions(web);
@@ -227,10 +268,17 @@ void g_web_unref(GWeb *web)
 
        g_free(web->accept_option);
        g_free(web->user_agent);
+       g_free(web->user_agent_profile);
+       g_free(web->http_version);
 
        g_free(web);
 }
 
+gboolean g_web_supports_tls(void)
+{
+       return g_io_channel_supports_tls();
+}
+
 void g_web_set_debug(GWeb *web, GWebDebugFunc func, gpointer user_data)
 {
        if (web == NULL)
@@ -248,7 +296,29 @@ gboolean g_web_set_proxy(GWeb *web, const char *proxy)
                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;
+}
+
+gboolean g_web_set_address_family(GWeb *web, int family)
+{
+       if (web == NULL)
+               return FALSE;
+
+       if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
+               return FALSE;
+
+       web->family = family;
+
+       g_resolv_set_address_family(web->resolv, family);
 
        return TRUE;
 }
@@ -323,6 +393,37 @@ gboolean g_web_set_user_agent(GWeb *web, const char *format, ...)
        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)
+               return FALSE;
+
+       g_free(web->http_version);
+
+       if (version == NULL) {
+               web->http_version = NULL;
+               debug(web, "clearing HTTP version");
+       } else {
+               web->http_version = g_strdup(version);
+               debug(web, "setting HTTP version %s", web->http_version);
+       }
+
+       return TRUE;
+}
+
 void g_web_set_close_connection(GWeb *web, gboolean enabled)
 {
        if (web == NULL)
@@ -341,6 +442,7 @@ gboolean g_web_get_close_connection(GWeb *web)
 
 static inline void call_result_func(struct web_session *session, guint16 status)
 {
+
        if (session->result_func == NULL)
                return;
 
@@ -348,30 +450,42 @@ static inline void call_result_func(struct web_session *session, guint16 status)
                session->result.status = status;
 
        session->result_func(&session->result, session->user_data);
+
+}
+
+static inline void call_route_func(struct web_session *session)
+{
+       if (session->route_func != NULL)
+               session->route_func(session->address, session->addr->ai_family,
+                               session->web->index, session->user_data);
 }
 
 static gboolean process_send_buffer(struct web_session *session)
 {
-       GString *buf = session->send_buffer;
+       GString *buf;
        gsize count, bytes_written;
        GIOStatus status;
 
+       if (session == NULL)
+               return FALSE;
+
+       buf = session->send_buffer;
        count = buf->len;
 
        if (count == 0) {
-               if (session->more_data == FALSE)
+               if (session->request_started == TRUE &&
+                                       session->more_data == FALSE &&
+                                       session->fd == -1)
                        session->body_done = TRUE;
 
                return FALSE;
        }
 
-       debug(session->web, "bytes to write %zu", count);
-
        status = g_io_channel_write_chars(session->transport_channel,
                                        buf->str, count, &bytes_written, NULL);
 
-       debug(session->web, "status %u bytes written %zu",
-                                       status, bytes_written);
+       debug(session->web, "status %u bytes to write %zu bytes written %zu",
+                                       status, count, bytes_written);
 
        if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN)
                return FALSE;
@@ -381,6 +495,43 @@ static gboolean process_send_buffer(struct web_session *session)
        return TRUE;
 }
 
+static gboolean process_send_file(struct web_session *session)
+{
+       int sk;
+       off_t offset;
+       ssize_t bytes_sent;
+
+       if (session->fd == -1)
+               return FALSE;
+
+       if (session->request_started == FALSE || session->more_data == TRUE)
+               return FALSE;
+
+       sk = g_io_channel_unix_get_fd(session->transport_channel);
+       if (sk < 0)
+               return FALSE;
+
+       offset = session->offset;
+
+       bytes_sent = sendfile(sk, session->fd, &offset, session->length);
+
+       debug(session->web, "errno: %d, bytes to send %zu / bytes sent %zu",
+                       errno, session->length, bytes_sent);
+
+       if (bytes_sent < 0 && errno != EAGAIN)
+               return FALSE;
+
+       session->offset = offset;
+       session->length -= bytes_sent;
+
+       if (session->length == 0) {
+               session->body_done = TRUE;
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
 static void process_next_chunk(struct web_session *session)
 {
        GString *buf = session->send_buffer;
@@ -408,6 +559,7 @@ static void process_next_chunk(struct web_session *session)
 static void start_request(struct web_session *session)
 {
        GString *buf = session->send_buffer;
+       const char *version;
        const guint8 *body;
        gsize length;
 
@@ -416,25 +568,39 @@ static void start_request(struct web_session *session)
 
        g_string_truncate(buf, 0);
 
+       if (session->web->http_version == NULL)
+               version = "1.1";
+       else
+               version = session->web->http_version;
+
        if (session->content_type == NULL)
-               g_string_append_printf(buf, "GET %s HTTP/1.1\r\n",
-                                                       session->request);
+               g_string_append_printf(buf, "GET %s HTTP/%s\r\n",
+                                               session->request, version);
        else
-               g_string_append_printf(buf, "POST %s HTTP/1.1\r\n",
-                                                       session->request);
+               g_string_append_printf(buf, "POST %s HTTP/%s\r\n",
+                                               session->request, version);
+
        g_string_append_printf(buf, "Host: %s\r\n", session->host);
+
        if (session->web->user_agent != NULL)
                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);
+
        if (session->content_type != NULL) {
                g_string_append_printf(buf, "Content-Type: %s\r\n",
                                                        session->content_type);
                if (session->input_func == NULL) {
                        session->more_data = FALSE;
-                       length = 0;
+                       length = session->length;
                } else
                        session->more_data = session->input_func(&body, &length,
                                                        session->user_data);
@@ -444,8 +610,10 @@ static void start_request(struct web_session *session)
                else
                        g_string_append(buf, "Transfer-Encoding: chunked\r\n");
        }
+
        if (session->web->close_connection == TRUE)
                g_string_append(buf, "Connection: close\r\n");
+
        g_string_append(buf, "\r\n");
 
        if (session->content_type != NULL && length > 0) {
@@ -453,7 +621,7 @@ static void start_request(struct web_session *session)
                        g_string_append_printf(buf, "%zx\r\n", length);
                        g_string_append_len(buf, (char *) body, length);
                        g_string_append(buf, "\r\n");
-               } else
+               } else if (session->fd == -1)
                        g_string_append_len(buf, (char *) body, length);
        }
 }
@@ -471,6 +639,9 @@ static gboolean send_data(GIOChannel *channel, GIOCondition cond,
        if (process_send_buffer(session) == TRUE)
                return TRUE;
 
+       if (process_send_file(session) == TRUE)
+               return TRUE;
+
        if (session->request_started == FALSE) {
                session->request_started = TRUE;
                start_request(session);
@@ -590,9 +761,11 @@ static int handle_body(struct web_session *session,
        debug(session->web, "[body] length %zu", len);
 
        if (session->result.use_chunk == FALSE) {
-               session->result.buffer = buf;
-               session->result.length = len;
-               call_result_func(session, 0);
+               if (len > 0) {
+                       session->result.buffer = buf;
+                       session->result.length = len;
+                       call_result_func(session, 0);
+               }
                return 0;
        }
 
@@ -608,6 +781,81 @@ static int handle_body(struct web_session *session,
        return err;
 }
 
+static void handle_multi_line(struct web_session *session)
+{
+       gsize count;
+       char *str;
+       gchar *value;
+
+       str = session->current_header->str;
+
+       if (str[0] != ' ' && str[0] != '\t')
+               return;
+
+       while (str[0] == ' ' || str[0] == '\t')
+               str++;
+
+       count = str - session->current_header->str;
+       if (count > 0) {
+               g_string_erase(session->current_header, 0, count);
+               g_string_insert_c(session->current_header, 0, ' ');
+       }
+
+       value = g_hash_table_lookup(session->result.headers,
+                                       session->result.last_key);
+       if (value != NULL) {
+               g_string_insert(session->current_header, 0, value);
+
+               str = session->current_header->str;
+
+               g_hash_table_replace(session->result.headers,
+                                       g_strdup(session->result.last_key),
+                                       g_strdup(str));
+       }
+}
+
+static void add_header_field(struct web_session *session)
+{
+       gsize count;
+       guint8 *pos;
+       char *str;
+       gchar *value;
+       gchar *key;
+
+       str = session->current_header->str;
+
+       pos = memchr(str, ':', session->current_header->len);
+       if (pos != NULL) {
+               *pos = '\0';
+               pos++;
+
+               key = g_strdup(str);
+
+               /* remove preceding white spaces */
+               while (*pos == ' ')
+                       pos++;
+
+               count = (char *) pos - str;
+
+               g_string_erase(session->current_header, 0, count);
+
+               value = g_hash_table_lookup(session->result.headers, key);
+               if (value != NULL) {
+                       g_string_insert_c(session->current_header, 0, ' ');
+                       g_string_insert_c(session->current_header, 0, ';');
+
+                       g_string_insert(session->current_header, 0, value);
+               }
+
+               str = session->current_header->str;
+               g_hash_table_replace(session->result.headers, key,
+                                                       g_strdup(str));
+
+               g_free(session->result.last_key);
+               session->result.last_key = g_strdup(key);
+       }
+}
+
 static gboolean received_data(GIOChannel *channel, GIOCondition cond,
                                                        gpointer user_data)
 {
@@ -678,7 +926,23 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
                        ptr = NULL;
 
                if (session->current_header->len == 0) {
+                       char *val;
+
                        session->header_done = TRUE;
+
+                       val = g_hash_table_lookup(session->result.headers,
+                                                       "Transfer-Encoding");
+                       if (val != NULL) {
+                               val = g_strrstr(val, "chunked");
+                               if (val != NULL) {
+                                       session->result.use_chunk = TRUE;
+
+                                       session->chunck_state = CHUNK_SIZE;
+                                       session->chunk_left = 0;
+                                       session->total_len = 0;
+                               }
+                       }
+
                        if (handle_body(session, ptr, bytes_read) < 0) {
                                session->transport_watch = 0;
                                return FALSE;
@@ -693,63 +957,119 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
 
                        if (sscanf(str, "HTTP/%*s %u %*s", &code) == 1)
                                session->result.status = code;
-               } else if (session->result.use_chunk == FALSE &&
-                               g_ascii_strncasecmp("Transfer-Encoding:",
-                                                               str, 18) == 0) {
-                       char *val;
-
-                       val = g_strrstr(str + 18, "chunked");
-                       if (val != NULL) {
-                               session->result.use_chunk = TRUE;
-
-                               session->chunck_state = CHUNK_SIZE;
-                               session->chunk_left = 0;
-                               session->chunk_left = 0;
-                               session->total_len = 0;
-                       }
                }
 
                debug(session->web, "[header] %s", str);
 
+               /* handle multi-line header */
+               if (str[0] == ' ' || str[0] == '\t')
+                       handle_multi_line(session);
+               else
+                       add_header_field(session);
+
                g_string_truncate(session->current_header, 0);
        }
 
        return TRUE;
 }
 
+static int bind_to_address(int sk, const char *interface, int family)
+{
+       struct ifaddrs *ifaddr_list, *ifaddr;
+       int size, err = -1;
+
+       if (getifaddrs(&ifaddr_list) < 0)
+               return err;
+
+       for (ifaddr = ifaddr_list; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
+               if (g_strcmp0(ifaddr->ifa_name, interface) != 0)
+                       continue;
+
+               if (ifaddr->ifa_addr == NULL ||
+                               ifaddr->ifa_addr->sa_family != family)
+                       continue;
+
+               switch (family) {
+               case AF_INET:
+                       size = sizeof(struct sockaddr_in);
+                       break;
+               case AF_INET6:
+                       size = sizeof(struct sockaddr_in6);
+                       break;
+               default:
+                       continue;
+               }
+
+               err = bind(sk, (struct sockaddr *) ifaddr->ifa_addr, size);
+               break;
+       }
+
+       freeifaddrs(ifaddr_list);
+       return err;
+}
+
+static inline int bind_socket(int sk, int index, int family)
+{
+       char interface[IF_NAMESIZE];
+       int err;
+
+       if (if_indextoname(index, interface) == NULL)
+               return -1;
+
+       err = setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
+                                       interface, IF_NAMESIZE);
+       if (err < 0)
+               err = bind_to_address(sk, interface, family);
+
+       return err;
+}
+
 static int connect_session_transport(struct web_session *session)
 {
-       struct sockaddr_in sin;
+       GIOFlags flags;
        int sk;
 
-       sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+       sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
+                       IPPROTO_TCP);
        if (sk < 0)
                return -EIO;
 
-       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 (session->web->index > 0) {
+               if (bind_socket(sk, session->web->index,
+                                       session->addr->ai_family) < 0) {
+                       debug(session->web, "bind() %s", strerror(errno));
+                       close(sk);
+                       return -EIO;
+               }
+       }
 
-       if (session->flags & SESSION_FLAG_USE_TLS)
+       if (session->flags & SESSION_FLAG_USE_TLS) {
+               debug(session->web, "using TLS encryption");
                session->transport_channel = g_io_channel_gnutls_new(sk);
-       else
+       } else {
+               debug(session->web, "no encryption");
                session->transport_channel = g_io_channel_unix_new(sk);
+       }
 
        if (session->transport_channel == NULL) {
+               debug(session->web, "channel missing");
                close(sk);
                return -ENOMEM;
        }
 
+       flags = g_io_channel_get_flags(session->transport_channel);
        g_io_channel_set_flags(session->transport_channel,
-                                       G_IO_FLAG_NONBLOCK, NULL);
+                                       flags | G_IO_FLAG_NONBLOCK, NULL);
+
        g_io_channel_set_encoding(session->transport_channel, NULL, NULL);
        g_io_channel_set_buffered(session->transport_channel, FALSE);
 
        g_io_channel_set_close_on_unref(session->transport_channel, TRUE);
 
-       if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+       if (connect(sk, session->addr->ai_addr,
+                       session->addr->ai_addrlen) < 0) {
                if (errno != EINPROGRESS) {
+                       debug(session->web, "connect() %s", strerror(errno));
                        close(sk);
                        return -EIO;
                }
@@ -780,7 +1100,8 @@ static int create_transport(struct web_session *session)
        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;
 
@@ -811,7 +1132,52 @@ static int parse_url(struct web_session *session, const char *url)
        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) {
@@ -824,7 +1190,7 @@ static int parse_url(struct web_session *session, const char *url)
                }
        }
 
-       session->host = g_strdup(host);
+       session->address = g_strdup(host);
 
        g_free(scheme);
 
@@ -835,6 +1201,9 @@ static void resolv_result(GResolvResultStatus status,
                                        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);
@@ -843,12 +1212,25 @@ static void resolv_result(GResolvResultStatus status,
 
        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;
+       hints.ai_family = session->web->family;
+
+       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;
        }
 
        session->address = g_strdup(results[0]);
+       call_route_func(session);
 
        if (create_transport(session) < 0) {
                call_result_func(session, 409);
@@ -858,7 +1240,8 @@ static void resolv_result(GResolvResultStatus status,
 
 static guint do_request(GWeb *web, const char *url,
                                const char *type, GWebInputFunc input,
-                               GWebResultFunc func, gpointer user_data)
+                               int fd, gsize length, GWebResultFunc func,
+                               GWebRouteFunc route, gpointer user_data)
 {
        struct web_session *session;
 
@@ -871,13 +1254,16 @@ static guint do_request(GWeb *web, const char *url,
        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);
@@ -888,7 +1274,11 @@ static guint do_request(GWeb *web, const char *url,
        session->web = web;
 
        session->result_func = func;
+       session->route_func = route;
        session->input_func = input;
+       session->fd = fd;
+       session->length = length;
+       session->offset = 0;
        session->user_data = user_data;
 
        session->receive_buffer = g_try_malloc(DEFAULT_BUFFER_SIZE);
@@ -897,13 +1287,20 @@ static guint do_request(GWeb *web, const char *url,
                return 0;
        }
 
+       session->result.headers = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                                       g_free, g_free);
+       if (session->result.headers == NULL) {
+               free_session(session);
+               return 0;
+       }
+
        session->receive_space = DEFAULT_BUFFER_SIZE;
        session->send_buffer = g_string_sized_new(0);
        session->current_header = g_string_sized_new(0);
        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) {
@@ -911,7 +1308,30 @@ static guint do_request(GWeb *web, const char *url,
                        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;
+               hints.ai_family = session->web->family;
+
+               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);
@@ -924,17 +1344,40 @@ static guint do_request(GWeb *web, const char *url,
        return web->next_query_id++;
 }
 
-guint g_web_request_get(GWeb *web, const char *url,
-                               GWebResultFunc func, gpointer user_data)
+guint g_web_request_get(GWeb *web, const char *url, GWebResultFunc func,
+               GWebRouteFunc route, gpointer user_data)
 {
-       return do_request(web, url, NULL, NULL, func, user_data);
+       return do_request(web, url, NULL, NULL, -1, 0, func, route, user_data);
 }
 
 guint g_web_request_post(GWeb *web, const char *url,
                                const char *type, GWebInputFunc input,
                                GWebResultFunc func, gpointer user_data)
 {
-       return do_request(web, url, type, input, func, user_data);
+       return do_request(web, url, type, input, -1, 0, func, NULL, user_data);
+}
+
+guint g_web_request_post_file(GWeb *web, const char *url,
+                               const char *type, const char *file,
+                               GWebResultFunc func, gpointer user_data)
+{
+       struct stat st;
+       int fd;
+       guint ret;
+
+       if (stat(file, &st) < 0)
+               return 0;
+
+       fd = open(file, O_RDONLY);
+       if (fd < 0)
+               return 0;
+
+       ret = do_request(web, url, type, NULL, fd, st.st_size, func, NULL,
+                       user_data);
+       if (ret == 0)
+               close(fd);
+
+       return ret;
 }
 
 gboolean g_web_cancel_request(GWeb *web, guint id)
@@ -970,6 +1413,23 @@ gboolean g_web_result_get_chunk(GWebResult *result,
        return TRUE;
 }
 
+gboolean g_web_result_get_header(GWebResult *result,
+                               const char *header, const char **value)
+{
+       if (result == NULL)
+               return FALSE;
+
+       if (value == NULL)
+               return FALSE;
+
+       *value = g_hash_table_lookup(result->headers, header);
+
+       if (*value == NULL)
+               return FALSE;
+
+       return TRUE;
+}
+
 struct _GWebParser {
        gint ref_count;
        char *begin_token;
@@ -1020,7 +1480,7 @@ GWebParser *g_web_parser_ref(GWebParser *parser)
        if (parser == NULL)
                return NULL;
 
-       g_atomic_int_inc(&parser->ref_count);
+       __sync_fetch_and_add(&parser->ref_count, 1);
 
        return parser;
 }
@@ -1030,7 +1490,7 @@ void g_web_parser_unref(GWebParser *parser)
        if (parser == NULL)
                return;
 
-       if (g_atomic_int_dec_and_test(&parser->ref_count) == FALSE)
+       if (__sync_fetch_and_sub(&parser->ref_count, 1) != 1)
                return;
 
        g_string_free(parser->content, TRUE);