don't leak the async_context
[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-session.h"
18 #include "soup-connection.h"
19 #include "soup-connection-ntlm.h"
20 #include "soup-marshal.h"
21 #include "soup-message-filter.h"
22 #include "soup-message-private.h"
23 #include "soup-message-queue.h"
24 #include "soup-ssl.h"
25 #include "soup-uri.h"
26
27 typedef struct {
28         SoupUri    *root_uri;
29
30         GSList     *connections;      /* CONTAINS: SoupConnection */
31         guint       num_conns;
32
33         GHashTable *auth_realms;      /* path -> scheme:realm */
34         GHashTable *auths;            /* scheme:realm -> SoupAuth */
35 } SoupSessionHost;
36
37 typedef struct {
38         SoupUri *proxy_uri;
39         guint max_conns, max_conns_per_host;
40         gboolean use_ntlm;
41
42         char *ssl_ca_file;
43         SoupSSLCredentials *ssl_creds;
44
45         GSList *filters;
46
47         GHashTable *hosts; /* SoupUri -> SoupSessionHost */
48         GHashTable *conns; /* SoupConnection -> SoupSessionHost */
49         guint num_conns;
50
51         SoupSessionHost *proxy_host;
52
53         /* Must hold the host_lock before potentially creating a
54          * new SoupSessionHost, or adding/removing a connection.
55          * Must not emit signals or destroy objects while holding it.
56          */
57         GMutex *host_lock;
58
59         GMainContext *async_context;
60
61         /* Holds the timeout value for the connection, when
62            no response is received.
63         */
64         guint timeout;
65 } SoupSessionPrivate;
66 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
67
68 static guint    host_uri_hash  (gconstpointer key);
69 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
70 static void     free_host      (SoupSessionHost *host);
71
72 static void setup_message   (SoupMessageFilter *filter, SoupMessage *msg);
73
74 static void queue_message   (SoupSession *session, SoupMessage *msg,
75                              SoupMessageCallbackFn callback,
76                              gpointer user_data);
77 static void requeue_message (SoupSession *session, SoupMessage *msg);
78 static void cancel_message  (SoupSession *session, SoupMessage *msg);
79
80 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
81 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
82
83 static void filter_iface_init (SoupMessageFilterClass *filter_class);
84
85 G_DEFINE_TYPE_EXTENDED (SoupSession, soup_session, G_TYPE_OBJECT, 0,
86                         G_IMPLEMENT_INTERFACE (SOUP_TYPE_MESSAGE_FILTER,
87                                                filter_iface_init))
88
89 enum {
90         AUTHENTICATE,
91         REAUTHENTICATE,
92         LAST_SIGNAL
93 };
94
95 static guint signals[LAST_SIGNAL] = { 0 };
96
97 enum {
98         PROP_0,
99
100         PROP_PROXY_URI,
101         PROP_MAX_CONNS,
102         PROP_MAX_CONNS_PER_HOST,
103         PROP_USE_NTLM,
104         PROP_SSL_CA_FILE,
105         PROP_ASYNC_CONTEXT,
106         PROP_TIMEOUT,
107
108         LAST_PROP
109 };
110
111 static void set_property (GObject *object, guint prop_id,
112                           const GValue *value, GParamSpec *pspec);
113 static void get_property (GObject *object, guint prop_id,
114                           GValue *value, GParamSpec *pspec);
115
116 static void
117 soup_session_init (SoupSession *session)
118 {
119         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
120
121         session->queue = soup_message_queue_new ();
122
123         priv->host_lock = g_mutex_new ();
124         priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
125         priv->conns = g_hash_table_new (NULL, NULL);
126
127         priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
128         priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
129
130         priv->timeout = 0;
131 }
132
133 static gboolean
134 foreach_free_host (gpointer key, gpointer host, gpointer data)
135 {
136         free_host (host);
137         return TRUE;
138 }
139
140 static void
141 cleanup_hosts (SoupSessionPrivate *priv)
142 {
143         GHashTable *old_hosts;
144
145         g_mutex_lock (priv->host_lock);
146         old_hosts = priv->hosts;
147         priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
148         g_mutex_unlock (priv->host_lock);
149
150         g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
151         g_hash_table_destroy (old_hosts);
152 }
153
154 static void
155 dispose (GObject *object)
156 {
157         SoupSession *session = SOUP_SESSION (object);
158         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
159         GSList *f;
160
161         soup_session_abort (session);
162         cleanup_hosts (priv);
163
164         if (priv->filters) {
165                 for (f = priv->filters; f; f = f->next)
166                         g_object_unref (f->data);
167                 g_slist_free (priv->filters);
168                 priv->filters = NULL;
169         }
170
171         G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
172 }
173
174 static void
175 finalize (GObject *object)
176 {
177         SoupSession *session = SOUP_SESSION (object);
178         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
179
180         soup_message_queue_destroy (session->queue);
181
182         g_mutex_free (priv->host_lock);
183         g_hash_table_destroy (priv->hosts);
184         g_hash_table_destroy (priv->conns);
185
186         if (priv->proxy_uri)
187                 soup_uri_free (priv->proxy_uri);
188         if (priv->proxy_host)
189                 free_host (priv->proxy_host);
190
191         if (priv->ssl_creds)
192                 soup_ssl_free_client_credentials (priv->ssl_creds);
193
194         if (priv->async_context)
195                 g_main_context_unref (priv->async_context);
196
197         G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
198 }
199
200 static void
201 soup_session_class_init (SoupSessionClass *session_class)
202 {
203         GObjectClass *object_class = G_OBJECT_CLASS (session_class);
204
205         g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
206
207         /* virtual method definition */
208         session_class->queue_message = queue_message;
209         session_class->requeue_message = requeue_message;
210         session_class->cancel_message = cancel_message;
211
212         /* virtual method override */
213         object_class->dispose = dispose;
214         object_class->finalize = finalize;
215         object_class->set_property = set_property;
216         object_class->get_property = get_property;
217
218         /* signals */
219
220         /**
221          * SoupSession::authenticate:
222          * @session: the session
223          * @msg: the #SoupMessage being sent
224          * @auth_type: the authentication type
225          * @auth_realm: the realm being authenticated to
226          * @username: the signal handler should set this to point to
227          * the provided username
228          * @password: the signal handler should set this to point to
229          * the provided password
230          *
231          * Emitted when the session requires authentication. The
232          * credentials may come from the user, or from cached
233          * information. If no credentials are available, leave
234          * @username and @password unchanged.
235          *
236          * If the provided credentials fail, the #reauthenticate
237          * signal will be emitted.
238          **/
239         signals[AUTHENTICATE] =
240                 g_signal_new ("authenticate",
241                               G_OBJECT_CLASS_TYPE (object_class),
242                               G_SIGNAL_RUN_FIRST,
243                               G_STRUCT_OFFSET (SoupSessionClass, authenticate),
244                               NULL, NULL,
245                               soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
246                               G_TYPE_NONE, 5,
247                               SOUP_TYPE_MESSAGE,
248                               G_TYPE_STRING,
249                               G_TYPE_STRING,
250                               G_TYPE_POINTER,
251                               G_TYPE_POINTER);
252
253         /**
254          * SoupSession::reauthenticate:
255          * @session: the session
256          * @msg: the #SoupMessage being sent
257          * @auth_type: the authentication type
258          * @auth_realm: the realm being authenticated to
259          * @username: the signal handler should set this to point to
260          * the provided username
261          * @password: the signal handler should set this to point to
262          * the provided password
263          *
264          * Emitted when the credentials provided by the application to
265          * the #authenticate signal have failed. This gives the
266          * application a second chance to provide authentication
267          * credentials. If the new credentials also fail, #SoupSession
268          * will emit #reauthenticate again, and will continue doing so
269          * until the provided credentials work, or a #reauthenticate
270          * signal emission "fails" (because the handler left @username
271          * and @password unchanged). At that point, the 401 or 407
272          * error status will be returned to the caller.
273          *
274          * If your application only uses cached passwords, it should
275          * only connect to #authenticate, and not #reauthenticate.
276          *
277          * If your application always prompts the user for a password,
278          * and never uses cached information, then you can connect the
279          * same handler to #authenticate and #reauthenticate.
280          *
281          * To get standard web-browser behavior, return either cached
282          * information or a user-provided password (whichever is
283          * available) from the #authenticate handler, but return only
284          * user-provided information from the #reauthenticate handler.
285          **/
286         signals[REAUTHENTICATE] =
287                 g_signal_new ("reauthenticate",
288                               G_OBJECT_CLASS_TYPE (object_class),
289                               G_SIGNAL_RUN_FIRST,
290                               G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
291                               NULL, NULL,
292                               soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
293                               G_TYPE_NONE, 5,
294                               SOUP_TYPE_MESSAGE,
295                               G_TYPE_STRING,
296                               G_TYPE_STRING,
297                               G_TYPE_POINTER,
298                               G_TYPE_POINTER);
299
300         /* properties */
301         g_object_class_install_property (
302                 object_class, PROP_PROXY_URI,
303                 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
304                                       "Proxy URI",
305                                       "The HTTP Proxy to use for this session",
306                                       G_PARAM_READWRITE));
307         g_object_class_install_property (
308                 object_class, PROP_MAX_CONNS,
309                 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
310                                   "Max Connection Count",
311                                   "The maximum number of connections that the session can open at once",
312                                   1,
313                                   G_MAXINT,
314                                   10,
315                                   G_PARAM_READWRITE));
316         g_object_class_install_property (
317                 object_class, PROP_MAX_CONNS_PER_HOST,
318                 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
319                                   "Max Per-Host Connection Count",
320                                   "The maximum number of connections that the session can open at once to a given host",
321                                   1,
322                                   G_MAXINT,
323                                   4,
324                                   G_PARAM_READWRITE));
325         g_object_class_install_property (
326                 object_class, PROP_USE_NTLM,
327                 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
328                                       "Use NTLM",
329                                       "Whether or not to use NTLM authentication",
330                                       FALSE,
331                                       G_PARAM_READWRITE));
332         g_object_class_install_property (
333                 object_class, PROP_SSL_CA_FILE,
334                 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
335                                      "SSL CA file",
336                                      "File containing SSL CA certificates",
337                                      NULL,
338                                      G_PARAM_READWRITE));
339         g_object_class_install_property (
340                 object_class, PROP_ASYNC_CONTEXT,
341                 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
342                                       "Async GMainContext",
343                                       "The GMainContext to dispatch async I/O in",
344                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
345         g_object_class_install_property (
346                 object_class, PROP_TIMEOUT,
347                 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
348                                    "Timeout value",
349                                    "Value in seconds to timeout a blocking I/O",
350                                    0, G_MAXUINT, 0,
351                                    G_PARAM_READWRITE));
352 }
353
354 static void
355 filter_iface_init (SoupMessageFilterClass *filter_class)
356 {
357         /* interface implementation */
358         filter_class->setup_message = setup_message;
359 }
360
361
362 static gboolean
363 safe_uri_equal (const SoupUri *a, const SoupUri *b)
364 {
365         if (!a && !b)
366                 return TRUE;
367
368         if ((a && !b) || (b && !a))
369                 return FALSE;
370
371         return soup_uri_equal (a, b);
372 }
373
374 static gboolean
375 safe_str_equal (const char *a, const char *b)
376 {
377         if (!a && !b)
378                 return TRUE;
379
380         if ((a && !b) || (b && !a))
381                 return FALSE;
382
383         return strcmp (a, b) == 0;
384 }
385
386 static void
387 set_property (GObject *object, guint prop_id,
388               const GValue *value, GParamSpec *pspec)
389 {
390         SoupSession *session = SOUP_SESSION (object);
391         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
392         gpointer pval;
393         gboolean need_abort = FALSE;
394         gboolean ca_file_changed = FALSE;
395         const char *new_ca_file;
396
397         switch (prop_id) {
398         case PROP_PROXY_URI:
399                 pval = g_value_get_pointer (value);
400
401                 if (!safe_uri_equal (priv->proxy_uri, pval))
402                         need_abort = TRUE;
403
404                 if (priv->proxy_uri)
405                         soup_uri_free (priv->proxy_uri);
406
407                 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
408
409                 if (need_abort) {
410                         soup_session_abort (session);
411                         cleanup_hosts (priv);
412                 }
413
414                 break;
415         case PROP_MAX_CONNS:
416                 priv->max_conns = g_value_get_int (value);
417                 break;
418         case PROP_MAX_CONNS_PER_HOST:
419                 priv->max_conns_per_host = g_value_get_int (value);
420                 break;
421         case PROP_USE_NTLM:
422                 priv->use_ntlm = g_value_get_boolean (value);
423                 break;
424         case PROP_SSL_CA_FILE:
425                 new_ca_file = g_value_get_string (value);
426
427                 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
428                         ca_file_changed = TRUE;
429
430                 g_free (priv->ssl_ca_file);
431                 priv->ssl_ca_file = g_strdup (new_ca_file);
432
433                 if (ca_file_changed) {
434                         if (priv->ssl_creds) {
435                                 soup_ssl_free_client_credentials (priv->ssl_creds);
436                                 priv->ssl_creds = NULL;
437                         }
438
439                         cleanup_hosts (priv);
440                 }
441
442                 break;
443         case PROP_ASYNC_CONTEXT:
444                 priv->async_context = g_value_get_pointer (value);
445                 if (priv->async_context)
446                         g_main_context_ref (priv->async_context);
447                 break;
448         case PROP_TIMEOUT:
449                 priv->timeout = g_value_get_uint (value);
450                 break;
451         default:
452                 break;
453         }
454 }
455
456 static void
457 get_property (GObject *object, guint prop_id,
458               GValue *value, GParamSpec *pspec)
459 {
460         SoupSession *session = SOUP_SESSION (object);
461         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
462
463         switch (prop_id) {
464         case PROP_PROXY_URI:
465                 g_value_set_pointer (value, priv->proxy_uri ?
466                                      soup_uri_copy (priv->proxy_uri) :
467                                      NULL);
468                 break;
469         case PROP_MAX_CONNS:
470                 g_value_set_int (value, priv->max_conns);
471                 break;
472         case PROP_MAX_CONNS_PER_HOST:
473                 g_value_set_int (value, priv->max_conns_per_host);
474                 break;
475         case PROP_USE_NTLM:
476                 g_value_set_boolean (value, priv->use_ntlm);
477                 break;
478         case PROP_SSL_CA_FILE:
479                 g_value_set_string (value, priv->ssl_ca_file);
480                 break;
481         case PROP_ASYNC_CONTEXT:
482                 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
483                 break;
484         case PROP_TIMEOUT:
485                 g_value_set_uint (value, priv->timeout);
486                 break;
487         default:
488                 break;
489         }
490 }
491
492
493 /**
494  * soup_session_add_filter:
495  * @session: a #SoupSession
496  * @filter: an object implementing the #SoupMessageFilter interface
497  *
498  * Adds @filter to @session's list of message filters to be applied to
499  * all messages.
500  **/
501 void
502 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
503 {
504         SoupSessionPrivate *priv;
505
506         g_return_if_fail (SOUP_IS_SESSION (session));
507         g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
508         priv = SOUP_SESSION_GET_PRIVATE (session);
509
510         g_object_ref (filter);
511         priv->filters = g_slist_prepend (priv->filters, filter);
512 }
513
514 /**
515  * soup_session_remove_filter:
516  * @session: a #SoupSession
517  * @filter: an object implementing the #SoupMessageFilter interface
518  *
519  * Removes @filter from @session's list of message filters
520  **/
521 void
522 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
523 {
524         SoupSessionPrivate *priv;
525
526         g_return_if_fail (SOUP_IS_SESSION (session));
527         g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
528         priv = SOUP_SESSION_GET_PRIVATE (session);
529
530         priv->filters = g_slist_remove (priv->filters, filter);
531         g_object_unref (filter);
532 }
533
534 /**
535  * soup_session_get_async_context:
536  * @session: a #SoupSession
537  *
538  * Gets @session's async_context. This does not add a ref to the
539  * context, so you will need to ref it yourself if you want it to
540  * outlive its session.
541  *
542  * Return value: @session's #GMainContext, which may be %NULL
543  **/
544 GMainContext *
545 soup_session_get_async_context (SoupSession *session)
546 {
547         SoupSessionPrivate *priv;
548
549         g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
550         priv = SOUP_SESSION_GET_PRIVATE (session);
551
552         return priv->async_context;
553 }
554
555 /* Hosts */
556 static guint
557 host_uri_hash (gconstpointer key)
558 {
559         const SoupUri *uri = key;
560
561         return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
562 }
563
564 static gboolean
565 host_uri_equal (gconstpointer v1, gconstpointer v2)
566 {
567         const SoupUri *one = v1;
568         const SoupUri *two = v2;
569
570         if (one->protocol != two->protocol)
571                 return FALSE;
572         if (one->port != two->port)
573                 return FALSE;
574
575         return strcmp (one->host, two->host) == 0;
576 }
577
578 static SoupSessionHost *
579 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
580 {
581         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
582         SoupSessionHost *host;
583
584         host = g_new0 (SoupSessionHost, 1);
585         host->root_uri = soup_uri_copy_root (source_uri);
586
587         if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
588             !priv->ssl_creds) {
589                 priv->ssl_creds =
590                         soup_ssl_get_client_credentials (priv->ssl_ca_file);
591         }
592
593         return host;
594 }
595
596 /* Note: get_host_for_message doesn't lock the host_lock. The caller
597  * must do it itself if there's a chance the host doesn't already
598  * exist.
599  */
600 static SoupSessionHost *
601 get_host_for_message (SoupSession *session, SoupMessage *msg)
602 {
603         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
604         SoupSessionHost *host;
605         const SoupUri *source = soup_message_get_uri (msg);
606
607         host = g_hash_table_lookup (priv->hosts, source);
608         if (host)
609                 return host;
610
611         host = soup_session_host_new (session, source);
612         g_hash_table_insert (priv->hosts, host->root_uri, host);
613
614         return host;
615 }
616
617 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
618  * it itself if there's a chance the host doesn't already exist.
619  */
620 static SoupSessionHost *
621 get_proxy_host (SoupSession *session)
622 {
623         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
624
625         if (priv->proxy_host || !priv->proxy_uri)
626                 return priv->proxy_host;
627
628         priv->proxy_host =
629                 soup_session_host_new (session, priv->proxy_uri);
630         return priv->proxy_host;
631 }
632
633 static void
634 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
635 {
636         g_free (path);
637         g_free (scheme_realm);
638 }
639
640 static void
641 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
642 {
643         g_free (scheme_realm);
644         g_object_unref (auth);
645 }
646
647 static void
648 free_host (SoupSessionHost *host)
649 {
650         while (host->connections) {
651                 SoupConnection *conn = host->connections->data;
652
653                 host->connections = g_slist_remove (host->connections, conn);
654                 soup_connection_disconnect (conn);
655         }
656
657         if (host->auth_realms) {
658                 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
659                 g_hash_table_destroy (host->auth_realms);
660         }
661         if (host->auths) {
662                 g_hash_table_foreach (host->auths, free_auth, NULL);
663                 g_hash_table_destroy (host->auths);
664         }
665
666         soup_uri_free (host->root_uri);
667         g_free (host);
668 }       
669
670 /* Authentication */
671
672 static SoupAuth *
673 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
674 {
675         SoupSessionHost *host;
676         char *path, *dir;
677         const char *realm, *const_path;
678
679         if (proxy) {
680                 host = get_proxy_host (session);
681                 const_path = "/";
682         } else {
683                 host = get_host_for_message (session, msg);
684                 const_path = soup_message_get_uri (msg)->path;
685
686                 if (!const_path)
687                         const_path = "/";
688         }
689         g_return_val_if_fail (host != NULL, NULL);
690
691         if (!host->auth_realms)
692                 return NULL;
693
694         path = g_strdup (const_path);
695         dir = path;
696         do {
697                 realm = g_hash_table_lookup (host->auth_realms, path);
698                 if (realm)
699                         break;
700
701                 dir = strrchr (path, '/');
702                 if (dir) {
703                         if (dir[1])
704                                 dir[1] = '\0';
705                         else
706                                 *dir = '\0';
707                 }
708         } while (dir);
709
710         g_free (path);
711         if (realm)
712                 return g_hash_table_lookup (host->auths, realm);
713         else
714                 return NULL;
715 }
716
717 static void
718 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
719 {
720         char *info;
721         gpointer key, value;
722
723         info = soup_auth_get_info (auth);
724         if (g_hash_table_lookup_extended (host->auths, info, &key, &value) &&
725             auth == (SoupAuth *)value) {
726                 g_hash_table_remove (host->auths, info);
727                 g_free (key);
728                 g_object_unref (auth);
729         }
730         g_free (info);
731 }
732
733 static gboolean
734 authenticate_auth (SoupSession *session, SoupAuth *auth,
735                    SoupMessage *msg, gboolean prior_auth_failed,
736                    gboolean proxy)
737 {
738         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
739         const SoupUri *uri;
740         char *username = NULL, *password = NULL;
741
742         if (proxy)
743                 uri = priv->proxy_uri;
744         else
745                 uri = soup_message_get_uri (msg);
746
747         if (uri->passwd && !prior_auth_failed) {
748                 soup_auth_authenticate (auth, uri->user, uri->passwd);
749                 return TRUE;
750         }
751
752         g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
753                        msg, soup_auth_get_scheme_name (auth),
754                        soup_auth_get_realm (auth),
755                        &username, &password);
756         if (username || password)
757                 soup_auth_authenticate (auth, username, password);
758         if (username)
759                 g_free (username);
760         if (password) {
761                 memset (password, 0, strlen (password));
762                 g_free (password);
763         }
764
765         return soup_auth_is_authenticated (auth);
766 }
767
768 static gboolean
769 update_auth_internal (SoupSession *session, SoupMessage *msg,
770                       const GSList *headers, gboolean proxy)
771 {
772         SoupSessionHost *host;
773         SoupAuth *new_auth, *prior_auth, *old_auth;
774         gpointer old_path, old_auth_info;
775         const SoupUri *msg_uri;
776         const char *path;
777         char *auth_info;
778         GSList *pspace, *p;
779         gboolean prior_auth_failed = FALSE;
780
781         if (proxy)
782                 host = get_proxy_host (session);
783         else
784                 host = get_host_for_message (session, msg);
785
786         g_return_val_if_fail (host != NULL, FALSE);
787
788         /* Try to construct a new auth from the headers; if we can't,
789          * there's no way we'll be able to authenticate.
790          */
791         msg_uri = soup_message_get_uri (msg);
792         new_auth = soup_auth_new_from_header_list (headers);
793         if (!new_auth)
794                 return FALSE;
795
796         auth_info = soup_auth_get_info (new_auth);
797
798         /* See if this auth is the same auth we used last time */
799         prior_auth = proxy ? soup_message_get_proxy_auth (msg) : soup_message_get_auth (msg);
800         if (prior_auth) {
801                 char *old_auth_info = soup_auth_get_info (prior_auth);
802
803                 if (!strcmp (old_auth_info, auth_info)) {
804                         /* The server didn't like the username/password we
805                          * provided before. Invalidate it and note this fact.
806                          */
807                         invalidate_auth (host, prior_auth);
808                         prior_auth_failed = TRUE;
809                 }
810                 g_free (old_auth_info);
811         }
812
813         if (!host->auth_realms) {
814                 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
815                 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
816         }
817
818         /* Record where this auth realm is used. RFC 2617 is somewhat
819          * unclear about the scope of protection spaces with regard to
820          * proxies. The only mention of it is as an aside in section
821          * 3.2.1, where it is defining the fields of a Digest
822          * challenge and says that the protection space is always the
823          * entire proxy. Is this the case for all authentication
824          * schemes or just Digest? Who knows, but we're assuming all.
825          */
826         if (proxy)
827                 pspace = g_slist_prepend (NULL, g_strdup (""));
828         else
829                 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
830
831         for (p = pspace; p; p = p->next) {
832                 path = p->data;
833                 if (g_hash_table_lookup_extended (host->auth_realms, path,
834                                                   &old_path, &old_auth_info)) {
835                         g_hash_table_remove (host->auth_realms, old_path);
836                         g_free (old_path);
837                         g_free (old_auth_info);
838                 }
839
840                 g_hash_table_insert (host->auth_realms,
841                                      g_strdup (path), g_strdup (auth_info));
842         }
843         soup_auth_free_protection_space (new_auth, pspace);
844
845         /* Now, make sure the auth is recorded. (If there's a
846          * pre-existing auth, we keep that rather than the new one,
847          * since the old one might already be authenticated.)
848          */
849         old_auth = g_hash_table_lookup (host->auths, auth_info);
850         if (old_auth) {
851                 g_free (auth_info);
852                 g_object_unref (new_auth);
853                 new_auth = old_auth;
854         } else 
855                 g_hash_table_insert (host->auths, auth_info, new_auth);
856
857         /* If we need to authenticate, try to do it. */
858         if (!soup_auth_is_authenticated (new_auth)) {
859                 return authenticate_auth (session, new_auth,
860                                           msg, prior_auth_failed, proxy);
861         }
862
863         /* Otherwise we're good. */
864         return TRUE;
865 }
866
867 static void
868 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
869                          const char *auth_type, const char *auth_realm,
870                          char **username, char **password, gpointer session)
871 {
872         g_signal_emit (session, signals[AUTHENTICATE], 0,
873                        msg, auth_type, auth_realm, username, password);
874 }
875
876 static void
877 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
878                            const char *auth_type, const char *auth_realm,
879                            char **username, char **password,
880                            gpointer user_data)
881 {
882         g_signal_emit (conn, signals[REAUTHENTICATE], 0,
883                        msg, auth_type, auth_realm, username, password);
884 }
885
886
887 static void
888 authorize_handler (SoupMessage *msg, gpointer user_data)
889 {
890         SoupSession *session = user_data;
891         const GSList *headers;
892         gboolean proxy;
893
894         if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
895                 headers = soup_message_get_header_list (msg->response_headers,
896                                                         "Proxy-Authenticate");
897                 proxy = TRUE;
898         } else {
899                 headers = soup_message_get_header_list (msg->response_headers,
900                                                         "WWW-Authenticate");
901                 proxy = FALSE;
902         }
903         if (!headers)
904                 return;
905
906         if (update_auth_internal (session, msg, headers, proxy))
907                 soup_session_requeue_message (session, msg);
908 }
909
910 static void
911 redirect_handler (SoupMessage *msg, gpointer user_data)
912 {
913         SoupSession *session = user_data;
914         const char *new_loc;
915         SoupUri *new_uri;
916
917         new_loc = soup_message_get_header (msg->response_headers, "Location");
918         if (!new_loc)
919                 return;
920
921         /* Location is supposed to be an absolute URI, but some sites
922          * are lame, so we use soup_uri_new_with_base().
923          */
924         new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
925         if (!new_uri) {
926                 soup_message_set_status_full (msg,
927                                               SOUP_STATUS_MALFORMED,
928                                               "Invalid Redirect URL");
929                 return;
930         }
931
932         soup_message_set_uri (msg, new_uri);
933         soup_uri_free (new_uri);
934
935         soup_session_requeue_message (session, msg);
936 }
937
938 static void
939 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
940 {
941         SoupAuth *auth;
942
943         auth = lookup_auth (session, msg, proxy);
944         if (auth && !soup_auth_is_authenticated (auth)) {
945                 if (!authenticate_auth (session, auth, msg, FALSE, proxy))
946                         auth = NULL;
947         }
948
949         if (proxy)
950                 soup_message_set_proxy_auth (msg, auth);
951         else
952                 soup_message_set_auth (msg, auth);
953 }
954
955 static void
956 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
957 {
958         SoupSession *session = SOUP_SESSION (filter);
959         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
960         GSList *f;
961
962         for (f = priv->filters; f; f = f->next) {
963                 filter = f->data;
964                 soup_message_filter_setup_message (filter, msg);
965         }
966
967         add_auth (session, msg, FALSE);
968         soup_message_add_status_code_handler (
969                 msg, SOUP_STATUS_UNAUTHORIZED,
970                 SOUP_HANDLER_POST_BODY,
971                 authorize_handler, session);
972
973         if (priv->proxy_uri) {
974                 add_auth (session, msg, TRUE);
975                 soup_message_add_status_code_handler  (
976                         msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
977                         SOUP_HANDLER_POST_BODY,
978                         authorize_handler, session);
979         }
980 }
981
982 static void
983 find_oldest_connection (gpointer key, gpointer host, gpointer data)
984 {
985         SoupConnection *conn = key, **oldest = data;
986
987         /* Don't prune a connection that is currently in use, or
988          * hasn't been used yet.
989          */
990         if (soup_connection_is_in_use (conn) ||
991             soup_connection_last_used (conn) == 0)
992                 return;
993
994         if (!*oldest || (soup_connection_last_used (conn) <
995                          soup_connection_last_used (*oldest)))
996                 *oldest = conn;
997 }
998
999 /**
1000  * soup_session_try_prune_connection:
1001  * @session: a #SoupSession
1002  *
1003  * Finds the least-recently-used idle connection in @session and closes
1004  * it.
1005  *
1006  * Return value: %TRUE if a connection was closed, %FALSE if there are
1007  * no idle connections.
1008  **/
1009 gboolean
1010 soup_session_try_prune_connection (SoupSession *session)
1011 {
1012         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1013         SoupConnection *oldest = NULL;
1014
1015         g_mutex_lock (priv->host_lock);
1016         g_hash_table_foreach (priv->conns, find_oldest_connection,
1017                               &oldest);
1018         if (oldest) {
1019                 /* Ref the connection before unlocking the mutex in
1020                  * case someone else tries to prune it too.
1021                  */
1022                 g_object_ref (oldest);
1023                 g_mutex_unlock (priv->host_lock);
1024                 soup_connection_disconnect (oldest);
1025                 g_object_unref (oldest);
1026                 return TRUE;
1027         } else {
1028                 g_mutex_unlock (priv->host_lock);
1029                 return FALSE;
1030         }
1031 }
1032
1033 static void
1034 connection_disconnected (SoupConnection *conn, gpointer user_data)
1035 {
1036         SoupSession *session = user_data;
1037         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1038         SoupSessionHost *host;
1039
1040         g_mutex_lock (priv->host_lock);
1041
1042         host = g_hash_table_lookup (priv->conns, conn);
1043         if (host) {
1044                 g_hash_table_remove (priv->conns, conn);
1045                 host->connections = g_slist_remove (host->connections, conn);
1046                 host->num_conns--;
1047         }
1048
1049         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1050         priv->num_conns--;
1051
1052         g_mutex_unlock (priv->host_lock);
1053         g_object_unref (conn);
1054 }
1055
1056 static void
1057 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1058 {
1059         SoupSession *session = user_data;
1060         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1061         SoupSessionHost *host;
1062         SoupMessageQueueIter iter;
1063         SoupMessage *msg;
1064
1065         g_mutex_lock (priv->host_lock);
1066
1067         host = g_hash_table_lookup (priv->conns, conn);
1068         if (!host) {
1069                 g_mutex_unlock (priv->host_lock);
1070                 return;
1071         }
1072
1073         if (status == SOUP_STATUS_OK) {
1074                 soup_connection_reserve (conn);
1075                 host->connections = g_slist_prepend (host->connections, conn);
1076                 g_mutex_unlock (priv->host_lock);
1077                 return;
1078         }
1079
1080         /* The connection failed. */
1081         g_mutex_unlock (priv->host_lock);
1082         connection_disconnected (conn, session);
1083
1084         if (host->connections) {
1085                 /* Something went wrong this time, but we have at
1086                  * least one open connection to this host. So just
1087                  * leave the message in the queue so it can use that
1088                  * connection once it's free.
1089                  */
1090                 return;
1091         }
1092
1093         /* There are two possibilities: either status is
1094          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1095          * will create a new connection (and all we need to do here
1096          * is downgrade the message from CONNECTING to QUEUED); or
1097          * status is something else, probably CANT_CONNECT or
1098          * CANT_RESOLVE or the like, in which case we need to cancel
1099          * any messages waiting for this host, since they're out
1100          * of luck.
1101          */
1102         for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1103                 if (get_host_for_message (session, msg) == host) {
1104                         if (status == SOUP_STATUS_TRY_AGAIN) {
1105                                 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1106                                         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1107                         } else {
1108                                 soup_message_set_status (msg, status);
1109                                 soup_session_cancel_message (session, msg);
1110                         }
1111                 }
1112         }
1113 }
1114
1115 /**
1116  * soup_session_get_connection:
1117  * @session: a #SoupSession
1118  * @msg: a #SoupMessage
1119  * @try_pruning: on return, whether or not to try pruning a connection
1120  * @is_new: on return, %TRUE if the returned connection is new and not
1121  * yet connected
1122  * 
1123  * Tries to find or create a connection for @msg; this is an internal
1124  * method for #SoupSession subclasses.
1125  *
1126  * If there is an idle connection to the relevant host available, then
1127  * that connection will be returned (with *@is_new set to %FALSE). The
1128  * connection will be marked "reserved", so the caller must call
1129  * soup_connection_release() if it ends up not using the connection
1130  * right away.
1131  *
1132  * If there is no idle connection available, but it is possible to
1133  * create a new connection, then one will be created and returned,
1134  * with *@is_new set to %TRUE. The caller MUST then call
1135  * soup_connection_connect_sync() or soup_connection_connect_async()
1136  * to connect it. If the connection attempt succeeds, the connection
1137  * will be marked "reserved" and added to @session's connection pool
1138  * once it connects. If the connection attempt fails, the connection
1139  * will be unreffed.
1140  *
1141  * If no connection is available and a new connection cannot be made,
1142  * soup_session_get_connection() will return %NULL. If @session has
1143  * the maximum number of open connections open, but does not have the
1144  * maximum number of per-host connections open to the relevant host,
1145  * then *@try_pruning will be set to %TRUE. In this case, the caller
1146  * can call soup_session_try_prune_connection() to close an idle
1147  * connection, and then try soup_session_get_connection() again. (If
1148  * calling soup_session_try_prune_connection() wouldn't help, then
1149  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1150  *
1151  * Return value: a #SoupConnection, or %NULL
1152  **/
1153 SoupConnection *
1154 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1155                              gboolean *try_pruning, gboolean *is_new)
1156 {
1157         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1158         SoupConnection *conn;
1159         SoupSessionHost *host;
1160         GSList *conns;
1161
1162         g_mutex_lock (priv->host_lock);
1163
1164         host = get_host_for_message (session, msg);
1165         for (conns = host->connections; conns; conns = conns->next) {
1166                 if (!soup_connection_is_in_use (conns->data)) {
1167                         soup_connection_reserve (conns->data);
1168                         g_mutex_unlock (priv->host_lock);
1169                         *is_new = FALSE;
1170                         return conns->data;
1171                 }
1172         }
1173
1174         if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1175                 /* We already started a connection for this
1176                  * message, so don't start another one.
1177                  */
1178                 g_mutex_unlock (priv->host_lock);
1179                 return NULL;
1180         }
1181
1182         if (host->num_conns >= priv->max_conns_per_host) {
1183                 g_mutex_unlock (priv->host_lock);
1184                 return NULL;
1185         }
1186
1187         if (priv->num_conns >= priv->max_conns) {
1188                 *try_pruning = TRUE;
1189                 g_mutex_unlock (priv->host_lock);
1190                 return NULL;
1191         }
1192
1193         /* Make sure priv->proxy_host gets set now while
1194          * we have the host_lock.
1195          */
1196         if (priv->proxy_uri)
1197                 get_proxy_host (session);
1198
1199         conn = g_object_new (
1200                 (priv->use_ntlm ?
1201                  SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1202                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1203                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1204                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1205                 SOUP_CONNECTION_MESSAGE_FILTER, session,
1206                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1207                 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1208                 NULL);
1209         g_signal_connect (conn, "connect_result",
1210                           G_CALLBACK (connect_result),
1211                           session);
1212         g_signal_connect (conn, "disconnected",
1213                           G_CALLBACK (connection_disconnected),
1214                           session);
1215         g_signal_connect (conn, "authenticate",
1216                           G_CALLBACK (connection_authenticate),
1217                           session);
1218         g_signal_connect (conn, "reauthenticate",
1219                           G_CALLBACK (connection_reauthenticate),
1220                           session);
1221
1222         g_hash_table_insert (priv->conns, conn, host);
1223
1224         /* We increment the connection counts so it counts against the
1225          * totals, but we don't add it to the host's connection list
1226          * yet, since it's not ready for use.
1227          */
1228         priv->num_conns++;
1229         host->num_conns++;
1230
1231         /* Mark the request as connecting, so we don't try to open
1232          * another new connection for it while waiting for this one.
1233          */
1234         msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1235
1236         g_mutex_unlock (priv->host_lock);
1237         *is_new = TRUE;
1238         return conn;
1239 }
1240
1241 static void
1242 message_finished (SoupMessage *msg, gpointer user_data)
1243 {
1244         SoupSession *session = user_data;
1245
1246         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1247                 soup_message_queue_remove_message (session->queue, msg);
1248                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1249         }
1250 }
1251
1252 static void
1253 queue_message (SoupSession *session, SoupMessage *msg,
1254                SoupMessageCallbackFn callback, gpointer user_data)
1255 {
1256         g_signal_connect_after (msg, "finished",
1257                                 G_CALLBACK (message_finished), session);
1258
1259         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1260                 soup_message_add_status_class_handler (
1261                         msg, SOUP_STATUS_CLASS_REDIRECT,
1262                         SOUP_HANDLER_POST_BODY,
1263                         redirect_handler, session);
1264         }
1265
1266         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1267         soup_message_queue_append (session->queue, msg);
1268 }
1269
1270 /**
1271  * soup_session_queue_message:
1272  * @session: a #SoupSession
1273  * @msg: the message to queue
1274  * @callback: a #SoupMessageCallbackFn which will be called after the
1275  * message completes or when an unrecoverable error occurs.
1276  * @user_data: a pointer passed to @callback.
1277  * 
1278  * Queues the message @msg for sending. All messages are processed
1279  * while the glib main loop runs. If @msg has been processed before,
1280  * any resources related to the time it was last sent are freed.
1281  *
1282  * Upon message completion, the callback specified in @callback will
1283  * be invoked (in the thread associated with @session's async
1284  * context). If after returning from this callback the message has not
1285  * been requeued, @msg will be unreffed.
1286  */
1287 void
1288 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1289                             SoupMessageCallbackFn callback, gpointer user_data)
1290 {
1291         g_return_if_fail (SOUP_IS_SESSION (session));
1292         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1293
1294         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1295                                                          callback, user_data);
1296 }
1297
1298 static void
1299 requeue_message (SoupSession *session, SoupMessage *msg)
1300 {
1301         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1302 }
1303
1304 /**
1305  * soup_session_requeue_message:
1306  * @session: a #SoupSession
1307  * @msg: the message to requeue
1308  *
1309  * This causes @msg to be placed back on the queue to be attempted
1310  * again.
1311  **/
1312 void
1313 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1314 {
1315         g_return_if_fail (SOUP_IS_SESSION (session));
1316         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1317
1318         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1319 }
1320
1321
1322 /**
1323  * soup_session_send_message:
1324  * @session: a #SoupSession
1325  * @msg: the message to send
1326  * 
1327  * Synchronously send @msg. This call will not return until the
1328  * transfer is finished successfully or there is an unrecoverable
1329  * error.
1330  *
1331  * @msg is not freed upon return.
1332  *
1333  * Return value: the HTTP status code of the response
1334  */
1335 guint
1336 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1337 {
1338         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1339         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1340
1341         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1342 }
1343
1344
1345 static void
1346 cancel_message (SoupSession *session, SoupMessage *msg)
1347 {
1348         soup_message_queue_remove_message (session->queue, msg);
1349         soup_message_finished (msg);
1350 }
1351
1352 /**
1353  * soup_session_cancel_message:
1354  * @session: a #SoupSession
1355  * @msg: the message to cancel
1356  *
1357  * Causes @session to immediately finish processing @msg. You should
1358  * set a status code on @msg with soup_message_set_status() before
1359  * calling this function.
1360  **/
1361 void
1362 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1363 {
1364         g_return_if_fail (SOUP_IS_SESSION (session));
1365         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1366
1367         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1368 }
1369
1370 static void
1371 gather_conns (gpointer key, gpointer host, gpointer data)
1372 {
1373         SoupConnection *conn = key;
1374         GSList **conns = data;
1375
1376         *conns = g_slist_prepend (*conns, conn);
1377 }
1378
1379 /**
1380  * soup_session_abort:
1381  * @session: the session
1382  *
1383  * Cancels all pending requests in @session.
1384  **/
1385 void
1386 soup_session_abort (SoupSession *session)
1387 {
1388         SoupSessionPrivate *priv;
1389         SoupMessageQueueIter iter;
1390         SoupMessage *msg;
1391         GSList *conns, *c;
1392
1393         g_return_if_fail (SOUP_IS_SESSION (session));
1394         priv = SOUP_SESSION_GET_PRIVATE (session);
1395
1396         for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1397                 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1398                 soup_session_cancel_message (session, msg);
1399         }
1400
1401         /* Close all connections */
1402         g_mutex_lock (priv->host_lock);
1403         conns = NULL;
1404         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1405
1406         for (c = conns; c; c = c->next)
1407                 g_object_ref (c->data);
1408         g_mutex_unlock (priv->host_lock);
1409         for (c = conns; c; c = c->next) {
1410                 soup_connection_disconnect (c->data);
1411                 g_object_unref (c->data);
1412         }
1413
1414         g_slist_free (conns);
1415 }