Change the SoupURI properties to SoupAddress properties.
[platform/upstream/libsoup.git] / libsoup / soup-session.c
index c4acf88..2b9e5eb 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
+#include "soup-address.h"
 #include "soup-auth.h"
 #include "soup-auth-basic.h"
 #include "soup-auth-digest.h"
-#include "soup-auth-manager.h"
 #include "soup-auth-manager-ntlm.h"
 #include "soup-connection.h"
 #include "soup-marshal.h"
 #include "soup-message-private.h"
 #include "soup-message-queue.h"
 #include "soup-session.h"
+#include "soup-session-feature.h"
 #include "soup-session-private.h"
 #include "soup-socket.h"
 #include "soup-ssl.h"
  **/
 
 typedef struct {
-       SoupURI    *root_uri;
+       SoupAddress *addr;
 
-       GSList     *connections;      /* CONTAINS: SoupConnection */
-       guint       num_conns;
-
-       GHashTable *auth_realms;      /* path -> scheme:realm */
-       GHashTable *auths;            /* scheme:realm -> SoupAuth */
+       GSList      *connections;      /* CONTAINS: SoupConnection */
+       guint        num_conns;
 } SoupSessionHost;
 
 typedef struct {
        SoupURI *proxy_uri;
+       SoupAddress *proxy_addr;
        SoupAuth *proxy_auth;
 
-       guint max_conns, max_conns_per_host;
-
        char *ssl_ca_file;
        SoupSSLCredentials *ssl_creds;
 
@@ -77,12 +74,14 @@ typedef struct {
 
        char *user_agent;
 
+       GSList *features;
        SoupAuthManager *auth_manager;
-       SoupAuthManagerNTLM *ntlm_manager;
 
-       GHashTable *hosts; /* SoupURI -> SoupSessionHost */
+       GHashTable *hosts; /* SoupAddress -> SoupSessionHost */
        GHashTable *conns; /* SoupConnection -> SoupSessionHost */
        guint num_conns;
+       guint max_conns, max_conns_per_host;
+       guint io_timeout, idle_timeout;
 
        /* Must hold the host_lock before potentially creating a
         * new SoupSessionHost, or adding/removing a connection.
@@ -91,11 +90,6 @@ typedef struct {
        GMutex *host_lock;
 
        GMainContext *async_context;
-
-       /* Holds the timeout value for the connection, when
-          no response is received.
-       */
-       guint timeout;
 } SoupSessionPrivate;
 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
 
@@ -107,11 +101,9 @@ static void requeue_message (SoupSession *session, SoupMessage *msg);
 static void cancel_message  (SoupSession *session, SoupMessage *msg,
                             guint status_code);
 
-/* temporary until we fix this to index hosts by SoupAddress */
-extern guint     soup_uri_host_hash  (gconstpointer  key);
-extern gboolean  soup_uri_host_equal (gconstpointer  v1,
-                                     gconstpointer  v2);
-extern SoupURI  *soup_uri_copy_root  (SoupURI *uri);
+static void auth_manager_authenticate (SoupAuthManager *manager,
+                                      SoupMessage *msg, SoupAuth *auth,
+                                      gboolean retrying, gpointer user_data);
 
 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
@@ -121,7 +113,9 @@ extern SoupURI  *soup_uri_copy_root  (SoupURI *uri);
 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
 
 enum {
+       REQUEST_QUEUED,
        REQUEST_STARTED,
+       REQUEST_UNQUEUED,
        AUTHENTICATE,
        LAST_SIGNAL
 };
@@ -139,6 +133,10 @@ enum {
        PROP_ASYNC_CONTEXT,
        PROP_TIMEOUT,
        PROP_USER_AGENT,
+       PROP_IDLE_TIMEOUT,
+       PROP_ADD_FEATURE,
+       PROP_ADD_FEATURE_BY_TYPE,
+       PROP_REMOVE_FEATURE_BY_TYPE,
 
        LAST_PROP
 };
@@ -153,21 +151,24 @@ soup_session_init (SoupSession *session)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
 
-       priv->queue = soup_message_queue_new ();
+       priv->queue = soup_message_queue_new (session);
 
        priv->host_lock = g_mutex_new ();
-       priv->hosts = g_hash_table_new (soup_uri_host_hash,
-                                       soup_uri_host_equal);
+       priv->hosts = g_hash_table_new (soup_address_hash_by_ip,
+                                       soup_address_equal_by_ip);
        priv->conns = g_hash_table_new (NULL, NULL);
 
        priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
        priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
 
-       priv->timeout = 0;
-
-       priv->auth_manager = soup_auth_manager_new (session);
+       priv->auth_manager = g_object_new (SOUP_TYPE_AUTH_MANAGER_NTLM,
+                                          SOUP_AUTH_MANAGER_NTLM_USE_NTLM, FALSE,
+                                          NULL);
+       g_signal_connect (priv->auth_manager, "authenticate",
+                         G_CALLBACK (auth_manager_authenticate), session);
        soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
        soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
+       soup_session_add_feature (session, SOUP_SESSION_FEATURE (priv->auth_manager));
 }
 
 static gboolean
