(soup_session_abort): New, to cancel all pending requests.
[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-session.h"
17 #include "soup-connection.h"
18 #include "soup-connection-ntlm.h"
19 #include "soup-marshal.h"
20 #include "soup-message-queue.h"
21 #include "soup-private.h"
22
23 typedef struct {
24         SoupUri    *root_uri;
25         guint       error;
26
27         GSList     *connections;      /* CONTAINS: SoupConnection */
28         guint       num_conns;
29
30         GHashTable *auth_realms;      /* path -> scheme:realm */
31         GHashTable *auths;            /* scheme:realm -> SoupAuth */
32 } SoupSessionHost;
33
34 struct SoupSessionPrivate {
35         SoupUri *proxy_uri;
36         guint max_conns, max_conns_per_host;
37         gboolean use_ntlm;
38
39         SoupMessageQueue *queue;
40
41         GHashTable *hosts; /* SoupUri -> SoupSessionHost */
42         GHashTable *conns; /* SoupConnection -> SoupSessionHost */
43         guint num_conns;
44
45         SoupSessionHost *proxy_host;
46 };
47
48 static guint    host_uri_hash  (gconstpointer key);
49 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
50
51 static gboolean run_queue (SoupSession *session, gboolean try_pruning);
52
53 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
54 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
55
56 #define PARENT_TYPE G_TYPE_OBJECT
57 static GObjectClass *parent_class;
58
59 enum {
60         AUTHENTICATE,
61         REAUTHENTICATE,
62         LAST_SIGNAL
63 };
64
65 static guint signals[LAST_SIGNAL] = { 0 };
66
67 enum {
68   PROP_0,
69
70   PROP_PROXY_URI,
71   PROP_MAX_CONNS,
72   PROP_MAX_CONNS_PER_HOST,
73   PROP_USE_NTLM,
74
75   LAST_PROP
76 };
77
78 static void set_property (GObject *object, guint prop_id,
79                           const GValue *value, GParamSpec *pspec);
80 static void get_property (GObject *object, guint prop_id,
81                           GValue *value, GParamSpec *pspec);
82
83 static void
84 init (GObject *object)
85 {
86         SoupSession *session = SOUP_SESSION (object);
87
88         session->priv = g_new0 (SoupSessionPrivate, 1);
89         session->priv->queue = soup_message_queue_new ();
90         session->priv->hosts = g_hash_table_new (host_uri_hash,
91                                                  host_uri_equal);
92         session->priv->conns = g_hash_table_new (NULL, NULL);
93
94         session->priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
95         session->priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
96 }
97
98 static void
99 dispose (GObject *object)
100 {
101         SoupSession *session = SOUP_SESSION (object);
102
103         soup_session_abort (session);
104
105         G_OBJECT_CLASS (parent_class)->dispose (object);
106 }
107
108 static void
109 finalize (GObject *object)
110 {
111         SoupSession *session = SOUP_SESSION (object);
112
113         soup_message_queue_destroy (session->priv->queue);
114         g_free (session->priv);
115
116         G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 static void
120 class_init (GObjectClass *object_class)
121 {
122         parent_class = g_type_class_ref (PARENT_TYPE);
123
124         /* virtual method override */
125         object_class->dispose = dispose;
126         object_class->finalize = finalize;
127         object_class->set_property = set_property;
128         object_class->get_property = get_property;
129
130         /* signals */
131         signals[AUTHENTICATE] =
132                 g_signal_new ("authenticate",
133                               G_OBJECT_CLASS_TYPE (object_class),
134                               G_SIGNAL_RUN_FIRST,
135                               G_STRUCT_OFFSET (SoupSessionClass, authenticate),
136                               NULL, NULL,
137                               soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
138                               G_TYPE_NONE, 5,
139                               SOUP_TYPE_MESSAGE,
140                               G_TYPE_STRING,
141                               G_TYPE_STRING,
142                               G_TYPE_POINTER,
143                               G_TYPE_POINTER);
144         signals[REAUTHENTICATE] =
145                 g_signal_new ("reauthenticate",
146                               G_OBJECT_CLASS_TYPE (object_class),
147                               G_SIGNAL_RUN_FIRST,
148                               G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
149                               NULL, NULL,
150                               soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
151                               G_TYPE_NONE, 5,
152                               SOUP_TYPE_MESSAGE,
153                               G_TYPE_STRING,
154                               G_TYPE_STRING,
155                               G_TYPE_POINTER,
156                               G_TYPE_POINTER);
157
158         /* properties */
159         g_object_class_install_property (
160                 object_class, PROP_PROXY_URI,
161                 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
162                                       "Proxy URI",
163                                       "The HTTP Proxy to use for this session",
164                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
165         g_object_class_install_property (
166                 object_class, PROP_MAX_CONNS,
167                 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
168                                   "Max Connection Count",
169                                   "The maximum number of connections that the session can open at once",
170                                   1,
171                                   G_MAXINT,
172                                   10,
173                                   G_PARAM_READWRITE));
174         g_object_class_install_property (
175                 object_class, PROP_MAX_CONNS_PER_HOST,
176                 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
177                                   "Max Per-Host Connection Count",
178                                   "The maximum number of connections that the session can open at once to a given host",
179                                   1,
180                                   G_MAXINT,
181                                   4,
182                                   G_PARAM_READWRITE));
183         g_object_class_install_property (
184                 object_class, PROP_USE_NTLM,
185                 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
186                                       "Use NTLM",
187                                       "Whether or not to use NTLM authentication",
188                                       FALSE,
189                                       G_PARAM_READWRITE));
190 }
191
192 SOUP_MAKE_TYPE (soup_session, SoupSession, class_init, init, PARENT_TYPE)
193
194 SoupSession *
195 soup_session_new (void)
196 {
197         return g_object_new (SOUP_TYPE_SESSION, NULL);
198 }
199
200 SoupSession *
201 soup_session_new_with_options (const char *optname1, ...)
202 {
203         SoupSession *session;
204         va_list ap;
205
206         va_start (ap, optname1);
207         session = (SoupSession *)g_object_new_valist (SOUP_TYPE_SESSION, optname1, ap);
208         va_end (ap);
209
210         return session;
211 }
212
213 static void
214 set_property (GObject *object, guint prop_id,
215               const GValue *value, GParamSpec *pspec)
216 {
217         SoupSession *session = SOUP_SESSION (object);
218         gpointer pval;
219
220         switch (prop_id) {
221         case PROP_PROXY_URI:
222                 pval = g_value_get_pointer (value);
223                 session->priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
224                 break;
225         case PROP_MAX_CONNS:
226                 session->priv->max_conns = g_value_get_int (value);
227                 break;
228         case PROP_MAX_CONNS_PER_HOST:
229                 session->priv->max_conns_per_host = g_value_get_int (value);
230                 break;
231         case PROP_USE_NTLM:
232                 session->priv->use_ntlm = g_value_get_boolean (value);
233                 break;
234         default:
235                 break;
236         }
237 }
238
239 static void
240 get_property (GObject *object, guint prop_id,
241               GValue *value, GParamSpec *pspec)
242 {
243         SoupSession *session = SOUP_SESSION (object);
244
245         switch (prop_id) {
246         case PROP_PROXY_URI:
247                 g_value_set_pointer (value, session->priv->proxy_uri ?
248                                      soup_uri_copy (session->priv->proxy_uri) :
249                                      NULL);
250                 break;
251         case PROP_MAX_CONNS:
252                 g_value_set_int (value, session->priv->max_conns);
253                 break;
254         case PROP_MAX_CONNS_PER_HOST:
255                 g_value_set_int (value, session->priv->max_conns_per_host);
256                 break;
257         case PROP_USE_NTLM:
258                 g_value_set_boolean (value, session->priv->use_ntlm);
259                 break;
260         default:
261                 break;
262         }
263 }
264
265
266 /* Hosts */
267 static guint
268 host_uri_hash (gconstpointer key)
269 {
270         const SoupUri *uri = key;
271
272         return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
273 }
274
275 static gboolean
276 host_uri_equal (gconstpointer v1, gconstpointer v2)
277 {
278         const SoupUri *one = v1;
279         const SoupUri *two = v2;
280
281         if (one->protocol != two->protocol)
282                 return FALSE;
283         if (one->port != two->port)
284                 return FALSE;
285
286         return strcmp (one->host, two->host) == 0;
287 }
288
289 static SoupSessionHost *
290 get_host_for_message (SoupSession *session, SoupMessage *msg)
291 {
292         SoupSessionHost *host;
293         const SoupUri *source = soup_message_get_uri (msg);
294
295         host = g_hash_table_lookup (session->priv->hosts, source);
296         if (host)
297                 return host;
298
299         host = g_new0 (SoupSessionHost, 1);
300         host->root_uri = soup_uri_copy_root (source);
301
302         g_hash_table_insert (session->priv->hosts, host->root_uri, host);
303         return host;
304 }
305
306
307 /* Authentication */
308
309 static SoupAuth *
310 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
311 {
312         SoupSessionHost *host;
313         char *path, *dir;
314         const char *realm, *const_path;
315
316         if (proxy) {
317                 host = session->priv->proxy_host;
318                 const_path = "/";
319         } else {
320                 host = get_host_for_message (session, msg);
321                 const_path = soup_message_get_uri (msg)->path;
322         }
323         g_return_val_if_fail (host != NULL, NULL);
324
325         if (!host->auth_realms)
326                 return NULL;
327
328         path = g_strdup (const_path);
329         dir = path;
330         do {
331                 realm = g_hash_table_lookup (host->auth_realms, path);
332                 if (realm)
333                         break;
334
335                 dir = strrchr (path, '/');
336                 if (dir)
337                         *dir = '\0';
338         } while (dir);
339
340         g_free (path);
341         if (realm)
342                 return g_hash_table_lookup (host->auths, realm);
343         else
344                 return NULL;
345 }
346
347 static void
348 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
349 {
350         char *realm;
351         gpointer key, value;
352
353         realm = g_strdup_printf ("%s:%s",
354                                  soup_auth_get_scheme_name (auth),
355                                  soup_auth_get_realm (auth));
356
357         if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
358             auth == (SoupAuth *)value) {
359                 g_hash_table_remove (host->auths, realm);
360                 g_free (key);
361                 g_object_unref (auth);
362         }
363         g_free (realm);
364 }
365
366 static gboolean
367 authenticate_auth (SoupSession *session, SoupAuth *auth,
368                    SoupMessage *msg, gboolean prior_auth_failed)
369 {
370         const SoupUri *uri = soup_message_get_uri (msg);
371         char *username = NULL, *password = NULL;
372
373         if (uri->passwd && !prior_auth_failed) {
374                 soup_auth_authenticate (auth, uri->user, uri->passwd);
375                 return TRUE;
376         }
377
378         g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
379                        msg, soup_auth_get_scheme_name (auth),
380                        soup_auth_get_realm (auth),
381                        &username, &password);
382         if (username || password)
383                 soup_auth_authenticate (auth, username, password);
384         if (username)
385                 g_free (username);
386         if (password) {
387                 memset (password, 0, strlen (password));
388                 g_free (password);
389         }
390
391         return soup_auth_is_authenticated (auth);
392 }
393
394 static gboolean
395 update_auth_internal (SoupSession *session, SoupMessage *msg,
396                       const GSList *headers, gboolean proxy,
397                       gboolean got_unauthorized)
398 {
399         SoupSessionHost *host;
400         SoupAuth *new_auth, *prior_auth, *old_auth;
401         gpointer old_path, old_realm;
402         const SoupUri *msg_uri;
403         const char *path;
404         char *realm;
405         GSList *pspace, *p;
406         gboolean prior_auth_failed = FALSE;
407
408         host = get_host_for_message (session, msg);
409         g_return_val_if_fail (host != NULL, FALSE);
410
411         /* Try to construct a new auth from the headers; if we can't,
412          * there's no way we'll be able to authenticate.
413          */
414         msg_uri = soup_message_get_uri (msg);
415         new_auth = soup_auth_new_from_header_list (headers);
416         if (!new_auth)
417                 return FALSE;
418
419         /* See if this auth is the same auth we used last time */
420         prior_auth = lookup_auth (session, msg, proxy);
421         if (prior_auth &&
422             G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
423             !strcmp (soup_auth_get_realm (prior_auth),
424                      soup_auth_get_realm (new_auth))) {
425                 if (!got_unauthorized) {
426                         /* The user is just trying to preauthenticate
427                          * using information we already have, so
428                          * there's nothing more that needs to be done.
429                          */
430                         g_object_unref (new_auth);
431                         return TRUE;
432                 }
433
434                 /* The server didn't like the username/password we
435                  * provided before. Invalidate it and note this fact.
436                  */
437                 invalidate_auth (host, prior_auth);
438                 prior_auth_failed = TRUE;
439         }
440
441         if (!host->auth_realms) {
442                 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
443                 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
444         }
445
446         /* Record where this auth realm is used */
447         realm = g_strdup_printf ("%s:%s",
448                                  soup_auth_get_scheme_name (new_auth),
449                                  soup_auth_get_realm (new_auth));
450         pspace = soup_auth_get_protection_space (new_auth, msg_uri);
451         for (p = pspace; p; p = p->next) {
452                 path = p->data;
453                 if (g_hash_table_lookup_extended (host->auth_realms, path,
454                                                   &old_path, &old_realm)) {
455                         g_hash_table_remove (host->auth_realms, old_path);
456                         g_free (old_path);
457                         g_free (old_realm);
458                 }
459
460                 g_hash_table_insert (host->auth_realms,
461                                      g_strdup (path), g_strdup (realm));
462         }
463         soup_auth_free_protection_space (new_auth, pspace);
464
465         /* Now, make sure the auth is recorded. (If there's a
466          * pre-existing auth, we keep that rather than the new one,
467          * since the old one might already be authenticated.)
468          */
469         old_auth = g_hash_table_lookup (host->auths, realm);
470         if (old_auth) {
471                 g_free (realm);
472                 g_object_unref (new_auth);
473                 new_auth = old_auth;
474         } else 
475                 g_hash_table_insert (host->auths, realm, new_auth);
476
477         /* If we need to authenticate, try to do it. */
478         if (!soup_auth_is_authenticated (new_auth)) {
479                 return authenticate_auth (session, new_auth,
480                                           msg, prior_auth_failed);
481         }
482
483         /* Otherwise we're good. */
484         return TRUE;
485 }
486
487 static void
488 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
489                          const char *auth_type, const char *auth_realm,
490                          char **username, char **password, gpointer session)
491 {
492         g_signal_emit (session, signals[AUTHENTICATE], 0,
493                        msg, auth_type, auth_realm, username, password);
494 }
495
496 static void
497 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
498                            const char *auth_type, const char *auth_realm,
499                            char **username, char **password,
500                            gpointer user_data)
501 {
502         g_signal_emit (conn, signals[REAUTHENTICATE], 0,
503                        msg, auth_type, auth_realm, username, password);
504 }
505
506
507 static void
508 authorize_handler (SoupMessage *msg, gpointer user_data)
509 {
510         SoupSession *session = user_data;
511         const GSList *headers;
512         gboolean proxy;
513
514         if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
515                 headers = soup_message_get_header_list (msg->response_headers,
516                                                         "Proxy-Authenticate");
517                 proxy = TRUE;
518         } else {
519                 headers = soup_message_get_header_list (msg->response_headers,
520                                                         "WWW-Authenticate");
521                 proxy = FALSE;
522         }
523         if (!headers)
524                 return;
525
526         if (update_auth_internal (session, msg, headers, proxy, TRUE))
527                 soup_session_requeue_message (session, msg);
528 }
529
530 static void
531 redirect_handler (SoupMessage *msg, gpointer user_data)
532 {
533         SoupSession *session = user_data;
534         const char *new_loc;
535         SoupUri *new_uri;
536
537         new_loc = soup_message_get_header (msg->response_headers, "Location");
538         if (!new_loc)
539                 return;
540         new_uri = soup_uri_new (new_loc);
541         if (!new_uri)
542                 goto INVALID_REDIRECT;
543
544         soup_message_set_uri (msg, new_uri);
545         soup_uri_free (new_uri);
546
547         soup_session_requeue_message (session, msg);
548         return;
549
550  INVALID_REDIRECT:
551         soup_message_set_status_full (msg,
552                                       SOUP_STATUS_MALFORMED,
553                                       "Invalid Redirect URL");
554 }
555
556 static void
557 request_finished (SoupMessage *req, gpointer user_data)
558 {
559         req->status = SOUP_MESSAGE_STATUS_FINISHED;
560 }
561
562 static void
563 final_finished (SoupMessage *req, gpointer user_data)
564 {
565         SoupSession *session = user_data;
566
567         if (!SOUP_MESSAGE_IS_STARTING (req)) {
568                 soup_message_queue_remove_message (session->priv->queue, req);
569
570                 g_signal_handlers_disconnect_by_func (req, request_finished, session);
571                 g_signal_handlers_disconnect_by_func (req, final_finished, session);
572                 g_object_unref (req);
573
574                 run_queue (session, FALSE);
575         }
576 }
577
578 static void
579 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
580 {
581         const char *header = proxy ? "Proxy-Authorization" : "Authorization";
582         SoupAuth *auth;
583         char *token;
584
585         soup_message_remove_header (msg->request_headers, header);
586
587         auth = lookup_auth (session, msg, proxy);
588         if (!auth)
589                 return;
590         if (!soup_auth_is_authenticated (auth) &&
591             !authenticate_auth (session, auth, msg, FALSE))
592                 return;
593
594         token = soup_auth_get_authorization (auth, msg);
595         if (token) {
596                 soup_message_add_header (msg->request_headers, header, token);
597                 g_free (token);
598         }
599 }
600
601 static void
602 send_request (SoupSession *session, SoupMessage *req, SoupConnection *conn)
603 {
604         req->status = SOUP_MESSAGE_STATUS_RUNNING;
605
606         add_auth (session, req, FALSE);
607         if (session->priv->proxy_uri)
608                 add_auth (session, req, TRUE);
609         soup_connection_send_request (conn, req);
610 }
611
612 static void
613 find_oldest_connection (gpointer key, gpointer host, gpointer data)
614 {
615         SoupConnection *conn = key, **oldest = data;
616
617         /* Don't prune a connection that hasn't even been used yet. */
618         if (soup_connection_last_used (conn) == 0)
619                 return;
620
621         if (!*oldest || (soup_connection_last_used (conn) <
622                          soup_connection_last_used (*oldest)))
623                 *oldest = conn;
624 }
625
626 static gboolean
627 try_prune_connection (SoupSession *session)
628 {
629         SoupConnection *oldest = NULL;
630
631         g_hash_table_foreach (session->priv->conns, find_oldest_connection,
632                               &oldest);
633         if (oldest) {
634                 soup_connection_disconnect (oldest);
635                 g_object_unref (oldest);
636                 return TRUE;
637         } else
638                 return FALSE;
639 }
640
641 static void connection_closed (SoupConnection *conn, SoupSession *session);
642
643 static void
644 cleanup_connection (SoupSession *session, SoupConnection *conn)
645 {
646         SoupSessionHost *host =
647                 g_hash_table_lookup (session->priv->conns, conn);
648
649         g_return_if_fail (host != NULL);
650
651         g_hash_table_remove (session->priv->conns, conn);
652         g_signal_handlers_disconnect_by_func (conn, connection_closed, session);
653         session->priv->num_conns--;
654
655         host->connections = g_slist_remove (host->connections, conn);
656         host->num_conns--;
657 }
658
659 static void
660 connection_closed (SoupConnection *conn, SoupSession *session)
661 {
662         cleanup_connection (session, conn);
663
664         /* Run the queue in case anyone was waiting for a connection
665          * to be closed.
666          */
667         run_queue (session, FALSE);
668 }
669
670 static void
671 got_connection (SoupConnection *conn, guint status, gpointer user_data)
672 {
673         SoupSession *session = user_data;
674         SoupSessionHost *host = g_hash_table_lookup (session->priv->conns, conn);
675
676         g_return_if_fail (host != NULL);
677
678         if (status == SOUP_STATUS_OK) {
679                 host->connections = g_slist_prepend (host->connections, conn);
680                 run_queue (session, FALSE);
681                 return;
682         }
683
684         /* We failed */
685         cleanup_connection (session, conn);
686         g_object_unref (conn);
687
688         if (host->connections) {
689                 /* Something went wrong this time, but we have at
690                  * least one open connection to this host. So just
691                  * leave the message in the queue so it can use that
692                  * connection once it's free.
693                  */
694                 return;
695         }
696
697         /* Flush any queued messages for this host */
698         host->error = status;
699         run_queue (session, FALSE);
700
701         if (status != SOUP_STATUS_CANT_RESOLVE &&
702             status != SOUP_STATUS_CANT_RESOLVE_PROXY) {
703                 /* If the error was "can't resolve", then it's not likely
704                  * to improve. But if it was something else, it may have
705                  * been transient, so we clear the error so the user can
706                  * try again later.
707                  */
708                 host->error = 0;
709         }
710 }
711
712 static gboolean
713 run_queue (SoupSession *session, gboolean try_pruning)
714 {
715         SoupMessageQueueIter iter;
716         SoupMessage *msg;
717         SoupConnection *conn;
718         SoupSessionHost *host;
719         gboolean skipped_any = FALSE, started_any = FALSE;
720         GSList *conns;
721
722         /* FIXME: prefer CONNECTING messages */
723
724  try_again:
725         for (msg = soup_message_queue_first (session->priv->queue, &iter); msg; msg = soup_message_queue_next (session->priv->queue, &iter)) {
726
727                 if (!SOUP_MESSAGE_IS_STARTING (msg))
728                         continue;
729
730                 host = get_host_for_message (session, msg);
731
732                 /* If the hostname is known to be bad, fail right away */
733                 if (host->error) {
734                         soup_message_set_status (msg, host->error);
735                         soup_message_finished (msg);
736                 }
737
738                 /* If there is an idle connection, use it */
739                 for (conns = host->connections; conns; conns = conns->next) {
740                         if (!soup_connection_is_in_use (conns->data))
741                                 break;
742                 }
743                 if (conns) {
744                         send_request (session, msg, conns->data);
745                         started_any = TRUE;
746                         continue;
747                 }
748
749                 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
750                         /* We already started a connection for this
751                          * message, so don't start another one.
752                          */
753                         continue;
754                 }
755
756                 /* If we have the max number of per-host connections
757                  * or total connections open, we'll have to wait.
758                  */
759                 if (host->num_conns >= session->priv->max_conns_per_host)
760                         continue;
761                 else if (session->priv->num_conns >= session->priv->max_conns) {
762                         /* In this case, closing an idle connection
763                          * somewhere else would let us open one here.
764                          */
765                         skipped_any = TRUE;
766                         continue;
767                 }
768
769                 /* Otherwise, open a new connection */
770                 conn = g_object_new (
771                         (session->priv->use_ntlm ?
772                          SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
773                         SOUP_CONNECTION_DEST_URI, host->root_uri,
774                         SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
775                         NULL);
776                 g_signal_connect (conn, "authenticate",
777                                   G_CALLBACK (connection_authenticate),
778                                   session);
779                 g_signal_connect (conn, "reauthenticate",
780                                   G_CALLBACK (connection_reauthenticate),
781                                   session);
782
783                 soup_connection_connect_async (conn, got_connection, session);
784                 g_signal_connect (conn, "disconnected",
785                                   G_CALLBACK (connection_closed), session);
786                 g_hash_table_insert (session->priv->conns, conn, host);
787                 session->priv->num_conns++;
788
789                 /* Increment the host's connection count, but don't add
790                  * this connection to the list yet, since it's not ready.
791                  */
792                 host->num_conns++;
793
794                 /* Mark the request as connecting, so we don't try to
795                  * open another new connection for it next time around.
796                  */
797                 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
798
799                 started_any = TRUE;
800         }
801
802         if (try_pruning && skipped_any && !started_any) {
803                 /* We didn't manage to start any message, but there is
804                  * at least one message in the queue that could be
805                  * sent if we pruned an idle connection from some
806                  * other server.
807                  */
808                 if (try_prune_connection (session)) {
809                         try_pruning = FALSE;
810                         goto try_again;
811                 }
812         }
813
814         return started_any;
815 }
816
817 static void
818 queue_message (SoupSession *session, SoupMessage *req, gboolean requeue)
819 {
820         req->status = SOUP_MESSAGE_STATUS_QUEUED;
821         if (!requeue) {
822                 soup_message_queue_append (session->priv->queue, req);
823                 run_queue (session, TRUE);
824         }
825 }
826
827 /**
828  * soup_session_queue_message:
829  * @session: a #SoupSession
830  * @req: the message to queue
831  * @callback: a #SoupMessageCallbackFn which will be called after the
832  * message completes or when an unrecoverable error occurs.
833  * @user_data: a pointer passed to @callback.
834  * 
835  * Queues the message @req for sending. All messages are processed
836  * while the glib main loop runs. If @req has been processed before,
837  * any resources related to the time it was last sent are freed.
838  *
839  * Upon message completion, the callback specified in @callback will
840  * be invoked. If after returning from this callback the message has
841  * not been requeued, @req will be unreffed.
842  */
843 void
844 soup_session_queue_message (SoupSession *session, SoupMessage *req,
845                             SoupMessageCallbackFn callback, gpointer user_data)
846 {
847         g_return_if_fail (SOUP_IS_SESSION (session));
848         g_return_if_fail (SOUP_IS_MESSAGE (req));
849
850         g_signal_connect (req, "finished",
851                           G_CALLBACK (request_finished), session);
852         if (callback) {
853                 g_signal_connect (req, "finished",
854                                   G_CALLBACK (callback), user_data);
855         }
856         g_signal_connect_after (req, "finished",
857                                 G_CALLBACK (final_finished), session);
858
859         soup_message_add_status_code_handler  (req, SOUP_STATUS_UNAUTHORIZED,
860                                                SOUP_HANDLER_POST_BODY,
861                                                authorize_handler, session);
862         soup_message_add_status_code_handler  (req,
863                                                SOUP_STATUS_PROXY_UNAUTHORIZED,
864                                                SOUP_HANDLER_POST_BODY,
865                                                authorize_handler, session);
866
867         if (!(soup_message_get_flags (req) & SOUP_MESSAGE_NO_REDIRECT)) {
868                 soup_message_add_status_class_handler (
869                         req, SOUP_STATUS_CLASS_REDIRECT,
870                         SOUP_HANDLER_POST_BODY,
871                         redirect_handler, session);
872         }
873
874         queue_message (session, req, FALSE);
875 }
876
877 /**
878  * soup_session_requeue_message:
879  * @session: a #SoupSession
880  * @req: the message to requeue
881  *
882  * This causes @req to be placed back on the queue to be attempted
883  * again.
884  **/
885 void
886 soup_session_requeue_message (SoupSession *session, SoupMessage *req)
887 {
888         g_return_if_fail (SOUP_IS_SESSION (session));
889         g_return_if_fail (SOUP_IS_MESSAGE (req));
890
891         queue_message (session, req, TRUE);
892 }
893
894
895 /**
896  * soup_session_send_message:
897  * @session: a #SoupSession
898  * @req: the message to send
899  * 
900  * Synchronously send @req. This call will not return until the
901  * transfer is finished successfully or there is an unrecoverable
902  * error.
903  *
904  * @req is not freed upon return.
905  *
906  * Return value: the HTTP status code of the response
907  */
908 guint
909 soup_session_send_message (SoupSession *session, SoupMessage *req)
910 {
911         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
912         g_return_val_if_fail (SOUP_IS_MESSAGE (req), SOUP_STATUS_MALFORMED);
913
914         /* Balance out the unref that final_finished will do */
915         g_object_ref (req);
916
917         soup_session_queue_message (session, req, NULL, NULL);
918
919         while (1) {
920                 g_main_iteration (TRUE);
921
922                 if (req->status == SOUP_MESSAGE_STATUS_FINISHED ||
923                     SOUP_STATUS_IS_TRANSPORT (req->status_code))
924                         break;
925         }
926
927         return req->status_code;
928 }
929
930 /**
931  * soup_session_abort:
932  * @session: the session
933  *
934  * Cancels all pending requests in @session.
935  **/
936 void
937 soup_session_abort (SoupSession *session)
938 {
939         SoupMessageQueueIter iter;
940         SoupMessage *msg;
941
942         for (msg = soup_message_queue_first (session->priv->queue, &iter); msg; msg = soup_message_queue_next (session->priv->queue, &iter)) {
943                 soup_message_queue_remove (session->priv->queue, &iter);
944                 soup_message_cancel (msg);
945         }
946 }