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