@@ -184,8 +185,8 @@ cleanup_hosts (SoupSessionPrivate *priv)
 
        g_mutex_lock (priv->host_lock);
        old_hosts = priv->hosts;
-       priv->hosts = g_hash_table_new (soup_uri_host_hash,
-                                       soup_uri_host_equal);
+       priv->hosts = g_hash_table_new (soup_address_hash_by_ip,
+                                       soup_address_equal_by_ip);
        g_mutex_unlock (priv->host_lock);
 
        g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
@@ -201,6 +202,9 @@ dispose (GObject *object)
        soup_session_abort (session);
        cleanup_hosts (priv);
 
+       while (priv->features)
+               soup_session_remove_feature (session, priv->features->data);
+
        G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
 }
 
@@ -216,12 +220,15 @@ finalize (GObject *object)
        g_hash_table_destroy (priv->hosts);
        g_hash_table_destroy (priv->conns);
 
-       soup_auth_manager_free (priv->auth_manager);
-       if (priv->ntlm_manager)
-               soup_auth_manager_ntlm_free (priv->ntlm_manager);
+       g_free (priv->user_agent);
+
+       if (priv->auth_manager)
+               g_object_unref (priv->auth_manager);
 
        if (priv->proxy_uri)
                soup_uri_free (priv->proxy_uri);
+       if (priv->proxy_addr)
+               g_object_unref (priv->proxy_addr);
 
        if (priv->ssl_creds)
                soup_ssl_free_client_credentials (priv->ssl_creds);
