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