Add a new property, SOUP_SESSION_IDLE_TIMEOUT, to specify a timeout after
[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 static void
812 find_oldest_connection (gpointer key, gpointer host, gpointer data)
813 {
814         SoupConnection *conn = key, **oldest = data;
815
816         /* Don't prune a connection that is currently in use, or
817          * hasn't been used yet.
818          */
819         if (soup_connection_is_in_use (conn) ||
820             soup_connection_last_used (conn) == 0)
821                 return;
822
823         if (!*oldest || (soup_connection_last_used (conn) <
824                          soup_connection_last_used (*oldest)))
825                 *oldest = conn;
826 }
827
828 /**
829  * soup_session_try_prune_connection:
830  * @session: a #SoupSession
831  *
832  * Finds the least-recently-used idle connection in @session and closes
833  * it.
834  *
835  * Return value: %TRUE if a connection was closed, %FALSE if there are
836  * no idle connections.
837  **/
838 gboolean
839 soup_session_try_prune_connection (SoupSession *session)
840 {
841         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
842         SoupConnection *oldest = NULL;
843
844         g_mutex_lock (priv->host_lock);
845         g_hash_table_foreach (priv->conns, find_oldest_connection,
846                               &oldest);
847         if (oldest) {
848                 /* Ref the connection before unlocking the mutex in
849                  * case someone else tries to prune it too.
850                  */
851                 g_object_ref (oldest);
852                 g_mutex_unlock (priv->host_lock);
853                 soup_connection_disconnect (oldest);
854                 g_object_unref (oldest);
855                 return TRUE;
856         } else {
857                 g_mutex_unlock (priv->host_lock);
858                 return FALSE;
859         }
860 }
861
862 static void
863 connection_disconnected (SoupConnection *conn, gpointer user_data)
864 {
865         SoupSession *session = user_data;
866         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
867         SoupSessionHost *host;
868
869         g_mutex_lock (priv->host_lock);
870
871         host = g_hash_table_lookup (priv->conns, conn);
872         if (host) {
873                 g_hash_table_remove (priv->conns, conn);
874                 host->connections = g_slist_remove (host->connections, conn);
875                 host->num_conns--;
876         }
877
878         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
879         priv->num_conns--;
880
881         g_mutex_unlock (priv->host_lock);
882         g_object_unref (conn);
883 }
884
885 static void
886 connect_result (SoupConnection *conn, guint status, gpointer user_data)
887 {
888         SoupSession *session = user_data;
889         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
890         SoupSessionHost *host;
891         SoupMessageQueueIter iter;
892         SoupMessage *msg;
893
894         g_mutex_lock (priv->host_lock);
895
896         host = g_hash_table_lookup (priv->conns, conn);
897         if (!host) {
898                 g_mutex_unlock (priv->host_lock);
899                 return;
900         }
901
902         if (status == SOUP_STATUS_OK) {
903                 soup_connection_reserve (conn);
904                 host->connections = g_slist_prepend (host->connections, conn);
905                 g_mutex_unlock (priv->host_lock);
906                 return;
907         }
908
909         /* The connection failed. */
910         g_mutex_unlock (priv->host_lock);
911         connection_disconnected (conn, session);
912
913         if (host->connections) {
914                 /* Something went wrong this time, but we have at
915                  * least one open connection to this host. So just
916                  * leave the message in the queue so it can use that
917                  * connection once it's free.
918                  */
919                 return;
920         }
921
922         /* There are two possibilities: either status is
923          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
924          * will create a new connection (and all we need to do here
925          * is downgrade the message from CONNECTING to QUEUED); or
926          * status is something else, probably CANT_CONNECT or
927          * CANT_RESOLVE or the like, in which case we need to cancel
928          * any messages waiting for this host, since they're out
929          * of luck.
930          */
931         for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
932                 if (get_host_for_message (session, msg) == host) {
933                         if (status == SOUP_STATUS_TRY_AGAIN) {
934                                 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
935                                         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
936                         } else {
937                                 soup_session_cancel_message (session, msg,
938                                                              status);
939                         }
940                 }
941         }
942 }
943
944 /**
945  * soup_session_get_connection:
946  * @session: a #SoupSession
947  * @msg: a #SoupMessage
948  * @try_pruning: on return, whether or not to try pruning a connection
949  * @is_new: on return, %TRUE if the returned connection is new and not
950  * yet connected
951  * 
952  * Tries to find or create a connection for @msg; this is an internal
953  * method for #SoupSession subclasses.
954  *
955  * If there is an idle connection to the relevant host available, then
956  * that connection will be returned (with *@is_new set to %FALSE). The
957  * connection will be marked "reserved", so the caller must call
958  * soup_connection_release() if it ends up not using the connection
959  * right away.
960  *
961  * If there is no idle connection available, but it is possible to
962  * create a new connection, then one will be created and returned,
963  * with *@is_new set to %TRUE. The caller MUST then call
964  * soup_connection_connect_sync() or soup_connection_connect_async()
965  * to connect it. If the connection attempt succeeds, the connection
966  * will be marked "reserved" and added to @session's connection pool
967  * once it connects. If the connection attempt fails, the connection
968  * will be unreffed.
969  *
970  * If no connection is available and a new connection cannot be made,
971  * soup_session_get_connection() will return %NULL. If @session has
972  * the maximum number of open connections open, but does not have the
973  * maximum number of per-host connections open to the relevant host,
974  * then *@try_pruning will be set to %TRUE. In this case, the caller
975  * can call soup_session_try_prune_connection() to close an idle
976  * connection, and then try soup_session_get_connection() again. (If
977  * calling soup_session_try_prune_connection() wouldn't help, then
978  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
979  *
980  * Return value: a #SoupConnection, or %NULL
981  **/
982 SoupConnection *
983 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
984                              gboolean *try_pruning, gboolean *is_new)
985 {
986         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
987         SoupConnection *conn;
988         SoupSessionHost *host;
989         GSList *conns;
990
991         g_mutex_lock (priv->host_lock);
992
993         host = get_host_for_message (session, msg);
994         for (conns = host->connections; conns; conns = conns->next) {
995                 if (!soup_connection_is_in_use (conns->data)) {
996                         soup_connection_reserve (conns->data);
997                         g_mutex_unlock (priv->host_lock);
998                         *is_new = FALSE;
999                         return conns->data;
1000                 }
1001         }
1002
1003         if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
1004                 /* We already started a connection for this
1005                  * message, so don't start another one.
1006                  */
1007                 g_mutex_unlock (priv->host_lock);
1008                 return NULL;
1009         }
1010
1011         if (host->num_conns >= priv->max_conns_per_host) {
1012                 g_mutex_unlock (priv->host_lock);
1013                 return NULL;
1014         }
1015
1016         if (priv->num_conns >= priv->max_conns) {
1017                 *try_pruning = TRUE;
1018                 g_mutex_unlock (priv->host_lock);
1019                 return NULL;
1020         }
1021
1022         conn = soup_connection_new (
1023                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1024                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1025                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1026                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1027                 SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
1028                 SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
1029                 NULL);
1030         g_signal_connect (conn, "connect_result",
1031                           G_CALLBACK (connect_result),
1032                           session);
1033         g_signal_connect (conn, "disconnected",
1034                           G_CALLBACK (connection_disconnected),
1035                           session);
1036         g_signal_connect (conn, "request_started",
1037                           G_CALLBACK (connection_started_request),
1038                           session);
1039
1040         g_hash_table_insert (priv->conns, conn, host);
1041
1042         /* We increment the connection counts so it counts against the
1043          * totals, but we don't add it to the host's connection list
1044          * yet, since it's not ready for use.
1045          */
1046         priv->num_conns++;
1047         host->num_conns++;
1048
1049         /* Mark the request as connecting, so we don't try to open
1050          * another new connection for it while waiting for this one.
1051          */
1052         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
1053
1054         g_mutex_unlock (priv->host_lock);
1055         *is_new = TRUE;
1056         return conn;
1057 }
1058
1059 SoupMessageQueue *
1060 soup_session_get_queue (SoupSession *session)
1061 {
1062         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1063
1064         return priv->queue;
1065 }
1066
1067 static void
1068 message_finished (SoupMessage *msg, gpointer user_data)
1069 {
1070         SoupSession *session = user_data;
1071         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1072
1073         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1074                 soup_message_queue_remove_message (priv->queue, msg);
1075                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1076                 g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
1077                 g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
1078         }
1079 }
1080
1081 static void
1082 queue_message (SoupSession *session, SoupMessage *msg,
1083                SoupSessionCallback callback, gpointer user_data)
1084 {
1085         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1086
1087         g_signal_connect_after (msg, "finished",
1088                                 G_CALLBACK (message_finished), session);
1089
1090         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1091                 soup_message_add_header_handler (
1092                         msg, "got_body", "Location",
1093                         G_CALLBACK (redirect_handler), session);
1094         }
1095
1096         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1097         soup_message_queue_append (priv->queue, msg);
1098
1099         g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
1100 }
1101
1102 /**
1103  * SoupSessionCallback:
1104  * @session: the session
1105  * @msg: the message that has finished
1106  * @user_data: the data passed to soup_session_queue_message
1107  *
1108  * Prototype for the callback passed to soup_session_queue_message(),
1109  * qv.
1110  **/
1111
1112 /**
1113  * soup_session_queue_message:
1114  * @session: a #SoupSession
1115  * @msg: the message to queue
1116  * @callback: a #SoupSessionCallback which will be called after the
1117  * message completes or when an unrecoverable error occurs.
1118  * @user_data: a pointer passed to @callback.
1119  * 
1120  * Queues the message @msg for sending. All messages are processed
1121  * while the glib main loop runs. If @msg has been processed before,
1122  * any resources related to the time it was last sent are freed.
1123  *
1124  * Upon message completion, the callback specified in @callback will
1125  * be invoked (in the thread associated with @session's async
1126  * context). If after returning from this callback the message has not
1127  * been requeued, @msg will be unreffed.
1128  */
1129 void
1130 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1131                             SoupSessionCallback callback, gpointer user_data)
1132 {
1133         g_return_if_fail (SOUP_IS_SESSION (session));
1134         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1135
1136         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1137                                                          callback, user_data);
1138 }
1139
1140 static void
1141 requeue_message (SoupSession *session, SoupMessage *msg)
1142 {
1143         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1144 }
1145
1146 /**
1147  * soup_session_requeue_message:
1148  * @session: a #SoupSession
1149  * @msg: the message to requeue
1150  *
1151  * This causes @msg to be placed back on the queue to be attempted
1152  * again.
1153  **/
1154 void
1155 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1156 {
1157         g_return_if_fail (SOUP_IS_SESSION (session));
1158         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1159
1160         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1161 }
1162
1163
1164 /**
1165  * soup_session_send_message:
1166  * @session: a #SoupSession
1167  * @msg: the message to send
1168  * 
1169  * Synchronously send @msg. This call will not return until the
1170  * transfer is finished successfully or there is an unrecoverable
1171  * error.
1172  *
1173  * @msg is not freed upon return.
1174  *
1175  * Return value: the HTTP status code of the response
1176  */
1177 guint
1178 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1179 {
1180         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1181         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1182
1183         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1184 }
1185
1186
1187 /**
1188  * soup_session_pause_message:
1189  * @session: a #SoupSession
1190  * @msg: a #SoupMessage currently running on @session
1191  *
1192  * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1193  * resume I/O.
1194  **/
1195 void
1196 soup_session_pause_message (SoupSession *session,
1197                             SoupMessage *msg)
1198 {
1199         g_return_if_fail (SOUP_IS_SESSION (session));
1200         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1201
1202         soup_message_io_pause (msg);
1203 }
1204
1205 /**
1206  * soup_session_unpause_message:
1207  * @session: a #SoupSession
1208  * @msg: a #SoupMessage currently running on @session
1209  *
1210  * Resumes HTTP I/O on @msg. Use this to resume after calling
1211  * soup_sessino_pause_message().
1212  *
1213  * If @msg is being sent via blocking I/O, this will resume reading or
1214  * writing immediately. If @msg is using non-blocking I/O, then
1215  * reading or writing won't resume until you return to the main loop.
1216  **/
1217 void
1218 soup_session_unpause_message (SoupSession *session,
1219                               SoupMessage *msg)
1220 {
1221         g_return_if_fail (SOUP_IS_SESSION (session));
1222         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1223
1224         soup_message_io_unpause (msg);
1225 }
1226
1227
1228 static void
1229 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1230 {
1231         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1232
1233         soup_message_queue_remove_message (priv->queue, msg);
1234         soup_message_io_stop (msg);
1235         soup_message_set_status (msg, status_code);
1236         soup_message_finished (msg);
1237 }
1238
1239 /**
1240  * soup_session_cancel_message:
1241  * @session: a #SoupSession
1242  * @msg: the message to cancel
1243  * @status_code: status code to set on @msg (generally
1244  * %SOUP_STATUS_CANCELLED)
1245  *
1246  * Causes @session to immediately finish processing @msg, with a final
1247  * status_code of @status_code. Depending on when you cancel it, the
1248  * response state may be incomplete or inconsistent.
1249  **/
1250 void
1251 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1252                              guint status_code)
1253 {
1254         g_return_if_fail (SOUP_IS_SESSION (session));
1255         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1256
1257         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1258 }
1259
1260 static void
1261 gather_conns (gpointer key, gpointer host, gpointer data)
1262 {
1263         SoupConnection *conn = key;
1264         GSList **conns = data;
1265
1266         *conns = g_slist_prepend (*conns, conn);
1267 }
1268
1269 /**
1270  * soup_session_abort:
1271  * @session: the session
1272  *
1273  * Cancels all pending requests in @session.
1274  **/
1275 void
1276 soup_session_abort (SoupSession *session)
1277 {
1278         SoupSessionPrivate *priv;
1279         SoupMessageQueueIter iter;
1280         SoupMessage *msg;
1281         GSList *conns, *c;
1282
1283         g_return_if_fail (SOUP_IS_SESSION (session));
1284         priv = SOUP_SESSION_GET_PRIVATE (session);
1285
1286         for (msg = soup_message_queue_first (priv->queue, &iter);
1287              msg;
1288              msg = soup_message_queue_next (priv->queue, &iter)) {
1289                 soup_session_cancel_message (session, msg,
1290                                              SOUP_STATUS_CANCELLED);
1291         }
1292
1293         /* Close all connections */
1294         g_mutex_lock (priv->host_lock);
1295         conns = NULL;
1296         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1297
1298         for (c = conns; c; c = c->next)
1299                 g_object_ref (c->data);
1300         g_mutex_unlock (priv->host_lock);
1301         for (c = conns; c; c = c->next) {
1302                 soup_connection_disconnect (c->data);
1303                 g_object_unref (c->data);
1304         }
1305
1306         g_slist_free (conns);
1307 }