@@ -253,12 +260,65 @@ soup_session_class_init (SoupSessionClass *session_class)
        /* signals */
 
        /**
+        * SoupSession::request-queued:
+        * @session: the session
+        * @msg: the request that was queued
+        *
+        * Emitted when a request is queued on @session. (Note that
+        * "queued" doesn't just mean soup_session_queue_message();
+        * soup_session_send_message() implicitly queues the message
+        * as well.)
+        *
+        * When sending a request, first #SoupSession::request_queued
+        * is emitted, indicating that the session has become aware of
+        * the request.
+        *
+        * Once a connection is available to send the request on, the
+        * session emits #SoupSession::request_started. Then, various
+        * #SoupMessage signals are emitted as the message is
+        * processed. If the message is requeued, it will emit
+        * #SoupMessage::restarted, which will then be followed by
+        * another #SoupSession::request_started and another set of
+        * #SoupMessage signals when the message is re-sent.
+        *
+        * Eventually, the message will emit #SoupMessage::finished.
+        * Normally, this signals the completion of message
+        * processing. However, it is possible that the application
+        * will requeue the message from the "finished" handler (or
+        * equivalently, from the soup_session_queue_message()
+        * callback). In that case, the process will loop back to
+        * #SoupSession::request_started.
+        *
+        * Eventually, a message will reach "finished" and not be
+        * requeued. At that point, the session will emit
+        * #SoupSession::request_unqueued to indicate that it is done
+        * with the message.
+        *
+        * To sum up: #SoupSession::request_queued and
+        * #SoupSession::request_unqueued are guaranteed to be emitted
+        * exactly once, but #SoupSession::request_started and
+        * #SoupMessage::finished (and all of the other #SoupMessage
+        * signals) may be invoked multiple times for a given message.
+        **/
+       signals[REQUEST_QUEUED] =
+               g_signal_new ("request-queued",
+                             G_OBJECT_CLASS_TYPE (object_class),
+                             G_SIGNAL_RUN_FIRST,
+                             0, /* FIXME? */
+                             NULL, NULL,
+                             soup_marshal_NONE__OBJECT,
+                             G_TYPE_NONE, 1,
+                             SOUP_TYPE_MESSAGE);
+
+       /**
         * SoupSession::request-started:
         * @session: the session
         * @msg: the request being sent
         * @socket: the socket the request is being sent on
         *
-        * Emitted just before a request is sent.
+        * Emitted just before a request is sent. See
+        * #SoupSession::request_queued for a detailed description of
+        * the message lifecycle within a session.
         **/
        signals[REQUEST_STARTED] =
                g_signal_new ("request-started",
@@ -272,6 +332,26 @@ soup_session_class_init (SoupSessionClass *session_class)
                              SOUP_TYPE_SOCKET);
 
        /**
+        * SoupSession::request-unqueued:
+        * @session: the session
+        * @msg: the request that was unqueued
+        *
+        * Emitted when a request is removed from @session's queue,
+        * indicating that @session is done with it. See
+        * #SoupSession::request_queued for a detailed description of the
+        * message lifecycle within a session.
+        **/
+       signals[REQUEST_UNQUEUED] =
+               g_signal_new ("request-unqueued",
+                             G_OBJECT_CLASS_TYPE (object_class),
+                             G_SIGNAL_RUN_FIRST,
+                             0, /* FIXME? */
+                             NULL, NULL,
+                             soup_marshal_NONE__OBJECT,
+                             G_TYPE_NONE, 1,
+                             SOUP_TYPE_MESSAGE);
+
+       /**
         * SoupSession::authenticate:
         * @session: the session
         * @msg: the #SoupMessage being sent
@@ -333,6 +413,13 @@ soup_session_class_init (SoupSessionClass *session_class)
                                  SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
                                  G_PARAM_READWRITE));
        g_object_class_install_property (
+               object_class, PROP_IDLE_TIMEOUT,
+               g_param_spec_uint (SOUP_SESSION_IDLE_TIMEOUT,
+                                  "Idle Timeout",
+                                  "Connection lifetime when idle",
+                                  0, G_MAXUINT, 0,
+                                  G_PARAM_READWRITE));
+       g_object_class_install_property (
                object_class, PROP_USE_NTLM,
                g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
                                      "Use NTLM",
@@ -359,6 +446,34 @@ soup_session_class_init (SoupSessionClass *session_class)
                                   "Value in seconds to timeout a blocking I/O",
                                   0, G_MAXUINT, 0,
                                   G_PARAM_READWRITE));
+
+       /**
+        * SoupSession:user-agent:
+        *
+        * If non-%NULL, the value to use for the "User-Agent" header
+        * on #SoupMessage<!-- -->s sent from this session.
+        *
+        * RFC 2616 says: "The User-Agent request-header field
+        * contains information about the user agent originating the
+        * request. This is for statistical purposes, the tracing of
+        * protocol violations, and automated recognition of user
+        * agents for the sake of tailoring responses to avoid
+        * particular user agent limitations. User agents SHOULD
+        * include this field with requests."
+        *
+        * The User-Agent header contains a list of one or more
+        * product tokens, separated by whitespace, with the most
+        * significant product token coming first. The tokens must be
+        * brief, ASCII, and mostly alphanumeric (although "-", "_",
+        * and "." are also allowed), and may optionally include a "/"
+        * followed by a version string. You may also put comments,
+        * enclosed in parentheses, between or after the tokens.
+        *
+        * If you set a %user_agent property that has trailing
+        * whitespace, #SoupSession will append its own product token
+        * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
+        * header for you.
+        **/
        g_object_class_install_property (
                object_class, PROP_USER_AGENT,
                g_param_spec_string (SOUP_SESSION_USER_AGENT,
@@ -366,6 +481,28 @@ soup_session_class_init (SoupSessionClass *session_class)
                                     "User-Agent string",
                                     NULL,
                                     G_PARAM_READWRITE));
