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 */
36 struct SoupSessionPrivate {
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.
59 static guint host_uri_hash (gconstpointer key);
60 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
61 static void free_host (SoupSessionHost *host, SoupSession *session);
63 static void setup_message (SoupMessageFilter *filter, SoupMessage *msg);
65 static void queue_message (SoupSession *session, SoupMessage *msg,
66 SoupMessageCallbackFn callback,
68 static void requeue_message (SoupSession *session, SoupMessage *msg);
69 static void cancel_message (SoupSession *session, SoupMessage *msg);
71 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
72 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
74 #define PARENT_TYPE G_TYPE_OBJECT
75 static GObjectClass *parent_class;
83 static guint signals[LAST_SIGNAL] = { 0 };
90 PROP_MAX_CONNS_PER_HOST,
97 static void set_property (GObject *object, guint prop_id,
98 const GValue *value, GParamSpec *pspec);
99 static void get_property (GObject *object, guint prop_id,
100 GValue *value, GParamSpec *pspec);
103 init (GObject *object)
105 SoupSession *session = SOUP_SESSION (object);
107 session->priv = g_new0 (SoupSessionPrivate, 1);
108 session->priv->host_lock = g_mutex_new ();
109 session->queue = soup_message_queue_new ();
110 session->priv->hosts = g_hash_table_new (host_uri_hash,
112 session->priv->conns = g_hash_table_new (NULL, NULL);
114 session->priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
115 session->priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
119 foreach_free_host (gpointer key, gpointer host, gpointer session)
121 free_host (host, session);
126 cleanup_hosts (SoupSession *session)
128 g_hash_table_foreach_remove (session->priv->hosts,
129 foreach_free_host, session);
133 dispose (GObject *object)
135 SoupSession *session = SOUP_SESSION (object);
138 soup_session_abort (session);
139 cleanup_hosts (session);
141 if (session->priv->filters) {
142 for (f = session->priv->filters; f; f = f->next)
143 g_object_unref (f->data);
144 g_slist_free (session->priv->filters);
145 session->priv->filters = NULL;
148 G_OBJECT_CLASS (parent_class)->dispose (object);
152 finalize (GObject *object)
154 SoupSession *session = SOUP_SESSION (object);
156 g_mutex_free (session->priv->host_lock);
157 soup_message_queue_destroy (session->queue);
158 g_hash_table_destroy (session->priv->hosts);
159 g_hash_table_destroy (session->priv->conns);
160 g_free (session->priv);
162 G_OBJECT_CLASS (parent_class)->finalize (object);
166 class_init (GObjectClass *object_class)
168 SoupSessionClass *session_class = SOUP_SESSION_CLASS (object_class);
170 parent_class = g_type_class_ref (PARENT_TYPE);
172 /* virtual method definition */
173 session_class->queue_message = queue_message;
174 session_class->requeue_message = requeue_message;
175 session_class->cancel_message = cancel_message;
177 /* virtual method override */
178 object_class->dispose = dispose;
179 object_class->finalize = finalize;
180 object_class->set_property = set_property;
181 object_class->get_property = get_property;
184 signals[AUTHENTICATE] =
185 g_signal_new ("authenticate",
186 G_OBJECT_CLASS_TYPE (object_class),
188 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
190 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
197 signals[REAUTHENTICATE] =
198 g_signal_new ("reauthenticate",
199 G_OBJECT_CLASS_TYPE (object_class),
201 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
203 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
212 g_object_class_install_property (
213 object_class, PROP_PROXY_URI,
214 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
216 "The HTTP Proxy to use for this session",
218 g_object_class_install_property (
219 object_class, PROP_MAX_CONNS,
220 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
221 "Max Connection Count",
222 "The maximum number of connections that the session can open at once",
227 g_object_class_install_property (
228 object_class, PROP_MAX_CONNS_PER_HOST,
229 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
230 "Max Per-Host Connection Count",
231 "The maximum number of connections that the session can open at once to a given host",
236 g_object_class_install_property (
237 object_class, PROP_USE_NTLM,
238 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
240 "Whether or not to use NTLM authentication",
243 g_object_class_install_property (
244 object_class, PROP_SSL_CA_FILE,
245 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
247 "File containing SSL CA certificates",
253 filter_iface_init (SoupMessageFilterClass *filter_class)
255 /* interface implementation */
256 filter_class->setup_message = setup_message;
259 SOUP_MAKE_TYPE_WITH_IFACE (soup_session, SoupSession, class_init, init, PARENT_TYPE, filter_iface_init, SOUP_TYPE_MESSAGE_FILTER)
262 safe_uri_equal (const SoupUri *a, const SoupUri *b)
267 if ((a && !b) || (b && !a))
270 return soup_uri_equal (a, b);
274 safe_str_equal (const char *a, const char *b)
279 if ((a && !b) || (b && !a))
282 return strcmp (a, b) == 0;
286 set_property (GObject *object, guint prop_id,
287 const GValue *value, GParamSpec *pspec)
289 SoupSession *session = SOUP_SESSION (object);
291 gboolean need_abort = FALSE;
292 gboolean ca_file_changed = FALSE;
293 const char *new_ca_file;
297 pval = g_value_get_pointer (value);
299 if (!safe_uri_equal (session->priv->proxy_uri, pval))
302 if (session->priv->proxy_uri)
303 soup_uri_free (session->priv->proxy_uri);
305 session->priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
308 soup_session_abort (session);
309 cleanup_hosts (session);
314 session->priv->max_conns = g_value_get_int (value);
316 case PROP_MAX_CONNS_PER_HOST:
317 session->priv->max_conns_per_host = g_value_get_int (value);
320 session->priv->use_ntlm = g_value_get_boolean (value);
322 case PROP_SSL_CA_FILE:
323 new_ca_file = g_value_get_string (value);
325 if (!safe_str_equal (session->priv->ssl_ca_file, new_ca_file))
326 ca_file_changed = TRUE;
328 g_free (session->priv->ssl_ca_file);
329 session->priv->ssl_ca_file = g_strdup (new_ca_file);
331 if (ca_file_changed) {
332 if (session->priv->ssl_creds) {
333 soup_ssl_free_client_credentials (session->priv->ssl_creds);
334 session->priv->ssl_creds = NULL;
337 cleanup_hosts (session);
347 get_property (GObject *object, guint prop_id,
348 GValue *value, GParamSpec *pspec)
350 SoupSession *session = SOUP_SESSION (object);
354 g_value_set_pointer (value, session->priv->proxy_uri ?
355 soup_uri_copy (session->priv->proxy_uri) :
359 g_value_set_int (value, session->priv->max_conns);
361 case PROP_MAX_CONNS_PER_HOST:
362 g_value_set_int (value, session->priv->max_conns_per_host);
365 g_value_set_boolean (value, session->priv->use_ntlm);
367 case PROP_SSL_CA_FILE:
368 g_value_set_string (value, session->priv->ssl_ca_file);
377 * soup_session_add_filter:
378 * @session: a #SoupSession
379 * @filter: an object implementing the #SoupMessageFilter interface
381 * Adds @filter to @session's list of message filters to be applied to
385 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
387 g_return_if_fail (SOUP_IS_SESSION (session));
388 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
390 g_object_ref (filter);
391 session->priv->filters = g_slist_prepend (session->priv->filters,
396 * soup_session_remove_filter:
397 * @session: a #SoupSession
398 * @filter: an object implementing the #SoupMessageFilter interface
400 * Removes @filter from @session's list of message filters
403 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
405 g_return_if_fail (SOUP_IS_SESSION (session));
406 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
408 session->priv->filters = g_slist_remove (session->priv->filters,
410 g_object_unref (filter);
416 host_uri_hash (gconstpointer key)
418 const SoupUri *uri = key;
420 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
424 host_uri_equal (gconstpointer v1, gconstpointer v2)
426 const SoupUri *one = v1;
427 const SoupUri *two = v2;
429 if (one->protocol != two->protocol)
431 if (one->port != two->port)
434 return strcmp (one->host, two->host) == 0;
437 static SoupSessionHost *
438 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
440 SoupSessionHost *host;
442 host = g_new0 (SoupSessionHost, 1);
443 host->root_uri = soup_uri_copy_root (source_uri);
445 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
446 !session->priv->ssl_creds) {
447 session->priv->ssl_creds =
448 soup_ssl_get_client_credentials (session->priv->ssl_ca_file);
454 /* Note: get_host_for_message doesn't lock the host_lock. The caller
455 * must do it itself if there's a chance the host doesn't already
458 static SoupSessionHost *
459 get_host_for_message (SoupSession *session, SoupMessage *msg)
461 SoupSessionHost *host;
462 const SoupUri *source = soup_message_get_uri (msg);
464 host = g_hash_table_lookup (session->priv->hosts, source);
468 host = soup_session_host_new (session, source);
469 g_hash_table_insert (session->priv->hosts, host->root_uri, host);
474 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
475 * it itself if there's a chance the host doesn't already exist.
477 static SoupSessionHost *
478 get_proxy_host (SoupSession *session)
480 if (session->priv->proxy_host || !session->priv->proxy_uri)
481 return session->priv->proxy_host;
483 session->priv->proxy_host =
484 soup_session_host_new (session, session->priv->proxy_uri);
485 return session->priv->proxy_host;
489 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
492 g_free (scheme_realm);
496 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
498 g_free (scheme_realm);
499 g_object_unref (auth);
503 free_host (SoupSessionHost *host, SoupSession *session)
505 while (host->connections) {
506 SoupConnection *conn = host->connections->data;
508 host->connections = g_slist_remove (host->connections, conn);
509 soup_connection_disconnect (conn);
512 if (host->auth_realms) {
513 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
514 g_hash_table_destroy (host->auth_realms);
517 g_hash_table_foreach (host->auths, free_auth, NULL);
518 g_hash_table_destroy (host->auths);
521 soup_uri_free (host->root_uri);
528 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
530 SoupSessionHost *host;
532 const char *realm, *const_path;
535 host = get_proxy_host (session);
538 host = get_host_for_message (session, msg);
539 const_path = soup_message_get_uri (msg)->path;
544 g_return_val_if_fail (host != NULL, NULL);
546 if (!host->auth_realms)
549 path = g_strdup (const_path);
552 realm = g_hash_table_lookup (host->auth_realms, path);
556 dir = strrchr (path, '/');
563 return g_hash_table_lookup (host->auths, realm);
569 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
574 realm = g_strdup_printf ("%s:%s",
575 soup_auth_get_scheme_name (auth),
576 soup_auth_get_realm (auth));
578 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
579 auth == (SoupAuth *)value) {
580 g_hash_table_remove (host->auths, realm);
582 g_object_unref (auth);
588 authenticate_auth (SoupSession *session, SoupAuth *auth,
589 SoupMessage *msg, gboolean prior_auth_failed,
593 char *username = NULL, *password = NULL;
596 uri = session->priv->proxy_uri;
598 uri = soup_message_get_uri (msg);
600 if (uri->passwd && !prior_auth_failed) {
601 soup_auth_authenticate (auth, uri->user, uri->passwd);
605 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
606 msg, soup_auth_get_scheme_name (auth),
607 soup_auth_get_realm (auth),
608 &username, &password);
609 if (username || password)
610 soup_auth_authenticate (auth, username, password);
614 memset (password, 0, strlen (password));
618 return soup_auth_is_authenticated (auth);
622 update_auth_internal (SoupSession *session, SoupMessage *msg,
623 const GSList *headers, gboolean proxy,
624 gboolean got_unauthorized)
626 SoupSessionHost *host;
627 SoupAuth *new_auth, *prior_auth, *old_auth;
628 gpointer old_path, old_realm;
629 const SoupUri *msg_uri;
633 gboolean prior_auth_failed = FALSE;
636 host = get_proxy_host (session);
638 host = get_host_for_message (session, msg);
640 g_return_val_if_fail (host != NULL, FALSE);
642 /* Try to construct a new auth from the headers; if we can't,
643 * there's no way we'll be able to authenticate.
645 msg_uri = soup_message_get_uri (msg);
646 new_auth = soup_auth_new_from_header_list (headers);
650 /* See if this auth is the same auth we used last time */
651 prior_auth = lookup_auth (session, msg, proxy);
653 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
654 !strcmp (soup_auth_get_realm (prior_auth),
655 soup_auth_get_realm (new_auth))) {
656 if (!got_unauthorized) {
657 /* The user is just trying to preauthenticate
658 * using information we already have, so
659 * there's nothing more that needs to be done.
661 g_object_unref (new_auth);
665 /* The server didn't like the username/password we
666 * provided before. Invalidate it and note this fact.
668 invalidate_auth (host, prior_auth);
669 prior_auth_failed = TRUE;
672 if (!host->auth_realms) {
673 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
674 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
677 /* Record where this auth realm is used */
678 realm = g_strdup_printf ("%s:%s",
679 soup_auth_get_scheme_name (new_auth),
680 soup_auth_get_realm (new_auth));
683 * RFC 2617 is somewhat unclear about the scope of protection
684 * spaces with regard to proxies. The only mention of it is
685 * as an aside in section 3.2.1, where it is defining the fields
686 * of a Digest challenge and says that the protection space is
687 * always the entire proxy. Is this the case for all authentication
688 * schemes or just Digest? Who knows, but we're assuming all.
691 pspace = g_slist_prepend (NULL, g_strdup (""));
693 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
695 for (p = pspace; p; p = p->next) {
697 if (g_hash_table_lookup_extended (host->auth_realms, path,
698 &old_path, &old_realm)) {
699 g_hash_table_remove (host->auth_realms, old_path);
704 g_hash_table_insert (host->auth_realms,
705 g_strdup (path), g_strdup (realm));
707 soup_auth_free_protection_space (new_auth, pspace);
709 /* Now, make sure the auth is recorded. (If there's a
710 * pre-existing auth, we keep that rather than the new one,
711 * since the old one might already be authenticated.)
713 old_auth = g_hash_table_lookup (host->auths, realm);
716 g_object_unref (new_auth);
719 g_hash_table_insert (host->auths, realm, new_auth);
721 /* If we need to authenticate, try to do it. */
722 if (!soup_auth_is_authenticated (new_auth)) {
723 return authenticate_auth (session, new_auth,
724 msg, prior_auth_failed, proxy);
727 /* Otherwise we're good. */
732 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
733 const char *auth_type, const char *auth_realm,
734 char **username, char **password, gpointer session)
736 g_signal_emit (session, signals[AUTHENTICATE], 0,
737 msg, auth_type, auth_realm, username, password);
741 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
742 const char *auth_type, const char *auth_realm,
743 char **username, char **password,
746 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
747 msg, auth_type, auth_realm, username, password);
752 authorize_handler (SoupMessage *msg, gpointer user_data)
754 SoupSession *session = user_data;
755 const GSList *headers;
758 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
759 headers = soup_message_get_header_list (msg->response_headers,
760 "Proxy-Authenticate");
763 headers = soup_message_get_header_list (msg->response_headers,
770 if (update_auth_internal (session, msg, headers, proxy, TRUE))
771 soup_session_requeue_message (session, msg);
775 redirect_handler (SoupMessage *msg, gpointer user_data)
777 SoupSession *session = user_data;
781 new_loc = soup_message_get_header (msg->response_headers, "Location");
784 new_uri = soup_uri_new (new_loc);
786 soup_message_set_status_full (msg,
787 SOUP_STATUS_MALFORMED,
788 "Invalid Redirect URL");
792 soup_message_set_uri (msg, new_uri);
793 soup_uri_free (new_uri);
795 soup_session_requeue_message (session, msg);
799 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
801 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
805 auth = lookup_auth (session, msg, proxy);
808 if (!soup_auth_is_authenticated (auth) &&
809 !authenticate_auth (session, auth, msg, FALSE, proxy))
812 token = soup_auth_get_authorization (auth, msg);
814 soup_message_remove_header (msg->request_headers, header);
815 soup_message_add_header (msg->request_headers, header, token);
821 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
823 SoupSession *session = SOUP_SESSION (filter);
826 for (f = session->priv->filters; f; f = f->next) {
828 soup_message_filter_setup_message (filter, msg);
831 add_auth (session, msg, FALSE);
832 soup_message_add_status_code_handler (
833 msg, SOUP_STATUS_UNAUTHORIZED,
834 SOUP_HANDLER_POST_BODY,
835 authorize_handler, session);
837 if (session->priv->proxy_uri) {
838 add_auth (session, msg, TRUE);
839 soup_message_add_status_code_handler (
840 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
841 SOUP_HANDLER_POST_BODY,
842 authorize_handler, session);
847 find_oldest_connection (gpointer key, gpointer host, gpointer data)
849 SoupConnection *conn = key, **oldest = data;
851 /* Don't prune a connection that is currently in use, or
852 * hasn't been used yet.
854 if (soup_connection_is_in_use (conn) ||
855 soup_connection_last_used (conn) == 0)
858 if (!*oldest || (soup_connection_last_used (conn) <
859 soup_connection_last_used (*oldest)))
864 * soup_session_try_prune_connection:
865 * @session: a #SoupSession
867 * Finds the least-recently-used idle connection in @session and closes
870 * Return value: %TRUE if a connection was closed, %FALSE if there are
871 * no idle connections.
874 soup_session_try_prune_connection (SoupSession *session)
876 SoupConnection *oldest = NULL;
878 g_mutex_lock (session->priv->host_lock);
879 g_hash_table_foreach (session->priv->conns, find_oldest_connection,
882 /* Ref the connection before unlocking the mutex in
883 * case someone else tries to prune it too.
885 g_object_ref (oldest);
886 g_mutex_unlock (session->priv->host_lock);
887 soup_connection_disconnect (oldest);
888 g_object_unref (oldest);
891 g_mutex_unlock (session->priv->host_lock);
897 connection_disconnected (SoupConnection *conn, gpointer user_data)
899 SoupSession *session = user_data;
900 SoupSessionHost *host;
902 g_mutex_lock (session->priv->host_lock);
904 host = g_hash_table_lookup (session->priv->conns, conn);
906 g_hash_table_remove (session->priv->conns, conn);
907 host->connections = g_slist_remove (host->connections, conn);
911 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
912 session->priv->num_conns--;
914 g_mutex_unlock (session->priv->host_lock);
915 g_object_unref (conn);
919 connect_result (SoupConnection *conn, guint status, gpointer user_data)
921 SoupSession *session = user_data;
922 SoupSessionHost *host;
923 SoupMessageQueueIter iter;
926 g_mutex_lock (session->priv->host_lock);
928 host = g_hash_table_lookup (session->priv->conns, conn);
930 g_mutex_unlock (session->priv->host_lock);
934 if (status == SOUP_STATUS_OK) {
935 soup_connection_reserve (conn);
936 host->connections = g_slist_prepend (host->connections, conn);
937 g_mutex_unlock (session->priv->host_lock);
941 /* The connection failed. */
942 g_mutex_unlock (session->priv->host_lock);
943 connection_disconnected (conn, session);
945 if (host->connections) {
946 /* Something went wrong this time, but we have at
947 * least one open connection to this host. So just
948 * leave the message in the queue so it can use that
949 * connection once it's free.
954 /* There are two possibilities: either status is
955 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
956 * will create a new connection (and all we need to do here
957 * is downgrade the message from CONNECTING to QUEUED); or
958 * status is something else, probably CANT_CONNECT or
959 * CANT_RESOLVE or the like, in which case we need to cancel
960 * any messages waiting for this host, since they're out
963 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
964 if (get_host_for_message (session, msg) == host) {
965 if (status == SOUP_STATUS_TRY_AGAIN) {
966 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
967 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
969 soup_message_set_status (msg, status);
970 soup_session_cancel_message (session, msg);
977 * soup_session_get_connection:
978 * @session: a #SoupSession
979 * @msg: a #SoupMessage
980 * @try_pruning: on return, whether or not to try pruning a connection
981 * @is_new: on return, %TRUE if the returned connection is new and not
984 * Tries to find or create a connection for @msg.
986 * If there is an idle connection to the relevant host available, then
987 * that connection will be returned (with *@is_new set to %FALSE). The
988 * connection will be marked "reserved", so the caller must call
989 * soup_connection_release() if it ends up not using the connection
992 * If there is no idle connection available, but it is possible to
993 * create a new connection, then one will be created and returned,
994 * with *@is_new set to %TRUE. The caller MUST then call
995 * soup_connection_connect_sync() or soup_connection_connect_async()
996 * to connect it. If the connection attempt succeeds, the connection
997 * will be marked "reserved" and added to @session's connection pool
998 * once it connects. If the connection attempt fails, the connection
1001 * If no connection is available and a new connection cannot be made,
1002 * soup_session_get_connection() will return %NULL. If @session has
1003 * the maximum number of open connections open, but does not have the
1004 * maximum number of per-host connections open to the relevant host,
1005 * then *@try_pruning will be set to %TRUE. In this case, the caller
1006 * can call soup_session_try_prune_connection() to close an idle
1007 * connection, and then try soup_session_get_connection() again. (If
1008 * calling soup_session_try_prune_connection() wouldn't help, then
1009 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
1011 * Return value: a #SoupConnection, or %NULL
1014 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
1015 gboolean *try_pruning, gboolean *is_new)
1017 SoupConnection *conn;
1018 SoupSessionHost *host;
1021 g_mutex_lock (session->priv->host_lock);
1023 host = get_host_for_message (session, msg);
1024 for (conns = host->connections; conns; conns = conns->next) {
1025 if (!soup_connection_is_in_use (conns->data)) {
1026 soup_connection_reserve (conns->data);
1027 g_mutex_unlock (session->priv->host_lock);
1033 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
1034 /* We already started a connection for this
1035 * message, so don't start another one.
1037 g_mutex_unlock (session->priv->host_lock);
1041 if (host->num_conns >= session->priv->max_conns_per_host) {
1042 g_mutex_unlock (session->priv->host_lock);
1046 if (session->priv->num_conns >= session->priv->max_conns) {
1047 *try_pruning = TRUE;
1048 g_mutex_unlock (session->priv->host_lock);
1052 /* Make sure session->priv->proxy_host gets set now while
1053 * we have the host_lock.
1055 if (session->priv->proxy_uri)
1056 get_proxy_host (session);
1058 conn = g_object_new (
1059 (session->priv->use_ntlm ?
1060 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1061 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1062 SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
1063 SOUP_CONNECTION_SSL_CREDENTIALS, session->priv->ssl_creds,
1064 SOUP_CONNECTION_MESSAGE_FILTER, session,
1066 g_signal_connect (conn, "connect_result",
1067 G_CALLBACK (connect_result),
1069 g_signal_connect (conn, "disconnected",
1070 G_CALLBACK (connection_disconnected),
1072 g_signal_connect (conn, "authenticate",
1073 G_CALLBACK (connection_authenticate),
1075 g_signal_connect (conn, "reauthenticate",
1076 G_CALLBACK (connection_reauthenticate),
1079 g_hash_table_insert (session->priv->conns, conn, host);
1081 /* We increment the connection counts so it counts against the
1082 * totals, but we don't add it to the host's connection list
1083 * yet, since it's not ready for use.
1085 session->priv->num_conns++;
1088 /* Mark the request as connecting, so we don't try to open
1089 * another new connection for it while waiting for this one.
1091 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1093 g_mutex_unlock (session->priv->host_lock);
1099 message_finished (SoupMessage *msg, gpointer user_data)
1101 SoupSession *session = user_data;
1103 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1104 soup_message_queue_remove_message (session->queue, msg);
1105 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1110 queue_message (SoupSession *session, SoupMessage *msg,
1111 SoupMessageCallbackFn callback, gpointer user_data)
1113 g_signal_connect_after (msg, "finished",
1114 G_CALLBACK (message_finished), session);
1116 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1117 soup_message_add_status_class_handler (
1118 msg, SOUP_STATUS_CLASS_REDIRECT,
1119 SOUP_HANDLER_POST_BODY,
1120 redirect_handler, session);
1123 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1124 soup_message_queue_append (session->queue, msg);
1128 * soup_session_queue_message:
1129 * @session: a #SoupSession
1130 * @msg: the message to queue
1131 * @callback: a #SoupMessageCallbackFn which will be called after the
1132 * message completes or when an unrecoverable error occurs.
1133 * @user_data: a pointer passed to @callback.
1135 * Queues the message @msg for sending. All messages are processed
1136 * while the glib main loop runs. If @msg has been processed before,
1137 * any resources related to the time it was last sent are freed.
1139 * Upon message completion, the callback specified in @callback will
1140 * be invoked. If after returning from this callback the message has
1141 * not been requeued, @msg will be unreffed.
1144 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1145 SoupMessageCallbackFn callback, gpointer user_data)
1147 g_return_if_fail (SOUP_IS_SESSION (session));
1148 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1150 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1151 callback, user_data);
1155 requeue_message (SoupSession *session, SoupMessage *msg)
1157 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1161 * soup_session_requeue_message:
1162 * @session: a #SoupSession
1163 * @msg: the message to requeue
1165 * This causes @msg to be placed back on the queue to be attempted
1169 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1171 g_return_if_fail (SOUP_IS_SESSION (session));
1172 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1174 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1179 * soup_session_send_message:
1180 * @session: a #SoupSession
1181 * @msg: the message to send
1183 * Synchronously send @msg. This call will not return until the
1184 * transfer is finished successfully or there is an unrecoverable
1187 * @msg is not freed upon return.
1189 * Return value: the HTTP status code of the response
1192 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1194 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1195 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1197 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1202 cancel_message (SoupSession *session, SoupMessage *msg)
1204 soup_message_queue_remove_message (session->queue, msg);
1205 soup_message_finished (msg);
1209 * soup_session_cancel_message:
1210 * @session: a #SoupSession
1211 * @msg: the message to cancel
1213 * Causes @session to immediately finish processing @msg. You should
1214 * set a status code on @msg with soup_message_set_status() before
1215 * calling this function.
1218 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1220 g_return_if_fail (SOUP_IS_SESSION (session));
1221 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1223 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1227 * soup_session_abort:
1228 * @session: the session
1230 * Cancels all pending requests in @session.
1233 soup_session_abort (SoupSession *session)
1235 SoupMessageQueueIter iter;
1238 g_return_if_fail (SOUP_IS_SESSION (session));
1240 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1241 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1242 soup_session_cancel_message (session, msg);