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