+
+       g_object_class_install_property (
+               object_class, PROP_ADD_FEATURE,
+               g_param_spec_object (SOUP_SESSION_ADD_FEATURE,
+                                    "Add Feature",
+                                    "Add a feature object to the session",
+                                    SOUP_TYPE_SESSION_FEATURE,
+                                    G_PARAM_READWRITE));
+       g_object_class_install_property (
+               object_class, PROP_ADD_FEATURE_BY_TYPE,
+               g_param_spec_gtype (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
+                                   "Add Feature By Type",
+                                   "Add a feature object of the given type to the session",
+                                   SOUP_TYPE_SESSION_FEATURE,
+                                   G_PARAM_READWRITE));
+       g_object_class_install_property (
+               object_class, PROP_REMOVE_FEATURE_BY_TYPE,
+               g_param_spec_gtype (SOUP_SESSION_REMOVE_FEATURE_BY_TYPE,
+                                   "Remove Feature By Type",
+                                   "Remove features of the given type from the session",
+                                   SOUP_TYPE_SESSION_FEATURE,
+                                   G_PARAM_READWRITE));
 }
 
 static gboolean
@@ -412,8 +549,13 @@ set_property (GObject *object, guint prop_id,
 
                if (priv->proxy_uri)
                        soup_uri_free (priv->proxy_uri);
+               if (priv->proxy_addr)
+                       g_object_unref (priv->proxy_addr);
 
                priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
+               priv->proxy_addr = uri ?
+                       soup_address_new (uri->host, uri->port) :
+                       NULL;
 
                if (need_abort) {
                        soup_session_abort (session);
@@ -428,15 +570,9 @@ set_property (GObject *object, guint prop_id,
                priv->max_conns_per_host = g_value_get_int (value);
                break;
        case PROP_USE_NTLM:
-               if (g_value_get_boolean (value)) {
-                       if (!priv->ntlm_manager)
-                               priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
-               } else {
-                       if (priv->ntlm_manager) {
-                               soup_auth_manager_ntlm_free (priv->ntlm_manager);
-                               priv->ntlm_manager = NULL;
-                       }
-               }
+               g_object_set_property (G_OBJECT (priv->auth_manager),
+                                      SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
+                                      value);
                break;
        case PROP_SSL_CA_FILE:
                new_ca_file = g_value_get_string (value);
@@ -463,7 +599,7 @@ set_property (GObject *object, guint prop_id,
                        g_main_context_ref (priv->async_context);
                break;
        case PROP_TIMEOUT:
-               priv->timeout = g_value_get_uint (value);
+               priv->io_timeout = g_value_get_uint (value);
                break;
        case PROP_USER_AGENT:
                g_free (priv->user_agent);
@@ -480,7 +616,20 @@ set_property (GObject *object, guint prop_id,
                } else
                        priv->user_agent = g_strdup (user_agent);
                break;
+       case PROP_IDLE_TIMEOUT:
+               priv->idle_timeout = g_value_get_uint (value);
+               break;
+       case PROP_ADD_FEATURE:
+               soup_session_add_feature (session, g_value_get_object (value));
+               break;
+       case PROP_ADD_FEATURE_BY_TYPE:
+               soup_session_add_feature_by_type (session, g_value_get_gtype (value));
+               break;
+       case PROP_REMOVE_FEATURE_BY_TYPE:
+               soup_session_remove_feature_by_type (session, g_value_get_gtype (value));
+               break;
        default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
 }
@@ -503,7 +652,9 @@ get_property (GObject *object, guint prop_id,
                g_value_set_int (value, priv->max_conns_per_host);
                break;
        case PROP_USE_NTLM:
-               g_value_set_boolean (value, priv->ntlm_manager != NULL);
+               g_object_get_property (G_OBJECT (priv->auth_manager),
+                                      SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
+                                      value);
                break;
        case PROP_SSL_CA_FILE:
                g_value_set_string (value, priv->ssl_ca_file);
@@ -512,12 +663,16 @@ get_property (GObject *object, guint prop_id,
                g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
                break;
        case PROP_TIMEOUT:
-               g_value_set_uint (value, priv->timeout);
+               g_value_set_uint (value, priv->io_timeout);
                break;
        case PROP_USER_AGENT:
                g_value_set_string (value, priv->user_agent);
                break;
+       case PROP_IDLE_TIMEOUT:
+               g_value_set_uint (value, priv->idle_timeout);
+               break;
        default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
 }
@@ -547,20 +702,12 @@ soup_session_get_async_context (SoupSession *session)
 /* Hosts */
 
 static SoupSessionHost *
-soup_session_host_new (SoupSession *session, SoupURI *source_uri)
+soup_session_host_new (SoupSession *session, SoupAddress *addr)
 {
-       SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
        SoupSessionHost *host;
 
        host = g_slice_new0 (SoupSessionHost);
-       host->root_uri = soup_uri_copy_root (source_uri);
-
-       if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
-           !priv->ssl_creds) {
-               priv->ssl_creds =
-                       soup_ssl_get_client_credentials (priv->ssl_ca_file);
-       }
-
+       host->addr = g_object_ref (addr);
        return host;
 }
 
@@ -573,14 +720,14 @@ get_host_for_message (SoupSession *session, SoupMessage *msg)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
        SoupSessionHost *host;
-       SoupURI *source = soup_message_get_uri (msg);
+       SoupAddress *addr = soup_message_get_address (msg);
 
-       host = g_hash_table_lookup (priv->hosts, source);
+       host = g_hash_table_lookup (priv->hosts, addr);
        if (host)
                return host;
 
-       host = soup_session_host_new (session, source);
-       g_hash_table_insert (priv->hosts, host->root_uri, host);
+       host = soup_session_host_new (session, addr);
+       g_hash_table_insert (priv->hosts, host->addr, host);
 
        return host;
 }
@@ -595,17 +742,23 @@ free_host (SoupSessionHost *host)
                soup_connection_disconnect (conn);
        }
 
-       soup_uri_free (host->root_uri);
+       g_object_unref (host->addr);
        g_slice_free (SoupSessionHost, host);
 }      
 
