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