doc fixups
[platform/upstream/libsoup.git] / libsoup / soup-session.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-session.c
4  *
5  * Copyright (C) 2000-2003, Ximian, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16 #include "soup-auth.h"
17 #include "soup-auth-basic.h"
18 #include "soup-auth-digest.h"
19 #include "soup-auth-manager-ntlm.h"
20 #include "soup-connection.h"
21 #include "soup-marshal.h"
22 #include "soup-message-private.h"
23 #include "soup-message-queue.h"
24 #include "soup-session.h"
25 #include "soup-session-feature.h"
26 #include "soup-session-private.h"
27 #include "soup-socket.h"
28 #include "soup-ssl.h"
29 #include "soup-uri.h"
30
31 /**
32  * SECTION:soup-session
33  * @short_description: Soup session state object
34  *
35  * #SoupSession is the object that controls client-side HTTP. A
36  * #SoupSession encapsulates all of the state that libsoup is keeping
37  * on behalf of your program; cached HTTP connections, authentication
38  * information, etc.
39  *
40  * Most applications will only need a single #SoupSession; the primary
41  * reason you might need multiple sessions is if you need to have
42  * multiple independent authentication contexts. (Eg, you are
43  * connecting to a server and authenticating as two different users at
44  * different times; the easiest way to ensure that each #SoupMessage
45  * is sent with the authentication information you intended is to use
46  * one session for the first user, and a second session for the other
47  * user.)
48  *
49  * #SoupSession itself is an abstract class, with two subclasses. If
50  * you are using the glib main loop, you will generally want to use
51  * #SoupSessionAsync, which uses non-blocking I/O and callbacks. On
52  * the other hand, if your application is threaded and you want to do
53  * synchronous I/O in a separate thread from the UI, use
54  * #SoupSessionSync.
55  **/
56
57 typedef struct {
58         SoupURI    *root_uri;
59
60         GSList     *connections;      /* CONTAINS: SoupConnection */
61         guint       num_conns;
62
63         GHashTable *auth_realms;      /* path -> scheme:realm */
64         GHashTable *auths;            /* scheme:realm -> SoupAuth */
65 } SoupSessionHost;
66
67 typedef struct {
68         SoupURI *proxy_uri;
69         SoupAuth *proxy_auth;
70
71         char *ssl_ca_file;
72         SoupSSLCredentials *ssl_creds;
73
74         SoupMessageQueue *queue;
75
76         char *user_agent;
77
78         GSList *features;
79         SoupAuthManager *auth_manager;
80
81         GHashTable *hosts; /* SoupURI -> SoupSessionHost */
82         GHashTable *conns; /* SoupConnection -> SoupSessionHost */
83         guint num_conns;
84         guint max_conns, max_conns_per_host;
85         guint io_timeout, idle_timeout;
86
87         /* Must hold the host_lock before potentially creating a
88          * new SoupSessionHost, or adding/removing a connection.
89          * Must not emit signals or destroy objects while holding it.
90          */
91         GMutex *host_lock;
92
93         GMainContext *async_context;
94 } SoupSessionPrivate;
95 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
96
97 static void     free_host      (SoupSessionHost *host);
98
99 static void queue_message   (SoupSession *session, SoupMessage *msg,
100                              SoupSessionCallback callback, gpointer user_data);
101 static void requeue_message (SoupSession *session, SoupMessage *msg);
102 static void cancel_message  (SoupSession *session, SoupMessage *msg,
103                              guint status_code);
104
105 static void auth_manager_authenticate (SoupAuthManager *manager,
106                                        SoupMessage *msg, SoupAuth *auth,
107                                        gboolean retrying, gpointer user_data);
108
109 /* temporary until we fix this to index hosts by SoupAddress */
110 extern guint     soup_uri_host_hash  (gconstpointer  key);
111 extern gboolean  soup_uri_host_equal (gconstpointer  v1,
112                                       gconstpointer  v2);
113 extern SoupURI  *soup_uri_copy_root  (SoupURI *uri);
114
115 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
116 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
117
118 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
119
120 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
121
122 enum {
123         REQUEST_QUEUED,
124         REQUEST_STARTED,
125         REQUEST_UNQUEUED,
126         AUTHENTICATE,
127         LAST_SIGNAL
128 };
129
130 static guint signals[LAST_SIGNAL] = { 0 };
131
132 enum {
133         PROP_0,
134
135         PROP_PROXY_URI,
136         PROP_MAX_CONNS,
137         PROP_MAX_CONNS_PER_HOST,
138         PROP_USE_NTLM,
139         PROP_SSL_CA_FILE,
140         PROP_ASYNC_CONTEXT,
141         PROP_TIMEOUT,
142         PROP_USER_AGENT,
143         PROP_IDLE_TIMEOUT,
144         PROP_ADD_FEATURE,
145         PROP_ADD_FEATURE_BY_TYPE,
146         PROP_REMOVE_FEATURE_BY_TYPE,
147
148         LAST_PROP
149 };
150
151 static void set_property (GObject *object, guint prop_id,
152                           const GValue *value, GParamSpec *pspec);
153 static void get_property (GObject *object, guint prop_id,
154                           GValue *value, GParamSpec *pspec);
155
156 static void
157 soup_session_init (SoupSession *session)
158 {
159         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
160
161         priv->queue = soup_message_queue_new ();
162
163         priv->host_lock = g_mutex_new ();
164         priv->hosts = g_hash_table_new (soup_uri_host_hash,
165                                         soup_uri_host_equal);
166         priv->conns = g_hash_table_new (NULL, NULL);
167
168         priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
169         priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
170
171         priv->auth_manager = g_object_new (SOUP_TYPE_AUTH_MANAGER_NTLM,
172                                            SOUP_AUTH_MANAGER_NTLM_USE_NTLM, FALSE,
173                                            NULL);
174         g_signal_connect (priv->auth_manager, "authenticate",
175                           G_CALLBACK (auth_manager_authenticate), session);
176         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
177         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
178         soup_session_add_feature (session, SOUP_SESSION_FEATURE (priv->auth_manager));
179 }
180
181 static gboolean
182 foreach_free_host (gpointer key, gpointer host, gpointer data)
183 {
184         free_host (host);
185         return TRUE;
186 }
187
188 static void
189 cleanup_hosts (SoupSessionPrivate *priv)
190 {
191         GHashTable *old_hosts;
192
193         g_mutex_lock (priv->host_lock);
194         old_hosts = priv->hosts;
195         priv->hosts = g_hash_table_new (soup_uri_host_hash,
196                                         soup_uri_host_equal);
197         g_mutex_unlock (priv->host_lock);
198
199         g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
200         g_hash_table_destroy (old_hosts);
201 }
202
203 static void
204 dispose (GObject *object)
205 {
206         SoupSession *session = SOUP_SESSION (object);
207         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
208
209         soup_session_abort (session);
210         cleanup_hosts (priv);
211
212         while (priv->features)
213                 soup_session_remove_feature (session, priv->features->data);
214
215         G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
216 }
217
218 static void
219 finalize (GObject *object)
220 {
221         SoupSession *session = SOUP_SESSION (object);
222         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
223
224         soup_message_queue_destroy (priv->queue);
225
226         g_mutex_free (priv->host_lock);
227         g_hash_table_destroy (priv->hosts);
228         g_hash_table_destroy (priv->conns);
229
230         g_free (priv->user_agent);
231
232         if (priv->auth_manager)
233                 g_object_unref (priv->auth_manager);
234
235         if (priv->proxy_uri)
236                 soup_uri_free (priv->proxy_uri);
237
238         if (priv->ssl_creds)
239                 soup_ssl_free_client_credentials (priv->ssl_creds);
240
241         if (priv->async_context)
242                 g_main_context_unref (priv->async_context);
243
244         G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
245 }
246
247 static void
248 soup_session_class_init (SoupSessionClass *session_class)
249 {
250         GObjectClass *object_class = G_OBJECT_CLASS (session_class);
251
252         g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
253
254         /* virtual method definition */
255         session_class->queue_message = queue_message;
256         session_class->requeue_message = requeue_message;
257         session_class->cancel_message = cancel_message;
258
259         /* virtual method override */
260         object_class->dispose = dispose;
261         object_class->finalize = finalize;
262         object_class->set_property = set_property;
263         object_class->get_property = get_property;
264
265         /* signals */
266
267         /**
268          * SoupSession::request-queued:
269          * @session: the session
270          * @msg: the request that was queued
271          *
272          * Emitted when a request is queued on @session. (Note that
273          * "queued" doesn't just mean soup_session_queue_message();
274          * soup_session_send_message() implicitly queues the message
275          * as well.)
276          *
277          * When sending a request, first #SoupSession::request_queued
278          * is emitted, indicating that the session has become aware of
279          * the request.
280          *
281          * Once a connection is available to send the request on, the
282          * session emits #SoupSession::request_started. Then, various
283          * #SoupMessage signals are emitted as the message is
284          * processed. If the message is requeued, it will emit
285          * #SoupMessage::restarted, which will then be followed by
286          * another #SoupSession::request_started and another set of
287          * #SoupMessage signals when the message is re-sent.
288          *
289          * Eventually, the message will emit #SoupMessage::finished.
290          * Normally, this signals the completion of message
291          * processing. However, it is possible that the application
292          * will requeue the message from the "finished" handler (or
293          * equivalently, from the soup_session_queue_message()
294          * callback). In that case, the process will loop back to
295          * #SoupSession::request_started.
296          *
297          * Eventually, a message will reach "finished" and not be
298          * requeued. At that point, the session will emit
299          * #SoupSession::request_unqueued to indicate that it is done
300          * with the message.
301          *
302          * To sum up: #SoupSession::request_queued and
303          * #SoupSession::request_unqueued are guaranteed to be emitted
304          * exactly once, but #SoupSession::request_started and
305          * #SoupMessage::finished (and all of the other #SoupMessage
306          * signals) may be invoked multiple times for a given message.
307          **/
308         signals[REQUEST_QUEUED] =
309                 g_signal_new ("request-queued",
310                               G_OBJECT_CLASS_TYPE (object_class),
311                               G_SIGNAL_RUN_FIRST,
312                               0, /* FIXME? */
313                               NULL, NULL,
314                               soup_marshal_NONE__OBJECT,
315                               G_TYPE_NONE, 1,
316                               SOUP_TYPE_MESSAGE);
317
318         /**
319          * SoupSession::request-started:
320          * @session: the session
321          * @msg: the request being sent
322          * @socket: the socket the request is being sent on
323          *
324          * Emitted just before a request is sent. See
325          * #SoupSession::request_queued for a detailed description of
326          * the message lifecycle within a session.
327          **/
328         signals[REQUEST_STARTED] =
329                 g_signal_new ("request-started",
330                               G_OBJECT_CLASS_TYPE (object_class),
331                               G_SIGNAL_RUN_FIRST,
332                               G_STRUCT_OFFSET (SoupSessionClass, request_started),
333                               NULL, NULL,
334                               soup_marshal_NONE__OBJECT_OBJECT,
335                               G_TYPE_NONE, 2,
336                               SOUP_TYPE_MESSAGE,
337                               SOUP_TYPE_SOCKET);
338
339         /**
340          * SoupSession::request-unqueued:
341          * @session: the session
342          * @msg: the request that was unqueued
343          *
344          * Emitted when a request is removed from @session's queue,
345          * indicating that @session is done with it. See
346          * #SoupSession::request_queued for a detailed description of the
347          * message lifecycle within a session.
348          **/
349         signals[REQUEST_UNQUEUED] =
350                 g_signal_new ("request-unqueued",
351                               G_OBJECT_CLASS_TYPE (object_class),
352                               G_SIGNAL_RUN_FIRST,
353                               0, /* FIXME? */
354                               NULL, NULL,
355                               soup_marshal_NONE__OBJECT,
356                               G_TYPE_NONE, 1,
357                               SOUP_TYPE_MESSAGE);
358
359         /**
360          * SoupSession::authenticate:
361          * @session: the session
362          * @msg: the #SoupMessage being sent
363          * @auth: the #SoupAuth to authenticate
364          * @retrying: %TRUE if this is the second (or later) attempt
365          *
366          * Emitted when the session requires authentication. If
367          * credentials are available call soup_auth_authenticate() on
368          * @auth. If these credentials fail, the signal will be
369          * emitted again, with @retrying set to %TRUE, which will
370          * continue until you return without calling
371          * soup_auth_authenticate() on @auth.
372          *
373          * Note that this may be emitted before @msg's body has been
374          * fully read.
375          *
376          * If you call soup_session_pause_message() on @msg before
377          * returning, then you can authenticate @auth asynchronously
378          * (as long as you g_object_ref() it to make sure it doesn't
379          * get destroyed), and then unpause @msg when you are ready
380          * for it to continue.
381          **/
382         signals[AUTHENTICATE] =
383                 g_signal_new ("authenticate",
384                               G_OBJECT_CLASS_TYPE (object_class),
385                               G_SIGNAL_RUN_FIRST,
386                               G_STRUCT_OFFSET (SoupSessionClass, authenticate),
387                               NULL, NULL,
388                               soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
389                               G_TYPE_NONE, 3,
390                               SOUP_TYPE_MESSAGE,
391                               SOUP_TYPE_AUTH,
392                               G_TYPE_BOOLEAN);
393
394         /* properties */
395         g_object_class_install_property (
396                 object_class, PROP_PROXY_URI,
397                 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
398                                     "Proxy URI",
399                                     "The HTTP Proxy to use for this session",
400                                     SOUP_TYPE_URI,
401                                     G_PARAM_READWRITE));
402         g_object_class_install_property (
403                 object_class, PROP_MAX_CONNS,
404                 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
405                                   "Max Connection Count",
406                                   "The maximum number of connections that the session can open at once",
407                                   1,
408                                   G_MAXINT,
409                                   SOUP_SESSION_MAX_CONNS_DEFAULT,
410                                   G_PARAM_READWRITE));
411         g_object_class_install_property (
412                 object_class, PROP_MAX_CONNS_PER_HOST,
413                 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
414                                   "Max Per-Host Connection Count",
415                                   "The maximum number of connections that the session can open at once to a given host",
416                                   1,
417                                   G_MAXINT,
418                                   SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
419                                   G_PARAM_READWRITE));
420         g_object_class_install_property (
421                 object_class, PROP_IDLE_TIMEOUT,
422                 g_param_spec_uint (SOUP_SESSION_IDLE_TIMEOUT,
423                                    "Idle Timeout",
424                                    "Connection lifetime when idle",
425                                    0, G_MAXUINT, 0,
426                                    G_PARAM_READWRITE));
427         g_object_class_install_property (
428                 object_class, PROP_USE_NTLM,
429                 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
430                                       "Use NTLM",
431                                       "Whether or not to use NTLM authentication",
432                                       FALSE,
433                                       G_PARAM_READWRITE));
434         g_object_class_install_property (
435                 object_class, PROP_SSL_CA_FILE,
436                 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
437                                      "SSL CA file",
438                                      "File containing SSL CA certificates",
439                                      NULL,
440                                      G_PARAM_READWRITE));
441         g_object_class_install_property (
442                 object_class, PROP_ASYNC_CONTEXT,
443                 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
444                                       "Async GMainContext",
445                                       "The GMainContext to dispatch async I/O in",
446                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
447         g_object_class_install_property (
448                 object_class, PROP_TIMEOUT,
449                 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
450                                    "Timeout value",
451                                    "Value in seconds to timeout a blocking I/O",
452                                    0, G_MAXUINT, 0,
453                                    G_PARAM_READWRITE));
454
455         /**
456          * SoupSession:user-agent:
457          *
458          * If non-%NULL, the value to use for the "User-Agent" header
459          * on #SoupMessage<!-- -->s sent from this session.
460          *
461          * RFC 2616 says: "The User-Agent request-header field
462          * contains information about the user agent originating the
463          * request. This is for statistical purposes, the tracing of
464          * protocol violations, and automated recognition of user
465          * agents for the sake of tailoring responses to avoid
466          * particular user agent limitations. User agents SHOULD
467          * include this field with requests."
468          *
469          * The User-Agent header contains a list of one or more
470          * product tokens, separated by whitespace, with the most
471          * significant product token coming first. The tokens must be
472          * brief, ASCII, and mostly alphanumeric (although "-", "_",
473          * and "." are also allowed), and may optionally include a "/"
474          * followed by a version string. You may also put comments,
475          * enclosed in parentheses, between or after the tokens.
476          *
477          * If you set a %user_agent property that has trailing
478          * whitespace, #SoupSession will append its own product token
479          * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
480          * header for you.
481          **/
482         g_object_class_install_property (
483                 object_class, PROP_USER_AGENT,
484                 g_param_spec_string (SOUP_SESSION_USER_AGENT,
485                                      "User-Agent string",
486                                      "User-Agent string",
487                                      NULL,
488                                      G_PARAM_READWRITE));
489
490         g_object_class_install_property (
491                 object_class, PROP_ADD_FEATURE,
492                 g_param_spec_object (SOUP_SESSION_ADD_FEATURE,
493                                      "Add Feature",
494                                      "Add a feature object to the session",
495                                      SOUP_TYPE_SESSION_FEATURE,
496                                      G_PARAM_READWRITE));
497         g_object_class_install_property (
498                 object_class, PROP_ADD_FEATURE_BY_TYPE,
499                 g_param_spec_gtype (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
500                                     "Add Feature By Type",
501                                     "Add a feature object of the given type to the session",
502                                     SOUP_TYPE_SESSION_FEATURE,
503                                     G_PARAM_READWRITE));
504         g_object_class_install_property (
505                 object_class, PROP_REMOVE_FEATURE_BY_TYPE,
506                 g_param_spec_gtype (SOUP_SESSION_REMOVE_FEATURE_BY_TYPE,
507                                     "Remove Feature By Type",
508                                     "Remove features of the given type from the session",
509                                     SOUP_TYPE_SESSION_FEATURE,
510                                     G_PARAM_READWRITE));
511 }
512
513 static gboolean
514 safe_uri_equal (SoupURI *a, SoupURI *b)
515 {
516         if (!a && !b)
517                 return TRUE;
518
519         if ((a && !b) || (b && !a))
520                 return FALSE;
521
522         return soup_uri_equal (a, b);
523 }
524
525 static gboolean
526 safe_str_equal (const char *a, const char *b)
527 {
528         if (!a && !b)
529                 return TRUE;
530
531         if ((a && !b) || (b && !a))
532                 return FALSE;
533
534         return strcmp (a, b) == 0;
535 }
536
537 static void
538 set_property (GObject *object, guint prop_id,
539               const GValue *value, GParamSpec *pspec)
540 {
541         SoupSession *session = SOUP_SESSION (object);
542         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
543         SoupURI *uri;
544         gboolean need_abort = FALSE;
545         gboolean ca_file_changed = FALSE;
546         const char *new_ca_file, *user_agent;
547
548         switch (prop_id) {
549         case PROP_PROXY_URI:
550                 uri = g_value_get_boxed (value);
551
552                 if (!safe_uri_equal (priv->proxy_uri, uri))
553                         need_abort = TRUE;
554
555                 if (priv->proxy_uri)
556                         soup_uri_free (priv->proxy_uri);
557
558                 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
559
560                 if (need_abort) {
561                         soup_session_abort (session);
562                         cleanup_hosts (priv);
563                 }
564
565                 break;
566         case PROP_MAX_CONNS:
567                 priv->max_conns = g_value_get_int (value);
568                 break;
569         case PROP_MAX_CONNS_PER_HOST:
570                 priv->max_conns_per_host = g_value_get_int (value);
571                 break;
572         case PROP_USE_NTLM:
573                 g_object_set_property (G_OBJECT (priv->auth_manager),
574                                        SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
575                                        value);
576                 break;
577         case PROP_SSL_CA_FILE:
578                 new_ca_file = g_value_get_string (value);
579
580                 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
581                         ca_file_changed = TRUE;
582
583                 g_free (priv->ssl_ca_file);
584                 priv->ssl_ca_file = g_strdup (new_ca_file);
585
586                 if (ca_file_changed) {
587                         if (priv->ssl_creds) {
588                                 soup_ssl_free_client_credentials (priv->ssl_creds);
589                                 priv->ssl_creds = NULL;
590                         }
591
592                         cleanup_hosts (priv);
593                 }
594
595                 break;
596         case PROP_ASYNC_CONTEXT:
597                 priv->async_context = g_value_get_pointer (value);
598                 if (priv->async_context)
599                         g_main_context_ref (priv->async_context);
600                 break;
601         case PROP_TIMEOUT:
602                 priv->io_timeout = g_value_get_uint (value);
603                 break;
604         case PROP_USER_AGENT:
605                 g_free (priv->user_agent);
606                 user_agent = g_value_get_string (value);
607                 if (!user_agent)
608                         priv->user_agent = NULL;
609                 else if (!*user_agent) {
610                         priv->user_agent =
611                                 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
612                 } else if (g_str_has_suffix (user_agent, " ")) {
613                         priv->user_agent =
614                                 g_strdup_printf ("%s%s", user_agent,
615                                                  SOUP_SESSION_USER_AGENT_BASE);
616                 } else
617                         priv->user_agent = g_strdup (user_agent);
618                 break;
619         case PROP_IDLE_TIMEOUT:
620                 priv->idle_timeout = g_value_get_uint (value);
621                 break;
622         case PROP_ADD_FEATURE:
623                 soup_session_add_feature (session, g_value_get_object (value));
624                 break;
625         case PROP_ADD_FEATURE_BY_TYPE:
626                 soup_session_add_feature_by_type (session, g_value_get_gtype (value));
627                 break;
628         case PROP_REMOVE_FEATURE_BY_TYPE:
629                 soup_session_remove_feature_by_type (session, g_value_get_gtype (value));
630                 break;
631         default:
632                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
633                 break;
634         }
635 }
636
637 static void
638 get_property (GObject *object, guint prop_id,
639               GValue *value, GParamSpec *pspec)
640 {
641         SoupSession *session = SOUP_SESSION (object);
642         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
643
644         switch (prop_id) {
645         case PROP_PROXY_URI:
646                 g_value_set_boxed (value, priv->proxy_uri);
647                 break;
648         case PROP_MAX_CONNS:
649                 g_value_set_int (value, priv->max_conns);
650                 break;
651         case PROP_MAX_CONNS_PER_HOST:
652                 g_value_set_int (value, priv->max_conns_per_host);
653                 break;
654         case PROP_USE_NTLM:
655                 g_object_get_property (G_OBJECT (priv->auth_manager),
656                                        SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
657                                        value);
658                 break;
659         case PROP_SSL_CA_FILE:
660                 g_value_set_string (value, priv->ssl_ca_file);
661                 break;
662         case PROP_ASYNC_CONTEXT:
663                 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
664                 break;
665         case PROP_TIMEOUT:
666                 g_value_set_uint (value, priv->io_timeout);
667                 break;
668         case PROP_USER_AGENT:
669                 g_value_set_string (value, priv->user_agent);
670                 break;
671         case PROP_IDLE_TIMEOUT:
672                 g_value_set_uint (value, priv->idle_timeout);
673                 break;
674         default:
675                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
676                 break;
677         }
678 }
679
680
681 /**
682  * soup_session_get_async_context:
683  * @session: a #SoupSession
684  *
685  * Gets @session's async_context. This does not add a ref to the
686  * context, so you will need to ref it yourself if you want it to
687  * outlive its session.
688  *
689  * Return value: @session's #GMainContext, which may be %NULL
690  **/
691 GMainContext *
692 soup_session_get_async_context (SoupSession *session)
693 {
694         SoupSessionPrivate *priv;
695
696         g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
697         priv = SOUP_SESSION_GET_PRIVATE (session);
698
699         return priv->async_context;
700 }
701
702 /* Hosts */
703
704 static SoupSessionHost *
705 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
706 {
707         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
708         SoupSessionHost *host;
709
710         host = g_slice_new0 (SoupSessionHost);
711         host->root_uri = soup_uri_copy_root (source_uri);
712
713         if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
714             !priv->ssl_creds) {
715                 priv->ssl_creds =
716                         soup_ssl_get_client_credentials (priv->ssl_ca_file);
717         }
718
719         return host;
720 }
721
722 /* Note: get_host_for_message doesn't lock the host_lock. The caller
723  * must do it itself if there's a chance the host doesn't already
724  * exist.
725  */
726 static SoupSessionHost *
727 get_host_for_message (SoupSession *session, SoupMessage *msg)
728 {
729         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
730         SoupSessionHost *host;
731         SoupURI *source = soup_message_get_uri (msg);
732
733         host = g_hash_table_lookup (priv->hosts, source);
734         if (host)
735                 return host;
736
737         host = soup_session_host_new (session, source);
738         g_hash_table_insert (priv->hosts, host->root_uri, host);
739
740         return host;
741 }
742
743 static void
744 free_host (SoupSessionHost *host)
745 {
746         while (host->connections) {
747                 SoupConnection *conn = host->connections->data;
748
749                 host->connections = g_slist_remove (host->connections, conn);
750                 soup_connection_disconnect (conn);
751         }
752
753         soup_uri_free (host->root_uri);
754         g_slice_free (SoupSessionHost, host);
755 }       
756
757 static void
758 auth_manager_authenticate (SoupAuthManager *manager, SoupMessage *msg,
759                            SoupAuth *auth, gboolean retrying,
760                            gpointer session)
761 {
762         g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
763 }
764
765 #define SOUP_METHOD_IS_SAFE(method) (method == SOUP_METHOD_GET || \
766                                      method == SOUP_METHOD_HEAD || \
767                                      method == SOUP_METHOD_OPTIONS || \
768                                      method == SOUP_METHOD_PROPFIND)
769
770 static void
771 redirect_handler (SoupMessage *msg, gpointer user_data)
772 {
773         SoupSession *session = user_data;
774         const char *new_loc;
775         SoupURI *new_uri;
776
777         new_loc = soup_message_headers_get (msg->response_headers, "Location");
778         g_return_if_fail (new_loc != NULL);
779
780         if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
781             (msg->status_code == SOUP_STATUS_FOUND &&
782              !SOUP_METHOD_IS_SAFE (msg->method))) {
783                 /* Redirect using a GET */
784                 g_object_set (msg,
785                               SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
786                               NULL);
787                 soup_message_set_request (msg, NULL,
788                                           SOUP_MEMORY_STATIC, NULL, 0);
789                 soup_message_headers_set_encoding (msg->request_headers,
790                                                    SOUP_ENCODING_NONE);
791         } else if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
792                    msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT ||
793                    msg->status_code == SOUP_STATUS_FOUND) {
794                 /* Don't redirect non-safe methods */
795                 if (!SOUP_METHOD_IS_SAFE (msg->method))
796                         return;
797         } else {
798                 /* Three possibilities:
799                  *
800                  *   1) This was a non-3xx response that happened to
801                  *      have a "Location" header
802                  *   2) It's a non-redirecty 3xx response (300, 304,
803                  *      305, 306)
804                  *   3) It's some newly-defined 3xx response (308+)
805                  *
806                  * We ignore all of these cases. In the first two,
807                  * redirecting would be explicitly wrong, and in the
808                  * last case, we have no clue if the 3xx response is
809                  * supposed to be redirecty or non-redirecty. Plus,
810                  * 2616 says unrecognized status codes should be
811                  * treated as the equivalent to the x00 code, and we
812                  * don't redirect on 300, so therefore we shouldn't
813                  * redirect on 308+ either.
814                  */
815                 return;
816         }
817
818         /* Location is supposed to be an absolute URI, but some sites
819          * are lame, so we use soup_uri_new_with_base().
820          */
821         new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
822         if (!new_uri) {
823                 soup_message_set_status_full (msg,
824                                               SOUP_STATUS_MALFORMED,
825                                               "Invalid Redirect URL");
826                 return;
827         }
828
829         soup_message_set_uri (msg, new_uri);
830         soup_uri_free (new_uri);
831
832         soup_session_requeue_message (session, msg);
833 }
834
835 static void
836 connection_started_request (SoupConnection *conn, SoupMessage *msg,
837                             gpointer data)
838 {
839         SoupSession *session = data;
840         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
841
842         if (priv->user_agent) {
843                 soup_message_headers_replace (msg->request_headers,
844                                               "User-Agent", priv->user_agent);
845         }
846
847         /* Kludge to deal with the fact that CONNECT msgs come from the
848          * SoupConnection rather than being queued normally.
849          */
850         if (msg->method == SOUP_METHOD_CONNECT)
851                 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
852
853         g_signal_emit (session, signals[REQUEST_STARTED], 0,
854                        msg, soup_connection_get_socket (conn));
855 }
856
857 gboolean
858 soup_session_try_prune_connection (SoupSession *session)
859 {
860         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
861         GPtrArray *conns;
862         GHashTableIter iter;
863         gpointer conn, host;
864         int i;
865
866         conns = g_ptr_array_new ();
867
868         g_mutex_lock (priv->host_lock);
869         g_hash_table_iter_init (&iter, priv->conns);
870         while (g_hash_table_iter_next (&iter, &conn, &host)) {
871                 /* Don't prune a connection that is currently in use,
872                  * or hasn't been used yet.
873                  */
874                 if (!soup_connection_is_in_use (conn) &&
875                     soup_connection_last_used (conn) > 0)
876                         g_ptr_array_add (conns, g_object_ref (conn));
877         }
878         g_mutex_unlock (priv->host_lock);
879
880         if (!conns->len) {
881                 g_ptr_array_free (conns, TRUE);
882                 return FALSE;
883         }
884
885         for (i = 0; i < conns->len; i++) {
886                 soup_connection_disconnect (conns->pdata[i]);
887                 g_object_unref (conns->pdata[i]);
888         }
889         g_ptr_array_free (conns, TRUE);
890
891         return TRUE;
892 }
893
894 static void
895 connection_disconnected (SoupConnection *conn, gpointer user_data)
896 {
897         SoupSession *session = user_data;
898         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
899         SoupSessionHost *host;
900
901         g_mutex_lock (priv->host_lock);
902
903         host = g_hash_table_lookup (priv->conns, conn);
904         if (host) {
905                 g_hash_table_remove (priv->conns, conn);
906                 host->connections = g_slist_remove (host->connections, conn);
907                 host->num_conns--;
908         }
909
910         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
911         priv->num_conns--;
912
913         g_mutex_unlock (priv->host_lock);
914         g_object_unref (conn);
915 }
916
917 static void
918 connect_result (SoupConnection *conn, guint status, gpointer user_data)
919 {
920         SoupSession *session = user_data;
921         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
922         SoupSessionHost *host;
923         SoupMessageQueueIter iter;
924         SoupMessage *msg;
925
926         g_mutex_lock (priv->host_lock);
927
928         host = g_hash_table_lookup (priv->conns, conn);
929         if (!host) {
930                 g_mutex_unlock (priv->host_lock);
931                 return;
932         }
933
934         if (status == SOUP_STATUS_OK) {
935                 soup_connection_reserve (conn);
936                 host->connections = g_slist_prepend (host->connections, conn);
937                 g_mutex_unlock (priv->host_lock);
938                 return;
939         }
940
941         /* The connection failed. */
942         g_mutex_unlock (priv->host_lock);
943         connection_disconnected (conn, session);
944
945         if (host->connections) {
946                 /* Something went wrong this time, but we have at
947                  * least one open connection to this host. So just
948                  * leave the message in the queue so it can use that
949                  * connection once it's free.
950                  */
951                 return;
952         }
953
954         /* There are two possibilities: either status is
955          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
956          * will create a new connection (and all we need to do here
957          * is downgrade the message from CONNECTING to QUEUED); or
958          * status is something else, probably CANT_CONNECT or
959          * CANT_RESOLVE or the like, in which case we need to cancel
960          * any messages waiting for this host, since they're out
961          * of luck.
962          */
963         g_object_ref (session);
964         for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
965                 if (get_host_for_message (session, msg) == host) {
966                         if (status == SOUP_STATUS_TRY_AGAIN) {
967                                 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
968                                         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
969                         } else {
970                                 soup_session_cancel_message (session, msg,
971                                                              status);
972                         }
973                 }
974         }
975         g_object_unref (session);
976 }
977
978 /**
979  * soup_session_get_connection:
980  * @session: a #SoupSession
981  * @msg: a #SoupMessage
982  * @try_pruning: on return, whether or not to try pruning a connection
983  * @is_new: on return, %TRUE if the returned connection is new and not
984  * yet connected
985  * 
986  * Tries to find or create a connection for @msg; this is an internal
987  * method for #SoupSession subclasses.
988  *
989  * If there is an idle connection to the relevant host available, then
990  * that connection will be returned (with *@is_new set to %FALSE). The
991  * connection will be marked "reserved", so the caller must call
992  * soup_connection_release() if it ends up not using the connection
993  * right away.
994  *
995  * If there is no idle connection available, but it is possible to
996  * create a new connection, then one will be created and returned,
997  * with *@is_new set to %TRUE. The caller MUST then call
998  * soup_connection_connect_sync() or soup_connection_connect_async()
999  * to connect it. If the connection attempt succeeds, the connection
1000  * will be marked "reserved" and added to @session's connection pool
1001  * once it connects. If the connection attempt fails, the connection
1002  * will be unreffed.
1003  *
1004  * If no connection is available and a new connection cannot be made,
1005  * soup_session_get_connection() will return %NULL. If @session has
1006  * the maximum number of open connections open, but does not have the
1007  * maximum number of per-host connections open to the relevant host,
1008  * then *@try_pruning will be set to %TRUE. In this case, the caller
1009  * can call soup_session_try_prune_connection() to close an idle
1010  * connection, and then try soup_session_get_connection() again. (If
1011  * calling soup_session_try_prune_connection() wouldn't help, then
1012  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1013  *
1014  * Return value: a #SoupConnection, or %NULL
1015  **/
1016 SoupConnection *
1017 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1018                              gboolean *try_pruning, gboolean *is_new)
1019 {
1020         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1021         SoupConnection *conn;
1022         SoupSessionHost *host;
1023         GSList *conns;
1024
1025         g_mutex_lock (priv->host_lock);
1026
1027         host = get_host_for_message (session, msg);
1028         for (conns = host->connections; conns; conns = conns->next) {
1029                 if (!soup_connection_is_in_use (conns->data)) {
1030                         soup_connection_reserve (conns->data);
1031                         g_mutex_unlock (priv->host_lock);
1032                         *is_new = FALSE;
1033                         return conns->data;
1034                 }
1035         }
1036
1037         if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
1038                 /* We already started a connection for this
1039                  * message, so don't start another one.
1040                  */
1041                 g_mutex_unlock (priv->host_lock);
1042                 return NULL;
1043         }
1044
1045         if (host->num_conns >= priv->max_conns_per_host) {
1046                 g_mutex_unlock (priv->host_lock);
1047                 return NULL;
1048         }
1049
1050         if (priv->num_conns >= priv->max_conns) {
1051                 *try_pruning = TRUE;
1052                 g_mutex_unlock (priv->host_lock);
1053                 return NULL;
1054         }
1055
1056         conn = soup_connection_new (
1057                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1058                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1059                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1060                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1061                 SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
1062                 SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
1063                 NULL);
1064         g_signal_connect (conn, "connect_result",
1065                           G_CALLBACK (connect_result),
1066                           session);
1067         g_signal_connect (conn, "disconnected",
1068                           G_CALLBACK (connection_disconnected),
1069                           session);
1070         g_signal_connect (conn, "request_started",
1071                           G_CALLBACK (connection_started_request),
1072                           session);
1073
1074         g_hash_table_insert (priv->conns, conn, host);
1075
1076         /* We increment the connection counts so it counts against the
1077          * totals, but we don't add it to the host's connection list
1078          * yet, since it's not ready for use.
1079          */
1080         priv->num_conns++;
1081         host->num_conns++;
1082
1083         /* Mark the request as connecting, so we don't try to open
1084          * another new connection for it while waiting for this one.
1085          */
1086         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
1087
1088         g_mutex_unlock (priv->host_lock);
1089         *is_new = TRUE;
1090         return conn;
1091 }
1092
1093 SoupMessageQueue *
1094 soup_session_get_queue (SoupSession *session)
1095 {
1096         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1097
1098         return priv->queue;
1099 }
1100
1101 static void
1102 message_finished (SoupMessage *msg, gpointer user_data)
1103 {
1104         SoupSession *session = user_data;
1105         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1106
1107         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1108                 soup_message_queue_remove_message (priv->queue, msg);
1109                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1110                 g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
1111                 g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
1112         }
1113 }
1114
1115 static void
1116 queue_message (SoupSession *session, SoupMessage *msg,
1117                SoupSessionCallback callback, gpointer user_data)
1118 {
1119         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1120
1121         g_signal_connect_after (msg, "finished",
1122                                 G_CALLBACK (message_finished), session);
1123
1124         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1125                 soup_message_add_header_handler (
1126                         msg, "got_body", "Location",
1127                         G_CALLBACK (redirect_handler), session);
1128         }
1129
1130         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1131         soup_message_queue_append (priv->queue, msg);
1132
1133         g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
1134 }
1135
1136 /**
1137  * SoupSessionCallback:
1138  * @session: the session
1139  * @msg: the message that has finished
1140  * @user_data: the data passed to soup_session_queue_message
1141  *
1142  * Prototype for the callback passed to soup_session_queue_message(),
1143  * qv.
1144  **/
1145
1146 /**
1147  * soup_session_queue_message:
1148  * @session: a #SoupSession
1149  * @msg: the message to queue
1150  * @callback: a #SoupSessionCallback which will be called after the
1151  * message completes or when an unrecoverable error occurs.
1152  * @user_data: a pointer passed to @callback.
1153  * 
1154  * Queues the message @msg for sending. All messages are processed
1155  * while the glib main loop runs. If @msg has been processed before,
1156  * any resources related to the time it was last sent are freed.
1157  *
1158  * Upon message completion, the callback specified in @callback will
1159  * be invoked (in the thread associated with @session's async
1160  * context). If after returning from this callback the message has not
1161  * been requeued, @msg will be unreffed.
1162  */
1163 void
1164 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1165                             SoupSessionCallback callback, gpointer user_data)
1166 {
1167         g_return_if_fail (SOUP_IS_SESSION (session));
1168         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1169
1170         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1171                                                          callback, user_data);
1172 }
1173
1174 static void
1175 requeue_message (SoupSession *session, SoupMessage *msg)
1176 {
1177         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1178 }
1179
1180 /**
1181  * soup_session_requeue_message:
1182  * @session: a #SoupSession
1183  * @msg: the message to requeue
1184  *
1185  * This causes @msg to be placed back on the queue to be attempted
1186  * again.
1187  **/
1188 void
1189 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1190 {
1191         g_return_if_fail (SOUP_IS_SESSION (session));
1192         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1193
1194         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1195 }
1196
1197
1198 /**
1199  * soup_session_send_message:
1200  * @session: a #SoupSession
1201  * @msg: the message to send
1202  * 
1203  * Synchronously send @msg. This call will not return until the
1204  * transfer is finished successfully or there is an unrecoverable
1205  * error.
1206  *
1207  * @msg is not freed upon return.
1208  *
1209  * Return value: the HTTP status code of the response
1210  */
1211 guint
1212 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1213 {
1214         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1215         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1216
1217         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1218 }
1219
1220
1221 /**
1222  * soup_session_pause_message:
1223  * @session: a #SoupSession
1224  * @msg: a #SoupMessage currently running on @session
1225  *
1226  * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1227  * resume I/O.
1228  **/
1229 void
1230 soup_session_pause_message (SoupSession *session,
1231                             SoupMessage *msg)
1232 {
1233         g_return_if_fail (SOUP_IS_SESSION (session));
1234         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1235
1236         soup_message_io_pause (msg);
1237 }
1238
1239 /**
1240  * soup_session_unpause_message:
1241  * @session: a #SoupSession
1242  * @msg: a #SoupMessage currently running on @session
1243  *
1244  * Resumes HTTP I/O on @msg. Use this to resume after calling
1245  * soup_sessino_pause_message().
1246  *
1247  * If @msg is being sent via blocking I/O, this will resume reading or
1248  * writing immediately. If @msg is using non-blocking I/O, then
1249  * reading or writing won't resume until you return to the main loop.
1250  **/
1251 void
1252 soup_session_unpause_message (SoupSession *session,
1253                               SoupMessage *msg)
1254 {
1255         g_return_if_fail (SOUP_IS_SESSION (session));
1256         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1257
1258         soup_message_io_unpause (msg);
1259 }
1260
1261
1262 static void
1263 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1264 {
1265         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1266
1267         soup_message_queue_remove_message (priv->queue, msg);
1268         soup_message_io_stop (msg);
1269         soup_message_set_status (msg, status_code);
1270         soup_message_finished (msg);
1271 }
1272
1273 /**
1274  * soup_session_cancel_message:
1275  * @session: a #SoupSession
1276  * @msg: the message to cancel
1277  * @status_code: status code to set on @msg (generally
1278  * %SOUP_STATUS_CANCELLED)
1279  *
1280  * Causes @session to immediately finish processing @msg, with a final
1281  * status_code of @status_code. Depending on when you cancel it, the
1282  * response state may be incomplete or inconsistent.
1283  **/
1284 void
1285 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1286                              guint status_code)
1287 {
1288         g_return_if_fail (SOUP_IS_SESSION (session));
1289         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1290
1291         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1292 }
1293
1294 static void
1295 gather_conns (gpointer key, gpointer host, gpointer data)
1296 {
1297         SoupConnection *conn = key;
1298         GSList **conns = data;
1299
1300         *conns = g_slist_prepend (*conns, conn);
1301 }
1302
1303 /**
1304  * soup_session_abort:
1305  * @session: the session
1306  *
1307  * Cancels all pending requests in @session.
1308  **/
1309 void
1310 soup_session_abort (SoupSession *session)
1311 {
1312         SoupSessionPrivate *priv;
1313         SoupMessageQueueIter iter;
1314         SoupMessage *msg;
1315         GSList *conns, *c;
1316
1317         g_return_if_fail (SOUP_IS_SESSION (session));
1318         priv = SOUP_SESSION_GET_PRIVATE (session);
1319
1320         for (msg = soup_message_queue_first (priv->queue, &iter);
1321              msg;
1322              msg = soup_message_queue_next (priv->queue, &iter)) {
1323                 soup_session_cancel_message (session, msg,
1324                                              SOUP_STATUS_CANCELLED);
1325         }
1326
1327         /* Close all connections */
1328         g_mutex_lock (priv->host_lock);
1329         conns = NULL;
1330         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1331
1332         for (c = conns; c; c = c->next)
1333                 g_object_ref (c->data);
1334         g_mutex_unlock (priv->host_lock);
1335         for (c = conns; c; c = c->next) {
1336                 soup_connection_disconnect (c->data);
1337                 g_object_unref (c->data);
1338         }
1339
1340         g_slist_free (conns);
1341 }
1342
1343 /**
1344  * soup_session_add_feature:
1345  * @session: a #SoupSession
1346  * @feature: an object that implements #SoupSessionFeature
1347  *
1348  * Adds @feature's functionality to @session. You can also add a
1349  * feature to the session at construct time by using the
1350  * %SOUP_SESSION_ADD_FEATURE property.
1351  **/
1352 void
1353 soup_session_add_feature (SoupSession *session, SoupSessionFeature *feature)
1354 {
1355         SoupSessionPrivate *priv;
1356
1357         g_return_if_fail (SOUP_IS_SESSION (session));
1358         g_return_if_fail (SOUP_IS_SESSION_FEATURE (feature));
1359
1360         priv = SOUP_SESSION_GET_PRIVATE (session);
1361         priv->features = g_slist_prepend (priv->features, g_object_ref (feature));
1362         soup_session_feature_attach (feature, session);
1363 }
1364
1365 /**
1366  * soup_session_add_feature_by_type:
1367  * @session: a #SoupSession
1368  * @feature_type: the #GType of a class that implements #SoupSessionFeature
1369  *
1370  * Creates a new feature of type @feature_type and adds it to
1371  * @session. You can use this instead of soup_session_add_feature() in
1372  * the case wher you don't need to customize the new feature in any
1373  * way. You can also add a feature to the session at construct time by
1374  * using the %SOUP_SESSION_ADD_FEATURE_BY_TYPE property.
1375  **/
1376 void
1377 soup_session_add_feature_by_type (SoupSession *session, GType feature_type)
1378 {
1379         SoupSessionFeature *feature;
1380
1381         g_return_if_fail (SOUP_IS_SESSION (session));
1382         g_return_if_fail (g_type_is_a (feature_type, SOUP_TYPE_SESSION_FEATURE));
1383
1384         feature = g_object_new (feature_type, NULL);
1385         soup_session_add_feature (session, feature);
1386         g_object_unref (feature);
1387 }
1388
1389 /**
1390  * soup_session_remove_feature:
1391  * @session: a #SoupSession
1392  * @feature: a feature that has previously been added to @session
1393  *
1394  * Removes @feature's functionality from @session.
1395  **/
1396 void
1397 soup_session_remove_feature (SoupSession *session, SoupSessionFeature *feature)
1398 {
1399         SoupSessionPrivate *priv;
1400
1401         g_return_if_fail (SOUP_IS_SESSION (session));
1402
1403         priv = SOUP_SESSION_GET_PRIVATE (session);
1404         if (g_slist_find (priv->features, feature)) {
1405                 priv->features = g_slist_remove (priv->features, feature);
1406                 soup_session_feature_detach (feature, session);
1407                 g_object_unref (feature);
1408         }
1409 }
1410
1411 /**
1412  * soup_session_remove_feature_by_type:
1413  * @session: a #SoupSession
1414  * @feature_type: the #GType of a class that implements #SoupSessionFeature
1415  *
1416  * Removes all features of type @feature_type (or any subclass of
1417  * @feature_type) from @session. You can also remove standard features
1418  * from the session at construct time by using the
1419  * %SOUP_SESSION_REMOVE_FEATURE_BY_TYPE property.
1420  **/
1421 void
1422 soup_session_remove_feature_by_type (SoupSession *session, GType feature_type)
1423 {
1424         SoupSessionPrivate *priv;
1425         GSList *f;
1426
1427         g_return_if_fail (SOUP_IS_SESSION (session));
1428
1429         priv = SOUP_SESSION_GET_PRIVATE (session);
1430 restart:
1431         for (f = priv->features; f; f = f->next) {
1432                 if (G_TYPE_CHECK_INSTANCE_TYPE (f->data, feature_type)) {
1433                         soup_session_remove_feature (session, f->data);
1434                         goto restart;
1435                 }
1436         }
1437 }