Be more aggressive about closing unused persistent connections
[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-auth-basic.h"
18 #include "soup-auth-digest.h"
19 #include "soup-auth-manager.h"
20 #include "soup-auth-manager-ntlm.h"
21 #include "soup-connection.h"
22 #include "soup-marshal.h"
23 #include "soup-message-private.h"
24 #include "soup-message-queue.h"
25 #include "soup-session.h"
26 #include "soup-session-private.h"
27 #include "soup-socket.h"
28 #include "soup-ssl.h"
29 #include "soup-uri.h"
30
31 /**
32  * SECTION:soup-session
33  * @short_description: Soup session state object
34  *
35  * #SoupSession is the object that controls client-side HTTP. A
36  * #SoupSession encapsulates all of the state that libsoup is keeping
37  * on behalf of your program; cached HTTP connections, authentication
38  * information, etc.
39  *
40  * Most applications will only need a single #SoupSession; the primary
41  * reason you might need multiple sessions is if you need to have
42  * multiple independent authentication contexts. (Eg, you are
43  * connecting to a server and authenticating as two different users at
44  * different times; the easiest way to ensure that each #SoupMessage
45  * is sent with the authentication information you intended is to use
46  * one session for the first user, and a second session for the other
47  * user.)
48  *
49  * #SoupSession itself is an abstract class, with two subclasses. If
50  * you are using the glib main loop, you will generally want to use
51  * #SoupSessionAsync, which uses non-blocking I/O and callbacks. On
52  * the other hand, if your application is threaded and you want to do
53  * synchronous I/O in a separate thread from the UI, use
54  * #SoupSessionSync.
55  **/
56
57 typedef struct {
58         SoupURI    *root_uri;
59
60         GSList     *connections;      /* CONTAINS: SoupConnection */
61         guint       num_conns;
62
63         GHashTable *auth_realms;      /* path -> scheme:realm */
64         GHashTable *auths;            /* scheme:realm -> SoupAuth */
65 } SoupSessionHost;
66
67 typedef struct {
68         SoupURI *proxy_uri;
69         SoupAuth *proxy_auth;
70
71         char *ssl_ca_file;
72         SoupSSLCredentials *ssl_creds;
73
74         SoupMessageQueue *queue;
75
76         char *user_agent;
77
78         SoupAuthManager *auth_manager;
79         SoupAuthManagerNTLM *ntlm_manager;
80
81         GHashTable *hosts; /* SoupURI -> SoupSessionHost */
82         GHashTable *conns; /* SoupConnection -> SoupSessionHost */
83         guint num_conns;
84         guint max_conns, max_conns_per_host;
85         guint io_timeout, idle_timeout;
86
87         /* Must hold the host_lock before potentially creating a
88          * new SoupSessionHost, or adding/removing a connection.
89          * Must not emit signals or destroy objects while holding it.
90          */
91         GMutex *host_lock;
92
93         GMainContext *async_context;
94 } SoupSessionPrivate;
95 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
96
97 static void     free_host      (SoupSessionHost *host);
98
99 static void queue_message   (SoupSession *session, SoupMessage *msg,
100                              SoupSessionCallback callback, gpointer user_data);
101 static void requeue_message (SoupSession *session, SoupMessage *msg);
102 static void cancel_message  (SoupSession *session, SoupMessage *msg,
103                              guint status_code);
104
105 /* temporary until we fix this to index hosts by SoupAddress */
106 extern guint     soup_uri_host_hash  (gconstpointer  key);
107 extern gboolean  soup_uri_host_equal (gconstpointer  v1,
108                                       gconstpointer  v2);
109 extern SoupURI  *soup_uri_copy_root  (SoupURI *uri);
110
111 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
112 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
113
114 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
115
116 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
117
118 enum {
119         REQUEST_QUEUED,
120         REQUEST_STARTED,
121         REQUEST_UNQUEUED,
122         AUTHENTICATE,
123         LAST_SIGNAL
124 };
125
126 static guint signals[LAST_SIGNAL] = { 0 };
127
128 enum {
129         PROP_0,
130
131         PROP_PROXY_URI,
132         PROP_MAX_CONNS,
133         PROP_MAX_CONNS_PER_HOST,
134         PROP_USE_NTLM,
135         PROP_SSL_CA_FILE,
136         PROP_ASYNC_CONTEXT,
137         PROP_TIMEOUT,
138         PROP_USER_AGENT,
139         PROP_IDLE_TIMEOUT,
140
141         LAST_PROP
142 };
143
144 static void set_property (GObject *object, guint prop_id,
145                           const GValue *value, GParamSpec *pspec);
146 static void get_property (GObject *object, guint prop_id,
147                           GValue *value, GParamSpec *pspec);
148
149 static void
150 soup_session_init (SoupSession *session)
151 {
152         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
153
154         priv->queue = soup_message_queue_new ();
155
156         priv->host_lock = g_mutex_new ();
157         priv->hosts = g_hash_table_new (soup_uri_host_hash,
158                                         soup_uri_host_equal);
159         priv->conns = g_hash_table_new (NULL, NULL);
160
161         priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
162         priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
163
164         priv->auth_manager = soup_auth_manager_new (session);
165         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
166         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
167 }
168
169 static gboolean
170 foreach_free_host (gpointer key, gpointer host, gpointer data)
171 {
172         free_host (host);
173         return TRUE;
174 }
175
176 static void
177 cleanup_hosts (SoupSessionPrivate *priv)
178 {
179         GHashTable *old_hosts;
180
181         g_mutex_lock (priv->host_lock);
182         old_hosts = priv->hosts;
183         priv->hosts = g_hash_table_new (soup_uri_host_hash,
184                                         soup_uri_host_equal);
185         g_mutex_unlock (priv->host_lock);
186
187         g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
188         g_hash_table_destroy (old_hosts);
189 }
190
191 static void
192 dispose (GObject *object)
193 {
194         SoupSession *session = SOUP_SESSION (object);
195         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
196
197         soup_session_abort (session);
198         cleanup_hosts (priv);
199
200         G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
201 }
202
203 static void
204 finalize (GObject *object)
205 {
206         SoupSession *session = SOUP_SESSION (object);
207         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
208
209         soup_message_queue_destroy (priv->queue);
210
211         g_mutex_free (priv->host_lock);
212         g_hash_table_destroy (priv->hosts);
213         g_hash_table_destroy (priv->conns);
214
215         g_free (priv->user_agent);
216
217         soup_auth_manager_free (priv->auth_manager);
218         if (priv->ntlm_manager)
219                 soup_auth_manager_ntlm_free (priv->ntlm_manager);
220
221         if (priv->proxy_uri)
222                 soup_uri_free (priv->proxy_uri);
223
224         if (priv->ssl_creds)
225                 soup_ssl_free_client_credentials (priv->ssl_creds);
226
227         if (priv->async_context)
228                 g_main_context_unref (priv->async_context);
229
230         G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
231 }
232
233 static void
234 soup_session_class_init (SoupSessionClass *session_class)
235 {
236         GObjectClass *object_class = G_OBJECT_CLASS (session_class);
237
238         g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
239
240         /* virtual method definition */
241         session_class->queue_message = queue_message;
242         session_class->requeue_message = requeue_message;
243         session_class->cancel_message = cancel_message;
244
245         /* virtual method override */
246         object_class->dispose = dispose;
247         object_class->finalize = finalize;
248         object_class->set_property = set_property;
249         object_class->get_property = get_property;
250
251         /* signals */
252
253         /**
254          * SoupSession::request-queued:
255          * @session: the session
256          * @msg: the request that was queued
257          *
258          * Emitted when a request is queued on @session. (Note that
259          * "queued" doesn't just mean soup_session_queue_message();
260          * soup_session_send_message() implicitly queues the message
261          * as well.)
262          *
263          * When sending a request, first #SoupSession::request_queued
264          * is emitted, indicating that the session has become aware of
265          * the request.
266          *
267          * Once a connection is available to send the request on, the
268          * session emits #SoupSession::request_started. Then, various
269          * #SoupMessage signals are emitted as the message is
270          * processed. If the message is requeued, it will emit
271          * #SoupMessage::restarted, which will then be followed by
272          * another #SoupSession::request_started and another set of
273          * #SoupMessage signals when the message is re-sent.
274          *
275          * Eventually, the message will emit #SoupMessage::finished.
276          * Normally, this signals the completion of message
277          * processing. However, it is possible that the application
278          * will requeue the message from the "finished" handler (or
279          * equivalently, from the soup_session_queue_message()
280          * callback). In that case, the process will loop back to
281          * #SoupSession::request_started.
282          *
283          * Eventually, a message will reach "finished" and not be
284          * requeued. At that point, the session will emit
285          * #SoupSession::request_unqueued to indicate that it is done
286          * with the message.
287          *
288          * To sum up: #SoupSession::request_queued and
289          * #SoupSession::request_unqueued are guaranteed to be emitted
290          * exactly once, but #SoupSession::request_started and
291          * #SoupMessage::finished (and all of the other #SoupMessage
292          * signals) may be invoked multiple times for a given message.
293          **/
294         signals[REQUEST_QUEUED] =
295                 g_signal_new ("request-queued",
296                               G_OBJECT_CLASS_TYPE (object_class),
297                               G_SIGNAL_RUN_FIRST,
298                               0, /* FIXME? */
299                               NULL, NULL,
300                               soup_marshal_NONE__OBJECT,
301                               G_TYPE_NONE, 1,
302                               SOUP_TYPE_MESSAGE);
303
304         /**
305          * SoupSession::request-started:
306          * @session: the session
307          * @msg: the request being sent
308          * @socket: the socket the request is being sent on
309          *
310          * Emitted just before a request is sent. See
311          * #SoupSession::request_queued for a detailed description of
312          * the message lifecycle within a session.
313          **/
314         signals[REQUEST_STARTED] =
315                 g_signal_new ("request-started",
316                               G_OBJECT_CLASS_TYPE (object_class),
317                               G_SIGNAL_RUN_FIRST,
318                               G_STRUCT_OFFSET (SoupSessionClass, request_started),
319                               NULL, NULL,
320                               soup_marshal_NONE__OBJECT_OBJECT,
321                               G_TYPE_NONE, 2,
322                               SOUP_TYPE_MESSAGE,
323                               SOUP_TYPE_SOCKET);
324
325         /**
326          * SoupSession::request-unqueued:
327          * @session: the session
328          * @msg: the request that was unqueued
329          *
330          * Emitted when a request is removed from @session's queue,
331          * indicating that @session is done with it. See
332          * #SoupSession::request_queued for a detailed description of the
333          * message lifecycle within a session.
334          **/
335         signals[REQUEST_UNQUEUED] =
336                 g_signal_new ("request-unqueued",
337                               G_OBJECT_CLASS_TYPE (object_class),
338                               G_SIGNAL_RUN_FIRST,
339                               0, /* FIXME? */
340                               NULL, NULL,
341                               soup_marshal_NONE__OBJECT,
342                               G_TYPE_NONE, 1,
343                               SOUP_TYPE_MESSAGE);
344
345         /**
346          * SoupSession::authenticate:
347          * @session: the session
348          * @msg: the #SoupMessage being sent
349          * @auth: the #SoupAuth to authenticate
350          * @retrying: %TRUE if this is the second (or later) attempt
351          *
352          * Emitted when the session requires authentication. If
353          * credentials are available call soup_auth_authenticate() on
354          * @auth. If these credentials fail, the signal will be
355          * emitted again, with @retrying set to %TRUE, which will
356          * continue until you return without calling
357          * soup_auth_authenticate() on @auth.
358          *
359          * Note that this may be emitted before @msg's body has been
360          * fully read.
361          *
362          * If you call soup_session_pause_message() on @msg before
363          * returning, then you can authenticate @auth asynchronously
364          * (as long as you g_object_ref() it to make sure it doesn't
365          * get destroyed), and then unpause @msg when you are ready
366          * for it to continue.
367          **/
368         signals[AUTHENTICATE] =
369                 g_signal_new ("authenticate",
370                               G_OBJECT_CLASS_TYPE (object_class),
371                               G_SIGNAL_RUN_FIRST,
372                               G_STRUCT_OFFSET (SoupSessionClass, authenticate),
373                               NULL, NULL,
374                               soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
375                               G_TYPE_NONE, 3,
376                               SOUP_TYPE_MESSAGE,
377                               SOUP_TYPE_AUTH,
378                               G_TYPE_BOOLEAN);
379
380         /* properties */
381         g_object_class_install_property (
382                 object_class, PROP_PROXY_URI,
383                 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
384                                     "Proxy URI",
385                                     "The HTTP Proxy to use for this session",
386                                     SOUP_TYPE_URI,
387                                     G_PARAM_READWRITE));
388         g_object_class_install_property (
389                 object_class, PROP_MAX_CONNS,
390                 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
391                                   "Max Connection Count",
392                                   "The maximum number of connections that the session can open at once",
393                                   1,
394                                   G_MAXINT,
395                                   SOUP_SESSION_MAX_CONNS_DEFAULT,
396                                   G_PARAM_READWRITE));
397         g_object_class_install_property (
398                 object_class, PROP_MAX_CONNS_PER_HOST,
399                 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
400                                   "Max Per-Host Connection Count",
401                                   "The maximum number of connections that the session can open at once to a given host",
402                                   1,
403                                   G_MAXINT,
404                                   SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
405                                   G_PARAM_READWRITE));
406         g_object_class_install_property (
407                 object_class, PROP_IDLE_TIMEOUT,
408                 g_param_spec_uint (SOUP_SESSION_IDLE_TIMEOUT,
409                                    "Idle Timeout",
410                                    "Connection lifetime when idle",
411                                    0, G_MAXUINT, 0,
412                                    G_PARAM_READWRITE));
413         g_object_class_install_property (
414                 object_class, PROP_USE_NTLM,
415                 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
416                                       "Use NTLM",
417                                       "Whether or not to use NTLM authentication",
418                                       FALSE,
419                                       G_PARAM_READWRITE));
420         g_object_class_install_property (
421                 object_class, PROP_SSL_CA_FILE,
422                 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
423                                      "SSL CA file",
424                                      "File containing SSL CA certificates",
425                                      NULL,
426                                      G_PARAM_READWRITE));
427         g_object_class_install_property (
428                 object_class, PROP_ASYNC_CONTEXT,
429                 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
430                                       "Async GMainContext",
431                                       "The GMainContext to dispatch async I/O in",
432                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
433         g_object_class_install_property (
434                 object_class, PROP_TIMEOUT,
435                 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
436                                    "Timeout value",
437                                    "Value in seconds to timeout a blocking I/O",
438                                    0, G_MAXUINT, 0,
439                                    G_PARAM_READWRITE));
440
441         /**
442          * SoupSession:user-agent:
443          *
444          * If non-%NULL, the value to use for the "User-Agent" header
445          * on #SoupMessage<!-- -->s sent from this session.
446          *
447          * RFC 2616 says: "The User-Agent request-header field
448          * contains information about the user agent originating the
449          * request. This is for statistical purposes, the tracing of
450          * protocol violations, and automated recognition of user
451          * agents for the sake of tailoring responses to avoid
452          * particular user agent limitations. User agents SHOULD
453          * include this field with requests."
454          *
455          * The User-Agent header contains a list of one or more
456          * product tokens, separated by whitespace, with the most
457          * significant product token coming first. The tokens must be
458          * brief, ASCII, and mostly alphanumeric (although "-", "_",
459          * and "." are also allowed), and may optionally include a "/"
460          * followed by a version string. You may also put comments,
461          * enclosed in parentheses, between or after the tokens.
462          *
463          * If you set a %user_agent property that has trailing
464          * whitespace, #SoupSession will append its own product token
465          * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
466          * header for you.
467          **/
468         g_object_class_install_property (
469                 object_class, PROP_USER_AGENT,
470                 g_param_spec_string (SOUP_SESSION_USER_AGENT,
471                                      "User-Agent string",
472                                      "User-Agent string",
473                                      NULL,
474                                      G_PARAM_READWRITE));
475 }
476
477 static gboolean
478 safe_uri_equal (SoupURI *a, SoupURI *b)
479 {
480         if (!a && !b)
481                 return TRUE;
482
483         if ((a && !b) || (b && !a))
484                 return FALSE;
485
486         return soup_uri_equal (a, b);
487 }
488
489 static gboolean
490 safe_str_equal (const char *a, const char *b)
491 {
492         if (!a && !b)
493                 return TRUE;
494
495         if ((a && !b) || (b && !a))
496                 return FALSE;
497
498         return strcmp (a, b) == 0;
499 }
500
501 static void
502 set_property (GObject *object, guint prop_id,
503               const GValue *value, GParamSpec *pspec)
504 {
505         SoupSession *session = SOUP_SESSION (object);
506         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
507         SoupURI *uri;
508         gboolean need_abort = FALSE;
509         gboolean ca_file_changed = FALSE;
510         const char *new_ca_file, *user_agent;
511
512         switch (prop_id) {
513         case PROP_PROXY_URI:
514                 uri = g_value_get_boxed (value);
515
516                 if (!safe_uri_equal (priv->proxy_uri, uri))
517                         need_abort = TRUE;
518
519                 if (priv->proxy_uri)
520                         soup_uri_free (priv->proxy_uri);
521
522                 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
523
524                 if (need_abort) {
525                         soup_session_abort (session);
526                         cleanup_hosts (priv);
527                 }
528
529                 break;
530         case PROP_MAX_CONNS:
531                 priv->max_conns = g_value_get_int (value);
532                 break;
533         case PROP_MAX_CONNS_PER_HOST:
534                 priv->max_conns_per_host = g_value_get_int (value);
535                 break;
536         case PROP_USE_NTLM:
537                 if (g_value_get_boolean (value)) {
538                         if (!priv->ntlm_manager)
539                                 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
540                 } else {
541                         if (priv->ntlm_manager) {
542                                 soup_auth_manager_ntlm_free (priv->ntlm_manager);
543                                 priv->ntlm_manager = NULL;
544                         }
545                 }
546                 break;
547         case PROP_SSL_CA_FILE:
548                 new_ca_file = g_value_get_string (value);
549
550                 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
551                         ca_file_changed = TRUE;
552
553                 g_free (priv->ssl_ca_file);
554                 priv->ssl_ca_file = g_strdup (new_ca_file);
555
556                 if (ca_file_changed) {
557                         if (priv->ssl_creds) {
558                                 soup_ssl_free_client_credentials (priv->ssl_creds);
559                                 priv->ssl_creds = NULL;
560                         }
561
562                         cleanup_hosts (priv);
563                 }
564
565                 break;
566         case PROP_ASYNC_CONTEXT:
567                 priv->async_context = g_value_get_pointer (value);
568                 if (priv->async_context)
569                         g_main_context_ref (priv->async_context);
570                 break;
571         case PROP_TIMEOUT:
572                 priv->io_timeout = g_value_get_uint (value);
573                 break;
574         case PROP_USER_AGENT:
575                 g_free (priv->user_agent);
576                 user_agent = g_value_get_string (value);
577                 if (!user_agent)
578                         priv->user_agent = NULL;
579                 else if (!*user_agent) {
580                         priv->user_agent =
581                                 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
582                 } else if (g_str_has_suffix (user_agent, " ")) {
583                         priv->user_agent =
584                                 g_strdup_printf ("%s%s", user_agent,
585                                                  SOUP_SESSION_USER_AGENT_BASE);
586                 } else
587                         priv->user_agent = g_strdup (user_agent);
588                 break;
589         case PROP_IDLE_TIMEOUT:
590                 priv->idle_timeout = g_value_get_uint (value);
591                 break;
592         default:
593                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594                 break;
595         }
596 }
597
598 static void
599 get_property (GObject *object, guint prop_id,
600               GValue *value, GParamSpec *pspec)
601 {
602         SoupSession *session = SOUP_SESSION (object);
603         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
604
605         switch (prop_id) {
606         case PROP_PROXY_URI:
607                 g_value_set_boxed (value, priv->proxy_uri);
608                 break;
609         case PROP_MAX_CONNS:
610                 g_value_set_int (value, priv->max_conns);
611                 break;
612         case PROP_MAX_CONNS_PER_HOST:
613                 g_value_set_int (value, priv->max_conns_per_host);
614                 break;
615         case PROP_USE_NTLM:
616                 g_value_set_boolean (value, priv->ntlm_manager != NULL);
617                 break;
618         case PROP_SSL_CA_FILE:
619                 g_value_set_string (value, priv->ssl_ca_file);
620                 break;
621         case PROP_ASYNC_CONTEXT:
622                 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
623                 break;
624         case PROP_TIMEOUT:
625                 g_value_set_uint (value, priv->io_timeout);
626                 break;
627         case PROP_USER_AGENT:
628                 g_value_set_string (value, priv->user_agent);
629                 break;
630         case PROP_IDLE_TIMEOUT:
631                 g_value_set_uint (value, priv->idle_timeout);
632                 break;
633         default:
634                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
635                 break;
636         }
637 }
638
639
640 /**
641  * soup_session_get_async_context:
642  * @session: a #SoupSession
643  *
644  * Gets @session's async_context. This does not add a ref to the
645  * context, so you will need to ref it yourself if you want it to
646  * outlive its session.
647  *
648  * Return value: @session's #GMainContext, which may be %NULL
649  **/
650 GMainContext *
651 soup_session_get_async_context (SoupSession *session)
652 {
653         SoupSessionPrivate *priv;
654
655         g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
656         priv = SOUP_SESSION_GET_PRIVATE (session);
657
658         return priv->async_context;
659 }
660
661 /* Hosts */
662
663 static SoupSessionHost *
664 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
665 {
666         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
667         SoupSessionHost *host;
668
669         host = g_slice_new0 (SoupSessionHost);
670         host->root_uri = soup_uri_copy_root (source_uri);
671
672         if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
673             !priv->ssl_creds) {
674                 priv->ssl_creds =
675                         soup_ssl_get_client_credentials (priv->ssl_ca_file);
676         }
677
678         return host;
679 }
680
681 /* Note: get_host_for_message doesn't lock the host_lock. The caller
682  * must do it itself if there's a chance the host doesn't already
683  * exist.
684  */
685 static SoupSessionHost *
686 get_host_for_message (SoupSession *session, SoupMessage *msg)
687 {
688         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
689         SoupSessionHost *host;
690         SoupURI *source = soup_message_get_uri (msg);
691
692         host = g_hash_table_lookup (priv->hosts, source);
693         if (host)
694                 return host;
695
696         host = soup_session_host_new (session, source);
697         g_hash_table_insert (priv->hosts, host->root_uri, host);
698
699         return host;
700 }
701
702 static void
703 free_host (SoupSessionHost *host)
704 {
705         while (host->connections) {
706                 SoupConnection *conn = host->connections->data;
707
708                 host->connections = g_slist_remove (host->connections, conn);
709                 soup_connection_disconnect (conn);
710         }
711
712         soup_uri_free (host->root_uri);
713         g_slice_free (SoupSessionHost, host);
714 }       
715
716 void
717 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
718                                 SoupAuth *auth, gboolean retrying)
719 {
720         g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
721 }
722
723 static void
724 redirect_handler (SoupMessage *msg, gpointer user_data)
725 {
726         SoupSession *session = user_data;
727         const char *new_loc;
728         SoupURI *new_uri;
729
730         new_loc = soup_message_headers_get (msg->response_headers, "Location");
731         g_return_if_fail (new_loc != NULL);
732
733         if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
734             msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
735                 /* Don't redirect non-safe methods */
736                 if (msg->method != SOUP_METHOD_GET &&
737                     msg->method != SOUP_METHOD_HEAD &&
738                     msg->method != SOUP_METHOD_OPTIONS &&
739                     msg->method != SOUP_METHOD_PROPFIND)
740                         return;
741         } else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
742                    msg->status_code == SOUP_STATUS_FOUND) {
743                 /* Redirect using a GET */
744                 g_object_set (msg,
745                               SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
746                               NULL);
747                 soup_message_set_request (msg, NULL,
748                                           SOUP_MEMORY_STATIC, NULL, 0);
749                 soup_message_headers_set_encoding (msg->request_headers,
750                                                    SOUP_ENCODING_NONE);
751         } else {
752                 /* Three possibilities:
753                  *
754                  *   1) This was a non-3xx response that happened to
755                  *      have a "Location" header
756                  *   2) It's a non-redirecty 3xx response (300, 304,
757                  *      305, 306)
758                  *   3) It's some newly-defined 3xx response (308+)
759                  *
760                  * We ignore all of these cases. In the first two,
761                  * redirecting would be explicitly wrong, and in the
762                  * last case, we have no clue if the 3xx response is
763                  * supposed to be redirecty or non-redirecty. Plus,
764                  * 2616 says unrecognized status codes should be
765                  * treated as the equivalent to the x00 code, and we
766                  * don't redirect on 300, so therefore we shouldn't
767                  * redirect on 308+ either.
768                  */
769                 return;
770         }
771
772         /* Location is supposed to be an absolute URI, but some sites
773          * are lame, so we use soup_uri_new_with_base().
774          */
775         new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
776         if (!new_uri) {
777                 soup_message_set_status_full (msg,
778                                               SOUP_STATUS_MALFORMED,
779                                               "Invalid Redirect URL");
780                 return;
781         }
782
783         soup_message_set_uri (msg, new_uri);
784         soup_uri_free (new_uri);
785
786         soup_session_requeue_message (session, msg);
787 }
788
789 static void
790 connection_started_request (SoupConnection *conn, SoupMessage *msg,
791                             gpointer data)
792 {
793         SoupSession *session = data;
794         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
795
796         if (priv->user_agent) {
797                 soup_message_headers_replace (msg->request_headers,
798                                               "User-Agent", priv->user_agent);
799         }
800
801         /* Kludge to deal with the fact that CONNECT msgs come from the
802          * SoupConnection rather than being queued normally.
803          */
804         if (msg->method == SOUP_METHOD_CONNECT)
805                 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
806
807         g_signal_emit (session, signals[REQUEST_STARTED], 0,
808                        msg, soup_connection_get_socket (conn));
809 }
810
811 gboolean
812 soup_session_try_prune_connection (SoupSession *session)
813 {
814         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
815         GPtrArray *conns;
816         GHashTableIter iter;
817         gpointer conn, host;
818         int i;
819
820         conns = g_ptr_array_new ();
821
822         g_mutex_lock (priv->host_lock);
823         g_hash_table_iter_init (&iter, priv->conns);
824         while (g_hash_table_iter_next (&iter, &conn, &host)) {
825                 /* Don't prune a connection that is currently in use,
826                  * or hasn't been used yet.
827                  */
828                 if (!soup_connection_is_in_use (conn) &&
829                     soup_connection_last_used (conn) > 0)
830                         g_ptr_array_add (conns, g_object_ref (conn));
831         }
832         g_mutex_unlock (priv->host_lock);
833
834         if (!conns->len) {
835                 g_ptr_array_free (conns, TRUE);
836                 return FALSE;
837         }
838
839         for (i = 0; i < conns->len; i++) {
840                 soup_connection_disconnect (conns->pdata[i]);
841                 g_object_unref (conns->pdata[i]);
842         }
843         g_ptr_array_free (conns, TRUE);
844
845         return TRUE;
846 }
847
848 static void
849 connection_disconnected (SoupConnection *conn, gpointer user_data)
850 {
851         SoupSession *session = user_data;
852         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
853         SoupSessionHost *host;
854
855         g_mutex_lock (priv->host_lock);
856
857         host = g_hash_table_lookup (priv->conns, conn);
858         if (host) {
859                 g_hash_table_remove (priv->conns, conn);
860                 host->connections = g_slist_remove (host->connections, conn);
861                 host->num_conns--;
862         }
863
864         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
865         priv->num_conns--;
866
867         g_mutex_unlock (priv->host_lock);
868         g_object_unref (conn);
869 }
870
871 static void
872 connect_result (SoupConnection *conn, guint status, gpointer user_data)
873 {
874         SoupSession *session = user_data;
875         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
876         SoupSessionHost *host;
877         SoupMessageQueueIter iter;
878         SoupMessage *msg;
879
880         g_mutex_lock (priv->host_lock);
881
882         host = g_hash_table_lookup (priv->conns, conn);
883         if (!host) {
884                 g_mutex_unlock (priv->host_lock);
885                 return;
886         }
887
888         if (status == SOUP_STATUS_OK) {
889                 soup_connection_reserve (conn);
890                 host->connections = g_slist_prepend (host->connections, conn);
891                 g_mutex_unlock (priv->host_lock);
892                 return;
893         }
894
895         /* The connection failed. */
896         g_mutex_unlock (priv->host_lock);
897         connection_disconnected (conn, session);
898
899         if (host->connections) {
900                 /* Something went wrong this time, but we have at
901                  * least one open connection to this host. So just
902                  * leave the message in the queue so it can use that
903                  * connection once it's free.
904                  */
905                 return;
906         }
907
908         /* There are two possibilities: either status is
909          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
910          * will create a new connection (and all we need to do here
911          * is downgrade the message from CONNECTING to QUEUED); or
912          * status is something else, probably CANT_CONNECT or
913          * CANT_RESOLVE or the like, in which case we need to cancel
914          * any messages waiting for this host, since they're out
915          * of luck.
916          */
917         for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
918                 if (get_host_for_message (session, msg) == host) {
919                         if (status == SOUP_STATUS_TRY_AGAIN) {
920                                 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
921                                         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
922                         } else {
923                                 soup_session_cancel_message (session, msg,
924                                                              status);
925                         }
926                 }
927         }
928 }
929
930 /**
931  * soup_session_get_connection:
932  * @session: a #SoupSession
933  * @msg: a #SoupMessage
934  * @try_pruning: on return, whether or not to try pruning a connection
935  * @is_new: on return, %TRUE if the returned connection is new and not
936  * yet connected
937  * 
938  * Tries to find or create a connection for @msg; this is an internal
939  * method for #SoupSession subclasses.
940  *
941  * If there is an idle connection to the relevant host available, then
942  * that connection will be returned (with *@is_new set to %FALSE). The
943  * connection will be marked "reserved", so the caller must call
944  * soup_connection_release() if it ends up not using the connection
945  * right away.
946  *
947  * If there is no idle connection available, but it is possible to
948  * create a new connection, then one will be created and returned,
949  * with *@is_new set to %TRUE. The caller MUST then call
950  * soup_connection_connect_sync() or soup_connection_connect_async()
951  * to connect it. If the connection attempt succeeds, the connection
952  * will be marked "reserved" and added to @session's connection pool
953  * once it connects. If the connection attempt fails, the connection
954  * will be unreffed.
955  *
956  * If no connection is available and a new connection cannot be made,
957  * soup_session_get_connection() will return %NULL. If @session has
958  * the maximum number of open connections open, but does not have the
959  * maximum number of per-host connections open to the relevant host,
960  * then *@try_pruning will be set to %TRUE. In this case, the caller
961  * can call soup_session_try_prune_connection() to close an idle
962  * connection, and then try soup_session_get_connection() again. (If
963  * calling soup_session_try_prune_connection() wouldn't help, then
964  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
965  *
966  * Return value: a #SoupConnection, or %NULL
967  **/
968 SoupConnection *
969 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
970                              gboolean *try_pruning, gboolean *is_new)
971 {
972         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
973         SoupConnection *conn;
974         SoupSessionHost *host;
975         GSList *conns;
976
977         g_mutex_lock (priv->host_lock);
978
979         host = get_host_for_message (session, msg);
980         for (conns = host->connections; conns; conns = conns->next) {
981                 if (!soup_connection_is_in_use (conns->data)) {
982                         soup_connection_reserve (conns->data);
983                         g_mutex_unlock (priv->host_lock);
984                         *is_new = FALSE;
985                         return conns->data;
986                 }
987         }
988
989         if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
990                 /* We already started a connection for this
991                  * message, so don't start another one.
992                  */
993                 g_mutex_unlock (priv->host_lock);
994                 return NULL;
995         }
996
997         if (host->num_conns >= priv->max_conns_per_host) {
998                 g_mutex_unlock (priv->host_lock);
999                 return NULL;
1000         }
1001
1002         if (priv->num_conns >= priv->max_conns) {
1003                 *try_pruning = TRUE;
1004                 g_mutex_unlock (priv->host_lock);
1005                 return NULL;
1006         }
1007
1008         conn = soup_connection_new (
1009                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1010                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1011                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1012                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1013                 SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
1014                 SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
1015                 NULL);
1016         g_signal_connect (conn, "connect_result",
1017                           G_CALLBACK (connect_result),
1018                           session);
1019         g_signal_connect (conn, "disconnected",
1020                           G_CALLBACK (connection_disconnected),
1021                           session);
1022         g_signal_connect (conn, "request_started",
1023                           G_CALLBACK (connection_started_request),
1024                           session);
1025
1026         g_hash_table_insert (priv->conns, conn, host);
1027
1028         /* We increment the connection counts so it counts against the
1029          * totals, but we don't add it to the host's connection list
1030          * yet, since it's not ready for use.
1031          */
1032         priv->num_conns++;
1033         host->num_conns++;
1034
1035         /* Mark the request as connecting, so we don't try to open
1036          * another new connection for it while waiting for this one.
1037          */
1038         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
1039
1040         g_mutex_unlock (priv->host_lock);
1041         *is_new = TRUE;
1042         return conn;
1043 }
1044
1045 SoupMessageQueue *
1046 soup_session_get_queue (SoupSession *session)
1047 {
1048         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1049
1050         return priv->queue;
1051 }
1052
1053 static void
1054 message_finished (SoupMessage *msg, gpointer user_data)
1055 {
1056         SoupSession *session = user_data;
1057         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1058
1059         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1060                 soup_message_queue_remove_message (priv->queue, msg);
1061                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1062                 g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
1063                 g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
1064         }
1065 }
1066
1067 static void
1068 queue_message (SoupSession *session, SoupMessage *msg,
1069                SoupSessionCallback callback, gpointer user_data)
1070 {
1071         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1072
1073         g_signal_connect_after (msg, "finished",
1074                                 G_CALLBACK (message_finished), session);
1075
1076         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1077                 soup_message_add_header_handler (
1078                         msg, "got_body", "Location",
1079                         G_CALLBACK (redirect_handler), session);
1080         }
1081
1082         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1083         soup_message_queue_append (priv->queue, msg);
1084
1085         g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
1086 }
1087
1088 /**
1089  * SoupSessionCallback:
1090  * @session: the session
1091  * @msg: the message that has finished
1092  * @user_data: the data passed to soup_session_queue_message
1093  *
1094  * Prototype for the callback passed to soup_session_queue_message(),
1095  * qv.
1096  **/
1097
1098 /**
1099  * soup_session_queue_message:
1100  * @session: a #SoupSession
1101  * @msg: the message to queue
1102  * @callback: a #SoupSessionCallback which will be called after the
1103  * message completes or when an unrecoverable error occurs.
1104  * @user_data: a pointer passed to @callback.
1105  * 
1106  * Queues the message @msg for sending. All messages are processed
1107  * while the glib main loop runs. If @msg has been processed before,
1108  * any resources related to the time it was last sent are freed.
1109  *
1110  * Upon message completion, the callback specified in @callback will
1111  * be invoked (in the thread associated with @session's async
1112  * context). If after returning from this callback the message has not
1113  * been requeued, @msg will be unreffed.
1114  */
1115 void
1116 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1117                             SoupSessionCallback callback, gpointer user_data)
1118 {
1119         g_return_if_fail (SOUP_IS_SESSION (session));
1120         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1121
1122         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1123                                                          callback, user_data);
1124 }
1125
1126 static void
1127 requeue_message (SoupSession *session, SoupMessage *msg)
1128 {
1129         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1130 }
1131
1132 /**
1133  * soup_session_requeue_message:
1134  * @session: a #SoupSession
1135  * @msg: the message to requeue
1136  *
1137  * This causes @msg to be placed back on the queue to be attempted
1138  * again.
1139  **/
1140 void
1141 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1142 {
1143         g_return_if_fail (SOUP_IS_SESSION (session));
1144         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1145
1146         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1147 }
1148
1149
1150 /**
1151  * soup_session_send_message:
1152  * @session: a #SoupSession
1153  * @msg: the message to send
1154  * 
1155  * Synchronously send @msg. This call will not return until the
1156  * transfer is finished successfully or there is an unrecoverable
1157  * error.
1158  *
1159  * @msg is not freed upon return.
1160  *
1161  * Return value: the HTTP status code of the response
1162  */
1163 guint
1164 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1165 {
1166         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1167         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1168
1169         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1170 }
1171
1172
1173 /**
1174  * soup_session_pause_message:
1175  * @session: a #SoupSession
1176  * @msg: a #SoupMessage currently running on @session
1177  *
1178  * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1179  * resume I/O.
1180  **/
1181 void
1182 soup_session_pause_message (SoupSession *session,
1183                             SoupMessage *msg)
1184 {
1185         g_return_if_fail (SOUP_IS_SESSION (session));
1186         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1187
1188         soup_message_io_pause (msg);
1189 }
1190
1191 /**
1192  * soup_session_unpause_message:
1193  * @session: a #SoupSession
1194  * @msg: a #SoupMessage currently running on @session
1195  *
1196  * Resumes HTTP I/O on @msg. Use this to resume after calling
1197  * soup_sessino_pause_message().
1198  *
1199  * If @msg is being sent via blocking I/O, this will resume reading or
1200  * writing immediately. If @msg is using non-blocking I/O, then
1201  * reading or writing won't resume until you return to the main loop.
1202  **/
1203 void
1204 soup_session_unpause_message (SoupSession *session,
1205                               SoupMessage *msg)
1206 {
1207         g_return_if_fail (SOUP_IS_SESSION (session));
1208         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1209
1210         soup_message_io_unpause (msg);
1211 }
1212
1213
1214 static void
1215 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1216 {
1217         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1218
1219         soup_message_queue_remove_message (priv->queue, msg);
1220         soup_message_io_stop (msg);
1221         soup_message_set_status (msg, status_code);
1222         soup_message_finished (msg);
1223 }
1224
1225 /**
1226  * soup_session_cancel_message:
1227  * @session: a #SoupSession
1228  * @msg: the message to cancel
1229  * @status_code: status code to set on @msg (generally
1230  * %SOUP_STATUS_CANCELLED)
1231  *
1232  * Causes @session to immediately finish processing @msg, with a final
1233  * status_code of @status_code. Depending on when you cancel it, the
1234  * response state may be incomplete or inconsistent.
1235  **/
1236 void
1237 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1238                              guint status_code)
1239 {
1240         g_return_if_fail (SOUP_IS_SESSION (session));
1241         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1242
1243         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1244 }
1245
1246 static void
1247 gather_conns (gpointer key, gpointer host, gpointer data)
1248 {
1249         SoupConnection *conn = key;
1250         GSList **conns = data;
1251
1252         *conns = g_slist_prepend (*conns, conn);
1253 }
1254
1255 /**
1256  * soup_session_abort:
1257  * @session: the session
1258  *
1259  * Cancels all pending requests in @session.
1260  **/
1261 void
1262 soup_session_abort (SoupSession *session)
1263 {
1264         SoupSessionPrivate *priv;
1265         SoupMessageQueueIter iter;
1266         SoupMessage *msg;
1267         GSList *conns, *c;
1268
1269         g_return_if_fail (SOUP_IS_SESSION (session));
1270         priv = SOUP_SESSION_GET_PRIVATE (session);
1271
1272         for (msg = soup_message_queue_first (priv->queue, &iter);
1273              msg;
1274              msg = soup_message_queue_next (priv->queue, &iter)) {
1275                 soup_session_cancel_message (session, msg,
1276                                              SOUP_STATUS_CANCELLED);
1277         }
1278
1279         /* Close all connections */
1280         g_mutex_lock (priv->host_lock);
1281         conns = NULL;
1282         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1283
1284         for (c = conns; c; c = c->next)
1285                 g_object_ref (c->data);
1286         g_mutex_unlock (priv->host_lock);
1287         for (c = conns; c; c = c->next) {
1288                 soup_connection_disconnect (c->data);
1289                 g_object_unref (c->data);
1290         }
1291
1292         g_slist_free (conns);
1293 }