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-queue.h"
29 GSList *connections; /* CONTAINS: SoupConnection */
32 GHashTable *auth_realms; /* path -> scheme:realm */
33 GHashTable *auths; /* scheme:realm -> SoupAuth */
38 guint max_conns, max_conns_per_host;
46 GHashTable *hosts; /* SoupUri -> SoupSessionHost */
47 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
50 SoupSessionHost *proxy_host;
52 /* Must hold the host_lock before potentially creating a
53 * new SoupSessionHost, or adding/removing a connection.
54 * Must not emit signals or destroy objects while holding it.
58 GMainContext *async_context;
60 /* Holds the timeout value for the connection, when
61 no response is received.
65 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
67 static guint host_uri_hash (gconstpointer key);
68 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
69 static void free_host (SoupSessionHost *host);
71 static void setup_message (SoupMessageFilter *filter, SoupMessage *msg);
73 static void queue_message (SoupSession *session, SoupMessage *msg,
74 SoupMessageCallbackFn callback,
76 static void requeue_message (SoupSession *session, SoupMessage *msg);
77 static void cancel_message (SoupSession *session, SoupMessage *msg);
79 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
80 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
82 static void filter_iface_init (SoupMessageFilterClass *filter_class);
84 G_DEFINE_TYPE_EXTENDED (SoupSession, soup_session, G_TYPE_OBJECT, 0,
85 G_IMPLEMENT_INTERFACE (SOUP_TYPE_MESSAGE_FILTER,
94 static guint signals[LAST_SIGNAL] = { 0 };
101 PROP_MAX_CONNS_PER_HOST,
110 static void set_property (GObject *object, guint prop_id,
111 const GValue *value, GParamSpec *pspec);
112 static void get_property (GObject *object, guint prop_id,
113 GValue *value, GParamSpec *pspec);
116 soup_session_init (SoupSession *session)
118 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
120 session->queue = soup_message_queue_new ();
122 priv->host_lock = g_mutex_new ();
123 priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
124 priv->conns = g_hash_table_new (NULL, NULL);
126 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
127 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
133 foreach_free_host (gpointer key, gpointer host, gpointer data)
140 cleanup_hosts (SoupSessionPrivate *priv)
142 GHashTable *old_hosts;
144 g_mutex_lock (priv->host_lock);
145 old_hosts = priv->hosts;
146 priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
147 g_mutex_unlock (priv->host_lock);
149 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
150 g_hash_table_destroy (old_hosts);
154 dispose (GObject *object)
156 SoupSession *session = SOUP_SESSION (object);
157 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
160 soup_session_abort (session);
161 cleanup_hosts (priv);
164 for (f = priv->filters; f; f = f->next)
165 g_object_unref (f->data);
166 g_slist_free (priv->filters);
167 priv->filters = NULL;
170 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
174 finalize (GObject *object)
176 SoupSession *session = SOUP_SESSION (object);
177 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
179 soup_message_queue_destroy (session->queue);
181 g_mutex_free (priv->host_lock);
182 g_hash_table_destroy (priv->hosts);
183 g_hash_table_destroy (priv->conns);
186 soup_ssl_free_client_credentials (priv->ssl_creds);
188 if (priv->async_context)
189 g_main_context_unref (priv->async_context);
191 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
195 soup_session_class_init (SoupSessionClass *session_class)
197 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
199 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
201 /* virtual method definition */
202 session_class->queue_message = queue_message;
203 session_class->requeue_message = requeue_message;
204 session_class->cancel_message = cancel_message;
206 /* virtual method override */
207 object_class->dispose = dispose;
208 object_class->finalize = finalize;
209 object_class->set_property = set_property;
210 object_class->get_property = get_property;
215 * SoupSession::authenticate:
216 * @session: the session
217 * @msg: the #SoupMessage being sent
218 * @auth_type: the authentication type
219 * @auth_realm: the realm being authenticated to
220 * @username: the signal handler should set this to point to
221 * the provided username
222 * @password: the signal handler should set this to point to
223 * the provided password
225 * Emitted when the session requires authentication. The
226 * credentials may come from the user, or from cached
227 * information. If no credentials are available, leave
228 * @username and @password unchanged.
230 * If the provided credentials fail, the #reauthenticate
231 * signal will be emitted.
233 signals[AUTHENTICATE] =
234 g_signal_new ("authenticate",
235 G_OBJECT_CLASS_TYPE (object_class),
237 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
239 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
248 * SoupSession::reauthenticate:
249 * @session: the session
250 * @msg: the #SoupMessage being sent
251 * @auth_type: the authentication type
252 * @auth_realm: the realm being authenticated to
253 * @username: the signal handler should set this to point to
254 * the provided username
255 * @password: the signal handler should set this to point to
256 * the provided password
258 * Emitted when the credentials provided by the application to
259 * the #authenticate signal have failed. This gives the
260 * application a second chance to provide authentication
261 * credentials. If the new credentials also fail, #SoupSession
262 * will emit #reauthenticate again, and will continue doing so
263 * until the provided credentials work, or a #reauthenticate
264 * signal emission "fails" (because the handler left @username
265 * and @password unchanged). At that point, the 401 or 407
266 * error status will be returned to the caller.
268 * If your application only uses cached passwords, it should
269 * only connect to #authenticate, and not #reauthenticate.
271 * If your application always prompts the user for a password,
272 * and never uses cached information, then you can connect the
273 * same handler to #authenticate and #reauthenticate.
275 * To get standard web-browser behavior, return either cached
276 * information or a user-provided password (whichever is
277 * available) from the #authenticate handler, but return only
278 * user-provided information from the #reauthenticate handler.
280 signals[REAUTHENTICATE] =
281 g_signal_new ("reauthenticate",
282 G_OBJECT_CLASS_TYPE (object_class),
284 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
286 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
295 g_object_class_install_property (
296 object_class, PROP_PROXY_URI,
297 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
299 "The HTTP Proxy to use for this session",
301 g_object_class_install_property (
302 object_class, PROP_MAX_CONNS,
303 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
304 "Max Connection Count",
305 "The maximum number of connections that the session can open at once",
310 g_object_class_install_property (
311 object_class, PROP_MAX_CONNS_PER_HOST,
312 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
313 "Max Per-Host Connection Count",
314 "The maximum number of connections that the session can open at once to a given host",
319 g_object_class_install_property (
320 object_class, PROP_USE_NTLM,
321 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
323 "Whether or not to use NTLM authentication",
326 g_object_class_install_property (
327 object_class, PROP_SSL_CA_FILE,
328 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
330 "File containing SSL CA certificates",
333 g_object_class_install_property (
334 object_class, PROP_ASYNC_CONTEXT,
335 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
336 "Async GMainContext",
337 "The GMainContext to dispatch async I/O in",
338 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
339 g_object_class_install_property (
340 object_class, PROP_TIMEOUT,
341 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
343 "Value in seconds to timeout a blocking I/O",
349 filter_iface_init (SoupMessageFilterClass *filter_class)
351 /* interface implementation */
352 filter_class->setup_message = setup_message;
357 safe_uri_equal (const SoupUri *a, const SoupUri *b)
362 if ((a && !b) || (b && !a))
365 return soup_uri_equal (a, b);
369 safe_str_equal (const char *a, const char *b)
374 if ((a && !b) || (b && !a))
377 return strcmp (a, b) == 0;
381 set_property (GObject *object, guint prop_id,
382 const GValue *value, GParamSpec *pspec)
384 SoupSession *session = SOUP_SESSION (object);
385 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
387 gboolean need_abort = FALSE;
388 gboolean ca_file_changed = FALSE;
389 const char *new_ca_file;
393 pval = g_value_get_pointer (value);
395 if (!safe_uri_equal (priv->proxy_uri, pval))
399 soup_uri_free (priv->proxy_uri);
401 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
404 soup_session_abort (session);
405 cleanup_hosts (priv);
410 priv->max_conns = g_value_get_int (value);
412 case PROP_MAX_CONNS_PER_HOST:
413 priv->max_conns_per_host = g_value_get_int (value);
416 priv->use_ntlm = g_value_get_boolean (value);
418 case PROP_SSL_CA_FILE:
419 new_ca_file = g_value_get_string (value);
421 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
422 ca_file_changed = TRUE;
424 g_free (priv->ssl_ca_file);
425 priv->ssl_ca_file = g_strdup (new_ca_file);
427 if (ca_file_changed) {
428 if (priv->ssl_creds) {
429 soup_ssl_free_client_credentials (priv->ssl_creds);
430 priv->ssl_creds = NULL;
433 cleanup_hosts (priv);
437 case PROP_ASYNC_CONTEXT:
438 priv->async_context = g_value_get_pointer (value);
439 if (priv->async_context)
440 g_main_context_ref (priv->async_context);
443 priv->timeout = g_value_get_uint (value);
451 get_property (GObject *object, guint prop_id,
452 GValue *value, GParamSpec *pspec)
454 SoupSession *session = SOUP_SESSION (object);
455 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
459 g_value_set_pointer (value, priv->proxy_uri ?
460 soup_uri_copy (priv->proxy_uri) :
464 g_value_set_int (value, priv->max_conns);
466 case PROP_MAX_CONNS_PER_HOST:
467 g_value_set_int (value, priv->max_conns_per_host);
470 g_value_set_boolean (value, priv->use_ntlm);
472 case PROP_SSL_CA_FILE:
473 g_value_set_string (value, priv->ssl_ca_file);
475 case PROP_ASYNC_CONTEXT:
476 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
479 g_value_set_uint (value, priv->timeout);
488 * soup_session_add_filter:
489 * @session: a #SoupSession
490 * @filter: an object implementing the #SoupMessageFilter interface
492 * Adds @filter to @session's list of message filters to be applied to
496 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
498 SoupSessionPrivate *priv;
500 g_return_if_fail (SOUP_IS_SESSION (session));
501 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
502 priv = SOUP_SESSION_GET_PRIVATE (session);
504 g_object_ref (filter);
505 priv->filters = g_slist_prepend (priv->filters, filter);
509 * soup_session_remove_filter:
510 * @session: a #SoupSession
511 * @filter: an object implementing the #SoupMessageFilter interface
513 * Removes @filter from @session's list of message filters
516 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
518 SoupSessionPrivate *priv;
520 g_return_if_fail (SOUP_IS_SESSION (session));
521 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
522 priv = SOUP_SESSION_GET_PRIVATE (session);
524 priv->filters = g_slist_remove (priv->filters, filter);
525 g_object_unref (filter);
531 host_uri_hash (gconstpointer key)
533 const SoupUri *uri = key;
535 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
539 host_uri_equal (gconstpointer v1, gconstpointer v2)
541 const SoupUri *one = v1;
542 const SoupUri *two = v2;
544 if (one->protocol != two->protocol)
546 if (one->port != two->port)
549 return strcmp (one->host, two->host) == 0;
552 static SoupSessionHost *
553 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
555 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
556 SoupSessionHost *host;
558 host = g_new0 (SoupSessionHost, 1);
559 host->root_uri = soup_uri_copy_root (source_uri);
561 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
564 soup_ssl_get_client_credentials (priv->ssl_ca_file);
570 /* Note: get_host_for_message doesn't lock the host_lock. The caller
571 * must do it itself if there's a chance the host doesn't already
574 static SoupSessionHost *
575 get_host_for_message (SoupSession *session, SoupMessage *msg)
577 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
578 SoupSessionHost *host;
579 const SoupUri *source = soup_message_get_uri (msg);
581 host = g_hash_table_lookup (priv->hosts, source);
585 host = soup_session_host_new (session, source);
586 g_hash_table_insert (priv->hosts, host->root_uri, host);
591 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
592 * it itself if there's a chance the host doesn't already exist.
594 static SoupSessionHost *
595 get_proxy_host (SoupSession *session)
597 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
599 if (priv->proxy_host || !priv->proxy_uri)
600 return priv->proxy_host;
603 soup_session_host_new (session, priv->proxy_uri);
604 return priv->proxy_host;
608 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
611 g_free (scheme_realm);
615 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
617 g_free (scheme_realm);
618 g_object_unref (auth);
622 free_host (SoupSessionHost *host)
624 while (host->connections) {
625 SoupConnection *conn = host->connections->data;
627 host->connections = g_slist_remove (host->connections, conn);
628 soup_connection_disconnect (conn);
631 if (host->auth_realms) {
632 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
633 g_hash_table_destroy (host->auth_realms);
636 g_hash_table_foreach (host->auths, free_auth, NULL);
637 g_hash_table_destroy (host->auths);
640 soup_uri_free (host->root_uri);
647 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
649 SoupSessionHost *host;
651 const char *realm, *const_path;
654 host = get_proxy_host (session);
657 host = get_host_for_message (session, msg);
658 const_path = soup_message_get_uri (msg)->path;
663 g_return_val_if_fail (host != NULL, NULL);
665 if (!host->auth_realms)
668 path = g_strdup (const_path);
671 realm = g_hash_table_lookup (host->auth_realms, path);
675 dir = strrchr (path, '/');
686 return g_hash_table_lookup (host->auths, realm);
692 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
697 realm = g_strdup_printf ("%s:%s",
698 soup_auth_get_scheme_name (auth),
699 soup_auth_get_realm (auth));
701 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
702 auth == (SoupAuth *)value) {
703 g_hash_table_remove (host->auths, realm);
705 g_object_unref (auth);
711 authenticate_auth (SoupSession *session, SoupAuth *auth,
712 SoupMessage *msg, gboolean prior_auth_failed,
715 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
717 char *username = NULL, *password = NULL;
720 uri = priv->proxy_uri;
722 uri = soup_message_get_uri (msg);
724 if (uri->passwd && !prior_auth_failed) {
725 soup_auth_authenticate (auth, uri->user, uri->passwd);
729 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
730 msg, soup_auth_get_scheme_name (auth),
731 soup_auth_get_realm (auth),
732 &username, &password);
733 if (username || password)
734 soup_auth_authenticate (auth, username, password);
738 memset (password, 0, strlen (password));
742 return soup_auth_is_authenticated (auth);
746 update_auth_internal (SoupSession *session, SoupMessage *msg,
747 const GSList *headers, gboolean proxy,
748 gboolean got_unauthorized)
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 = lookup_auth (session, msg, proxy);
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 if (!got_unauthorized) {
781 /* The user is just trying to preauthenticate
782 * using information we already have, so
783 * there's nothing more that needs to be done.
785 g_object_unref (new_auth);
789 /* The server didn't like the username/password we
790 * provided before. Invalidate it and note this fact.
792 invalidate_auth (host, prior_auth);
793 prior_auth_failed = TRUE;
796 if (!host->auth_realms) {
797 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
798 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
801 /* Record where this auth realm is used */
802 realm = g_strdup_printf ("%s:%s",
803 soup_auth_get_scheme_name (new_auth),
804 soup_auth_get_realm (new_auth));
807 * RFC 2617 is somewhat unclear about the scope of protection
808 * spaces with regard to proxies. The only mention of it is
809 * as an aside in section 3.2.1, where it is defining the fields
810 * of a Digest challenge and says that the protection space is
811 * always the entire proxy. Is this the case for all authentication
812 * schemes or just Digest? Who knows, but we're assuming all.
815 pspace = g_slist_prepend (NULL, g_strdup (""));
817 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
819 for (p = pspace; p; p = p->next) {
821 if (g_hash_table_lookup_extended (host->auth_realms, path,
822 &old_path, &old_realm)) {
823 g_hash_table_remove (host->auth_realms, old_path);
828 g_hash_table_insert (host->auth_realms,
829 g_strdup (path), g_strdup (realm));
831 soup_auth_free_protection_space (new_auth, pspace);
833 /* Now, make sure the auth is recorded. (If there's a
834 * pre-existing auth, we keep that rather than the new one,
835 * since the old one might already be authenticated.)
837 old_auth = g_hash_table_lookup (host->auths, realm);
840 g_object_unref (new_auth);
843 g_hash_table_insert (host->auths, realm, new_auth);
845 /* If we need to authenticate, try to do it. */
846 if (!soup_auth_is_authenticated (new_auth)) {
847 return authenticate_auth (session, new_auth,
848 msg, prior_auth_failed, proxy);
851 /* Otherwise we're good. */
856 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
857 const char *auth_type, const char *auth_realm,
858 char **username, char **password, gpointer session)
860 g_signal_emit (session, signals[AUTHENTICATE], 0,
861 msg, auth_type, auth_realm, username, password);
865 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
866 const char *auth_type, const char *auth_realm,
867 char **username, char **password,
870 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
871 msg, auth_type, auth_realm, username, password);
876 authorize_handler (SoupMessage *msg, gpointer user_data)
878 SoupSession *session = user_data;
879 const GSList *headers;
882 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
883 headers = soup_message_get_header_list (msg->response_headers,
884 "Proxy-Authenticate");
887 headers = soup_message_get_header_list (msg->response_headers,
894 if (update_auth_internal (session, msg, headers, proxy, TRUE))
895 soup_session_requeue_message (session, msg);
899 redirect_handler (SoupMessage *msg, gpointer user_data)
901 SoupSession *session = user_data;
905 new_loc = soup_message_get_header (msg->response_headers, "Location");
909 /* Location is supposed to be an absolute URI, but some sites
910 * are lame, so we use soup_uri_new_with_base().
912 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
914 soup_message_set_status_full (msg,
915 SOUP_STATUS_MALFORMED,
916 "Invalid Redirect URL");
920 soup_message_set_uri (msg, new_uri);
921 soup_uri_free (new_uri);
923 soup_session_requeue_message (session, msg);
927 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
929 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
933 auth = lookup_auth (session, msg, proxy);
936 if (!soup_auth_is_authenticated (auth) &&
937 !authenticate_auth (session, auth, msg, FALSE, proxy))
940 token = soup_auth_get_authorization (auth, msg);
942 soup_message_remove_header (msg->request_headers, header);
943 soup_message_add_header (msg->request_headers, header, token);
949 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
951 SoupSession *session = SOUP_SESSION (filter);
952 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
955 for (f = priv->filters; f; f = f->next) {
957 soup_message_filter_setup_message (filter, msg);
960 add_auth (session, msg, FALSE);
961 soup_message_add_status_code_handler (
962 msg, SOUP_STATUS_UNAUTHORIZED,
963 SOUP_HANDLER_POST_BODY,
964 authorize_handler, session);
966 if (priv->proxy_uri) {
967 add_auth (session, msg, TRUE);
968 soup_message_add_status_code_handler (
969 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
970 SOUP_HANDLER_POST_BODY,
971 authorize_handler, session);
976 find_oldest_connection (gpointer key, gpointer host, gpointer data)
978 SoupConnection *conn = key, **oldest = data;
980 /* Don't prune a connection that is currently in use, or
981 * hasn't been used yet.
983 if (soup_connection_is_in_use (conn) ||
984 soup_connection_last_used (conn) == 0)
987 if (!*oldest || (soup_connection_last_used (conn) <
988 soup_connection_last_used (*oldest)))
993 * soup_session_try_prune_connection:
994 * @session: a #SoupSession
996 * Finds the least-recently-used idle connection in @session and closes
999 * Return value: %TRUE if a connection was closed, %FALSE if there are
1000 * no idle connections.
1003 soup_session_try_prune_connection (SoupSession *session)
1005 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1006 SoupConnection *oldest = NULL;
1008 g_mutex_lock (priv->host_lock);
1009 g_hash_table_foreach (priv->conns, find_oldest_connection,
1012 /* Ref the connection before unlocking the mutex in
1013 * case someone else tries to prune it too.
1015 g_object_ref (oldest);
1016 g_mutex_unlock (priv->host_lock);
1017 soup_connection_disconnect (oldest);
1018 g_object_unref (oldest);
1021 g_mutex_unlock (priv->host_lock);
1027 connection_disconnected (SoupConnection *conn, gpointer user_data)
1029 SoupSession *session = user_data;
1030 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1031 SoupSessionHost *host;
1033 g_mutex_lock (priv->host_lock);
1035 host = g_hash_table_lookup (priv->conns, conn);
1037 g_hash_table_remove (priv->conns, conn);
1038 host->connections = g_slist_remove (host->connections, conn);
1042 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1045 g_mutex_unlock (priv->host_lock);
1046 g_object_unref (conn);
1050 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1052 SoupSession *session = user_data;
1053 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1054 SoupSessionHost *host;
1055 SoupMessageQueueIter iter;
1058 g_mutex_lock (priv->host_lock);
1060 host = g_hash_table_lookup (priv->conns, conn);
1062 g_mutex_unlock (priv->host_lock);
1066 if (status == SOUP_STATUS_OK) {
1067 soup_connection_reserve (conn);
1068 host->connections = g_slist_prepend (host->connections, conn);
1069 g_mutex_unlock (priv->host_lock);
1073 /* The connection failed. */
1074 g_mutex_unlock (priv->host_lock);
1075 connection_disconnected (conn, session);
1077 if (host->connections) {
1078 /* Something went wrong this time, but we have at
1079 * least one open connection to this host. So just
1080 * leave the message in the queue so it can use that
1081 * connection once it's free.
1086 /* There are two possibilities: either status is
1087 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1088 * will create a new connection (and all we need to do here
1089 * is downgrade the message from CONNECTING to QUEUED); or
1090 * status is something else, probably CANT_CONNECT or
1091 * CANT_RESOLVE or the like, in which case we need to cancel
1092 * any messages waiting for this host, since they're out
1095 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1096 if (get_host_for_message (session, msg) == host) {
1097 if (status == SOUP_STATUS_TRY_AGAIN) {
1098 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1099 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1101 soup_message_set_status (msg, status);
1102 soup_session_cancel_message (session, msg);
1109 * soup_session_get_connection:
1110 * @session: a #SoupSession
1111 * @msg: a #SoupMessage
1112 * @try_pruning: on return, whether or not to try pruning a connection
1113 * @is_new: on return, %TRUE if the returned connection is new and not
1116 * Tries to find or create a connection for @msg; this is an internal
1117 * method for #SoupSession subclasses.
1119 * If there is an idle connection to the relevant host available, then
1120 * that connection will be returned (with *@is_new set to %FALSE). The
1121 * connection will be marked "reserved", so the caller must call
1122 * soup_connection_release() if it ends up not using the connection
1125 * If there is no idle connection available, but it is possible to
1126 * create a new connection, then one will be created and returned,
1127 * with *@is_new set to %TRUE. The caller MUST then call
1128 * soup_connection_connect_sync() or soup_connection_connect_async()
1129 * to connect it. If the connection attempt succeeds, the connection
1130 * will be marked "reserved" and added to @session's connection pool
1131 * once it connects. If the connection attempt fails, the connection
1134 * If no connection is available and a new connection cannot be made,
1135 * soup_session_get_connection() will return %NULL. If @session has
1136 * the maximum number of open connections open, but does not have the
1137 * maximum number of per-host connections open to the relevant host,
1138 * then *@try_pruning will be set to %TRUE. In this case, the caller
1139 * can call soup_session_try_prune_connection() to close an idle
1140 * connection, and then try soup_session_get_connection() again. (If
1141 * calling soup_session_try_prune_connection() wouldn't help, then
1142 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1144 * Return value: a #SoupConnection, or %NULL
1147 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1148 gboolean *try_pruning, gboolean *is_new)
1150 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1151 SoupConnection *conn;
1152 SoupSessionHost *host;
1155 g_mutex_lock (priv->host_lock);
1157 host = get_host_for_message (session, msg);
1158 for (conns = host->connections; conns; conns = conns->next) {
1159 if (!soup_connection_is_in_use (conns->data)) {
1160 soup_connection_reserve (conns->data);
1161 g_mutex_unlock (priv->host_lock);
1167 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1168 /* We already started a connection for this
1169 * message, so don't start another one.
1171 g_mutex_unlock (priv->host_lock);
1175 if (host->num_conns >= priv->max_conns_per_host) {
1176 g_mutex_unlock (priv->host_lock);
1180 if (priv->num_conns >= priv->max_conns) {
1181 *try_pruning = TRUE;
1182 g_mutex_unlock (priv->host_lock);
1186 /* Make sure priv->proxy_host gets set now while
1187 * we have the host_lock.
1189 if (priv->proxy_uri)
1190 get_proxy_host (session);
1192 conn = g_object_new (
1194 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1195 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1196 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1197 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1198 SOUP_CONNECTION_MESSAGE_FILTER, session,
1199 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1200 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1202 g_signal_connect (conn, "connect_result",
1203 G_CALLBACK (connect_result),
1205 g_signal_connect (conn, "disconnected",
1206 G_CALLBACK (connection_disconnected),
1208 g_signal_connect (conn, "authenticate",
1209 G_CALLBACK (connection_authenticate),
1211 g_signal_connect (conn, "reauthenticate",
1212 G_CALLBACK (connection_reauthenticate),
1215 g_hash_table_insert (priv->conns, conn, host);
1217 /* We increment the connection counts so it counts against the
1218 * totals, but we don't add it to the host's connection list
1219 * yet, since it's not ready for use.
1224 /* Mark the request as connecting, so we don't try to open
1225 * another new connection for it while waiting for this one.
1227 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1229 g_mutex_unlock (priv->host_lock);
1235 message_finished (SoupMessage *msg, gpointer user_data)
1237 SoupSession *session = user_data;
1239 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1240 soup_message_queue_remove_message (session->queue, msg);
1241 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1246 queue_message (SoupSession *session, SoupMessage *msg,
1247 SoupMessageCallbackFn callback, gpointer user_data)
1249 g_signal_connect_after (msg, "finished",
1250 G_CALLBACK (message_finished), session);
1252 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1253 soup_message_add_status_class_handler (
1254 msg, SOUP_STATUS_CLASS_REDIRECT,
1255 SOUP_HANDLER_POST_BODY,
1256 redirect_handler, session);
1259 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1260 soup_message_queue_append (session->queue, msg);
1264 * soup_session_queue_message:
1265 * @session: a #SoupSession
1266 * @msg: the message to queue
1267 * @callback: a #SoupMessageCallbackFn which will be called after the
1268 * message completes or when an unrecoverable error occurs.
1269 * @user_data: a pointer passed to @callback.
1271 * Queues the message @msg for sending. All messages are processed
1272 * while the glib main loop runs. If @msg has been processed before,
1273 * any resources related to the time it was last sent are freed.
1275 * Upon message completion, the callback specified in @callback will
1276 * be invoked. If after returning from this callback the message has
1277 * not been requeued, @msg will be unreffed.
1280 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1281 SoupMessageCallbackFn callback, gpointer user_data)
1283 g_return_if_fail (SOUP_IS_SESSION (session));
1284 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1286 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1287 callback, user_data);
1291 requeue_message (SoupSession *session, SoupMessage *msg)
1293 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1297 * soup_session_requeue_message:
1298 * @session: a #SoupSession
1299 * @msg: the message to requeue
1301 * This causes @msg to be placed back on the queue to be attempted
1305 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1307 g_return_if_fail (SOUP_IS_SESSION (session));
1308 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1310 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1315 * soup_session_send_message:
1316 * @session: a #SoupSession
1317 * @msg: the message to send
1319 * Synchronously send @msg. This call will not return until the
1320 * transfer is finished successfully or there is an unrecoverable
1323 * @msg is not freed upon return.
1325 * Return value: the HTTP status code of the response
1328 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1330 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1331 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1333 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1338 cancel_message (SoupSession *session, SoupMessage *msg)
1340 soup_message_queue_remove_message (session->queue, msg);
1341 soup_message_finished (msg);
1345 * soup_session_cancel_message:
1346 * @session: a #SoupSession
1347 * @msg: the message to cancel
1349 * Causes @session to immediately finish processing @msg. You should
1350 * set a status code on @msg with soup_message_set_status() before
1351 * calling this function.
1354 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1356 g_return_if_fail (SOUP_IS_SESSION (session));
1357 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1359 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1363 * soup_session_abort:
1364 * @session: the session
1366 * Cancels all pending requests in @session.
1369 soup_session_abort (SoupSession *session)
1371 SoupMessageQueueIter iter;
1374 g_return_if_fail (SOUP_IS_SESSION (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);