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 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
60 static guint host_uri_hash (gconstpointer key);
61 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
62 static void free_host (SoupSessionHost *host);
64 static void setup_message (SoupMessageFilter *filter, SoupMessage *msg);
66 static void queue_message (SoupSession *session, SoupMessage *msg,
67 SoupMessageCallbackFn callback,
69 static void requeue_message (SoupSession *session, SoupMessage *msg);
70 static void cancel_message (SoupSession *session, SoupMessage *msg);
72 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
73 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
75 static void filter_iface_init (SoupMessageFilterClass *filter_class);
77 G_DEFINE_TYPE_EXTENDED (SoupSession, soup_session, G_TYPE_OBJECT, 0,
78 G_IMPLEMENT_INTERFACE (SOUP_TYPE_MESSAGE_FILTER,
87 static guint signals[LAST_SIGNAL] = { 0 };
94 PROP_MAX_CONNS_PER_HOST,
101 static void set_property (GObject *object, guint prop_id,
102 const GValue *value, GParamSpec *pspec);
103 static void get_property (GObject *object, guint prop_id,
104 GValue *value, GParamSpec *pspec);
107 soup_session_init (SoupSession *session)
109 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
111 session->queue = soup_message_queue_new ();
113 priv->host_lock = g_mutex_new ();
114 priv->hosts = g_hash_table_new (host_uri_hash, host_uri_equal);
115 priv->conns = g_hash_table_new (NULL, NULL);
117 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
118 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
122 foreach_free_host (gpointer key, gpointer host, gpointer data)
129 cleanup_hosts (SoupSessionPrivate *priv)
131 g_mutex_lock (priv->host_lock);
132 g_hash_table_foreach_remove (priv->hosts, foreach_free_host, NULL);
133 g_mutex_unlock (priv->host_lock);
137 dispose (GObject *object)
139 SoupSession *session = SOUP_SESSION (object);
140 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
143 soup_session_abort (session);
144 cleanup_hosts (priv);
147 for (f = priv->filters; f; f = f->next)
148 g_object_unref (f->data);
149 g_slist_free (priv->filters);
150 priv->filters = NULL;
153 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
157 finalize (GObject *object)
159 SoupSession *session = SOUP_SESSION (object);
160 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
162 soup_message_queue_destroy (session->queue);
164 g_mutex_free (priv->host_lock);
165 g_hash_table_destroy (priv->hosts);
166 g_hash_table_destroy (priv->conns);
168 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
172 soup_session_class_init (SoupSessionClass *session_class)
174 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
176 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
178 /* virtual method definition */
179 session_class->queue_message = queue_message;
180 session_class->requeue_message = requeue_message;
181 session_class->cancel_message = cancel_message;
183 /* virtual method override */
184 object_class->dispose = dispose;
185 object_class->finalize = finalize;
186 object_class->set_property = set_property;
187 object_class->get_property = get_property;
190 signals[AUTHENTICATE] =
191 g_signal_new ("authenticate",
192 G_OBJECT_CLASS_TYPE (object_class),
194 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
196 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
203 signals[REAUTHENTICATE] =
204 g_signal_new ("reauthenticate",
205 G_OBJECT_CLASS_TYPE (object_class),
207 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
209 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
218 g_object_class_install_property (
219 object_class, PROP_PROXY_URI,
220 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
222 "The HTTP Proxy to use for this session",
224 g_object_class_install_property (
225 object_class, PROP_MAX_CONNS,
226 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
227 "Max Connection Count",
228 "The maximum number of connections that the session can open at once",
233 g_object_class_install_property (
234 object_class, PROP_MAX_CONNS_PER_HOST,
235 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
236 "Max Per-Host Connection Count",
237 "The maximum number of connections that the session can open at once to a given host",
242 g_object_class_install_property (
243 object_class, PROP_USE_NTLM,
244 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
246 "Whether or not to use NTLM authentication",
249 g_object_class_install_property (
250 object_class, PROP_SSL_CA_FILE,
251 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
253 "File containing SSL CA certificates",
259 filter_iface_init (SoupMessageFilterClass *filter_class)
261 /* interface implementation */
262 filter_class->setup_message = setup_message;
267 safe_uri_equal (const SoupUri *a, const SoupUri *b)
272 if ((a && !b) || (b && !a))
275 return soup_uri_equal (a, b);
279 safe_str_equal (const char *a, const char *b)
284 if ((a && !b) || (b && !a))
287 return strcmp (a, b) == 0;
291 set_property (GObject *object, guint prop_id,
292 const GValue *value, GParamSpec *pspec)
294 SoupSession *session = SOUP_SESSION (object);
295 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
297 gboolean need_abort = FALSE;
298 gboolean ca_file_changed = FALSE;
299 const char *new_ca_file;
303 pval = g_value_get_pointer (value);
305 if (!safe_uri_equal (priv->proxy_uri, pval))
309 soup_uri_free (priv->proxy_uri);
311 priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
314 soup_session_abort (session);
315 cleanup_hosts (priv);
320 priv->max_conns = g_value_get_int (value);
322 case PROP_MAX_CONNS_PER_HOST:
323 priv->max_conns_per_host = g_value_get_int (value);
326 priv->use_ntlm = g_value_get_boolean (value);
328 case PROP_SSL_CA_FILE:
329 new_ca_file = g_value_get_string (value);
331 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
332 ca_file_changed = TRUE;
334 g_free (priv->ssl_ca_file);
335 priv->ssl_ca_file = g_strdup (new_ca_file);
337 if (ca_file_changed) {
338 if (priv->ssl_creds) {
339 soup_ssl_free_client_credentials (priv->ssl_creds);
340 priv->ssl_creds = NULL;
343 cleanup_hosts (priv);
353 get_property (GObject *object, guint prop_id,
354 GValue *value, GParamSpec *pspec)
356 SoupSession *session = SOUP_SESSION (object);
357 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
361 g_value_set_pointer (value, priv->proxy_uri ?
362 soup_uri_copy (priv->proxy_uri) :
366 g_value_set_int (value, priv->max_conns);
368 case PROP_MAX_CONNS_PER_HOST:
369 g_value_set_int (value, priv->max_conns_per_host);
372 g_value_set_boolean (value, priv->use_ntlm);
374 case PROP_SSL_CA_FILE:
375 g_value_set_string (value, priv->ssl_ca_file);
384 * soup_session_add_filter:
385 * @session: a #SoupSession
386 * @filter: an object implementing the #SoupMessageFilter interface
388 * Adds @filter to @session's list of message filters to be applied to
392 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
394 SoupSessionPrivate *priv;
396 g_return_if_fail (SOUP_IS_SESSION (session));
397 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
398 priv = SOUP_SESSION_GET_PRIVATE (session);
400 g_object_ref (filter);
401 priv->filters = g_slist_prepend (priv->filters, filter);
405 * soup_session_remove_filter:
406 * @session: a #SoupSession
407 * @filter: an object implementing the #SoupMessageFilter interface
409 * Removes @filter from @session's list of message filters
412 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
414 SoupSessionPrivate *priv;
416 g_return_if_fail (SOUP_IS_SESSION (session));
417 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
418 priv = SOUP_SESSION_GET_PRIVATE (session);
420 priv->filters = g_slist_remove (priv->filters, filter);
421 g_object_unref (filter);
427 host_uri_hash (gconstpointer key)
429 const SoupUri *uri = key;
431 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
435 host_uri_equal (gconstpointer v1, gconstpointer v2)
437 const SoupUri *one = v1;
438 const SoupUri *two = v2;
440 if (one->protocol != two->protocol)
442 if (one->port != two->port)
445 return strcmp (one->host, two->host) == 0;
448 static SoupSessionHost *
449 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
451 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
452 SoupSessionHost *host;
454 host = g_new0 (SoupSessionHost, 1);
455 host->root_uri = soup_uri_copy_root (source_uri);
457 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
460 soup_ssl_get_client_credentials (priv->ssl_ca_file);
466 /* Note: get_host_for_message doesn't lock the host_lock. The caller
467 * must do it itself if there's a chance the host doesn't already
470 static SoupSessionHost *
471 get_host_for_message (SoupSession *session, SoupMessage *msg)
473 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
474 SoupSessionHost *host;
475 const SoupUri *source = soup_message_get_uri (msg);
477 host = g_hash_table_lookup (priv->hosts, source);
481 host = soup_session_host_new (session, source);
482 g_hash_table_insert (priv->hosts, host->root_uri, host);
487 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
488 * it itself if there's a chance the host doesn't already exist.
490 static SoupSessionHost *
491 get_proxy_host (SoupSession *session)
493 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
495 if (priv->proxy_host || !priv->proxy_uri)
496 return priv->proxy_host;
499 soup_session_host_new (session, priv->proxy_uri);
500 return priv->proxy_host;
504 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
507 g_free (scheme_realm);
511 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
513 g_free (scheme_realm);
514 g_object_unref (auth);
518 free_host (SoupSessionHost *host)
520 while (host->connections) {
521 SoupConnection *conn = host->connections->data;
523 host->connections = g_slist_remove (host->connections, conn);
524 soup_connection_disconnect (conn);
527 if (host->auth_realms) {
528 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
529 g_hash_table_destroy (host->auth_realms);
532 g_hash_table_foreach (host->auths, free_auth, NULL);
533 g_hash_table_destroy (host->auths);
536 soup_uri_free (host->root_uri);
543 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
545 SoupSessionHost *host;
547 const char *realm, *const_path;
550 host = get_proxy_host (session);
553 host = get_host_for_message (session, msg);
554 const_path = soup_message_get_uri (msg)->path;
559 g_return_val_if_fail (host != NULL, NULL);
561 if (!host->auth_realms)
564 path = g_strdup (const_path);
567 realm = g_hash_table_lookup (host->auth_realms, path);
571 dir = strrchr (path, '/');
578 return g_hash_table_lookup (host->auths, realm);
584 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
589 realm = g_strdup_printf ("%s:%s",
590 soup_auth_get_scheme_name (auth),
591 soup_auth_get_realm (auth));
593 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
594 auth == (SoupAuth *)value) {
595 g_hash_table_remove (host->auths, realm);
597 g_object_unref (auth);
603 authenticate_auth (SoupSession *session, SoupAuth *auth,
604 SoupMessage *msg, gboolean prior_auth_failed,
607 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
609 char *username = NULL, *password = NULL;
612 uri = priv->proxy_uri;
614 uri = soup_message_get_uri (msg);
616 if (uri->passwd && !prior_auth_failed) {
617 soup_auth_authenticate (auth, uri->user, uri->passwd);
621 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
622 msg, soup_auth_get_scheme_name (auth),
623 soup_auth_get_realm (auth),
624 &username, &password);
625 if (username || password)
626 soup_auth_authenticate (auth, username, password);
630 memset (password, 0, strlen (password));
634 return soup_auth_is_authenticated (auth);
638 update_auth_internal (SoupSession *session, SoupMessage *msg,
639 const GSList *headers, gboolean proxy,
640 gboolean got_unauthorized)
642 SoupSessionHost *host;
643 SoupAuth *new_auth, *prior_auth, *old_auth;
644 gpointer old_path, old_realm;
645 const SoupUri *msg_uri;
649 gboolean prior_auth_failed = FALSE;
652 host = get_proxy_host (session);
654 host = get_host_for_message (session, msg);
656 g_return_val_if_fail (host != NULL, FALSE);
658 /* Try to construct a new auth from the headers; if we can't,
659 * there's no way we'll be able to authenticate.
661 msg_uri = soup_message_get_uri (msg);
662 new_auth = soup_auth_new_from_header_list (headers);
666 /* See if this auth is the same auth we used last time */
667 prior_auth = lookup_auth (session, msg, proxy);
669 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
670 !strcmp (soup_auth_get_realm (prior_auth),
671 soup_auth_get_realm (new_auth))) {
672 if (!got_unauthorized) {
673 /* The user is just trying to preauthenticate
674 * using information we already have, so
675 * there's nothing more that needs to be done.
677 g_object_unref (new_auth);
681 /* The server didn't like the username/password we
682 * provided before. Invalidate it and note this fact.
684 invalidate_auth (host, prior_auth);
685 prior_auth_failed = TRUE;
688 if (!host->auth_realms) {
689 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
690 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
693 /* Record where this auth realm is used */
694 realm = g_strdup_printf ("%s:%s",
695 soup_auth_get_scheme_name (new_auth),
696 soup_auth_get_realm (new_auth));
699 * RFC 2617 is somewhat unclear about the scope of protection
700 * spaces with regard to proxies. The only mention of it is
701 * as an aside in section 3.2.1, where it is defining the fields
702 * of a Digest challenge and says that the protection space is
703 * always the entire proxy. Is this the case for all authentication
704 * schemes or just Digest? Who knows, but we're assuming all.
707 pspace = g_slist_prepend (NULL, g_strdup (""));
709 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
711 for (p = pspace; p; p = p->next) {
713 if (g_hash_table_lookup_extended (host->auth_realms, path,
714 &old_path, &old_realm)) {
715 g_hash_table_remove (host->auth_realms, old_path);
720 g_hash_table_insert (host->auth_realms,
721 g_strdup (path), g_strdup (realm));
723 soup_auth_free_protection_space (new_auth, pspace);
725 /* Now, make sure the auth is recorded. (If there's a
726 * pre-existing auth, we keep that rather than the new one,
727 * since the old one might already be authenticated.)
729 old_auth = g_hash_table_lookup (host->auths, realm);
732 g_object_unref (new_auth);
735 g_hash_table_insert (host->auths, realm, new_auth);
737 /* If we need to authenticate, try to do it. */
738 if (!soup_auth_is_authenticated (new_auth)) {
739 return authenticate_auth (session, new_auth,
740 msg, prior_auth_failed, proxy);
743 /* Otherwise we're good. */
748 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
749 const char *auth_type, const char *auth_realm,
750 char **username, char **password, gpointer session)
752 g_signal_emit (session, signals[AUTHENTICATE], 0,
753 msg, auth_type, auth_realm, username, password);
757 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
758 const char *auth_type, const char *auth_realm,
759 char **username, char **password,
762 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
763 msg, auth_type, auth_realm, username, password);
768 authorize_handler (SoupMessage *msg, gpointer user_data)
770 SoupSession *session = user_data;
771 const GSList *headers;
774 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
775 headers = soup_message_get_header_list (msg->response_headers,
776 "Proxy-Authenticate");
779 headers = soup_message_get_header_list (msg->response_headers,
786 if (update_auth_internal (session, msg, headers, proxy, TRUE))
787 soup_session_requeue_message (session, msg);
791 redirect_handler (SoupMessage *msg, gpointer user_data)
793 SoupSession *session = user_data;
797 new_loc = soup_message_get_header (msg->response_headers, "Location");
800 new_uri = soup_uri_new (new_loc);
802 soup_message_set_status_full (msg,
803 SOUP_STATUS_MALFORMED,
804 "Invalid Redirect URL");
808 soup_message_set_uri (msg, new_uri);
809 soup_uri_free (new_uri);
811 soup_session_requeue_message (session, msg);
815 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
817 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
821 auth = lookup_auth (session, msg, proxy);
824 if (!soup_auth_is_authenticated (auth) &&
825 !authenticate_auth (session, auth, msg, FALSE, proxy))
828 token = soup_auth_get_authorization (auth, msg);
830 soup_message_remove_header (msg->request_headers, header);
831 soup_message_add_header (msg->request_headers, header, token);
837 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
839 SoupSession *session = SOUP_SESSION (filter);
840 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
843 for (f = priv->filters; f; f = f->next) {
845 soup_message_filter_setup_message (filter, msg);
848 add_auth (session, msg, FALSE);
849 soup_message_add_status_code_handler (
850 msg, SOUP_STATUS_UNAUTHORIZED,
851 SOUP_HANDLER_POST_BODY,
852 authorize_handler, session);
854 if (priv->proxy_uri) {
855 add_auth (session, msg, TRUE);
856 soup_message_add_status_code_handler (
857 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
858 SOUP_HANDLER_POST_BODY,
859 authorize_handler, session);
864 find_oldest_connection (gpointer key, gpointer host, gpointer data)
866 SoupConnection *conn = key, **oldest = data;
868 /* Don't prune a connection that is currently in use, or
869 * hasn't been used yet.
871 if (soup_connection_is_in_use (conn) ||
872 soup_connection_last_used (conn) == 0)
875 if (!*oldest || (soup_connection_last_used (conn) <
876 soup_connection_last_used (*oldest)))
881 * soup_session_try_prune_connection:
882 * @session: a #SoupSession
884 * Finds the least-recently-used idle connection in @session and closes
887 * Return value: %TRUE if a connection was closed, %FALSE if there are
888 * no idle connections.
891 soup_session_try_prune_connection (SoupSession *session)
893 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
894 SoupConnection *oldest = NULL;
896 g_mutex_lock (priv->host_lock);
897 g_hash_table_foreach (priv->conns, find_oldest_connection,
900 /* Ref the connection before unlocking the mutex in
901 * case someone else tries to prune it too.
903 g_object_ref (oldest);
904 g_mutex_unlock (priv->host_lock);
905 soup_connection_disconnect (oldest);
906 g_object_unref (oldest);
909 g_mutex_unlock (priv->host_lock);
915 connection_disconnected (SoupConnection *conn, gpointer user_data)
917 SoupSession *session = user_data;
918 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
919 SoupSessionHost *host;
921 g_mutex_lock (priv->host_lock);
923 host = g_hash_table_lookup (priv->conns, conn);
925 g_hash_table_remove (priv->conns, conn);
926 host->connections = g_slist_remove (host->connections, conn);
930 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
933 g_mutex_unlock (priv->host_lock);
934 g_object_unref (conn);
938 connect_result (SoupConnection *conn, guint status, gpointer user_data)
940 SoupSession *session = user_data;
941 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
942 SoupSessionHost *host;
943 SoupMessageQueueIter iter;
946 g_mutex_lock (priv->host_lock);
948 host = g_hash_table_lookup (priv->conns, conn);
950 g_mutex_unlock (priv->host_lock);
954 if (status == SOUP_STATUS_OK) {
955 soup_connection_reserve (conn);
956 host->connections = g_slist_prepend (host->connections, conn);
957 g_mutex_unlock (priv->host_lock);
961 /* The connection failed. */
962 g_mutex_unlock (priv->host_lock);
963 connection_disconnected (conn, session);
965 if (host->connections) {
966 /* Something went wrong this time, but we have at
967 * least one open connection to this host. So just
968 * leave the message in the queue so it can use that
969 * connection once it's free.
974 /* There are two possibilities: either status is
975 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
976 * will create a new connection (and all we need to do here
977 * is downgrade the message from CONNECTING to QUEUED); or
978 * status is something else, probably CANT_CONNECT or
979 * CANT_RESOLVE or the like, in which case we need to cancel
980 * any messages waiting for this host, since they're out
983 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
984 if (get_host_for_message (session, msg) == host) {
985 if (status == SOUP_STATUS_TRY_AGAIN) {
986 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
987 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
989 soup_message_set_status (msg, status);
990 soup_session_cancel_message (session, msg);
997 * soup_session_get_connection:
998 * @session: a #SoupSession
999 * @msg: a #SoupMessage
1000 * @try_pruning: on return, whether or not to try pruning a connection
1001 * @is_new: on return, %TRUE if the returned connection is new and not
1004 * Tries to find or create a connection for @msg.
1006 * If there is an idle connection to the relevant host available, then
1007 * that connection will be returned (with *@is_new set to %FALSE). The
1008 * connection will be marked "reserved", so the caller must call
1009 * soup_connection_release() if it ends up not using the connection
1012 * If there is no idle connection available, but it is possible to
1013 * create a new connection, then one will be created and returned,
1014 * with *@is_new set to %TRUE. The caller MUST then call
1015 * soup_connection_connect_sync() or soup_connection_connect_async()
1016 * to connect it. If the connection attempt succeeds, the connection
1017 * will be marked "reserved" and added to @session's connection pool
1018 * once it connects. If the connection attempt fails, the connection
1021 * If no connection is available and a new connection cannot be made,
1022 * soup_session_get_connection() will return %NULL. If @session has
1023 * the maximum number of open connections open, but does not have the
1024 * maximum number of per-host connections open to the relevant host,
1025 * then *@try_pruning will be set to %TRUE. In this case, the caller
1026 * can call soup_session_try_prune_connection() to close an idle
1027 * connection, and then try soup_session_get_connection() again. (If
1028 * calling soup_session_try_prune_connection() wouldn't help, then
1029 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1031 * Return value: a #SoupConnection, or %NULL
1034 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1035 gboolean *try_pruning, gboolean *is_new)
1037 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1038 SoupConnection *conn;
1039 SoupSessionHost *host;
1042 g_mutex_lock (priv->host_lock);
1044 host = get_host_for_message (session, msg);
1045 for (conns = host->connections; conns; conns = conns->next) {
1046 if (!soup_connection_is_in_use (conns->data)) {
1047 soup_connection_reserve (conns->data);
1048 g_mutex_unlock (priv->host_lock);
1054 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1055 /* We already started a connection for this
1056 * message, so don't start another one.
1058 g_mutex_unlock (priv->host_lock);
1062 if (host->num_conns >= priv->max_conns_per_host) {
1063 g_mutex_unlock (priv->host_lock);
1067 if (priv->num_conns >= priv->max_conns) {
1068 *try_pruning = TRUE;
1069 g_mutex_unlock (priv->host_lock);
1073 /* Make sure priv->proxy_host gets set now while
1074 * we have the host_lock.
1076 if (priv->proxy_uri)
1077 get_proxy_host (session);
1079 conn = g_object_new (
1081 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1082 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1083 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1084 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1085 SOUP_CONNECTION_MESSAGE_FILTER, session,
1087 g_signal_connect (conn, "connect_result",
1088 G_CALLBACK (connect_result),
1090 g_signal_connect (conn, "disconnected",
1091 G_CALLBACK (connection_disconnected),
1093 g_signal_connect (conn, "authenticate",
1094 G_CALLBACK (connection_authenticate),
1096 g_signal_connect (conn, "reauthenticate",
1097 G_CALLBACK (connection_reauthenticate),
1100 g_hash_table_insert (priv->conns, conn, host);
1102 /* We increment the connection counts so it counts against the
1103 * totals, but we don't add it to the host's connection list
1104 * yet, since it's not ready for use.
1109 /* Mark the request as connecting, so we don't try to open
1110 * another new connection for it while waiting for this one.
1112 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1114 g_mutex_unlock (priv->host_lock);
1120 message_finished (SoupMessage *msg, gpointer user_data)
1122 SoupSession *session = user_data;
1124 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1125 soup_message_queue_remove_message (session->queue, msg);
1126 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1131 queue_message (SoupSession *session, SoupMessage *msg,
1132 SoupMessageCallbackFn callback, gpointer user_data)
1134 g_signal_connect_after (msg, "finished",
1135 G_CALLBACK (message_finished), session);
1137 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1138 soup_message_add_status_class_handler (
1139 msg, SOUP_STATUS_CLASS_REDIRECT,
1140 SOUP_HANDLER_POST_BODY,
1141 redirect_handler, session);
1144 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1145 soup_message_queue_append (session->queue, msg);
1149 * soup_session_queue_message:
1150 * @session: a #SoupSession
1151 * @msg: the message to queue
1152 * @callback: a #SoupMessageCallbackFn which will be called after the
1153 * message completes or when an unrecoverable error occurs.
1154 * @user_data: a pointer passed to @callback.
1156 * Queues the message @msg for sending. All messages are processed
1157 * while the glib main loop runs. If @msg has been processed before,
1158 * any resources related to the time it was last sent are freed.
1160 * Upon message completion, the callback specified in @callback will
1161 * be invoked. If after returning from this callback the message has
1162 * not been requeued, @msg will be unreffed.
1165 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1166 SoupMessageCallbackFn callback, gpointer user_data)
1168 g_return_if_fail (SOUP_IS_SESSION (session));
1169 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1171 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1172 callback, user_data);
1176 requeue_message (SoupSession *session, SoupMessage *msg)
1178 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1182 * soup_session_requeue_message:
1183 * @session: a #SoupSession
1184 * @msg: the message to requeue
1186 * This causes @msg to be placed back on the queue to be attempted
1190 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1192 g_return_if_fail (SOUP_IS_SESSION (session));
1193 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1195 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1200 * soup_session_send_message:
1201 * @session: a #SoupSession
1202 * @msg: the message to send
1204 * Synchronously send @msg. This call will not return until the
1205 * transfer is finished successfully or there is an unrecoverable
1208 * @msg is not freed upon return.
1210 * Return value: the HTTP status code of the response
1213 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1215 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1216 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1218 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1223 cancel_message (SoupSession *session, SoupMessage *msg)
1225 soup_message_queue_remove_message (session->queue, msg);
1226 soup_message_finished (msg);
1230 * soup_session_cancel_message:
1231 * @session: a #SoupSession
1232 * @msg: the message to cancel
1234 * Causes @session to immediately finish processing @msg. You should
1235 * set a status code on @msg with soup_message_set_status() before
1236 * calling this function.
1239 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1241 g_return_if_fail (SOUP_IS_SESSION (session));
1242 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1244 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1248 * soup_session_abort:
1249 * @session: the session
1251 * Cancels all pending requests in @session.
1254 soup_session_abort (SoupSession *session)
1256 SoupMessageQueueIter iter;
1259 g_return_if_fail (SOUP_IS_SESSION (session));
1261 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1262 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1263 soup_session_cancel_message (session, msg);