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