don't leak the SoupAddress.
[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 /* Hosts */
536 static guint
537 host_uri_hash (gconstpointer key)
538 {
539         const SoupUri *uri = key;
540
541         return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
542 }
543
544 static gboolean
545 host_uri_equal (gconstpointer v1, gconstpointer v2)
546 {
547         const SoupUri *one = v1;
548         const SoupUri *two = v2;
549
550         if (one->protocol != two->protocol)
551                 return FALSE;
552         if (one->port != two->port)
553                 return FALSE;
554
555         return strcmp (one->host, two->host) == 0;
556 }
557
558 static SoupSessionHost *
559 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
560 {
561         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
562         SoupSessionHost *host;
563
564         host = g_new0 (SoupSessionHost, 1);
565         host->root_uri = soup_uri_copy_root (source_uri);
566
567         if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
568             !priv->ssl_creds) {
569                 priv->ssl_creds =
570                         soup_ssl_get_client_credentials (priv->ssl_ca_file);
571         }
572
573         return host;
574 }
575
576 /* Note: get_host_for_message doesn't lock the host_lock. The caller
577  * must do it itself if there's a chance the host doesn't already
578  * exist.
579  */
580 static SoupSessionHost *
581 get_host_for_message (SoupSession *session, SoupMessage *msg)
582 {
583         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
584         SoupSessionHost *host;
585         const SoupUri *source = soup_message_get_uri (msg);
586
587         host = g_hash_table_lookup (priv->hosts, source);
588         if (host)
589                 return host;
590
591         host = soup_session_host_new (session, source);
592         g_hash_table_insert (priv->hosts, host->root_uri, host);
593
594         return host;
595 }
596
597 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
598  * it itself if there's a chance the host doesn't already exist.
599  */
600 static SoupSessionHost *
601 get_proxy_host (SoupSession *session)
602 {
603         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
604
605         if (priv->proxy_host || !priv->proxy_uri)
606                 return priv->proxy_host;
607
608         priv->proxy_host =
609                 soup_session_host_new (session, priv->proxy_uri);
610         return priv->proxy_host;
611 }
612
613 static void
614 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
615 {
616         g_free (path);
617         g_free (scheme_realm);
618 }
619
620 static void
621 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
622 {
623         g_free (scheme_realm);
624         g_object_unref (auth);
625 }
626
627 static void
628 free_host (SoupSessionHost *host)
629 {
630         while (host->connections) {
631                 SoupConnection *conn = host->connections->data;
632
633                 host->connections = g_slist_remove (host->connections, conn);
634                 soup_connection_disconnect (conn);
635         }
636
637         if (host->auth_realms) {
638                 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
639                 g_hash_table_destroy (host->auth_realms);
640         }
641         if (host->auths) {
642                 g_hash_table_foreach (host->auths, free_auth, NULL);
643                 g_hash_table_destroy (host->auths);
644         }
645
646         soup_uri_free (host->root_uri);
647         g_free (host);
648 }       
649
650 /* Authentication */
651
652 static SoupAuth *
653 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
654 {
655         SoupSessionHost *host;
656         char *path, *dir;
657         const char *realm, *const_path;
658
659         if (proxy) {
660                 host = get_proxy_host (session);
661                 const_path = "/";
662         } else {
663                 host = get_host_for_message (session, msg);
664                 const_path = soup_message_get_uri (msg)->path;
665
666                 if (!const_path)
667                         const_path = "/";
668         }
669         g_return_val_if_fail (host != NULL, NULL);
670
671         if (!host->auth_realms)
672                 return NULL;
673
674         path = g_strdup (const_path);
675         dir = path;
676         do {
677                 realm = g_hash_table_lookup (host->auth_realms, path);
678                 if (realm)
679                         break;
680
681                 dir = strrchr (path, '/');
682                 if (dir) {
683                         if (dir[1])
684                                 dir[1] = '\0';
685                         else
686                                 *dir = '\0';
687                 }
688         } while (dir);
689
690         g_free (path);
691         if (realm)
692                 return g_hash_table_lookup (host->auths, realm);
693         else
694                 return NULL;
695 }
696
697 static void
698 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
699 {
700         char *info;
701         gpointer key, value;
702
703         info = soup_auth_get_info (auth);
704         if (g_hash_table_lookup_extended (host->auths, info, &key, &value) &&
705             auth == (SoupAuth *)value) {
706                 g_hash_table_remove (host->auths, info);
707                 g_free (key);
708                 g_object_unref (auth);
709         }
710         g_free (info);
711 }
712
713 static gboolean
714 authenticate_auth (SoupSession *session, SoupAuth *auth,
715                    SoupMessage *msg, gboolean prior_auth_failed,
716                    gboolean proxy)
717 {
718         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
719         const SoupUri *uri;
720         char *username = NULL, *password = NULL;
721
722         if (proxy)
723                 uri = priv->proxy_uri;
724         else
725                 uri = soup_message_get_uri (msg);
726
727         if (uri->passwd && !prior_auth_failed) {
728                 soup_auth_authenticate (auth, uri->user, uri->passwd);
729                 return TRUE;
730         }
731
732         g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
733                        msg, soup_auth_get_scheme_name (auth),
734                        soup_auth_get_realm (auth),
735                        &username, &password);
736         if (username || password)
737                 soup_auth_authenticate (auth, username, password);
738         if (username)
739                 g_free (username);
740         if (password) {
741                 memset (password, 0, strlen (password));
742                 g_free (password);
743         }
744
745         return soup_auth_is_authenticated (auth);
746 }
747
748 static gboolean
749 update_auth_internal (SoupSession *session, SoupMessage *msg,
750                       const GSList *headers, gboolean proxy)
751 {
752         SoupSessionHost *host;
753         SoupAuth *new_auth, *prior_auth, *old_auth;
754         gpointer old_path, old_auth_info;
755         const SoupUri *msg_uri;
756         const char *path;
757         char *auth_info;
758         GSList *pspace, *p;
759         gboolean prior_auth_failed = FALSE;
760
761         if (proxy)
762                 host = get_proxy_host (session);
763         else
764                 host = get_host_for_message (session, msg);
765
766         g_return_val_if_fail (host != NULL, FALSE);
767
768         /* Try to construct a new auth from the headers; if we can't,
769          * there's no way we'll be able to authenticate.
770          */
771         msg_uri = soup_message_get_uri (msg);
772         new_auth = soup_auth_new_from_header_list (headers);
773         if (!new_auth)
774                 return FALSE;
775
776         auth_info = soup_auth_get_info (new_auth);
777
778         /* See if this auth is the same auth we used last time */
779         prior_auth = proxy ? soup_message_get_proxy_auth (msg) : soup_message_get_auth (msg);
780         if (prior_auth) {
781                 char *old_auth_info = soup_auth_get_info (prior_auth);
782
783                 if (!strcmp (old_auth_info, auth_info)) {
784                         /* The server didn't like the username/password we
785                          * provided before. Invalidate it and note this fact.
786                          */
787                         invalidate_auth (host, prior_auth);
788                         prior_auth_failed = TRUE;
789                 }
790                 g_free (old_auth_info);
791         }
792
793         if (!host->auth_realms) {
794                 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
795                 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
796         }
797
798         /* Record where this auth realm is used. RFC 2617 is somewhat
799          * unclear about the scope of protection spaces with regard to
800          * proxies. The only mention of it is as an aside in section
801          * 3.2.1, where it is defining the fields of a Digest
802          * challenge and says that the protection space is always the
803          * entire proxy. Is this the case for all authentication
804          * schemes or just Digest? Who knows, but we're assuming all.
805          */
806         if (proxy)
807                 pspace = g_slist_prepend (NULL, g_strdup (""));
808         else
809                 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
810
811         for (p = pspace; p; p = p->next) {
812                 path = p->data;
813                 if (g_hash_table_lookup_extended (host->auth_realms, path,
814                                                   &old_path, &old_auth_info)) {
815                         g_hash_table_remove (host->auth_realms, old_path);
816                         g_free (old_path);
817                         g_free (old_auth_info);
818                 }
819
820                 g_hash_table_insert (host->auth_realms,
821                                      g_strdup (path), g_strdup (auth_info));
822         }
823         soup_auth_free_protection_space (new_auth, pspace);
824
825         /* Now, make sure the auth is recorded. (If there's a
826          * pre-existing auth, we keep that rather than the new one,
827          * since the old one might already be authenticated.)
828          */
829         old_auth = g_hash_table_lookup (host->auths, auth_info);
830         if (old_auth) {
831                 g_free (auth_info);
832                 g_object_unref (new_auth);
833                 new_auth = old_auth;
834         } else 
835                 g_hash_table_insert (host->auths, auth_info, new_auth);
836
837         /* If we need to authenticate, try to do it. */
838         if (!soup_auth_is_authenticated (new_auth)) {
839                 return authenticate_auth (session, new_auth,
840                                           msg, prior_auth_failed, proxy);
841         }
842
843         /* Otherwise we're good. */
844         return TRUE;
845 }
846
847 static void
848 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
849                          const char *auth_type, const char *auth_realm,
850                          char **username, char **password, gpointer session)
851 {
852         g_signal_emit (session, signals[AUTHENTICATE], 0,
853                        msg, auth_type, auth_realm, username, password);
854 }
855
856 static void
857 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
858                            const char *auth_type, const char *auth_realm,
859                            char **username, char **password,
860                            gpointer user_data)
861 {
862         g_signal_emit (conn, signals[REAUTHENTICATE], 0,
863                        msg, auth_type, auth_realm, username, password);
864 }
865
866
867 static void
868 authorize_handler (SoupMessage *msg, gpointer user_data)
869 {
870         SoupSession *session = user_data;
871         const GSList *headers;
872         gboolean proxy;
873
874         if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
875                 headers = soup_message_get_header_list (msg->response_headers,
876                                                         "Proxy-Authenticate");
877                 proxy = TRUE;
878         } else {
879                 headers = soup_message_get_header_list (msg->response_headers,
880                                                         "WWW-Authenticate");
881                 proxy = FALSE;
882         }
883         if (!headers)
884                 return;
885
886         if (update_auth_internal (session, msg, headers, proxy))
887                 soup_session_requeue_message (session, msg);
888 }
889
890 static void
891 redirect_handler (SoupMessage *msg, gpointer user_data)
892 {
893         SoupSession *session = user_data;
894         const char *new_loc;
895         SoupUri *new_uri;
896
897         new_loc = soup_message_get_header (msg->response_headers, "Location");
898         if (!new_loc)
899                 return;
900
901         /* Location is supposed to be an absolute URI, but some sites
902          * are lame, so we use soup_uri_new_with_base().
903          */
904         new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
905         if (!new_uri) {
906                 soup_message_set_status_full (msg,
907                                               SOUP_STATUS_MALFORMED,
908                                               "Invalid Redirect URL");
909                 return;
910         }
911
912         soup_message_set_uri (msg, new_uri);
913         soup_uri_free (new_uri);
914
915         soup_session_requeue_message (session, msg);
916 }
917
918 static void
919 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
920 {
921         SoupAuth *auth;
922
923         auth = lookup_auth (session, msg, proxy);
924         if (auth && !soup_auth_is_authenticated (auth)) {
925                 if (!authenticate_auth (session, auth, msg, FALSE, proxy))
926                         auth = NULL;
927         }
928
929         if (proxy)
930                 soup_message_set_proxy_auth (msg, auth);
931         else
932                 soup_message_set_auth (msg, auth);
933 }
934
935 static void
936 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
937 {
938         SoupSession *session = SOUP_SESSION (filter);
939         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
940         GSList *f;
941
942         for (f = priv->filters; f; f = f->next) {
943                 filter = f->data;
944                 soup_message_filter_setup_message (filter, msg);
945         }
946
947         add_auth (session, msg, FALSE);
948         soup_message_add_status_code_handler (
949                 msg, SOUP_STATUS_UNAUTHORIZED,
950                 SOUP_HANDLER_POST_BODY,
951                 authorize_handler, session);
952
953         if (priv->proxy_uri) {
954                 add_auth (session, msg, TRUE);
955                 soup_message_add_status_code_handler  (
956                         msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
957                         SOUP_HANDLER_POST_BODY,
958                         authorize_handler, session);
959         }
960 }
961
962 static void
963 find_oldest_connection (gpointer key, gpointer host, gpointer data)
964 {
965         SoupConnection *conn = key, **oldest = data;
966
967         /* Don't prune a connection that is currently in use, or
968          * hasn't been used yet.
969          */
970         if (soup_connection_is_in_use (conn) ||
971             soup_connection_last_used (conn) == 0)
972                 return;
973
974         if (!*oldest || (soup_connection_last_used (conn) <
975                          soup_connection_last_used (*oldest)))
976                 *oldest = conn;
977 }
978
979 /**
980  * soup_session_try_prune_connection:
981  * @session: a #SoupSession
982  *
983  * Finds the least-recently-used idle connection in @session and closes
984  * it.
985  *
986  * Return value: %TRUE if a connection was closed, %FALSE if there are
987  * no idle connections.
988  **/
989 gboolean
990 soup_session_try_prune_connection (SoupSession *session)
991 {
992         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
993         SoupConnection *oldest = NULL;
994
995         g_mutex_lock (priv->host_lock);
996         g_hash_table_foreach (priv->conns, find_oldest_connection,
997                               &oldest);
998         if (oldest) {
999                 /* Ref the connection before unlocking the mutex in
1000                  * case someone else tries to prune it too.
1001                  */
1002                 g_object_ref (oldest);
1003                 g_mutex_unlock (priv->host_lock);
1004                 soup_connection_disconnect (oldest);
1005                 g_object_unref (oldest);
1006                 return TRUE;
1007         } else {
1008                 g_mutex_unlock (priv->host_lock);
1009                 return FALSE;
1010         }
1011 }
1012
1013 static void
1014 connection_disconnected (SoupConnection *conn, gpointer user_data)
1015 {
1016         SoupSession *session = user_data;
1017         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1018         SoupSessionHost *host;
1019
1020         g_mutex_lock (priv->host_lock);
1021
1022         host = g_hash_table_lookup (priv->conns, conn);
1023         if (host) {
1024                 g_hash_table_remove (priv->conns, conn);
1025                 host->connections = g_slist_remove (host->connections, conn);
1026                 host->num_conns--;
1027         }
1028
1029         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1030         priv->num_conns--;
1031
1032         g_mutex_unlock (priv->host_lock);
1033         g_object_unref (conn);
1034 }
1035
1036 static void
1037 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1038 {
1039         SoupSession *session = user_data;
1040         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1041         SoupSessionHost *host;
1042         SoupMessageQueueIter iter;
1043         SoupMessage *msg;
1044
1045         g_mutex_lock (priv->host_lock);
1046
1047         host = g_hash_table_lookup (priv->conns, conn);
1048         if (!host) {
1049                 g_mutex_unlock (priv->host_lock);
1050                 return;
1051         }
1052
1053         if (status == SOUP_STATUS_OK) {
1054                 soup_connection_reserve (conn);
1055                 host->connections = g_slist_prepend (host->connections, conn);
1056                 g_mutex_unlock (priv->host_lock);
1057                 return;
1058         }
1059
1060         /* The connection failed. */
1061         g_mutex_unlock (priv->host_lock);
1062         connection_disconnected (conn, session);
1063
1064         if (host->connections) {
1065                 /* Something went wrong this time, but we have at
1066                  * least one open connection to this host. So just
1067                  * leave the message in the queue so it can use that
1068                  * connection once it's free.
1069                  */
1070                 return;
1071         }
1072
1073         /* There are two possibilities: either status is
1074          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1075          * will create a new connection (and all we need to do here
1076          * is downgrade the message from CONNECTING to QUEUED); or
1077          * status is something else, probably CANT_CONNECT or
1078          * CANT_RESOLVE or the like, in which case we need to cancel
1079          * any messages waiting for this host, since they're out
1080          * of luck.
1081          */
1082         for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1083                 if (get_host_for_message (session, msg) == host) {
1084                         if (status == SOUP_STATUS_TRY_AGAIN) {
1085                                 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1086                                         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1087                         } else {
1088                                 soup_message_set_status (msg, status);
1089                                 soup_session_cancel_message (session, msg);
1090                         }
1091                 }
1092         }
1093 }
1094
1095 /**
1096  * soup_session_get_connection:
1097  * @session: a #SoupSession
1098  * @msg: a #SoupMessage
1099  * @try_pruning: on return, whether or not to try pruning a connection
1100  * @is_new: on return, %TRUE if the returned connection is new and not
1101  * yet connected
1102  * 
1103  * Tries to find or create a connection for @msg; this is an internal
1104  * method for #SoupSession subclasses.
1105  *
1106  * If there is an idle connection to the relevant host available, then
1107  * that connection will be returned (with *@is_new set to %FALSE). The
1108  * connection will be marked "reserved", so the caller must call
1109  * soup_connection_release() if it ends up not using the connection
1110  * right away.
1111  *
1112  * If there is no idle connection available, but it is possible to
1113  * create a new connection, then one will be created and returned,
1114  * with *@is_new set to %TRUE. The caller MUST then call
1115  * soup_connection_connect_sync() or soup_connection_connect_async()
1116  * to connect it. If the connection attempt succeeds, the connection
1117  * will be marked "reserved" and added to @session's connection pool
1118  * once it connects. If the connection attempt fails, the connection
1119  * will be unreffed.
1120  *
1121  * If no connection is available and a new connection cannot be made,
1122  * soup_session_get_connection() will return %NULL. If @session has
1123  * the maximum number of open connections open, but does not have the
1124  * maximum number of per-host connections open to the relevant host,
1125  * then *@try_pruning will be set to %TRUE. In this case, the caller
1126  * can call soup_session_try_prune_connection() to close an idle
1127  * connection, and then try soup_session_get_connection() again. (If
1128  * calling soup_session_try_prune_connection() wouldn't help, then
1129  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1130  *
1131  * Return value: a #SoupConnection, or %NULL
1132  **/
1133 SoupConnection *
1134 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1135                              gboolean *try_pruning, gboolean *is_new)
1136 {
1137         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1138         SoupConnection *conn;
1139         SoupSessionHost *host;
1140         GSList *conns;
1141
1142         g_mutex_lock (priv->host_lock);
1143
1144         host = get_host_for_message (session, msg);
1145         for (conns = host->connections; conns; conns = conns->next) {
1146                 if (!soup_connection_is_in_use (conns->data)) {
1147                         soup_connection_reserve (conns->data);
1148                         g_mutex_unlock (priv->host_lock);
1149                         *is_new = FALSE;
1150                         return conns->data;
1151                 }
1152         }
1153
1154         if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1155                 /* We already started a connection for this
1156                  * message, so don't start another one.
1157                  */
1158                 g_mutex_unlock (priv->host_lock);
1159                 return NULL;
1160         }
1161
1162         if (host->num_conns >= priv->max_conns_per_host) {
1163                 g_mutex_unlock (priv->host_lock);
1164                 return NULL;
1165         }
1166
1167         if (priv->num_conns >= priv->max_conns) {
1168                 *try_pruning = TRUE;
1169                 g_mutex_unlock (priv->host_lock);
1170                 return NULL;
1171         }
1172
1173         /* Make sure priv->proxy_host gets set now while
1174          * we have the host_lock.
1175          */
1176         if (priv->proxy_uri)
1177                 get_proxy_host (session);
1178
1179         conn = g_object_new (
1180                 (priv->use_ntlm ?
1181                  SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1182                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1183                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1184                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1185                 SOUP_CONNECTION_MESSAGE_FILTER, session,
1186                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1187                 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1188                 NULL);
1189         g_signal_connect (conn, "connect_result",
1190                           G_CALLBACK (connect_result),
1191                           session);
1192         g_signal_connect (conn, "disconnected",
1193                           G_CALLBACK (connection_disconnected),
1194                           session);
1195         g_signal_connect (conn, "authenticate",
1196                           G_CALLBACK (connection_authenticate),
1197                           session);
1198         g_signal_connect (conn, "reauthenticate",
1199                           G_CALLBACK (connection_reauthenticate),
1200                           session);
1201
1202         g_hash_table_insert (priv->conns, conn, host);
1203
1204         /* We increment the connection counts so it counts against the
1205          * totals, but we don't add it to the host's connection list
1206          * yet, since it's not ready for use.
1207          */
1208         priv->num_conns++;
1209         host->num_conns++;
1210
1211         /* Mark the request as connecting, so we don't try to open
1212          * another new connection for it while waiting for this one.
1213          */
1214         msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1215
1216         g_mutex_unlock (priv->host_lock);
1217         *is_new = TRUE;
1218         return conn;
1219 }
1220
1221 static void
1222 message_finished (SoupMessage *msg, gpointer user_data)
1223 {
1224         SoupSession *session = user_data;
1225
1226         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1227                 soup_message_queue_remove_message (session->queue, msg);
1228                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1229         }
1230 }
1231
1232 static void
1233 queue_message (SoupSession *session, SoupMessage *msg,
1234                SoupMessageCallbackFn callback, gpointer user_data)
1235 {
1236         g_signal_connect_after (msg, "finished",
1237                                 G_CALLBACK (message_finished), session);
1238
1239         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1240                 soup_message_add_status_class_handler (
1241                         msg, SOUP_STATUS_CLASS_REDIRECT,
1242                         SOUP_HANDLER_POST_BODY,
1243                         redirect_handler, session);
1244         }
1245
1246         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1247         soup_message_queue_append (session->queue, msg);
1248 }
1249
1250 /**
1251  * soup_session_queue_message:
1252  * @session: a #SoupSession
1253  * @msg: the message to queue
1254  * @callback: a #SoupMessageCallbackFn which will be called after the
1255  * message completes or when an unrecoverable error occurs.
1256  * @user_data: a pointer passed to @callback.
1257  * 
1258  * Queues the message @msg for sending. All messages are processed
1259  * while the glib main loop runs. If @msg has been processed before,
1260  * any resources related to the time it was last sent are freed.
1261  *
1262  * Upon message completion, the callback specified in @callback will
1263  * be invoked (in the thread associated with @session's async
1264  * context). If after returning from this callback the message has not
1265  * been requeued, @msg will be unreffed.
1266  */
1267 void
1268 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1269                             SoupMessageCallbackFn callback, gpointer user_data)
1270 {
1271         g_return_if_fail (SOUP_IS_SESSION (session));
1272         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1273
1274         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1275                                                          callback, user_data);
1276 }
1277
1278 static void
1279 requeue_message (SoupSession *session, SoupMessage *msg)
1280 {
1281         msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1282 }
1283
1284 /**
1285  * soup_session_requeue_message:
1286  * @session: a #SoupSession
1287  * @msg: the message to requeue
1288  *
1289  * This causes @msg to be placed back on the queue to be attempted
1290  * again.
1291  **/
1292 void
1293 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1294 {
1295         g_return_if_fail (SOUP_IS_SESSION (session));
1296         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1297
1298         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1299 }
1300
1301
1302 /**
1303  * soup_session_send_message:
1304  * @session: a #SoupSession
1305  * @msg: the message to send
1306  * 
1307  * Synchronously send @msg. This call will not return until the
1308  * transfer is finished successfully or there is an unrecoverable
1309  * error.
1310  *
1311  * @msg is not freed upon return.
1312  *
1313  * Return value: the HTTP status code of the response
1314  */
1315 guint
1316 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1317 {
1318         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1319         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1320
1321         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1322 }
1323
1324
1325 static void
1326 cancel_message (SoupSession *session, SoupMessage *msg)
1327 {
1328         soup_message_queue_remove_message (session->queue, msg);
1329         soup_message_finished (msg);
1330 }
1331
1332 /**
1333  * soup_session_cancel_message:
1334  * @session: a #SoupSession
1335  * @msg: the message to cancel
1336  *
1337  * Causes @session to immediately finish processing @msg. You should
1338  * set a status code on @msg with soup_message_set_status() before
1339  * calling this function.
1340  **/
1341 void
1342 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1343 {
1344         g_return_if_fail (SOUP_IS_SESSION (session));
1345         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1346
1347         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1348 }
1349
1350 static void
1351 gather_conns (gpointer key, gpointer host, gpointer data)
1352 {
1353         SoupConnection *conn = key;
1354         GSList **conns = data;
1355
1356         *conns = g_slist_prepend (*conns, conn);
1357 }
1358
1359 /**
1360  * soup_session_abort:
1361  * @session: the session
1362  *
1363  * Cancels all pending requests in @session.
1364  **/
1365 void
1366 soup_session_abort (SoupSession *session)
1367 {
1368         SoupSessionPrivate *priv;
1369         SoupMessageQueueIter iter;
1370         SoupMessage *msg;
1371         GSList *conns, *c;
1372
1373         g_return_if_fail (SOUP_IS_SESSION (session));
1374         priv = SOUP_SESSION_GET_PRIVATE (session);
1375
1376         for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1377                 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1378                 soup_session_cancel_message (session, msg);
1379         }
1380
1381         /* Close all connections */
1382         g_mutex_lock (priv->host_lock);
1383         conns = NULL;
1384         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1385
1386         for (c = conns; c; c = c->next)
1387                 g_object_ref (c->data);
1388         g_mutex_unlock (priv->host_lock);
1389         for (c = conns; c; c = c->next) {
1390                 soup_connection_disconnect (c->data);
1391                 g_object_unref (c->data);
1392         }
1393
1394         g_slist_free (conns);
1395 }