-void
-soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
-                               SoupAuth *auth, gboolean retrying)
+static void
+auth_manager_authenticate (SoupAuthManager *manager, SoupMessage *msg,
+                          SoupAuth *auth, gboolean retrying,
+                          gpointer session)
 {
        g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
 }
 
+#define SOUP_METHOD_IS_SAFE(method) (method == SOUP_METHOD_GET || \
+                                    method == SOUP_METHOD_HEAD || \
+                                    method == SOUP_METHOD_OPTIONS || \
+                                    method == SOUP_METHOD_PROPFIND)
+
 static void
 redirect_handler (SoupMessage *msg, gpointer user_data)
 {
@@ -616,15 +769,9 @@ redirect_handler (SoupMessage *msg, gpointer user_data)
        new_loc = soup_message_headers_get (msg->response_headers, "Location");
        g_return_if_fail (new_loc != NULL);
 
-       if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
-           msg->status_code == SOUP_STATUS_FOUND ||
-           msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
-               /* Don't redirect non-safe methods */
-               if (msg->method != SOUP_METHOD_GET &&
-                   msg->method != SOUP_METHOD_HEAD &&
-                   msg->method != SOUP_METHOD_OPTIONS)
-                       return;
-       } else if (msg->status_code == SOUP_STATUS_SEE_OTHER) {
+       if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
+           (msg->status_code == SOUP_STATUS_FOUND &&
+            !SOUP_METHOD_IS_SAFE (msg->method))) {
                /* Redirect using a GET */
                g_object_set (msg,
                              SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
@@ -633,6 +780,12 @@ redirect_handler (SoupMessage *msg, gpointer user_data)
                                          SOUP_MEMORY_STATIC, NULL, 0);
                soup_message_headers_set_encoding (msg->request_headers,
                                                   SOUP_ENCODING_NONE);
+       } else if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
+                  msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT ||
+                  msg->status_code == SOUP_STATUS_FOUND) {
+               /* Don't redirect non-safe methods */
+               if (!SOUP_METHOD_IS_SAFE (msg->method))
+                       return;
        } else {
                /* Three possibilities:
                 *
@@ -683,59 +836,51 @@ connection_started_request (SoupConnection *conn, SoupMessage *msg,
                                              "User-Agent", priv->user_agent);
        }
 
-       g_signal_emit (session, signals[REQUEST_STARTED], 0,
-                      msg, soup_connection_get_socket (conn));
-}
-
-static void
-find_oldest_connection (gpointer key, gpointer host, gpointer data)
-{
-       SoupConnection *conn = key, **oldest = data;
-
-       /* Don't prune a connection that is currently in use, or
-        * hasn't been used yet.
+       /* Kludge to deal with the fact that CONNECT msgs come from the
+        * SoupConnection rather than being queued normally.
         */
-       if (soup_connection_is_in_use (conn) ||
-           soup_connection_last_used (conn) == 0)
-               return;
+       if (msg->method == SOUP_METHOD_CONNECT)
+               g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
 
-       if (!*oldest || (soup_connection_last_used (conn) <
-                        soup_connection_last_used (*oldest)))
-               *oldest = conn;
+       g_signal_emit (session, signals[REQUEST_STARTED], 0,
+                      msg, soup_connection_get_socket (conn));
 }
 
