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