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 set_property (GObject *object, guint prop_id,
275 const GValue *value, GParamSpec *pspec)
277 SoupSession *session = SOUP_SESSION (object);
279 gboolean need_abort = FALSE;
283 pval = g_value_get_pointer (value);
285 if (!safe_uri_equal (session->priv->proxy_uri, pval))
288 if (session->priv->proxy_uri)
289 soup_uri_free (session->priv->proxy_uri);
291 session->priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
294 soup_session_abort (session);
295 cleanup_hosts (session);
300 session->priv->max_conns = g_value_get_int (value);
302 case PROP_MAX_CONNS_PER_HOST:
303 session->priv->max_conns_per_host = g_value_get_int (value);
306 session->priv->use_ntlm = g_value_get_boolean (value);
308 case PROP_SSL_CA_FILE:
309 g_free (session->priv->ssl_ca_file);
310 session->priv->ssl_ca_file = g_strdup (g_value_get_string (value));
318 get_property (GObject *object, guint prop_id,
319 GValue *value, GParamSpec *pspec)
321 SoupSession *session = SOUP_SESSION (object);
325 g_value_set_pointer (value, session->priv->proxy_uri ?
326 soup_uri_copy (session->priv->proxy_uri) :
330 g_value_set_int (value, session->priv->max_conns);
332 case PROP_MAX_CONNS_PER_HOST:
333 g_value_set_int (value, session->priv->max_conns_per_host);
336 g_value_set_boolean (value, session->priv->use_ntlm);
338 case PROP_SSL_CA_FILE:
339 g_value_set_string (value, session->priv->ssl_ca_file);
348 * soup_session_add_filter:
349 * @session: a #SoupSession
350 * @filter: an object implementing the #SoupMessageFilter interface
352 * Adds @filter to @session's list of message filters to be applied to
356 soup_session_add_filter (SoupSession *session, SoupMessageFilter *filter)
358 g_return_if_fail (SOUP_IS_SESSION (session));
359 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
361 session->priv->filters = g_slist_prepend (session->priv->filters,
366 * soup_session_remove_filter:
367 * @session: a #SoupSession
368 * @filter: an object implementing the #SoupMessageFilter interface
370 * Removes @filter from @session's list of message filters
373 soup_session_remove_filter (SoupSession *session, SoupMessageFilter *filter)
375 g_return_if_fail (SOUP_IS_SESSION (session));
376 g_return_if_fail (SOUP_IS_MESSAGE_FILTER (filter));
378 session->priv->filters = g_slist_remove (session->priv->filters,
385 host_uri_hash (gconstpointer key)
387 const SoupUri *uri = key;
389 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
393 host_uri_equal (gconstpointer v1, gconstpointer v2)
395 const SoupUri *one = v1;
396 const SoupUri *two = v2;
398 if (one->protocol != two->protocol)
400 if (one->port != two->port)
403 return strcmp (one->host, two->host) == 0;
406 static SoupSessionHost *
407 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
409 SoupSessionHost *host;
411 host = g_new0 (SoupSessionHost, 1);
412 host->root_uri = soup_uri_copy_root (source_uri);
414 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
415 !session->priv->ssl_creds) {
416 session->priv->ssl_creds =
417 soup_ssl_get_client_credentials (session->priv->ssl_ca_file);
423 /* Note: get_host_for_message doesn't lock the host_lock. The caller
424 * must do it itself if there's a chance the host doesn't already
427 static SoupSessionHost *
428 get_host_for_message (SoupSession *session, SoupMessage *msg)
430 SoupSessionHost *host;
431 const SoupUri *source = soup_message_get_uri (msg);
433 host = g_hash_table_lookup (session->priv->hosts, source);
437 host = soup_session_host_new (session, source);
438 g_hash_table_insert (session->priv->hosts, host->root_uri, host);
443 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
444 * it itself if there's a chance the host doesn't already exist.
446 static SoupSessionHost *
447 get_proxy_host (SoupSession *session)
449 if (session->priv->proxy_host || !session->priv->proxy_uri)
450 return session->priv->proxy_host;
452 session->priv->proxy_host =
453 soup_session_host_new (session, session->priv->proxy_uri);
454 return session->priv->proxy_host;
458 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
461 g_free (scheme_realm);
465 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
467 g_free (scheme_realm);
468 g_object_unref (auth);
472 free_host (SoupSessionHost *host, SoupSession *session)
474 while (host->connections) {
475 SoupConnection *conn = host->connections->data;
477 host->connections = g_slist_remove (host->connections, conn);
478 soup_connection_disconnect (conn);
481 if (host->auth_realms) {
482 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
483 g_hash_table_destroy (host->auth_realms);
486 g_hash_table_foreach (host->auths, free_auth, NULL);
487 g_hash_table_destroy (host->auths);
490 soup_uri_free (host->root_uri);
497 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
499 SoupSessionHost *host;
501 const char *realm, *const_path;
504 host = get_proxy_host (session);
507 host = get_host_for_message (session, msg);
508 const_path = soup_message_get_uri (msg)->path;
510 g_return_val_if_fail (host != NULL, NULL);
512 if (!host->auth_realms)
515 path = g_strdup (const_path);
518 realm = g_hash_table_lookup (host->auth_realms, path);
522 dir = strrchr (path, '/');
529 return g_hash_table_lookup (host->auths, realm);
535 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
540 realm = g_strdup_printf ("%s:%s",
541 soup_auth_get_scheme_name (auth),
542 soup_auth_get_realm (auth));
544 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
545 auth == (SoupAuth *)value) {
546 g_hash_table_remove (host->auths, realm);
548 g_object_unref (auth);
554 authenticate_auth (SoupSession *session, SoupAuth *auth,
555 SoupMessage *msg, gboolean prior_auth_failed,
559 char *username = NULL, *password = NULL;
562 uri = session->priv->proxy_uri;
564 uri = soup_message_get_uri (msg);
566 if (uri->passwd && !prior_auth_failed) {
567 soup_auth_authenticate (auth, uri->user, uri->passwd);
571 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
572 msg, soup_auth_get_scheme_name (auth),
573 soup_auth_get_realm (auth),
574 &username, &password);
575 if (username || password)
576 soup_auth_authenticate (auth, username, password);
580 memset (password, 0, strlen (password));
584 return soup_auth_is_authenticated (auth);
588 update_auth_internal (SoupSession *session, SoupMessage *msg,
589 const GSList *headers, gboolean proxy,
590 gboolean got_unauthorized)
592 SoupSessionHost *host;
593 SoupAuth *new_auth, *prior_auth, *old_auth;
594 gpointer old_path, old_realm;
595 const SoupUri *msg_uri;
599 gboolean prior_auth_failed = FALSE;
602 host = get_proxy_host (session);
604 host = get_host_for_message (session, msg);
606 g_return_val_if_fail (host != NULL, FALSE);
608 /* Try to construct a new auth from the headers; if we can't,
609 * there's no way we'll be able to authenticate.
611 msg_uri = soup_message_get_uri (msg);
612 new_auth = soup_auth_new_from_header_list (headers);
616 /* See if this auth is the same auth we used last time */
617 prior_auth = lookup_auth (session, msg, proxy);
619 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
620 !strcmp (soup_auth_get_realm (prior_auth),
621 soup_auth_get_realm (new_auth))) {
622 if (!got_unauthorized) {
623 /* The user is just trying to preauthenticate
624 * using information we already have, so
625 * there's nothing more that needs to be done.
627 g_object_unref (new_auth);
631 /* The server didn't like the username/password we
632 * provided before. Invalidate it and note this fact.
634 invalidate_auth (host, prior_auth);
635 prior_auth_failed = TRUE;
638 if (!host->auth_realms) {
639 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
640 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
643 /* Record where this auth realm is used */
644 realm = g_strdup_printf ("%s:%s",
645 soup_auth_get_scheme_name (new_auth),
646 soup_auth_get_realm (new_auth));
649 * RFC 2617 is somewhat unclear about the scope of protection
650 * spaces with regard to proxies. The only mention of it is
651 * as an aside in section 3.2.1, where it is defining the fields
652 * of a Digest challenge and says that the protection space is
653 * always the entire proxy. Is this the case for all authentication
654 * schemes or just Digest? Who knows, but we're assuming all.
657 pspace = g_slist_prepend (NULL, g_strdup (""));
659 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
661 for (p = pspace; p; p = p->next) {
663 if (g_hash_table_lookup_extended (host->auth_realms, path,
664 &old_path, &old_realm)) {
665 g_hash_table_remove (host->auth_realms, old_path);
670 g_hash_table_insert (host->auth_realms,
671 g_strdup (path), g_strdup (realm));
673 soup_auth_free_protection_space (new_auth, pspace);
675 /* Now, make sure the auth is recorded. (If there's a
676 * pre-existing auth, we keep that rather than the new one,
677 * since the old one might already be authenticated.)
679 old_auth = g_hash_table_lookup (host->auths, realm);
682 g_object_unref (new_auth);
685 g_hash_table_insert (host->auths, realm, new_auth);
687 /* If we need to authenticate, try to do it. */
688 if (!soup_auth_is_authenticated (new_auth)) {
689 return authenticate_auth (session, new_auth,
690 msg, prior_auth_failed, proxy);
693 /* Otherwise we're good. */
698 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
699 const char *auth_type, const char *auth_realm,
700 char **username, char **password, gpointer session)
702 g_signal_emit (session, signals[AUTHENTICATE], 0,
703 msg, auth_type, auth_realm, username, password);
707 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
708 const char *auth_type, const char *auth_realm,
709 char **username, char **password,
712 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
713 msg, auth_type, auth_realm, username, password);
718 authorize_handler (SoupMessage *msg, gpointer user_data)
720 SoupSession *session = user_data;
721 const GSList *headers;
724 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
725 headers = soup_message_get_header_list (msg->response_headers,
726 "Proxy-Authenticate");
729 headers = soup_message_get_header_list (msg->response_headers,
736 if (update_auth_internal (session, msg, headers, proxy, TRUE))
737 soup_session_requeue_message (session, msg);
741 redirect_handler (SoupMessage *msg, gpointer user_data)
743 SoupSession *session = user_data;
747 new_loc = soup_message_get_header (msg->response_headers, "Location");
750 new_uri = soup_uri_new (new_loc);
752 soup_message_set_status_full (msg,
753 SOUP_STATUS_MALFORMED,
754 "Invalid Redirect URL");
758 soup_message_set_uri (msg, new_uri);
759 soup_uri_free (new_uri);
761 soup_session_requeue_message (session, msg);
765 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
767 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
771 soup_message_remove_header (msg->request_headers, header);
773 auth = lookup_auth (session, msg, proxy);
776 if (!soup_auth_is_authenticated (auth) &&
777 !authenticate_auth (session, auth, msg, FALSE, proxy))
780 token = soup_auth_get_authorization (auth, msg);
782 soup_message_add_header (msg->request_headers, header, token);
788 setup_message (SoupMessageFilter *filter, SoupMessage *msg)
790 SoupSession *session = SOUP_SESSION (filter);
793 for (f = session->priv->filters; f; f = f->next) {
795 soup_message_filter_setup_message (filter, msg);
798 add_auth (session, msg, FALSE);
799 soup_message_add_status_code_handler (
800 msg, SOUP_STATUS_UNAUTHORIZED,
801 SOUP_HANDLER_POST_BODY,
802 authorize_handler, session);
804 if (session->priv->proxy_uri) {
805 add_auth (session, msg, TRUE);
806 soup_message_add_status_code_handler (
807 msg, SOUP_STATUS_PROXY_UNAUTHORIZED,
808 SOUP_HANDLER_POST_BODY,
809 authorize_handler, session);
814 find_oldest_connection (gpointer key, gpointer host, gpointer data)
816 SoupConnection *conn = key, **oldest = data;
818 /* Don't prune a connection that is currently in use, or
819 * hasn't been used yet.
821 if (soup_connection_is_in_use (conn) ||
822 soup_connection_last_used (conn) == 0)
825 if (!*oldest || (soup_connection_last_used (conn) <
826 soup_connection_last_used (*oldest)))
831 * soup_session_try_prune_connection:
832 * @session: a #SoupSession
834 * Finds the least-recently-used idle connection in @session and closes
837 * Return value: %TRUE if a connection was closed, %FALSE if there are
838 * no idle connections.
841 soup_session_try_prune_connection (SoupSession *session)
843 SoupConnection *oldest = NULL;
845 g_mutex_lock (session->priv->host_lock);
846 g_hash_table_foreach (session->priv->conns, find_oldest_connection,
849 /* Ref the connection before unlocking the mutex in
850 * case someone else tries to prune it too.
852 g_object_ref (oldest);
853 g_mutex_unlock (session->priv->host_lock);
854 soup_connection_disconnect (oldest);
855 g_object_unref (oldest);
858 g_mutex_unlock (session->priv->host_lock);
864 connection_disconnected (SoupConnection *conn, gpointer user_data)
866 SoupSession *session = user_data;
867 SoupSessionHost *host;
869 g_mutex_lock (session->priv->host_lock);
871 host = g_hash_table_lookup (session->priv->conns, conn);
873 g_hash_table_remove (session->priv->conns, conn);
874 host->connections = g_slist_remove (host->connections, conn);
878 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
879 session->priv->num_conns--;
881 g_mutex_unlock (session->priv->host_lock);
882 g_object_unref (conn);
886 connect_result (SoupConnection *conn, guint status, gpointer user_data)
888 SoupSession *session = user_data;
889 SoupSessionHost *host;
890 SoupMessageQueueIter iter;
893 g_mutex_lock (session->priv->host_lock);
895 host = g_hash_table_lookup (session->priv->conns, conn);
897 g_mutex_unlock (session->priv->host_lock);
901 if (status == SOUP_STATUS_OK) {
902 host->connections = g_slist_prepend (host->connections, conn);
903 g_mutex_unlock (session->priv->host_lock);
907 /* The connection failed. */
908 g_mutex_unlock (session->priv->host_lock);
909 connection_disconnected (conn, session);
911 if (host->connections) {
912 /* Something went wrong this time, but we have at
913 * least one open connection to this host. So just
914 * leave the message in the queue so it can use that
915 * connection once it's free.
920 /* There are two possibilities: either status is
921 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
922 * will create a new connection (and all we need to do here
923 * is downgrade the message from CONNECTING to QUEUED); or
924 * status is something else, probably CANT_CONNECT or
925 * CANT_RESOLVE or the like, in which case we need to cancel
926 * any messages waiting for this host, since they're out
929 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
930 if (get_host_for_message (session, msg) == host) {
931 if (status == SOUP_STATUS_TRY_AGAIN) {
932 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING)
933 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
935 soup_message_set_status (msg, status);
936 soup_session_cancel_message (session, msg);
943 * soup_session_get_connection:
944 * @session: a #SoupSession
945 * @msg: a #SoupMessage
946 * @try_pruning: on return, whether or not to try pruning a connection
947 * @is_new: on return, %TRUE if the returned connection is new and not
950 * Tries to find or create a connection for @msg. If there is an idle
951 * connection to the relevant host available, then it will be returned
952 * (with *@is_new set to %FALSE). Otherwise, if it is possible to
953 * create a new connection, one will be created and returned, with
954 * *@is_new set to %TRUE.
956 * If no connection can be made, it will return %NULL. If @session has
957 * the maximum number of open connections open, but does not have the
958 * maximum number of per-host connections open to the relevant host, then
959 * *@try_pruning will be set to %TRUE. In this case, the caller can
960 * call soup_session_try_prune_connection() to close an idle connection,
961 * and then try soup_session_get_connection() again. (If calling
962 * soup_session_try_prune_connection() wouldn't help, then *@try_pruning
963 * is left untouched; it is NOT set to %FALSE.)
965 * Return value: a #SoupConnection, or %NULL
968 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
969 gboolean *try_pruning, gboolean *is_new)
971 SoupConnection *conn;
972 SoupSessionHost *host;
975 g_mutex_lock (session->priv->host_lock);
977 host = get_host_for_message (session, msg);
978 for (conns = host->connections; conns; conns = conns->next) {
979 if (!soup_connection_is_in_use (conns->data)) {
980 soup_connection_reserve (conns->data);
981 g_mutex_unlock (session->priv->host_lock);
987 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
988 /* We already started a connection for this
989 * message, so don't start another one.
991 g_mutex_unlock (session->priv->host_lock);
995 if (host->num_conns >= session->priv->max_conns_per_host) {
996 g_mutex_unlock (session->priv->host_lock);
1000 if (session->priv->num_conns >= session->priv->max_conns) {
1001 *try_pruning = TRUE;
1002 g_mutex_unlock (session->priv->host_lock);
1006 /* Make sure session->priv->proxy_host gets set now while
1007 * we have the host_lock.
1009 if (session->priv->proxy_uri)
1010 get_proxy_host (session);
1012 conn = g_object_new (
1013 (session->priv->use_ntlm ?
1014 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
1015 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1016 SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
1017 SOUP_CONNECTION_SSL_CREDENTIALS, session->priv->ssl_creds,
1018 SOUP_CONNECTION_MESSAGE_FILTER, session,
1020 g_signal_connect (conn, "connect_result",
1021 G_CALLBACK (connect_result),
1023 g_signal_connect (conn, "disconnected",
1024 G_CALLBACK (connection_disconnected),
1026 g_signal_connect (conn, "authenticate",
1027 G_CALLBACK (connection_authenticate),
1029 g_signal_connect (conn, "reauthenticate",
1030 G_CALLBACK (connection_reauthenticate),
1033 g_hash_table_insert (session->priv->conns, conn, host);
1035 /* We increment the connection counts so it counts against the
1036 * totals, but we don't add it to the host's connection list
1037 * yet, since it's not ready for use.
1039 session->priv->num_conns++;
1042 /* Mark the request as connecting, so we don't try to open
1043 * another new connection for it while waiting for this one.
1045 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
1047 g_mutex_unlock (session->priv->host_lock);
1053 message_finished (SoupMessage *msg, gpointer user_data)
1055 SoupSession *session = user_data;
1057 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1058 soup_message_queue_remove_message (session->queue, msg);
1059 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1064 queue_message (SoupSession *session, SoupMessage *msg,
1065 SoupMessageCallbackFn callback, gpointer user_data)
1067 g_signal_connect_after (msg, "finished",
1068 G_CALLBACK (message_finished), session);
1070 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1071 soup_message_add_status_class_handler (
1072 msg, SOUP_STATUS_CLASS_REDIRECT,
1073 SOUP_HANDLER_POST_BODY,
1074 redirect_handler, session);
1077 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1078 soup_message_queue_append (session->queue, msg);
1082 * soup_session_queue_message:
1083 * @session: a #SoupSession
1084 * @msg: the message to queue
1085 * @callback: a #SoupMessageCallbackFn which will be called after the
1086 * message completes or when an unrecoverable error occurs.
1087 * @user_data: a pointer passed to @callback.
1089 * Queues the message @msg for sending. All messages are processed
1090 * while the glib main loop runs. If @msg has been processed before,
1091 * any resources related to the time it was last sent are freed.
1093 * Upon message completion, the callback specified in @callback will
1094 * be invoked. If after returning from this callback the message has
1095 * not been requeued, @msg will be unreffed.
1098 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1099 SoupMessageCallbackFn callback, gpointer user_data)
1101 g_return_if_fail (SOUP_IS_SESSION (session));
1102 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1104 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1105 callback, user_data);
1109 requeue_message (SoupSession *session, SoupMessage *msg)
1111 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1115 * soup_session_requeue_message:
1116 * @session: a #SoupSession
1117 * @msg: the message to requeue
1119 * This causes @msg to be placed back on the queue to be attempted
1123 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1125 g_return_if_fail (SOUP_IS_SESSION (session));
1126 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1128 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1133 * soup_session_send_message:
1134 * @session: a #SoupSession
1135 * @msg: the message to send
1137 * Synchronously send @msg. This call will not return until the
1138 * transfer is finished successfully or there is an unrecoverable
1141 * @msg is not freed upon return.
1143 * Return value: the HTTP status code of the response
1146 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1148 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1149 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1151 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1156 cancel_message (SoupSession *session, SoupMessage *msg)
1158 soup_message_queue_remove_message (session->queue, msg);
1159 soup_message_finished (msg);
1163 * soup_session_cancel_message:
1164 * @session: a #SoupSession
1165 * @msg: the message to cancel
1167 * Causes @session to immediately finish processing @msg. You should
1168 * set a status code on @msg with soup_message_set_status() before
1169 * calling this function.
1172 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1174 g_return_if_fail (SOUP_IS_SESSION (session));
1175 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1177 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1181 * soup_session_abort:
1182 * @session: the session
1184 * Cancels all pending requests in @session.
1187 soup_session_abort (SoupSession *session)
1189 SoupMessageQueueIter iter;
1192 g_return_if_fail (SOUP_IS_SESSION (session));
1194 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1195 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1196 soup_session_cancel_message (session, msg);