1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2000-2003, Ximian, Inc.
16 #include "soup-auth.h"
17 #include "soup-session.h"
18 #include "soup-connection.h"
19 #include "soup-connection-ntlm.h"
20 #include "soup-marshal.h"
21 #include "soup-message-filter.h"
22 #include "soup-message-private.h"
23 #include "soup-message-queue.h"
30 GSList *connections; /* CONTAINS: SoupConnection */
33 GHashTable *auth_realms; /* path -> scheme:realm */
34 GHashTable *auths; /* scheme:realm -> SoupAuth */
39 guint max_conns, max_conns_per_host;
47 GHashTable *hosts; /* SoupUri -> SoupSessionHost */
48 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
51 SoupSessionHost *proxy_host;
53 /* Must hold the host_lock before potentially creating a
54 * new SoupSessionHost, or adding/removing a connection.
55 * Must not emit signals or destroy objects while holding it.
59 GMainContext *async_context;
61 /* Holds the timeout value for the connection, when
62 no response is received.
66 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
68 static guint host_uri_hash (gconstpointer key);
69 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
70 static void free_host (SoupSessionHost *host);
72 static void setup_message (SoupMessageFilter *filter, SoupMessage *msg);
74 static void queue_message (SoupSession *session, SoupMessage *msg,
75 SoupMessageCallbackFn callback,
77 static void requeue_message (SoupSession *session, SoupMessage *msg);
78 static void cancel_message (SoupSession *session, SoupMessage *msg);
80 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
81 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
83 static void filter_iface_init (SoupMessageFilterClass *filter_class);
85 G_DEFINE_TYPE_EXTENDED (SoupSession, soup_session, G_TYPE_OBJECT, 0,
86 G_IMPLEMENT_INTERFACE (SOUP_TYPE_MESSAGE_FILTER,
95 static guint signals[LAST_SIGNAL] = { 0 };
102 PROP_MAX_CONNS_PER_HOST,
111 static void set_property (GObject *object, guint prop_id,
112 const GValue *value, GParamSpec *pspec);
113 static void get_property (GObject *object, guint prop_id,
114 GValue *value, GParamSpec *pspec);
117 soup_session_init (SoupSession *session)
119 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
121 session->queue = soup_message_queue_new ();
123 priv->host_lock = g_mutex_new ();
124 priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
125 priv->conns = g_hash_table_new (NULL, NULL);
127 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
128 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
134 foreach_free_host (gpointer key, gpointer host, gpointer data)
141 cleanup_hosts (SoupSessionPrivate *priv)
143 GHashTable *old_hosts;
145 g_mutex_lock (priv->host_lock);
146 old_hosts = priv->hosts;
147 priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
148 g_mutex_unlock (priv->host_lock);
150 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
151 g_hash_table_destroy (old_hosts);
155 dispose (GObject *object)
157 SoupSession *session = SOUP_SESSION (object);
158 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
161 soup_session_abort (session);
162 cleanup_hosts (priv);
165 for (f = priv->filters; f; f = f->next)
166 g_object_unref (f->data);
167 g_slist_free (priv->filters);
168 priv->filters = NULL;
171 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
175 finalize (GObject *object)
177 SoupSession *session = SOUP_SESSION (object);
178 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
180 soup_message_queue_destroy (session->queue);
182 g_mutex_free (priv->host_lock);
183 g_hash_table_destroy (priv->hosts);
184 g_hash_table_destroy (priv->conns);
187 soup_ssl_free_client_credentials (priv->ssl_creds);
189 if (priv->async_context)
190 g_main_context_unref (priv->async_context);
192 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
196 soup_session_class_init (SoupSessionClass *session_class)
198 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
200 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
202 /* virtual method definition */
203 session_class->queue_message = queue_message;
204 session_class->requeue_message = requeue_message;
205 session_class->cancel_message = cancel_message;
207 /* virtual method override */
208 object_class->dispose = dispose;
209 object_class->finalize = finalize;
210 object_class->set_property = set_property;
211 object_class->get_property = get_property;
216 * SoupSession::authenticate:
217 * @session: the session
218 * @msg: the #SoupMessage being sent
219 * @auth_type: the authentication type
220 * @auth_realm: the realm being authenticated to
221 * @username: the signal handler should set this to point to
222 * the provided username
223 * @password: the signal handler should set this to point to
224 * the provided password
226 * Emitted when the session requires authentication. The
227 * credentials may come from the user, or from cached
228 * information. If no credentials are available, leave
229 * @username and @password unchanged.
231 * If the provided credentials fail, the #reauthenticate
232 * signal will be emitted.
234 signals[AUTHENTICATE] =
235 g_signal_new ("authenticate",
236 G_OBJECT_CLASS_TYPE (object_class),
238 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
240 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
249 * SoupSession::reauthenticate:
250 * @session: the session
251 * @msg: the #SoupMessage being sent
252 * @auth_type: the authentication type
253 * @auth_realm: the realm being authenticated to
254 * @username: the signal handler should set this to point to
255 * the provided username
256 * @password: the signal handler should set this to point to
257 * the provided password
259 * Emitted when the credentials provided by the application to
260 * the #authenticate signal have failed. This gives the
261 * application a second chance to provide authentication
262 * credentials. If the new credentials also fail, #SoupSession
263 * will emit #reauthenticate again, and will continue doing so
264 * until the provided credentials work, or a #reauthenticate
265 * signal emission "fails" (because the handler left @username
266 * and @password unchanged). At that point, the 401 or 407
267 * error status will be returned to the caller.
269 * If your application only uses cached passwords, it should
270 * only connect to #authenticate, and not #reauthenticate.
272 * If your application always prompts the user for a password,
273 * and never uses cached information, then you can connect the
274 * same handler to #authenticate and #reauthenticate.
276 * To get standard web-browser behavior, return either cached
277 * information or a user-provided password (whichever is
278 * available) from the #authenticate handler, but return only
279 * user-provided information from the #reauthenticate handler.
281 signals[REAUTHENTICATE] =
282 g_signal_new ("reauthenticate",
283 G_OBJECT_CLASS_TYPE (object_class),
285 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
287 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
296 g_object_class_install_property (
297 object_class, PROP_PROXY_URI,
298 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
300 "The HTTP Proxy to use for this session",
302 g_object_class_install_property (
303 object_class, PROP_MAX_CONNS,
304 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
305 "Max Connection Count",
306 "The maximum number of connections that the session can open at once",
311 g_object_class_install_property (
312 object_class, PROP_MAX_CONNS_PER_HOST,
313 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
314 "Max Per-Host Connection Count",
315 "The maximum number of connections that the session can open at once to a given host",
320 g_object_class_install_property (
321 object_class, PROP_USE_NTLM,
322 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
324 "Whether or not to use NTLM authentication",
327 g_object_class_install_property (
328 object_class, PROP_SSL_CA_FILE,
329 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
331 "File containing SSL CA certificates",
334 g_object_class_install_property (
335 object_class, PROP_ASYNC_CONTEXT,
336 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
337 "Async GMainContext",
338 "The GMainContext to dispatch async I/O in",
339 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
340 g_object_class_install_property (
341 object_class, PROP_TIMEOUT,
342 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
344 "Value in seconds to timeout a blocking I/O",
350 filter_iface_init (SoupMessageFilterClass *filter_class)
352 /* interface implementation */
353 filter_class->setup_message = setup_message;
358 safe_uri_equal (const SoupUri *a, const SoupUri *b)
363 if ((a && !b) || (b && !a))
366 return soup_uri_equal (a, b);
370 safe_str_equal (const char *a, const char *b)
375 if ((a && !b) || (b && !a))
378 return strcmp (a, b) == 0;
382 set_property (GObject *object, guint prop_id,
383 const GValue *value, GParamSpec *pspec)
385 SoupSession *session = SOUP_SESSION (object);
386 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
388 gboolean need_abort = FALSE;
389 gboolean ca_file_changed = FALSE;
390 const char *new_ca_file;
394 pval = g_value_get_pointer (value);
396 if (!safe_uri_equal (priv->proxy_uri, pval))
400 soup_uri_free (priv->proxy_uri);
402 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
405 soup_session_abort (session);
406 cleanup_hosts (priv);
411 priv->max_conns = g_value_get_int (value);
413 case PROP_MAX_CONNS_PER_HOST:
414 priv->max_conns_per_host = g_value_get_int (value);
417 priv->use_ntlm = g_value_get_boolean (value);
419 case PROP_SSL_CA_FILE:
420 new_ca_file = g_value_get_string (value);
422 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
423 ca_file_changed = TRUE;
425 g_free (priv->ssl_ca_file);
426 priv->ssl_ca_file = g_strdup (new_ca_file);
428 if (ca_file_changed) {
429 if (priv->ssl_creds) {
430 soup_ssl_free_client_credentials (priv->ssl_creds);
431 priv->ssl_creds = NULL;
434 cleanup_hosts (priv);
438 case PROP_ASYNC_CONTEXT:
439 priv->async_context = g_value_get_pointer (value);
440 if (priv->async_context)
441 g_main_context_ref (priv->async_context);
444 priv->timeout = g_value_get_uint (value);
452 get_property (GObject *object, guint prop_id,
453 GValue *value, GParamSpec *pspec)
455 SoupSession *session = SOUP_SESSION (object);
456 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
460 g_value_set_pointer (value, priv->proxy_uri ?
461 soup_uri_copy (priv->proxy_uri) :
465 g_value_set_int (value, priv->max_conns);
467 case PROP_MAX_CONNS_PER_HOST:
468 g_value_set_int (value, priv->max_conns_per_host);
471 g_value_set_boolean (value, priv->use_ntlm);
473 case PROP_SSL_CA_FILE:
474 g_value_set_string (value, priv->ssl_ca_file);
476 case PROP_ASYNC_CONTEXT:
477 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
480 g_value_set_uint (value, priv->timeout);
489 * soup_session_add_filter:
490 * @session: a #SoupSession
491 * @filter: an object implementing the #SoupMessageFilter interface
493 * Adds @filter to @session's list of message filters to be applied to
497 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
499 SoupSessionPrivate *priv;
501 g_return_if_fail (SOUP_IS_SESSION (session));
502 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
503 priv = SOUP_SESSION_GET_PRIVATE (session);
505 g_object_ref (filter);
506 priv->filters = g_slist_prepend (priv->filters, filter);
510 * soup_session_remove_filter:
511 * @session: a #SoupSession
512 * @filter: an object implementing the #SoupMessageFilter interface
514 * Removes @filter from @session's list of message filters
517 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
519 SoupSessionPrivate *priv;
521 g_return_if_fail (SOUP_IS_SESSION (session));
522 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
523 priv = SOUP_SESSION_GET_PRIVATE (session);
525 priv->filters = g_slist_remove (priv->filters, filter);
526 g_object_unref (filter);
532 host_uri_hash (gconstpointer key)
534 const SoupUri *uri = key;
536 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
540 host_uri_equal (gconstpointer v1, gconstpointer v2)
542 const SoupUri *one = v1;
543 const SoupUri *two = v2;
545 if (one->protocol != two->protocol)
547 if (one->port != two->port)
550 return strcmp (one->host, two->host) == 0;
553 static SoupSessionHost *
554 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
556 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
557 SoupSessionHost *host;
559 host = g_new0 (SoupSessionHost, 1);
560 host->root_uri = soup_uri_copy_root (source_uri);
562 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
565 soup_ssl_get_client_credentials (priv->ssl_ca_file);
571 /* Note: get_host_for_message doesn't lock the host_lock. The caller
572 * must do it itself if there's a chance the host doesn't already
575 static SoupSessionHost *
576 get_host_for_message (SoupSession *session, SoupMessage *msg)
578 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
579 SoupSessionHost *host;
580 const SoupUri *source = soup_message_get_uri (msg);
582 host = g_hash_table_lookup (priv->hosts, source);
586 host = soup_session_host_new (session, source);
587 g_hash_table_insert (priv->hosts, host->root_uri, host);
592 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
593 * it itself if there's a chance the host doesn't already exist.
595 static SoupSessionHost *
596 get_proxy_host (SoupSession *session)
598 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
600 if (priv->proxy_host || !priv->proxy_uri)
601 return priv->proxy_host;
604 soup_session_host_new (session, priv->proxy_uri);
605 return priv->proxy_host;
609 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
612 g_free (scheme_realm);
616 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
618 g_free (scheme_realm);
619 g_object_unref (auth);
623 free_host (SoupSessionHost *host)
625 while (host->connections) {
626 SoupConnection *conn = host->connections->data;
628 host->connections = g_slist_remove (host->connections, conn);
629 soup_connection_disconnect (conn);
632 if (host->auth_realms) {
633 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
634 g_hash_table_destroy (host->auth_realms);
637 g_hash_table_foreach (host->auths, free_auth, NULL);
638 g_hash_table_destroy (host->auths);
641 soup_uri_free (host->root_uri);
648 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
650 SoupSessionHost *host;
652 const char *realm, *const_path;
655 host = get_proxy_host (session);
658 host = get_host_for_message (session, msg);
659 const_path = soup_message_get_uri (msg)->path;
664 g_return_val_if_fail (host != NULL, NULL);
666 if (!host->auth_realms)
669 path = g_strdup (const_path);
672 realm = g_hash_table_lookup (host->auth_realms, path);
676 dir = strrchr (path, '/');
687 return g_hash_table_lookup (host->auths, realm);
693 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
698 realm = g_strdup_printf ("%s:%s",
699 soup_auth_get_scheme_name (auth),
700 soup_auth_get_realm (auth));
702 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
703 auth == (SoupAuth *)value) {
704 g_hash_table_remove (host->auths, realm);
706 g_object_unref (auth);
712 authenticate_auth (SoupSession *session, SoupAuth *auth,
713 SoupMessage *msg, gboolean prior_auth_failed,
716 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
718 char *username = NULL, *password = NULL;
721 uri = priv->proxy_uri;
723 uri = soup_message_get_uri (msg);
725 if (uri->passwd && !prior_auth_failed) {
726 soup_auth_authenticate (auth, uri->user, uri->passwd);
730 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
731 msg, soup_auth_get_scheme_name (auth),
732 soup_auth_get_realm (auth),
733 &username, &password);
734 if (username || password)
735 soup_auth_authenticate (auth, username, password);
739 memset (password, 0, strlen (password));
743 return soup_auth_is_authenticated (auth);
747 update_auth_internal (SoupSession *session, SoupMessage *msg,
748 const GSList *headers, gboolean proxy)
750 SoupSessionHost *host;
751 SoupAuth *new_auth, *prior_auth, *old_auth;
752 gpointer old_path, old_realm;
753 const SoupUri *msg_uri;
757 gboolean prior_auth_failed = FALSE;
760 host = get_proxy_host (session);
762 host = get_host_for_message (session, msg);
764 g_return_val_if_fail (host != NULL, FALSE);
766 /* Try to construct a new auth from the headers; if we can't,
767 * there's no way we'll be able to authenticate.
769 msg_uri = soup_message_get_uri (msg);
770 new_auth = soup_auth_new_from_header_list (headers);
774 /* See if this auth is the same auth we used last time */
775 prior_auth = proxy ? soup_message_get_proxy_auth (msg) : soup_message_get_auth (msg);
777 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
778 !strcmp (soup_auth_get_realm (prior_auth),
779 soup_auth_get_realm (new_auth))) {
780 /* The server didn't like the username/password we
781 * provided before. Invalidate it and note this fact.
783 invalidate_auth (host, prior_auth);
784 prior_auth_failed = TRUE;
787 if (!host->auth_realms) {
788 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
789 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
792 /* Record where this auth realm is used */
793 realm = g_strdup_printf ("%s:%s",
794 soup_auth_get_scheme_name (new_auth),
795 soup_auth_get_realm (new_auth));
798 * RFC 2617 is somewhat unclear about the scope of protection
799 * spaces with regard to proxies. The only mention of it is
800 * as an aside in section 3.2.1, where it is defining the fields
801 * of a Digest challenge and says that the protection space is
802 * always the entire proxy. Is this the case for all authentication
803 * schemes or just Digest? Who knows, but we're assuming all.
806 pspace = g_slist_prepend (NULL, g_strdup (""));
808 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
810 for (p = pspace; p; p = p->next) {
812 if (g_hash_table_lookup_extended (host->auth_realms, path,
813 &old_path, &old_realm)) {
814 g_hash_table_remove (host->auth_realms, old_path);
819 g_hash_table_insert (host->auth_realms,
820 g_strdup (path), g_strdup (realm));
822 soup_auth_free_protection_space (new_auth, pspace);
824 /* Now, make sure the auth is recorded. (If there's a
825 * pre-existing auth, we keep that rather than the new one,
826 * since the old one might already be authenticated.)
828 old_auth = g_hash_table_lookup (host->auths, realm);
831 g_object_unref (new_auth);
834 g_hash_table_insert (host->auths, realm, new_auth);
836 /* If we need to authenticate, try to do it. */
837 if (!soup_auth_is_authenticated (new_auth)) {
838 return authenticate_auth (session, new_auth,
839 msg, prior_auth_failed, proxy);
842 /* Otherwise we're good. */
847 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
848 const char *auth_type, const char *auth_realm,
849 char **username, char **password, gpointer session)
851 g_signal_emit (session, signals[AUTHENTICATE], 0,
852 msg, auth_type, auth_realm, username, password);
856 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
857 const char *auth_type, const char *auth_realm,
858 char **username, char **password,
861 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
862 msg, auth_type, auth_realm, username, password);
867 authorize_handler (SoupMessage *msg, gpointer user_data)
869 SoupSession *session = user_data;
870 const GSList *headers;
873 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
874 headers = soup_message_get_header_list (msg->response_headers,
875 "Proxy-Authenticate");
878 headers = soup_message_get_header_list (msg->response_headers,
885 if (update_auth_internal (session, msg, headers, proxy))
886 soup_session_requeue_message (session, msg);
890 redirect_handler (SoupMessage *msg, gpointer user_data)
892 SoupSession *session = user_data;
896 new_loc = soup_message_get_header (msg->response_headers, "Location");
900 /* Location is supposed to be an absolute URI, but some sites
901 * are lame, so we use soup_uri_new_with_base().
903 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
905 soup_message_set_status_full (msg,
906 SOUP_STATUS_MALFORMED,
907 "Invalid Redirect URL");
911 soup_message_set_uri (msg, new_uri);
912 soup_uri_free (new_uri);
914 soup_session_requeue_message (session, msg);
918 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
922 auth = lookup_auth (session, msg, proxy);
923 if (auth && !soup_auth_is_authenticated (auth)) {
924 if (!authenticate_auth (session, auth, msg, FALSE, proxy))
929 soup_message_set_proxy_auth (msg, auth);
931 soup_message_set_auth (msg, auth);
935 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
937 SoupSession *session = SOUP_SESSION (filter);
938 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
941 for (f = priv->filters; f; f = f->next) {
943 soup_message_filter_setup_message (filter, msg);
946 add_auth (session, msg, FALSE);
947 soup_message_add_status_code_handler (
948 msg, SOUP_STATUS_UNAUTHORIZED,
949 SOUP_HANDLER_POST_BODY,
950 authorize_handler, session);
952 if (priv->proxy_uri) {
953 add_auth (session, msg, TRUE);
954 soup_message_add_status_code_handler (
955 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
956 SOUP_HANDLER_POST_BODY,
957 authorize_handler, session);
962 find_oldest_connection (gpointer key, gpointer host, gpointer data)
964 SoupConnection *conn = key, **oldest = data;
966 /* Don't prune a connection that is currently in use, or
967 * hasn't been used yet.
969 if (soup_connection_is_in_use (conn) ||
970 soup_connection_last_used (conn) == 0)
973 if (!*oldest || (soup_connection_last_used (conn) <
974 soup_connection_last_used (*oldest)))
979 * soup_session_try_prune_connection:
980 * @session: a #SoupSession
982 * Finds the least-recently-used idle connection in @session and closes
985 * Return value: %TRUE if a connection was closed, %FALSE if there are
986 * no idle connections.
989 soup_session_try_prune_connection (SoupSession *session)
991 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
992 SoupConnection *oldest = NULL;
994 g_mutex_lock (priv->host_lock);
995 g_hash_table_foreach (priv->conns, find_oldest_connection,
998 /* Ref the connection before unlocking the mutex in
999 * case someone else tries to prune it too.
1001 g_object_ref (oldest);
1002 g_mutex_unlock (priv->host_lock);
1003 soup_connection_disconnect (oldest);
1004 g_object_unref (oldest);
1007 g_mutex_unlock (priv->host_lock);
1013 connection_disconnected (SoupConnection *conn, gpointer user_data)
1015 SoupSession *session = user_data;
1016 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1017 SoupSessionHost *host;
1019 g_mutex_lock (priv->host_lock);
1021 host = g_hash_table_lookup (priv->conns, conn);
1023 g_hash_table_remove (priv->conns, conn);
1024 host->connections = g_slist_remove (host->connections, conn);
1028 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1031 g_mutex_unlock (priv->host_lock);
1032 g_object_unref (conn);
1036 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1038 SoupSession *session = user_data;
1039 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1040 SoupSessionHost *host;
1041 SoupMessageQueueIter iter;
1044 g_mutex_lock (priv->host_lock);
1046 host = g_hash_table_lookup (priv->conns, conn);
1048 g_mutex_unlock (priv->host_lock);
1052 if (status == SOUP_STATUS_OK) {
1053 soup_connection_reserve (conn);
1054 host->connections = g_slist_prepend (host->connections, conn);
1055 g_mutex_unlock (priv->host_lock);
1059 /* The connection failed. */
1060 g_mutex_unlock (priv->host_lock);
1061 connection_disconnected (conn, session);
1063 if (host->connections) {
1064 /* Something went wrong this time, but we have at
1065 * least one open connection to this host. So just
1066 * leave the message in the queue so it can use that
1067 * connection once it's free.
1072 /* There are two possibilities: either status is
1073 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1074 * will create a new connection (and all we need to do here
1075 * is downgrade the message from CONNECTING to QUEUED); or
1076 * status is something else, probably CANT_CONNECT or
1077 * CANT_RESOLVE or the like, in which case we need to cancel
1078 * any messages waiting for this host, since they're out
1081 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1082 if (get_host_for_message (session, msg) == host) {
1083 if (status == SOUP_STATUS_TRY_AGAIN) {
1084 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1085 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1087 soup_message_set_status (msg, status);
1088 soup_session_cancel_message (session, msg);
1095 * soup_session_get_connection:
1096 * @session: a #SoupSession
1097 * @msg: a #SoupMessage
1098 * @try_pruning: on return, whether or not to try pruning a connection
1099 * @is_new: on return, %TRUE if the returned connection is new and not
1102 * Tries to find or create a connection for @msg; this is an internal
1103 * method for #SoupSession subclasses.
1105 * If there is an idle connection to the relevant host available, then
1106 * that connection will be returned (with *@is_new set to %FALSE). The
1107 * connection will be marked "reserved", so the caller must call
1108 * soup_connection_release() if it ends up not using the connection
1111 * If there is no idle connection available, but it is possible to
1112 * create a new connection, then one will be created and returned,
1113 * with *@is_new set to %TRUE. The caller MUST then call
1114 * soup_connection_connect_sync() or soup_connection_connect_async()
1115 * to connect it. If the connection attempt succeeds, the connection
1116 * will be marked "reserved" and added to @session's connection pool
1117 * once it connects. If the connection attempt fails, the connection
1120 * If no connection is available and a new connection cannot be made,
1121 * soup_session_get_connection() will return %NULL. If @session has
1122 * the maximum number of open connections open, but does not have the
1123 * maximum number of per-host connections open to the relevant host,
1124 * then *@try_pruning will be set to %TRUE. In this case, the caller
1125 * can call soup_session_try_prune_connection() to close an idle
1126 * connection, and then try soup_session_get_connection() again. (If
1127 * calling soup_session_try_prune_connection() wouldn't help, then
1128 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1130 * Return value: a #SoupConnection, or %NULL
1133 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1134 gboolean *try_pruning, gboolean *is_new)
1136 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1137 SoupConnection *conn;
1138 SoupSessionHost *host;
1141 g_mutex_lock (priv->host_lock);
1143 host = get_host_for_message (session, msg);
1144 for (conns = host->connections; conns; conns = conns->next) {
1145 if (!soup_connection_is_in_use (conns->data)) {
1146 soup_connection_reserve (conns->data);
1147 g_mutex_unlock (priv->host_lock);
1153 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1154 /* We already started a connection for this
1155 * message, so don't start another one.
1157 g_mutex_unlock (priv->host_lock);
1161 if (host->num_conns >= priv->max_conns_per_host) {
1162 g_mutex_unlock (priv->host_lock);
1166 if (priv->num_conns >= priv->max_conns) {
1167 *try_pruning = TRUE;
1168 g_mutex_unlock (priv->host_lock);
1172 /* Make sure priv->proxy_host gets set now while
1173 * we have the host_lock.
1175 if (priv->proxy_uri)
1176 get_proxy_host (session);
1178 conn = g_object_new (
1180 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1181 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1182 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1183 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1184 SOUP_CONNECTION_MESSAGE_FILTER, session,
1185 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1186 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1188 g_signal_connect (conn, "connect_result",
1189 G_CALLBACK (connect_result),
1191 g_signal_connect (conn, "disconnected",
1192 G_CALLBACK (connection_disconnected),
1194 g_signal_connect (conn, "authenticate",
1195 G_CALLBACK (connection_authenticate),
1197 g_signal_connect (conn, "reauthenticate",
1198 G_CALLBACK (connection_reauthenticate),
1201 g_hash_table_insert (priv->conns, conn, host);
1203 /* We increment the connection counts so it counts against the
1204 * totals, but we don't add it to the host's connection list
1205 * yet, since it's not ready for use.
1210 /* Mark the request as connecting, so we don't try to open
1211 * another new connection for it while waiting for this one.
1213 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1215 g_mutex_unlock (priv->host_lock);
1221 message_finished (SoupMessage *msg, gpointer user_data)
1223 SoupSession *session = user_data;
1225 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1226 soup_message_queue_remove_message (session->queue, msg);
1227 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1232 queue_message (SoupSession *session, SoupMessage *msg,
1233 SoupMessageCallbackFn callback, gpointer user_data)
1235 g_signal_connect_after (msg, "finished",
1236 G_CALLBACK (message_finished), session);
1238 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1239 soup_message_add_status_class_handler (
1240 msg, SOUP_STATUS_CLASS_REDIRECT,
1241 SOUP_HANDLER_POST_BODY,
1242 redirect_handler, session);
1245 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1246 soup_message_queue_append (session->queue, msg);
1250 * soup_session_queue_message:
1251 * @session: a #SoupSession
1252 * @msg: the message to queue
1253 * @callback: a #SoupMessageCallbackFn which will be called after the
1254 * message completes or when an unrecoverable error occurs.
1255 * @user_data: a pointer passed to @callback.
1257 * Queues the message @msg for sending. All messages are processed
1258 * while the glib main loop runs. If @msg has been processed before,
1259 * any resources related to the time it was last sent are freed.
1261 * Upon message completion, the callback specified in @callback will
1262 * be invoked (in the thread associated with @session's async
1263 * context). If after returning from this callback the message has not
1264 * been requeued, @msg will be unreffed.
1267 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1268 SoupMessageCallbackFn callback, gpointer user_data)
1270 g_return_if_fail (SOUP_IS_SESSION (session));
1271 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1273 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1274 callback, user_data);
1278 requeue_message (SoupSession *session, SoupMessage *msg)
1280 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1284 * soup_session_requeue_message:
1285 * @session: a #SoupSession
1286 * @msg: the message to requeue
1288 * This causes @msg to be placed back on the queue to be attempted
1292 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1294 g_return_if_fail (SOUP_IS_SESSION (session));
1295 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1297 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1302 * soup_session_send_message:
1303 * @session: a #SoupSession
1304 * @msg: the message to send
1306 * Synchronously send @msg. This call will not return until the
1307 * transfer is finished successfully or there is an unrecoverable
1310 * @msg is not freed upon return.
1312 * Return value: the HTTP status code of the response
1315 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1317 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1318 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1320 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1325 cancel_message (SoupSession *session, SoupMessage *msg)
1327 soup_message_queue_remove_message (session->queue, msg);
1328 soup_message_finished (msg);
1332 * soup_session_cancel_message:
1333 * @session: a #SoupSession
1334 * @msg: the message to cancel
1336 * Causes @session to immediately finish processing @msg. You should
1337 * set a status code on @msg with soup_message_set_status() before
1338 * calling this function.
1341 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1343 g_return_if_fail (SOUP_IS_SESSION (session));
1344 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1346 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1350 * soup_session_abort:
1351 * @session: the session
1353 * Cancels all pending requests in @session.
1356 soup_session_abort (SoupSession *session)
1358 SoupMessageQueueIter iter;
1361 g_return_if_fail (SOUP_IS_SESSION (session));
1363 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1364 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1365 soup_session_cancel_message (session, msg);