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);
185 if (priv->async_context)
186 g_main_context_unref (priv->async_context);
188 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
192 soup_session_class_init (SoupSessionClass *session_class)
194 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
196 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
198 /* virtual method definition */
199 session_class->queue_message = queue_message;
200 session_class->requeue_message = requeue_message;
201 session_class->cancel_message = cancel_message;
203 /* virtual method override */
204 object_class->dispose = dispose;
205 object_class->finalize = finalize;
206 object_class->set_property = set_property;
207 object_class->get_property = get_property;
212 * SoupSession::authenticate:
213 * @session: the session
214 * @msg: the #SoupMessage being sent
215 * @auth_type: the authentication type
216 * @auth_realm: the realm being authenticated to
217 * @username: the signal handler should set this to point to
218 * the provided username
219 * @password: the signal handler should set this to point to
220 * the provided password
222 * Emitted when the session requires authentication. The
223 * credentials may come from the user, or from cached
224 * information. If no credentials are available, leave
225 * @username and @password unchanged.
227 * If the provided credentials fail, the #reauthenticate
228 * signal will be emitted.
230 signals[AUTHENTICATE] =
231 g_signal_new ("authenticate",
232 G_OBJECT_CLASS_TYPE (object_class),
234 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
236 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
245 * SoupSession::reauthenticate:
246 * @session: the session
247 * @msg: the #SoupMessage being sent
248 * @auth_type: the authentication type
249 * @auth_realm: the realm being authenticated to
250 * @username: the signal handler should set this to point to
251 * the provided username
252 * @password: the signal handler should set this to point to
253 * the provided password
255 * Emitted when the credentials provided by the application to
256 * the #authenticate signal have failed. This gives the
257 * application a second chance to provide authentication
258 * credentials. If the new credentials also fail, #SoupSession
259 * will emit #reauthenticate again, and will continue doing so
260 * until the provided credentials work, or a #reauthenticate
261 * signal emission "fails" (because the handler left @username
262 * and @password unchanged). At that point, the 401 or 407
263 * error status will be returned to the caller.
265 * If your application only uses cached passwords, it should
266 * only connect to #authenticate, and not #reauthenticate.
268 * If your application always prompts the user for a password,
269 * and never uses cached information, then you can connect the
270 * same handler to #authenticate and #reauthenticate.
272 * To get standard web-browser behavior, return either cached
273 * information or a user-provided password (whichever is
274 * available) from the #authenticate handler, but return only
275 * user-provided information from the #reauthenticate handler.
277 signals[REAUTHENTICATE] =
278 g_signal_new ("reauthenticate",
279 G_OBJECT_CLASS_TYPE (object_class),
281 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
283 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
292 g_object_class_install_property (
293 object_class, PROP_PROXY_URI,
294 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
296 "The HTTP Proxy to use for this session",
298 g_object_class_install_property (
299 object_class, PROP_MAX_CONNS,
300 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
301 "Max Connection Count",
302 "The maximum number of connections that the session can open at once",
307 g_object_class_install_property (
308 object_class, PROP_MAX_CONNS_PER_HOST,
309 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
310 "Max Per-Host Connection Count",
311 "The maximum number of connections that the session can open at once to a given host",
316 g_object_class_install_property (
317 object_class, PROP_USE_NTLM,
318 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
320 "Whether or not to use NTLM authentication",
323 g_object_class_install_property (
324 object_class, PROP_SSL_CA_FILE,
325 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
327 "File containing SSL CA certificates",
330 g_object_class_install_property (
331 object_class, PROP_ASYNC_CONTEXT,
332 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
333 "Async GMainContext",
334 "The GMainContext to dispatch async I/O in",
335 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
336 g_object_class_install_property (
337 object_class, PROP_TIMEOUT,
338 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
340 "Value in seconds to timeout a blocking I/O",
346 filter_iface_init (SoupMessageFilterClass *filter_class)
348 /* interface implementation */
349 filter_class->setup_message = setup_message;
354 safe_uri_equal (const SoupUri *a, const SoupUri *b)
359 if ((a && !b) || (b && !a))
362 return soup_uri_equal (a, b);
366 safe_str_equal (const char *a, const char *b)
371 if ((a && !b) || (b && !a))
374 return strcmp (a, b) == 0;
378 set_property (GObject *object, guint prop_id,
379 const GValue *value, GParamSpec *pspec)
381 SoupSession *session = SOUP_SESSION (object);
382 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
384 gboolean need_abort = FALSE;
385 gboolean ca_file_changed = FALSE;
386 const char *new_ca_file;
390 pval = g_value_get_pointer (value);
392 if (!safe_uri_equal (priv->proxy_uri, pval))
396 soup_uri_free (priv->proxy_uri);
398 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
401 soup_session_abort (session);
402 cleanup_hosts (priv);
407 priv->max_conns = g_value_get_int (value);
409 case PROP_MAX_CONNS_PER_HOST:
410 priv->max_conns_per_host = g_value_get_int (value);
413 priv->use_ntlm = g_value_get_boolean (value);
415 case PROP_SSL_CA_FILE:
416 new_ca_file = g_value_get_string (value);
418 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
419 ca_file_changed = TRUE;
421 g_free (priv->ssl_ca_file);
422 priv->ssl_ca_file = g_strdup (new_ca_file);
424 if (ca_file_changed) {
425 if (priv->ssl_creds) {
426 soup_ssl_free_client_credentials (priv->ssl_creds);
427 priv->ssl_creds = NULL;
430 cleanup_hosts (priv);
434 case PROP_ASYNC_CONTEXT:
435 priv->async_context = g_value_get_pointer (value);
436 if (priv->async_context)
437 g_main_context_ref (priv->async_context);
440 priv->timeout = g_value_get_uint (value);
448 get_property (GObject *object, guint prop_id,
449 GValue *value, GParamSpec *pspec)
451 SoupSession *session = SOUP_SESSION (object);
452 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
456 g_value_set_pointer (value, priv->proxy_uri ?
457 soup_uri_copy (priv->proxy_uri) :
461 g_value_set_int (value, priv->max_conns);
463 case PROP_MAX_CONNS_PER_HOST:
464 g_value_set_int (value, priv->max_conns_per_host);
467 g_value_set_boolean (value, priv->use_ntlm);
469 case PROP_SSL_CA_FILE:
470 g_value_set_string (value, priv->ssl_ca_file);
472 case PROP_ASYNC_CONTEXT:
473 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
476 g_value_set_uint (value, priv->timeout);
485 * soup_session_add_filter:
486 * @session: a #SoupSession
487 * @filter: an object implementing the #SoupMessageFilter interface
489 * Adds @filter to @session's list of message filters to be applied to
493 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
495 SoupSessionPrivate *priv;
497 g_return_if_fail (SOUP_IS_SESSION (session));
498 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
499 priv = SOUP_SESSION_GET_PRIVATE (session);
501 g_object_ref (filter);
502 priv->filters = g_slist_prepend (priv->filters, filter);
506 * soup_session_remove_filter:
507 * @session: a #SoupSession
508 * @filter: an object implementing the #SoupMessageFilter interface
510 * Removes @filter from @session's list of message filters
513 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
515 SoupSessionPrivate *priv;
517 g_return_if_fail (SOUP_IS_SESSION (session));
518 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
519 priv = SOUP_SESSION_GET_PRIVATE (session);
521 priv->filters = g_slist_remove (priv->filters, filter);
522 g_object_unref (filter);
528 host_uri_hash (gconstpointer key)
530 const SoupUri *uri = key;
532 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
536 host_uri_equal (gconstpointer v1, gconstpointer v2)
538 const SoupUri *one = v1;
539 const SoupUri *two = v2;
541 if (one->protocol != two->protocol)
543 if (one->port != two->port)
546 return strcmp (one->host, two->host) == 0;
549 static SoupSessionHost *
550 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
552 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
553 SoupSessionHost *host;
555 host = g_new0 (SoupSessionHost, 1);
556 host->root_uri = soup_uri_copy_root (source_uri);
558 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
561 soup_ssl_get_client_credentials (priv->ssl_ca_file);
567 /* Note: get_host_for_message doesn't lock the host_lock. The caller
568 * must do it itself if there's a chance the host doesn't already
571 static SoupSessionHost *
572 get_host_for_message (SoupSession *session, SoupMessage *msg)
574 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
575 SoupSessionHost *host;
576 const SoupUri *source = soup_message_get_uri (msg);
578 host = g_hash_table_lookup (priv->hosts, source);
582 host = soup_session_host_new (session, source);
583 g_hash_table_insert (priv->hosts, host->root_uri, host);
588 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
589 * it itself if there's a chance the host doesn't already exist.
591 static SoupSessionHost *
592 get_proxy_host (SoupSession *session)
594 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
596 if (priv->proxy_host || !priv->proxy_uri)
597 return priv->proxy_host;
600 soup_session_host_new (session, priv->proxy_uri);
601 return priv->proxy_host;
605 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
608 g_free (scheme_realm);
612 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
614 g_free (scheme_realm);
615 g_object_unref (auth);
619 free_host (SoupSessionHost *host)
621 while (host->connections) {
622 SoupConnection *conn = host->connections->data;
624 host->connections = g_slist_remove (host->connections, conn);
625 soup_connection_disconnect (conn);
628 if (host->auth_realms) {
629 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
630 g_hash_table_destroy (host->auth_realms);
633 g_hash_table_foreach (host->auths, free_auth, NULL);
634 g_hash_table_destroy (host->auths);
637 soup_uri_free (host->root_uri);
644 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
646 SoupSessionHost *host;
648 const char *realm, *const_path;
651 host = get_proxy_host (session);
654 host = get_host_for_message (session, msg);
655 const_path = soup_message_get_uri (msg)->path;
660 g_return_val_if_fail (host != NULL, NULL);
662 if (!host->auth_realms)
665 path = g_strdup (const_path);
668 realm = g_hash_table_lookup (host->auth_realms, path);
672 dir = strrchr (path, '/');
679 return g_hash_table_lookup (host->auths, realm);
685 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
690 realm = g_strdup_printf ("%s:%s",
691 soup_auth_get_scheme_name (auth),
692 soup_auth_get_realm (auth));
694 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
695 auth == (SoupAuth *)value) {
696 g_hash_table_remove (host->auths, realm);
698 g_object_unref (auth);
704 authenticate_auth (SoupSession *session, SoupAuth *auth,
705 SoupMessage *msg, gboolean prior_auth_failed,
708 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
710 char *username = NULL, *password = NULL;
713 uri = priv->proxy_uri;
715 uri = soup_message_get_uri (msg);
717 if (uri->passwd && !prior_auth_failed) {
718 soup_auth_authenticate (auth, uri->user, uri->passwd);
722 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
723 msg, soup_auth_get_scheme_name (auth),
724 soup_auth_get_realm (auth),
725 &username, &password);
726 if (username || password)
727 soup_auth_authenticate (auth, username, password);
731 memset (password, 0, strlen (password));
735 return soup_auth_is_authenticated (auth);
739 update_auth_internal (SoupSession *session, SoupMessage *msg,
740 const GSList *headers, gboolean proxy,
741 gboolean got_unauthorized)
743 SoupSessionHost *host;
744 SoupAuth *new_auth, *prior_auth, *old_auth;
745 gpointer old_path, old_realm;
746 const SoupUri *msg_uri;
750 gboolean prior_auth_failed = FALSE;
753 host = get_proxy_host (session);
755 host = get_host_for_message (session, msg);
757 g_return_val_if_fail (host != NULL, FALSE);
759 /* Try to construct a new auth from the headers; if we can't,
760 * there's no way we'll be able to authenticate.
762 msg_uri = soup_message_get_uri (msg);
763 new_auth = soup_auth_new_from_header_list (headers);
767 /* See if this auth is the same auth we used last time */
768 prior_auth = lookup_auth (session, msg, proxy);
770 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
771 !strcmp (soup_auth_get_realm (prior_auth),
772 soup_auth_get_realm (new_auth))) {
773 if (!got_unauthorized) {
774 /* The user is just trying to preauthenticate
775 * using information we already have, so
776 * there's nothing more that needs to be done.
778 g_object_unref (new_auth);
782 /* The server didn't like the username/password we
783 * provided before. Invalidate it and note this fact.
785 invalidate_auth (host, prior_auth);
786 prior_auth_failed = TRUE;
789 if (!host->auth_realms) {
790 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
791 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
794 /* Record where this auth realm is used */
795 realm = g_strdup_printf ("%s:%s",
796 soup_auth_get_scheme_name (new_auth),
797 soup_auth_get_realm (new_auth));
800 * RFC 2617 is somewhat unclear about the scope of protection
801 * spaces with regard to proxies. The only mention of it is
802 * as an aside in section 3.2.1, where it is defining the fields
803 * of a Digest challenge and says that the protection space is
804 * always the entire proxy. Is this the case for all authentication
805 * schemes or just Digest? Who knows, but we're assuming all.
808 pspace = g_slist_prepend (NULL, g_strdup (""));
810 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
812 for (p = pspace; p; p = p->next) {
814 if (g_hash_table_lookup_extended (host->auth_realms, path,
815 &old_path, &old_realm)) {
816 g_hash_table_remove (host->auth_realms, old_path);
821 g_hash_table_insert (host->auth_realms,
822 g_strdup (path), g_strdup (realm));
824 soup_auth_free_protection_space (new_auth, pspace);
826 /* Now, make sure the auth is recorded. (If there's a
827 * pre-existing auth, we keep that rather than the new one,
828 * since the old one might already be authenticated.)
830 old_auth = g_hash_table_lookup (host->auths, realm);
833 g_object_unref (new_auth);
836 g_hash_table_insert (host->auths, realm, new_auth);
838 /* If we need to authenticate, try to do it. */
839 if (!soup_auth_is_authenticated (new_auth)) {
840 return authenticate_auth (session, new_auth,
841 msg, prior_auth_failed, proxy);
844 /* Otherwise we're good. */
849 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
850 const char *auth_type, const char *auth_realm,
851 char **username, char **password, gpointer session)
853 g_signal_emit (session, signals[AUTHENTICATE], 0,
854 msg, auth_type, auth_realm, username, password);
858 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
859 const char *auth_type, const char *auth_realm,
860 char **username, char **password,
863 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
864 msg, auth_type, auth_realm, username, password);
869 authorize_handler (SoupMessage *msg, gpointer user_data)
871 SoupSession *session = user_data;
872 const GSList *headers;
875 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
876 headers = soup_message_get_header_list (msg->response_headers,
877 "Proxy-Authenticate");
880 headers = soup_message_get_header_list (msg->response_headers,
887 if (update_auth_internal (session, msg, headers, proxy, TRUE))
888 soup_session_requeue_message (session, msg);
892 redirect_handler (SoupMessage *msg, gpointer user_data)
894 SoupSession *session = user_data;
898 new_loc = soup_message_get_header (msg->response_headers, "Location");
902 /* Location is supposed to be an absolute URI, but some sites
903 * are lame, so we use soup_uri_new_with_base().
905 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
907 soup_message_set_status_full (msg,
908 SOUP_STATUS_MALFORMED,
909 "Invalid Redirect URL");
913 soup_message_set_uri (msg, new_uri);
914 soup_uri_free (new_uri);
916 soup_session_requeue_message (session, msg);
920 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
922 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
926 auth = lookup_auth (session, msg, proxy);
929 if (!soup_auth_is_authenticated (auth) &&
930 !authenticate_auth (session, auth, msg, FALSE, proxy))
933 token = soup_auth_get_authorization (auth, msg);
935 soup_message_remove_header (msg->request_headers, header);
936 soup_message_add_header (msg->request_headers, header, token);
942 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
944 SoupSession *session = SOUP_SESSION (filter);
945 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
948 for (f = priv->filters; f; f = f->next) {
950 soup_message_filter_setup_message (filter, msg);
953 add_auth (session, msg, FALSE);
954 soup_message_add_status_code_handler (
955 msg, SOUP_STATUS_UNAUTHORIZED,
956 SOUP_HANDLER_POST_BODY,
957 authorize_handler, session);
959 if (priv->proxy_uri) {
960 add_auth (session, msg, TRUE);
961 soup_message_add_status_code_handler (
962 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
963 SOUP_HANDLER_POST_BODY,
964 authorize_handler, session);
969 find_oldest_connection (gpointer key, gpointer host, gpointer data)
971 SoupConnection *conn = key, **oldest = data;
973 /* Don't prune a connection that is currently in use, or
974 * hasn't been used yet.
976 if (soup_connection_is_in_use (conn) ||
977 soup_connection_last_used (conn) == 0)
980 if (!*oldest || (soup_connection_last_used (conn) <
981 soup_connection_last_used (*oldest)))
986 * soup_session_try_prune_connection:
987 * @session: a #SoupSession
989 * Finds the least-recently-used idle connection in @session and closes
992 * Return value: %TRUE if a connection was closed, %FALSE if there are
993 * no idle connections.
996 soup_session_try_prune_connection (SoupSession *session)
998 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
999 SoupConnection *oldest = NULL;
1001 g_mutex_lock (priv->host_lock);
1002 g_hash_table_foreach (priv->conns, find_oldest_connection,
1005 /* Ref the connection before unlocking the mutex in
1006 * case someone else tries to prune it too.
1008 g_object_ref (oldest);
1009 g_mutex_unlock (priv->host_lock);
1010 soup_connection_disconnect (oldest);
1011 g_object_unref (oldest);
1014 g_mutex_unlock (priv->host_lock);
1020 connection_disconnected (SoupConnection *conn, gpointer user_data)
1022 SoupSession *session = user_data;
1023 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1024 SoupSessionHost *host;
1026 g_mutex_lock (priv->host_lock);
1028 host = g_hash_table_lookup (priv->conns, conn);
1030 g_hash_table_remove (priv->conns, conn);
1031 host->connections = g_slist_remove (host->connections, conn);
1035 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
1038 g_mutex_unlock (priv->host_lock);
1039 g_object_unref (conn);
1043 connect_result (SoupConnection *conn, guint status, gpointer user_data)
1045 SoupSession *session = user_data;
1046 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1047 SoupSessionHost *host;
1048 SoupMessageQueueIter iter;
1051 g_mutex_lock (priv->host_lock);
1053 host = g_hash_table_lookup (priv->conns, conn);
1055 g_mutex_unlock (priv->host_lock);
1059 if (status == SOUP_STATUS_OK) {
1060 soup_connection_reserve (conn);
1061 host->connections = g_slist_prepend (host->connections, conn);
1062 g_mutex_unlock (priv->host_lock);
1066 /* The connection failed. */
1067 g_mutex_unlock (priv->host_lock);
1068 connection_disconnected (conn, session);
1070 if (host->connections) {
1071 /* Something went wrong this time, but we have at
1072 * least one open connection to this host. So just
1073 * leave the message in the queue so it can use that
1074 * connection once it's free.
1079 /* There are two possibilities: either status is
1080 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
1081 * will create a new connection (and all we need to do here
1082 * is downgrade the message from CONNECTING to QUEUED); or
1083 * status is something else, probably CANT_CONNECT or
1084 * CANT_RESOLVE or the like, in which case we need to cancel
1085 * any messages waiting for this host, since they're out
1088 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1089 if (get_host_for_message (session, msg) == host) {
1090 if (status == SOUP_STATUS_TRY_AGAIN) {
1091 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
1092 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1094 soup_message_set_status (msg, status);
1095 soup_session_cancel_message (session, msg);
1102 * soup_session_get_connection:
1103 * @session: a #SoupSession
1104 * @msg: a #SoupMessage
1105 * @try_pruning: on return, whether or not to try pruning a connection
1106 * @is_new: on return, %TRUE if the returned connection is new and not
1109 * Tries to find or create a connection for @msg; this is an internal
1110 * method for #SoupSession subclasses.
1112 * If there is an idle connection to the relevant host available, then
1113 * that connection will be returned (with *@is_new set to %FALSE). The
1114 * connection will be marked "reserved", so the caller must call
1115 * soup_connection_release() if it ends up not using the connection
1118 * If there is no idle connection available, but it is possible to
1119 * create a new connection, then one will be created and returned,
1120 * with *@is_new set to %TRUE. The caller MUST then call
1121 * soup_connection_connect_sync() or soup_connection_connect_async()
1122 * to connect it. If the connection attempt succeeds, the connection
1123 * will be marked "reserved" and added to @session's connection pool
1124 * once it connects. If the connection attempt fails, the connection
1127 * If no connection is available and a new connection cannot be made,
1128 * soup_session_get_connection() will return %NULL. If @session has
1129 * the maximum number of open connections open, but does not have the
1130 * maximum number of per-host connections open to the relevant host,
1131 * then *@try_pruning will be set to %TRUE. In this case, the caller
1132 * can call soup_session_try_prune_connection() to close an idle
1133 * connection, and then try soup_session_get_connection() again. (If
1134 * calling soup_session_try_prune_connection() wouldn't help, then
1135 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1137 * Return value: a #SoupConnection, or %NULL
1140 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1141 gboolean *try_pruning, gboolean *is_new)
1143 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1144 SoupConnection *conn;
1145 SoupSessionHost *host;
1148 g_mutex_lock (priv->host_lock);
1150 host = get_host_for_message (session, msg);
1151 for (conns = host->connections; conns; conns = conns->next) {
1152 if (!soup_connection_is_in_use (conns->data)) {
1153 soup_connection_reserve (conns->data);
1154 g_mutex_unlock (priv->host_lock);
1160 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1161 /* We already started a connection for this
1162 * message, so don't start another one.
1164 g_mutex_unlock (priv->host_lock);
1168 if (host->num_conns >= priv->max_conns_per_host) {
1169 g_mutex_unlock (priv->host_lock);
1173 if (priv->num_conns >= priv->max_conns) {
1174 *try_pruning = TRUE;
1175 g_mutex_unlock (priv->host_lock);
1179 /* Make sure priv->proxy_host gets set now while
1180 * we have the host_lock.
1182 if (priv->proxy_uri)
1183 get_proxy_host (session);
1185 conn = g_object_new (
1187 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1188 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1189 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1190 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1191 SOUP_CONNECTION_MESSAGE_FILTER, session,
1192 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1193 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1195 g_signal_connect (conn, "connect_result",
1196 G_CALLBACK (connect_result),
1198 g_signal_connect (conn, "disconnected",
1199 G_CALLBACK (connection_disconnected),
1201 g_signal_connect (conn, "authenticate",
1202 G_CALLBACK (connection_authenticate),
1204 g_signal_connect (conn, "reauthenticate",
1205 G_CALLBACK (connection_reauthenticate),
1208 g_hash_table_insert (priv->conns, conn, host);
1210 /* We increment the connection counts so it counts against the
1211 * totals, but we don't add it to the host's connection list
1212 * yet, since it's not ready for use.
1217 /* Mark the request as connecting, so we don't try to open
1218 * another new connection for it while waiting for this one.
1220 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1222 g_mutex_unlock (priv->host_lock);
1228 message_finished (SoupMessage *msg, gpointer user_data)
1230 SoupSession *session = user_data;
1232 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1233 soup_message_queue_remove_message (session->queue, msg);
1234 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1239 queue_message (SoupSession *session, SoupMessage *msg,
1240 SoupMessageCallbackFn callback, gpointer user_data)
1242 g_signal_connect_after (msg, "finished",
1243 G_CALLBACK (message_finished), session);
1245 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1246 soup_message_add_status_class_handler (
1247 msg, SOUP_STATUS_CLASS_REDIRECT,
1248 SOUP_HANDLER_POST_BODY,
1249 redirect_handler, session);
1252 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1253 soup_message_queue_append (session->queue, msg);
1257 * soup_session_queue_message:
1258 * @session: a #SoupSession
1259 * @msg: the message to queue
1260 * @callback: a #SoupMessageCallbackFn which will be called after the
1261 * message completes or when an unrecoverable error occurs.
1262 * @user_data: a pointer passed to @callback.
1264 * Queues the message @msg for sending. All messages are processed
1265 * while the glib main loop runs. If @msg has been processed before,
1266 * any resources related to the time it was last sent are freed.
1268 * Upon message completion, the callback specified in @callback will
1269 * be invoked. If after returning from this callback the message has
1270 * not been requeued, @msg will be unreffed.
1273 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1274 SoupMessageCallbackFn callback, gpointer user_data)
1276 g_return_if_fail (SOUP_IS_SESSION (session));
1277 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1279 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1280 callback, user_data);
1284 requeue_message (SoupSession *session, SoupMessage *msg)
1286 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1290 * soup_session_requeue_message:
1291 * @session: a #SoupSession
1292 * @msg: the message to requeue
1294 * This causes @msg to be placed back on the queue to be attempted
1298 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1300 g_return_if_fail (SOUP_IS_SESSION (session));
1301 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1303 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1308 * soup_session_send_message:
1309 * @session: a #SoupSession
1310 * @msg: the message to send
1312 * Synchronously send @msg. This call will not return until the
1313 * transfer is finished successfully or there is an unrecoverable
1316 * @msg is not freed upon return.
1318 * Return value: the HTTP status code of the response
1321 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1323 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1324 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1326 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1331 cancel_message (SoupSession *session, SoupMessage *msg)
1333 soup_message_queue_remove_message (session->queue, msg);
1334 soup_message_finished (msg);
1338 * soup_session_cancel_message:
1339 * @session: a #SoupSession
1340 * @msg: the message to cancel
1342 * Causes @session to immediately finish processing @msg. You should
1343 * set a status code on @msg with soup_message_set_status() before
1344 * calling this function.
1347 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1349 g_return_if_fail (SOUP_IS_SESSION (session));
1350 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1352 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1356 * soup_session_abort:
1357 * @session: the session
1359 * Cancels all pending requests in @session.
1362 soup_session_abort (SoupSession *session)
1364 SoupMessageQueueIter iter;
1367 g_return_if_fail (SOUP_IS_SESSION (session));
1369 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1370 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1371 soup_session_cancel_message (session, msg);