Replaces the three previous soup_connection_new* functions and uses
[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         if (!oldest || (soup_connection_last_used (conn) <
615                         soup_connection_last_used (*oldest)))
616                 *oldest = conn;
617 }
618
619 static gboolean
620 try_prune_connection (SoupSession *session)
621 {
622         SoupConnection *oldest = NULL;
623
624         g_hash_table_foreach (session->priv->conns, find_oldest_connection,
625                               &oldest);
626         if (oldest) {
627                 soup_connection_disconnect (oldest);
628                 g_object_unref (oldest);
629                 return TRUE;
630         } else
631                 return FALSE;
632 }
633
634 static void connection_closed (SoupConnection *conn, SoupSession *session);
635
636 static void
637 cleanup_connection (SoupSession *session, SoupConnection *conn)
638 {
639         SoupSessionHost *host =
640                 g_hash_table_lookup (session->priv->conns, conn);
641
642         g_return_if_fail (host != NULL);
643
644         g_hash_table_remove (session->priv->conns, conn);
645         g_signal_handlers_disconnect_by_func (conn, connection_closed, session);
646         session->priv->num_conns--;
647
648         host->connections = g_slist_remove (host->connections, conn);
649         host->num_conns--;
650 }
651
652 static void
653 connection_closed (SoupConnection *conn, SoupSession *session)
654 {
655         cleanup_connection (session, conn);
656
657         /* Run the queue in case anyone was waiting for a connection
658          * to be closed.
659          */
660         run_queue (session, FALSE);
661 }
662
663 static void
664 got_connection (SoupConnection *conn, guint status, gpointer user_data)
665 {
666         SoupSession *session = user_data;
667         SoupSessionHost *host = g_hash_table_lookup (session->priv->conns, conn);
668
669         g_return_if_fail (host != NULL);
670
671         if (status == SOUP_STATUS_OK) {
672                 host->connections = g_slist_prepend (host->connections, conn);
673                 run_queue (session, FALSE);
674                 return;
675         }
676
677         /* We failed */
678         cleanup_connection (session, conn);
679         g_object_unref (conn);
680
681         if (host->connections) {
682                 /* Something went wrong this time, but we have at
683                  * least one open connection to this host. So just
684                  * leave the message in the queue so it can use that
685                  * connection once it's free.
686                  */
687                 return;
688         }
689
690         /* Flush any queued messages for this host */
691         host->error = status;
692         run_queue (session, FALSE);
693
694         if (status != SOUP_STATUS_CANT_RESOLVE &&
695             status != SOUP_STATUS_CANT_RESOLVE_PROXY) {
696                 /* If the error was "can't resolve", then it's not likely
697                  * to improve. But if it was something else, it may have
698                  * been transient, so we clear the error so the user can
699                  * try again later.
700                  */
701                 host->error = 0;
702         }
703 }
704
705 static gboolean
706 run_queue (SoupSession *session, gboolean try_pruning)
707 {
708         SoupMessageQueueIter iter;
709         SoupMessage *msg;
710         SoupConnection *conn;
711         SoupSessionHost *host;
712         gboolean skipped_any = FALSE, started_any = FALSE;
713         GSList *conns;
714
715         /* FIXME: prefer CONNECTING messages */
716
717  try_again:
718         for (msg = soup_message_queue_first (session->priv->queue, &iter); msg; msg = soup_message_queue_next (session->priv->queue, &iter)) {
719
720                 if (!SOUP_MESSAGE_IS_STARTING (msg))
721                         continue;
722
723                 host = get_host_for_message (session, msg);
724
725                 /* If the hostname is known to be bad, fail right away */
726                 if (host->error) {
727                         soup_message_set_status (msg, host->error);
728                         soup_message_finished (msg);
729                 }
730
731                 /* If there is an idle connection, use it */
732                 for (conns = host->connections; conns; conns = conns->next) {
733                         if (!soup_connection_is_in_use (conns->data))
734                                 break;
735                 }
736                 if (conns) {
737                         send_request (session, msg, conns->data);
738                         started_any = TRUE;
739                         continue;
740                 }
741
742                 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
743                         /* We already started a connection for this
744                          * message, so don't start another one.
745                          */
746                         continue;
747                 }
748
749                 /* If we have the max number of per-host connections
750                  * or total connections open, we'll have to wait.
751                  */
752                 if (host->num_conns >= session->priv->max_conns_per_host)
753                         continue;
754                 else if (session->priv->num_conns >= session->priv->max_conns) {
755                         /* In this case, closing an idle connection
756                          * somewhere else would let us open one here.
757                          */
758                         skipped_any = TRUE;
759                         continue;
760                 }
761
762                 /* Otherwise, open a new connection */
763                 conn = g_object_new (
764                         (session->priv->use_ntlm ?
765                          SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
766                         SOUP_CONNECTION_DEST_URI, host->root_uri,
767                         SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
768                         NULL);
769                 g_signal_connect (conn, "authenticate",
770                                   G_CALLBACK (connection_authenticate),
771                                   session);
772                 g_signal_connect (conn, "reauthenticate",
773                                   G_CALLBACK (connection_reauthenticate),
774                                   session);
775
776                 soup_connection_connect_async (conn, got_connection, session);
777                 g_signal_connect (conn, "disconnected",
778                                   G_CALLBACK (connection_closed), session);
779                 g_hash_table_insert (session->priv->conns, conn, host);
780                 session->priv->num_conns++;
781
782                 /* Increment the host's connection count, but don't add
783                  * this connection to the list yet, since it's not ready.
784                  */
785                 host->num_conns++;
786
787                 /* Mark the request as connecting, so we don't try to
788                  * open another new connection for it next time around.
789                  */
790                 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
791
792                 started_any = TRUE;
793         }
794
795         if (try_pruning && skipped_any && !started_any) {
796                 /* We didn't manage to start any message, but there is
797                  * at least one message in the queue that could be
798                  * sent if we pruned an idle connection from some
799                  * other server.
800                  */
801                 if (try_prune_connection (session)) {
802                         try_pruning = FALSE;
803                         goto try_again;
804                 }
805         }
806
807         return started_any;
808 }
809
810 static void
811 queue_message (SoupSession *session, SoupMessage *req, gboolean requeue)
812 {
813         req->status = SOUP_MESSAGE_STATUS_QUEUED;
814         if (!requeue)
815                 soup_message_queue_append (session->priv->queue, req);
816
817         run_queue (session, TRUE);
818 }
819
820 /**
821  * soup_session_queue_message:
822  * @session: a #SoupSession
823  * @req: the message to queue
824  * @callback: a #SoupMessageCallbackFn which will be called after the
825  * message completes or when an unrecoverable error occurs.
826  * @user_data: a pointer passed to @callback.
827  * 
828  * Queues the message @req for sending. All messages are processed
829  * while the glib main loop runs. If @req has been processed before,
830  * any resources related to the time it was last sent are freed.
831  *
832  * Upon message completion, the callback specified in @callback will
833  * be invoked. If after returning from this callback the message has
834  * not been requeued, @req will be unreffed.
835  */
836 void
837 soup_session_queue_message (SoupSession *session, SoupMessage *req,
838                             SoupMessageCallbackFn callback, gpointer user_data)
839 {
840         g_return_if_fail (SOUP_IS_SESSION (session));
841         g_return_if_fail (SOUP_IS_MESSAGE (req));
842
843         g_signal_connect (req, "finished",
844                           G_CALLBACK (request_finished), session);
845         if (callback) {
846                 g_signal_connect (req, "finished",
847                                   G_CALLBACK (callback), user_data);
848         }
849         g_signal_connect_after (req, "finished",
850                                 G_CALLBACK (final_finished), session);
851
852         soup_message_add_status_code_handler  (req, SOUP_STATUS_UNAUTHORIZED,
853                                                SOUP_HANDLER_POST_BODY,
854                                                authorize_handler, session);
855         soup_message_add_status_code_handler  (req,
856                                                SOUP_STATUS_PROXY_UNAUTHORIZED,
857                                                SOUP_HANDLER_POST_BODY,
858                                                authorize_handler, session);
859
860         if (!(soup_message_get_flags (req) & SOUP_MESSAGE_NO_REDIRECT)) {
861                 soup_message_add_status_class_handler (
862                         req, SOUP_STATUS_CLASS_REDIRECT,
863                         SOUP_HANDLER_POST_BODY,
864                         redirect_handler, session);
865         }
866
867         queue_message (session, req, FALSE);
868 }
869
870 /**
871  * soup_session_requeue_message:
872  * @session: a #SoupSession
873  * @req: the message to requeue
874  *
875  * This causes @req to be placed back on the queue to be attempted
876  * again.
877  **/
878 void
879 soup_session_requeue_message (SoupSession *session, SoupMessage *req)
880 {
881         g_return_if_fail (SOUP_IS_SESSION (session));
882         g_return_if_fail (SOUP_IS_MESSAGE (req));
883
884         queue_message (session, req, TRUE);
885 }
886
887
888 /**
889  * soup_session_send_message:
890  * @session: a #SoupSession
891  * @req: the message to send
892  * 
893  * Synchronously send @req. This call will not return until the
894  * transfer is finished successfully or there is an unrecoverable
895  * error.
896  *
897  * @req is not freed upon return.
898  *
899  * Return value: the HTTP status code of the response
900  */
901 guint
902 soup_session_send_message (SoupSession *session, SoupMessage *req)
903 {
904         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
905         g_return_val_if_fail (SOUP_IS_MESSAGE (req), SOUP_STATUS_MALFORMED);
906
907         /* Balance out the unref that final_finished will do */
908         g_object_ref (req);
909
910         soup_session_queue_message (session, req, NULL, NULL);
911
912         while (1) {
913                 g_main_iteration (TRUE);
914
915                 if (req->status == SOUP_MESSAGE_STATUS_FINISHED ||
916                     SOUP_STATUS_IS_TRANSPORT (req->status_code))
917                         break;
918         }
919
920         return req->status_code;
921 }