-/**
- * soup_session_try_prune_connection:
- * @session: a #SoupSession
- *
- * Finds the least-recently-used idle connection in @session and closes
- * it.
- *
- * Return value: %TRUE if a connection was closed, %FALSE if there are
- * no idle connections.
- **/
 gboolean
 soup_session_try_prune_connection (SoupSession *session)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
-       SoupConnection *oldest = NULL;
+       GPtrArray *conns;
+       GHashTableIter iter;
+       gpointer conn, host;
+       int i;
+
+       conns = g_ptr_array_new ();
 
        g_mutex_lock (priv->host_lock);
-       g_hash_table_foreach (priv->conns, find_oldest_connection,
-                             &oldest);
-       if (oldest) {
-               /* Ref the connection before unlocking the mutex in
-                * case someone else tries to prune it too.
+       g_hash_table_iter_init (&iter, priv->conns);
+       while (g_hash_table_iter_next (&iter, &conn, &host)) {
+               /* Don't prune a connection that is currently in use,
+                * or hasn't been used yet.
                 */
-               g_object_ref (oldest);
-               g_mutex_unlock (priv->host_lock);
-               soup_connection_disconnect (oldest);
-               g_object_unref (oldest);
-               return TRUE;
-       } else {
-               g_mutex_unlock (priv->host_lock);
+               if (!soup_connection_is_in_use (conn) &&
+                   soup_connection_last_used (conn) > 0)
+                       g_ptr_array_add (conns, g_object_ref (conn));
+       }
+       g_mutex_unlock (priv->host_lock);
+
+       if (!conns->len) {
+               g_ptr_array_free (conns, TRUE);
                return FALSE;
        }
+
+       for (i = 0; i < conns->len; i++) {
+               soup_connection_disconnect (conns->pdata[i]);
+               g_object_unref (conns->pdata[i]);
+       }
+       g_ptr_array_free (conns, TRUE);
+
+       return TRUE;
 }
 
 static void
@@ -767,7 +912,7 @@ connect_result (SoupConnection *conn, guint status, gpointer user_data)
        SoupSession *session = user_data;
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
        SoupSessionHost *host;
-       SoupMessageQueueIter iter;
+       SoupMessageQueueItem *item;
        SoupMessage *msg;
 
        g_mutex_lock (priv->host_lock);
@@ -807,7 +952,9 @@ connect_result (SoupConnection *conn, guint status, gpointer user_data)
         * any messages waiting for this host, since they're out
         * of luck.
         */
-       for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
+       g_object_ref (session);
+       for (item = soup_message_queue_first (priv->queue); item; item = soup_message_queue_next (priv->queue, item)) {
+               msg = item->msg;
                if (get_host_for_message (session, msg) == host) {
                        if (status == SOUP_STATUS_TRY_AGAIN) {
                                if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
@@ -818,6 +965,7 @@ connect_result (SoupConnection *conn, guint status, gpointer user_data)
                        }
                }
        }
