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