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;
43 SoupSSLCredentials *ssl_creds;
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_uri_free (priv->proxy_uri);
188 if (priv->proxy_host)
189 free_host (priv->proxy_host);
192 soup_ssl_free_client_credentials (priv->ssl_creds);
194 if (priv->async_context)
195 g_main_context_unref (priv->async_context);
197 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
201 soup_session_class_init (SoupSessionClass *session_class)
203 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
205 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
207 /* virtual method definition */
208 session_class->queue_message = queue_message;
209 session_class->requeue_message = requeue_message;
210 session_class->cancel_message = cancel_message;
212 /* virtual method override */
213 object_class->dispose = dispose;
214 object_class->finalize = finalize;
215 object_class->set_property = set_property;
216 object_class->get_property = get_property;
221 * SoupSession::authenticate:
222 * @session: the session
223 * @msg: the #SoupMessage being sent
224 * @auth_type: the authentication type
225 * @auth_realm: the realm being authenticated to
226 * @username: the signal handler should set this to point to
227 * the provided username
228 * @password: the signal handler should set this to point to
229 * the provided password
231 * Emitted when the session requires authentication. The
232 * credentials may come from the user, or from cached
233 * information. If no credentials are available, leave
234 * @username and @password unchanged.
236 * If the provided credentials fail, the #reauthenticate
237 * signal will be emitted.
239 signals[AUTHENTICATE] =
240 g_signal_new ("authenticate",
241 G_OBJECT_CLASS_TYPE (object_class),
243 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
245 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
254 * SoupSession::reauthenticate:
255 * @session: the session
256 * @msg: the #SoupMessage being sent
257 * @auth_type: the authentication type
258 * @auth_realm: the realm being authenticated to
259 * @username: the signal handler should set this to point to
260 * the provided username
261 * @password: the signal handler should set this to point to
262 * the provided password
264 * Emitted when the credentials provided by the application to
265 * the #authenticate signal have failed. This gives the
266 * application a second chance to provide authentication
267 * credentials. If the new credentials also fail, #SoupSession
268 * will emit #reauthenticate again, and will continue doing so
269 * until the provided credentials work, or a #reauthenticate
270 * signal emission "fails" (because the handler left @username
271 * and @password unchanged). At that point, the 401 or 407
272 * error status will be returned to the caller.
274 * If your application only uses cached passwords, it should
275 * only connect to #authenticate, and not #reauthenticate.
277 * If your application always prompts the user for a password,
278 * and never uses cached information, then you can connect the
279 * same handler to #authenticate and #reauthenticate.
281 * To get standard web-browser behavior, return either cached
282 * information or a user-provided password (whichever is
283 * available) from the #authenticate handler, but return only
284 * user-provided information from the #reauthenticate handler.
286 signals[REAUTHENTICATE] =
287 g_signal_new ("reauthenticate",
288 G_OBJECT_CLASS_TYPE (object_class),
290 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
292 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
301 g_object_class_install_property (
302 object_class, PROP_PROXY_URI,
303 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
305 "The HTTP Proxy to use for this session",
307 g_object_class_install_property (
308 object_class, PROP_MAX_CONNS,
309 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
310 "Max Connection Count",
311 "The maximum number of connections that the session can open at once",
316 g_object_class_install_property (
317 object_class, PROP_MAX_CONNS_PER_HOST,
318 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
319 "Max Per-Host Connection Count",
320 "The maximum number of connections that the session can open at once to a given host",
325 g_object_class_install_property (
326 object_class, PROP_USE_NTLM,
327 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
329 "Whether or not to use NTLM authentication",
332 g_object_class_install_property (
333 object_class, PROP_SSL_CA_FILE,
334 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
336 "File containing SSL CA certificates",
339 g_object_class_install_property (
340 object_class, PROP_ASYNC_CONTEXT,
341 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
342 "Async GMainContext",
343 "The GMainContext to dispatch async I/O in",
344 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
345 g_object_class_install_property (
346 object_class, PROP_TIMEOUT,
347 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
349 "Value in seconds to timeout a blocking I/O",
355 filter_iface_init (SoupMessageFilterClass *filter_class)
357 /* interface implementation */
358 filter_class->setup_message = setup_message;
363 safe_uri_equal (const SoupUri *a, const SoupUri *b)
368 if ((a && !b) || (b && !a))
371 return soup_uri_equal (a, b);
375 safe_str_equal (const char *a, const char *b)
380 if ((a && !b) || (b && !a))
383 return strcmp (a, b) == 0;
387 set_property (GObject *object, guint prop_id,
388 const GValue *value, GParamSpec *pspec)
390 SoupSession *session = SOUP_SESSION (object);
391 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
393 gboolean need_abort = FALSE;
394 gboolean ca_file_changed = FALSE;
395 const char *new_ca_file;
399 pval = g_value_get_pointer (value);
401 if (!safe_uri_equal (priv->proxy_uri, pval))
405 soup_uri_free (priv->proxy_uri);
407 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
410 soup_session_abort (session);
411 cleanup_hosts (priv);
416 priv->max_conns = g_value_get_int (value);
418 case PROP_MAX_CONNS_PER_HOST:
419 priv->max_conns_per_host = g_value_get_int (value);
422 priv->use_ntlm = g_value_get_boolean (value);
424 case PROP_SSL_CA_FILE:
425 new_ca_file = g_value_get_string (value);
427 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
428 ca_file_changed = TRUE;
430 g_free (priv->ssl_ca_file);
431 priv->ssl_ca_file = g_strdup (new_ca_file);
433 if (ca_file_changed) {
434 if (priv->ssl_creds) {
435 soup_ssl_free_client_credentials (priv->ssl_creds);
436 priv->ssl_creds = NULL;
439 cleanup_hosts (priv);
443 case PROP_ASYNC_CONTEXT:
444 priv->async_context = g_value_get_pointer (value);
445 if (priv->async_context)
446 g_main_context_ref (priv->async_context);
449 priv->timeout = g_value_get_uint (value);
457 get_property (GObject *object, guint prop_id,
458 GValue *value, GParamSpec *pspec)
460 SoupSession *session = SOUP_SESSION (object);
461 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
465 g_value_set_pointer (value, priv->proxy_uri ?
466 soup_uri_copy (priv->proxy_uri) :
470 g_value_set_int (value, priv->max_conns);
472 case PROP_MAX_CONNS_PER_HOST:
473 g_value_set_int (value, priv->max_conns_per_host);
476 g_value_set_boolean (value, priv->use_ntlm);
478 case PROP_SSL_CA_FILE:
479 g_value_set_string (value, priv->ssl_ca_file);
481 case PROP_ASYNC_CONTEXT:
482 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
485 g_value_set_uint (value, priv->timeout);
494 * soup_session_add_filter:
495 * @session: a #SoupSession
496 * @filter: an object implementing the #SoupMessageFilter interface
498 * Adds @filter to @session's list of message filters to be applied to
502 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
504 SoupSessionPrivate *priv;
506 g_return_if_fail (SOUP_IS_SESSION (session));
507 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
508 priv = SOUP_SESSION_GET_PRIVATE (session);
510 g_object_ref (filter);
511 priv->filters = g_slist_prepend (priv->filters, filter);
515 * soup_session_remove_filter:
516 * @session: a #SoupSession
517 * @filter: an object implementing the #SoupMessageFilter interface
519 * Removes @filter from @session's list of message filters
522 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
524 SoupSessionPrivate *priv;
526 g_return_if_fail (SOUP_IS_SESSION (session));
527 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
528 priv = SOUP_SESSION_GET_PRIVATE (session);
530 priv->filters = g_slist_remove (priv->filters, filter);
531 g_object_unref (filter);
537 host_uri_hash (gconstpointer key)
539 const SoupUri *uri = key;
541 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
545 host_uri_equal (gconstpointer v1, gconstpointer v2)
547 const SoupUri *one = v1;
548 const SoupUri *two = v2;
550 if (one->protocol != two->protocol)
552 if (one->port != two->port)
555 return strcmp (one->host, two->host) == 0;
558 static SoupSessionHost *
559 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
561 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
562 SoupSessionHost *host;
564 host = g_new0 (SoupSessionHost, 1);
565 host->root_uri = soup_uri_copy_root (source_uri);
567 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
570 soup_ssl_get_client_credentials (priv->ssl_ca_file);
576 /* Note: get_host_for_message doesn't lock the host_lock. The caller
577 * must do it itself if there's a chance the host doesn't already
580 static SoupSessionHost *
581 get_host_for_message (SoupSession *session, SoupMessage *msg)
583 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
584 SoupSessionHost *host;
585 const SoupUri *source = soup_message_get_uri (msg);
587 host = g_hash_table_lookup (priv->hosts, source);
591 host = soup_session_host_new (session, source);
592 g_hash_table_insert (priv->hosts, host->root_uri, host);
597 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
598 * it itself if there's a chance the host doesn't already exist.
600 static SoupSessionHost *
601 get_proxy_host (SoupSession *session)
603 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
605 if (priv->proxy_host || !priv->proxy_uri)
606 return priv->proxy_host;
609 soup_session_host_new (session, priv->proxy_uri);
610 return priv->proxy_host;
614 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
617 g_free (scheme_realm);
621 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
623 g_free (scheme_realm);
624 g_object_unref (auth);
628 free_host (SoupSessionHost *host)
630 while (host->connections) {
631 SoupConnection *conn = host->connections->data;
633 host->connections = g_slist_remove (host->connections, conn);
634 soup_connection_disconnect (conn);
637 if (host->auth_realms) {
638 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
639 g_hash_table_destroy (host->auth_realms);
642 g_hash_table_foreach (host->auths, free_auth, NULL);
643 g_hash_table_destroy (host->auths);
646 soup_uri_free (host->root_uri);
653 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
655 SoupSessionHost *host;
657 const char *realm, *const_path;
660 host = get_proxy_host (session);
663 host = get_host_for_message (session, msg);
664 const_path = soup_message_get_uri (msg)->path;
669 g_return_val_if_fail (host != NULL, NULL);
671 if (!host->auth_realms)
674 path = g_strdup (const_path);
677 realm = g_hash_table_lookup (host->auth_realms, path);
681 dir = strrchr (path, '/');
692 return g_hash_table_lookup (host->auths, realm);
698 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
703 info = soup_auth_get_info (auth);
704 if (g_hash_table_lookup_extended (host->auths, info, &key, &value) &&
705 auth == (SoupAuth *)value) {
706 g_hash_table_remove (host->auths, info);
708 g_object_unref (auth);
714 authenticate_auth (SoupSession *session, SoupAuth *auth,
715 SoupMessage *msg, gboolean prior_auth_failed,
718 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
720 char *username = NULL, *password = NULL;
723 uri = priv->proxy_uri;
725 uri = soup_message_get_uri (msg);
727 if (uri->passwd && !prior_auth_failed) {
728 soup_auth_authenticate (auth, uri->user, uri->passwd);
732 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
733 msg, soup_auth_get_scheme_name (auth),
734 soup_auth_get_realm (auth),
735 &username, &password);
736 if (username || password)
737 soup_auth_authenticate (auth, username, password);
741 memset (password, 0, strlen (password));
745 return soup_auth_is_authenticated (auth);
749 update_auth_internal (SoupSession *session, SoupMessage *msg,
750 const GSList *headers, gboolean proxy)
752 SoupSessionHost *host;
753 SoupAuth *new_auth, *prior_auth, *old_auth;
754 gpointer old_path, old_auth_info;
755 const SoupUri *msg_uri;
759 gboolean prior_auth_failed = FALSE;
762 host = get_proxy_host (session);
764 host = get_host_for_message (session, msg);
766 g_return_val_if_fail (host != NULL, FALSE);
768 /* Try to construct a new auth from the headers; if we can't,
769 * there's no way we'll be able to authenticate.
771 msg_uri = soup_message_get_uri (msg);
772 new_auth = soup_auth_new_from_header_list (headers);
776 auth_info = soup_auth_get_info (new_auth);
778 /* See if this auth is the same auth we used last time */
779 prior_auth = proxy ? soup_message_get_proxy_auth (msg) : soup_message_get_auth (msg);
781 char *old_auth_info = soup_auth_get_info (prior_auth);
783 if (!strcmp (old_auth_info, auth_info)) {
784 /* The server didn't like the username/password we
785 * provided before. Invalidate it and note this fact.
787 invalidate_auth (host, prior_auth);
788 prior_auth_failed = TRUE;
790 g_free (old_auth_info);
793 if (!host->auth_realms) {
794 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
795 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
798 /* Record where this auth realm is used. RFC 2617 is somewhat
799 * unclear about the scope of protection spaces with regard to
800 * proxies. The only mention of it is as an aside in section
801 * 3.2.1, where it is defining the fields of a Digest
802 * challenge and says that the protection space is always the
803 * entire proxy. Is this the case for all authentication
804 * schemes or just Digest? Who knows, but we're assuming all.
807 pspace = g_slist_prepend (NULL, g_strdup (""));
809 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
811 for (p = pspace; p; p = p->next) {
813 if (g_hash_table_lookup_extended (host->auth_realms, path,
814 &old_path, &old_auth_info)) {
815 g_hash_table_remove (host->auth_realms, old_path);
817 g_free (old_auth_info);
820 g_hash_table_insert (host->auth_realms,
821 g_strdup (path), g_strdup (auth_info));
823 soup_auth_free_protection_space (new_auth, pspace);
825 /* Now, make sure the auth is recorded. (If there's a
826 * pre-existing auth, we keep that rather than the new one,
827 * since the old one might already be authenticated.)
829 old_auth = g_hash_table_lookup (host->auths, auth_info);
832 g_object_unref (new_auth);
835 g_hash_table_insert (host->auths, auth_info, new_auth);
837 /* If we need to authenticate, try to do it. */
838 if (!soup_auth_is_authenticated (new_auth)) {
839 return authenticate_auth (session, new_auth,
840 msg, prior_auth_failed, proxy);
843 /* Otherwise we're good. */
848 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
849 const char *auth_type, const char *auth_realm,
850 char **username, char **password, gpointer session)
852 g_signal_emit (session, signals[AUTHENTICATE], 0,
853 msg, auth_type, auth_realm, username, password);
857 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
858 const char *auth_type, const char *auth_realm,
859 char **username, char **password,
862 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
863 msg, auth_type, auth_realm, username, password);
868 authorize_handler (SoupMessage *msg, gpointer user_data)
870 SoupSession *session = user_data;
871 const GSList *headers;
874 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
875 headers = soup_message_get_header_list (msg->response_headers,
876 "Proxy-Authenticate");
879 headers = soup_message_get_header_list (msg->response_headers,
886 if (update_auth_internal (session, msg, headers, proxy))
887 soup_session_requeue_message (session, msg);
891 redirect_handler (SoupMessage *msg, gpointer user_data)
893 SoupSession *session = user_data;
897 new_loc = soup_message_get_header (msg->response_headers, "Location");
901 /* Location is supposed to be an absolute URI, but some sites
902 * are lame, so we use soup_uri_new_with_base().
904 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
906 soup_message_set_status_full (msg,
907 SOUP_STATUS_MALFORMED,
908 "Invalid Redirect URL");
912 soup_message_set_uri (msg, new_uri);
913 soup_uri_free (new_uri);
915 soup_session_requeue_message (session, msg);
919 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
923 auth = lookup_auth (session, msg, proxy);
924 if (auth && !soup_auth_is_authenticated (auth)) {
925 if (!authenticate_auth (session, auth, msg, FALSE, proxy))
930 soup_message_set_proxy_auth (msg, auth);
932 soup_message_set_auth (msg, auth);
936 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
938 SoupSession *session = SOUP_SESSION (filter);
939 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
942 for (f = priv->filters; f; f = f->next) {
944 soup_message_filter_setup_message (filter, msg);
947 add_auth (session, msg, FALSE);
948 soup_message_add_status_code_handler (
949 msg, SOUP_STATUS_UNAUTHORIZED,
950 SOUP_HANDLER_POST_BODY,
951 authorize_handler, session);
953 if (priv->proxy_uri) {
954 add_auth (session, msg, TRUE);
955 soup_message_add_status_code_handler (
956 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
957 SOUP_HANDLER_POST_BODY,
958 authorize_handler, session);
963 find_oldest_connection (gpointer key, gpointer host, gpointer data)
965 SoupConnection *conn = key, **oldest = data;
967 /* Don't prune a connection that is currently in use, or
968 * hasn't been used yet.
970 if (soup_connection_is_in_use (conn) ||
971 soup_connection_last_used (conn) == 0)
974 if (!*oldest || (soup_connection_last_used (conn) <
975 soup_connection_last_used (*oldest)))
980 * soup_session_try_prune_connection:
981 * @session: a #SoupSession
983 * Finds the least-recently-used idle connection in @session and closes
986 * Return value: %TRUE if a connection was closed, %FALSE if there are
987 * no idle connections.
990 soup_session_try_prune_connection (SoupSession *session)
992 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
993 SoupConnection *oldest = NULL;
995 g_mutex_lock (priv->host_lock);
996 g_hash_table_foreach (priv->conns, find_oldest_connection,
999 /* Ref the connection before unlocking the mutex in
1000 * case someone else tries to prune it too.
1002 g_object_ref (oldest);
1003 g_mutex_unlock (priv->host_lock);
1004 soup_connection_disconnect (oldest);
1005 g_object_unref (oldest);
1008 g_mutex_unlock (priv->host_lock);
1014 connection_disconnected (SoupConnection *conn, gpointer user_data)
1016 SoupSession *session = user_data;
1017 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1018 SoupSessionHost *host;
1020 g_mutex_lock (priv->host_lock);
1022 host = g_hash_table_lookup (priv->conns, conn);
1024 g_hash_table_remove (priv->conns, conn);
1025 host->connections = g_slist_remove (host->connections, conn);
1029 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1032 g_mutex_unlock (priv->host_lock);
1033 g_object_unref (conn);
1037 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1039 SoupSession *session = user_data;
1040 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1041 SoupSessionHost *host;
1042 SoupMessageQueueIter iter;
1045 g_mutex_lock (priv->host_lock);
1047 host = g_hash_table_lookup (priv->conns, conn);
1049 g_mutex_unlock (priv->host_lock);
1053 if (status == SOUP_STATUS_OK) {
1054 soup_connection_reserve (conn);
1055 host->connections = g_slist_prepend (host->connections, conn);
1056 g_mutex_unlock (priv->host_lock);
1060 /* The connection failed. */
1061 g_mutex_unlock (priv->host_lock);
1062 connection_disconnected (conn, session);
1064 if (host->connections) {
1065 /* Something went wrong this time, but we have at
1066 * least one open connection to this host. So just
1067 * leave the message in the queue so it can use that
1068 * connection once it's free.
1073 /* There are two possibilities: either status is
1074 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1075 * will create a new connection (and all we need to do here
1076 * is downgrade the message from CONNECTING to QUEUED); or
1077 * status is something else, probably CANT_CONNECT or
1078 * CANT_RESOLVE or the like, in which case we need to cancel
1079 * any messages waiting for this host, since they're out
1082 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1083 if (get_host_for_message (session, msg) == host) {
1084 if (status == SOUP_STATUS_TRY_AGAIN) {
1085 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1086 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1088 soup_message_set_status (msg, status);
1089 soup_session_cancel_message (session, msg);
1096 * soup_session_get_connection:
1097 * @session: a #SoupSession
1098 * @msg: a #SoupMessage
1099 * @try_pruning: on return, whether or not to try pruning a connection
1100 * @is_new: on return, %TRUE if the returned connection is new and not
1103 * Tries to find or create a connection for @msg; this is an internal
1104 * method for #SoupSession subclasses.
1106 * If there is an idle connection to the relevant host available, then
1107 * that connection will be returned (with *@is_new set to %FALSE). The
1108 * connection will be marked "reserved", so the caller must call
1109 * soup_connection_release() if it ends up not using the connection
1112 * If there is no idle connection available, but it is possible to
1113 * create a new connection, then one will be created and returned,
1114 * with *@is_new set to %TRUE. The caller MUST then call
1115 * soup_connection_connect_sync() or soup_connection_connect_async()
1116 * to connect it. If the connection attempt succeeds, the connection
1117 * will be marked "reserved" and added to @session's connection pool
1118 * once it connects. If the connection attempt fails, the connection
1121 * If no connection is available and a new connection cannot be made,
1122 * soup_session_get_connection() will return %NULL. If @session has
1123 * the maximum number of open connections open, but does not have the
1124 * maximum number of per-host connections open to the relevant host,
1125 * then *@try_pruning will be set to %TRUE. In this case, the caller
1126 * can call soup_session_try_prune_connection() to close an idle
1127 * connection, and then try soup_session_get_connection() again. (If
1128 * calling soup_session_try_prune_connection() wouldn't help, then
1129 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1131 * Return value: a #SoupConnection, or %NULL
1134 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1135 gboolean *try_pruning, gboolean *is_new)
1137 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1138 SoupConnection *conn;
1139 SoupSessionHost *host;
1142 g_mutex_lock (priv->host_lock);
1144 host = get_host_for_message (session, msg);
1145 for (conns = host->connections; conns; conns = conns->next) {
1146 if (!soup_connection_is_in_use (conns->data)) {
1147 soup_connection_reserve (conns->data);
1148 g_mutex_unlock (priv->host_lock);
1154 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1155 /* We already started a connection for this
1156 * message, so don't start another one.
1158 g_mutex_unlock (priv->host_lock);
1162 if (host->num_conns >= priv->max_conns_per_host) {
1163 g_mutex_unlock (priv->host_lock);
1167 if (priv->num_conns >= priv->max_conns) {
1168 *try_pruning = TRUE;
1169 g_mutex_unlock (priv->host_lock);
1173 /* Make sure priv->proxy_host gets set now while
1174 * we have the host_lock.
1176 if (priv->proxy_uri)
1177 get_proxy_host (session);
1179 conn = g_object_new (
1181 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1182 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1183 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1184 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1185 SOUP_CONNECTION_MESSAGE_FILTER, session,
1186 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1187 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1189 g_signal_connect (conn, "connect_result",
1190 G_CALLBACK (connect_result),
1192 g_signal_connect (conn, "disconnected",
1193 G_CALLBACK (connection_disconnected),
1195 g_signal_connect (conn, "authenticate",
1196 G_CALLBACK (connection_authenticate),
1198 g_signal_connect (conn, "reauthenticate",
1199 G_CALLBACK (connection_reauthenticate),
1202 g_hash_table_insert (priv->conns, conn, host);
1204 /* We increment the connection counts so it counts against the
1205 * totals, but we don't add it to the host's connection list
1206 * yet, since it's not ready for use.
1211 /* Mark the request as connecting, so we don't try to open
1212 * another new connection for it while waiting for this one.
1214 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1216 g_mutex_unlock (priv->host_lock);
1222 message_finished (SoupMessage *msg, gpointer user_data)
1224 SoupSession *session = user_data;
1226 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1227 soup_message_queue_remove_message (session->queue, msg);
1228 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1233 queue_message (SoupSession *session, SoupMessage *msg,
1234 SoupMessageCallbackFn callback, gpointer user_data)
1236 g_signal_connect_after (msg, "finished",
1237 G_CALLBACK (message_finished), session);
1239 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1240 soup_message_add_status_class_handler (
1241 msg, SOUP_STATUS_CLASS_REDIRECT,
1242 SOUP_HANDLER_POST_BODY,
1243 redirect_handler, session);
1246 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1247 soup_message_queue_append (session->queue, msg);
1251 * soup_session_queue_message:
1252 * @session: a #SoupSession
1253 * @msg: the message to queue
1254 * @callback: a #SoupMessageCallbackFn which will be called after the
1255 * message completes or when an unrecoverable error occurs.
1256 * @user_data: a pointer passed to @callback.
1258 * Queues the message @msg for sending. All messages are processed
1259 * while the glib main loop runs. If @msg has been processed before,
1260 * any resources related to the time it was last sent are freed.
1262 * Upon message completion, the callback specified in @callback will
1263 * be invoked (in the thread associated with @session's async
1264 * context). If after returning from this callback the message has not
1265 * been requeued, @msg will be unreffed.
1268 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1269 SoupMessageCallbackFn callback, gpointer user_data)
1271 g_return_if_fail (SOUP_IS_SESSION (session));
1272 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1274 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1275 callback, user_data);
1279 requeue_message (SoupSession *session, SoupMessage *msg)
1281 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1285 * soup_session_requeue_message:
1286 * @session: a #SoupSession
1287 * @msg: the message to requeue
1289 * This causes @msg to be placed back on the queue to be attempted
1293 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1295 g_return_if_fail (SOUP_IS_SESSION (session));
1296 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1298 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1303 * soup_session_send_message:
1304 * @session: a #SoupSession
1305 * @msg: the message to send
1307 * Synchronously send @msg. This call will not return until the
1308 * transfer is finished successfully or there is an unrecoverable
1311 * @msg is not freed upon return.
1313 * Return value: the HTTP status code of the response
1316 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1318 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1319 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1321 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1326 cancel_message (SoupSession *session, SoupMessage *msg)
1328 soup_message_queue_remove_message (session->queue, msg);
1329 soup_message_finished (msg);
1333 * soup_session_cancel_message:
1334 * @session: a #SoupSession
1335 * @msg: the message to cancel
1337 * Causes @session to immediately finish processing @msg. You should
1338 * set a status code on @msg with soup_message_set_status() before
1339 * calling this function.
1342 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1344 g_return_if_fail (SOUP_IS_SESSION (session));
1345 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1347 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1351 gather_conns (gpointer key, gpointer host, gpointer data)
1353 SoupConnection *conn = key;
1354 GSList **conns = data;
1356 *conns = g_slist_prepend (*conns, conn);
1360 * soup_session_abort:
1361 * @session: the session
1363 * Cancels all pending requests in @session.
1366 soup_session_abort (SoupSession *session)
1368 SoupSessionPrivate *priv;
1369 SoupMessageQueueIter iter;
1373 g_return_if_fail (SOUP_IS_SESSION (session));
1374 priv = SOUP_SESSION_GET_PRIVATE (session);
1376 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1377 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1378 soup_session_cancel_message (session, msg);
1381 /* Close all connections */
1382 g_mutex_lock (priv->host_lock);
1384 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1386 for (c = conns; c; c = c->next)
1387 g_object_ref (c->data);
1388 g_mutex_unlock (priv->host_lock);
1389 for (c = conns; c; c = c->next) {
1390 soup_connection_disconnect (c->data);
1391 g_object_unref (c->data);
1394 g_slist_free (conns);