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