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