+       g_object_unref (session);
 }
 
 /**
@@ -865,7 +1013,9 @@ soup_session_get_connection (SoupSession *session, SoupMessage *msg,
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
        SoupConnection *conn;
        SoupSessionHost *host;
+       SoupSSLCredentials *ssl_creds;
        GSList *conns;
+       SoupURI *uri;
 
        g_mutex_lock (priv->host_lock);
 
@@ -898,12 +1048,21 @@ soup_session_get_connection (SoupSession *session, SoupMessage *msg,
                return NULL;
        }
 
+       uri = soup_message_get_uri (msg);
+       if (uri->scheme == SOUP_URI_SCHEME_HTTPS) {
+               if (!priv->ssl_creds)
+                       priv->ssl_creds = soup_ssl_get_client_credentials (priv->ssl_ca_file);
+               ssl_creds = priv->ssl_creds;
+       } else
+               ssl_creds = NULL;
+
        conn = soup_connection_new (
-               SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
-               SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
-               SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
+               SOUP_CONNECTION_SERVER_ADDRESS, host->addr,
+               SOUP_CONNECTION_PROXY_ADDRESS, priv->proxy_addr,
+               SOUP_CONNECTION_SSL_CREDENTIALS, ssl_creds,
                SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
-               SOUP_CONNECTION_TIMEOUT, priv->timeout,
+               SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
+               SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
                NULL);
        g_signal_connect (conn, "connect_result",
                          G_CALLBACK (connect_result),
@@ -945,12 +1104,16 @@ soup_session_get_queue (SoupSession *session)
 static void
 message_finished (SoupMessage *msg, gpointer user_data)
 {
-       SoupSession *session = user_data;
+       SoupMessageQueueItem *item = user_data;
+       SoupSession *session = item->session;
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
 
        if (!SOUP_MESSAGE_IS_STARTING (msg)) {
-               soup_message_queue_remove_message (priv->queue, msg);
+               soup_message_queue_remove (priv->queue, item);
                g_signal_handlers_disconnect_by_func (msg, message_finished, session);
+               g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
+               g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
+               soup_message_queue_item_unref (item);
        }
 }
 
@@ -959,9 +1122,13 @@ queue_message (SoupSession *session, SoupMessage *msg,
               SoupSessionCallback callback, gpointer user_data)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
+       SoupMessageQueueItem *item;
+
+       item = soup_message_queue_append (priv->queue, msg, callback, user_data);
+       soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
 
        g_signal_connect_after (msg, "finished",
-                               G_CALLBACK (message_finished), session);
+                               G_CALLBACK (message_finished), item);
 
        if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
                soup_message_add_header_handler (
@@ -969,8 +1136,7 @@ queue_message (SoupSession *session, SoupMessage *msg,
                        G_CALLBACK (redirect_handler), session);
        }
 
-       soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
-       soup_message_queue_append (priv->queue, msg);
+       g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
 }
 
 /**
@@ -1103,8 +1269,16 @@ static void
 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
 {
        SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
+       SoupMessageQueueItem *item;
+
+       item = soup_message_queue_lookup (priv->queue, msg);
+       if (item) {
+               soup_message_queue_remove (priv->queue, item);
+               if (item->cancellable)
+                       g_cancellable_cancel (item->cancellable);
+               soup_message_queue_item_unref (item);
+       }
 
-       soup_message_queue_remove_message (priv->queue, msg);
        soup_message_io_stop (msg);
        soup_message_set_status (msg, status_code);
        soup_message_finished (msg);
@@ -1137,7 +1311,7 @@ gather_conns (gpointer key, gpointer host, gpointer data)
        SoupConnection *conn = key;
        GSList **conns = data;
 
-       *conns = g_slist_prepend (*conns, conn);
+       *conns = g_slist_prepend (*conns, g_object_ref (conn));
 }
 
 /**
@@ -1150,17 +1324,16 @@ void
 soup_session_abort (SoupSession *session)
 {
        SoupSessionPrivate *priv;
-       SoupMessageQueueIter iter;
-       SoupMessage *msg;
+       SoupMessageQueueItem *item;
        GSList *conns, *c;
 
        g_return_if_fail (SOUP_IS_SESSION (session));
        priv = SOUP_SESSION_GET_PRIVATE (session);
 
-       for (msg = soup_message_queue_first (priv->queue, &iter);
-            msg;
-            msg = soup_message_queue_next (priv->queue, &iter)) {
-               soup_session_cancel_message (session, msg,
+       for (item = soup_message_queue_first (priv->queue);
+            item;
+            item = soup_message_queue_next (priv->queue, item)) {
+               soup_session_cancel_message (session, item->msg,
                                             SOUP_STATUS_CANCELLED);
        }
 
@@ -1169,8 +1342,6 @@ soup_session_abort (SoupSession *session)
        conns = NULL;
        g_hash_table_foreach (priv->conns, gather_conns, &conns);
 
-       for (c = conns; c; c = c->next)
-               g_object_ref (c->data);
        g_mutex_unlock (priv->host_lock);
        for (c = conns; c; c = c->next) {
                soup_connection_disconnect (c->data);
@@ -1179,3 +1350,99 @@ soup_session_abort (SoupSession *session)
 
        g_slist_free (conns);
 }
+
+/**
+ * soup_session_add_feature:
+ * @session: a #SoupSession
+ * @feature: an object that implements #SoupSessionFeature
+ *
+ * Adds @feature's functionality to @session. You can also add a
+ * feature to the session at construct time by using the
+ * %SOUP_SESSION_ADD_FEATURE property.
+ **/
+void
+soup_session_add_feature (SoupSession *session, SoupSessionFeature *feature)
+{
+       SoupSessionPrivate *priv;
+
+       g_return_if_fail (SOUP_IS_SESSION (session));
+       g_return_if_fail (SOUP_IS_SESSION_FEATURE (feature));
+
+       priv = SOUP_SESSION_GET_PRIVATE (session);
+       priv->features = g_slist_prepend (priv->features, g_object_ref (feature));
+       soup_session_feature_attach (feature, session);
+}
+
+/**
+ * soup_session_add_feature_by_type:
+ * @session: a #SoupSession
+ * @feature_type: the #GType of a class that implements #SoupSessionFeature
+ *
+ * Creates a new feature of type @feature_type and adds it to
+ * @session. You can use this instead of soup_session_add_feature() in
+ * the case wher you don't need to customize the new feature in any
+ * way. You can also add a feature to the session at construct time by
+ * using the %SOUP_SESSION_ADD_FEATURE_BY_TYPE property.
+ **/
+void
+soup_session_add_feature_by_type (SoupSession *session, GType feature_type)
+{
+       SoupSessionFeature *feature;
+
+       g_return_if_fail (SOUP_IS_SESSION (session));
+       g_return_if_fail (g_type_is_a (feature_type, SOUP_TYPE_SESSION_FEATURE));
+
+       feature = g_object_new (feature_type, NULL);
+       soup_session_add_feature (session, feature);
+       g_object_unref (feature);
+}
+
+/**
+ * soup_session_remove_feature:
+ * @session: a #SoupSession
+ * @feature: a feature that has previously been added to @session
+ *
+ * Removes @feature's functionality from @session.
+ **/
+void
+soup_session_remove_feature (SoupSession *session, SoupSessionFeature *feature)
+{
+       SoupSessionPrivate *priv;
+
+       g_return_if_fail (SOUP_IS_SESSION (session));
+
+       priv = SOUP_SESSION_GET_PRIVATE (session);
+       if (g_slist_find (priv->features, feature)) {
+               priv->features = g_slist_remove (priv->features, feature);
+               soup_session_feature_detach (feature, session);
+               g_object_unref (feature);
+       }
+}
+
+/**
+ * soup_session_remove_feature_by_type:
+ * @session: a #SoupSession
+ * @feature_type: the #GType of a class that implements #SoupSessionFeature
+ *
+ * Removes all features of type @feature_type (or any subclass of
+ * @feature_type) from @session. You can also remove standard features
+ * from the session at construct time by using the
+ * %SOUP_SESSION_REMOVE_FEATURE_BY_TYPE property.
+ **/
+void
+soup_session_remove_feature_by_type (SoupSession *session, GType feature_type)
+{
+       SoupSessionPrivate *priv;
+       GSList *f;
+
+       g_return_if_fail (SOUP_IS_SESSION (session));
+
+       priv = SOUP_SESSION_GET_PRIVATE (session);
+restart:
+       for (f = priv->features; f; f = f->next) {
+               if (G_TYPE_CHECK_INSTANCE_TYPE (f->data, feature_type)) {
+                       soup_session_remove_feature (session, f->data);
+                       goto restart;
+               }
+       }
+}