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-queue.h"
28 GSList *connections; /* CONTAINS: SoupConnection */
31 GHashTable *auth_realms; /* path -> scheme:realm */
32 GHashTable *auths; /* scheme:realm -> SoupAuth */
35 struct SoupSessionPrivate {
37 guint max_conns, max_conns_per_host;
43 GHashTable *hosts; /* SoupUri -> SoupSessionHost */
44 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
47 SoupSessionHost *proxy_host;
49 /* Must hold the host_lock before potentially creating a
50 * new SoupSessionHost, or adding/removing a connection.
51 * Must not emit signals or destroy objects while holding it.
56 static guint host_uri_hash (gconstpointer key);
57 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
58 static void free_host (SoupSessionHost *host, SoupSession *session);
60 static void queue_message (SoupSession *session, SoupMessage *msg,
61 SoupMessageCallbackFn callback,
63 static void requeue_message (SoupSession *session, SoupMessage *msg);
64 static void cancel_message (SoupSession *session, SoupMessage *msg);
66 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
67 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
69 #define PARENT_TYPE G_TYPE_OBJECT
70 static GObjectClass *parent_class;
78 static guint signals[LAST_SIGNAL] = { 0 };
85 PROP_MAX_CONNS_PER_HOST,
92 static void set_property (GObject *object, guint prop_id,
93 const GValue *value, GParamSpec *pspec);
94 static void get_property (GObject *object, guint prop_id,
95 GValue *value, GParamSpec *pspec);
98 init (GObject *object)
100 SoupSession *session = SOUP_SESSION (object);
102 session->priv = g_new0 (SoupSessionPrivate, 1);
103 session->priv->host_lock = g_mutex_new ();
104 session->queue = soup_message_queue_new ();
105 session->priv->hosts = g_hash_table_new (host_uri_hash,
107 session->priv->conns = g_hash_table_new (NULL, NULL);
109 session->priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
110 session->priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
114 foreach_free_host (gpointer key, gpointer host, gpointer session)
116 free_host (host, session);
121 cleanup_hosts (SoupSession *session)
123 g_hash_table_foreach_remove (session->priv->hosts,
124 foreach_free_host, session);
128 dispose (GObject *object)
130 SoupSession *session = SOUP_SESSION (object);
132 soup_session_abort (session);
133 cleanup_hosts (session);
135 G_OBJECT_CLASS (parent_class)->dispose (object);
139 finalize (GObject *object)
141 SoupSession *session = SOUP_SESSION (object);
143 g_mutex_free (session->priv->host_lock);
144 soup_message_queue_destroy (session->queue);
145 g_hash_table_destroy (session->priv->hosts);
146 g_hash_table_destroy (session->priv->conns);
147 g_free (session->priv);
149 G_OBJECT_CLASS (parent_class)->finalize (object);
153 class_init (GObjectClass *object_class)
155 SoupSessionClass *session_class = SOUP_SESSION_CLASS (object_class);
157 parent_class = g_type_class_ref (PARENT_TYPE);
159 /* virtual method definition */
160 session_class->queue_message = queue_message;
161 session_class->requeue_message = requeue_message;
162 session_class->cancel_message = cancel_message;
164 /* virtual method override */
165 object_class->dispose = dispose;
166 object_class->finalize = finalize;
167 object_class->set_property = set_property;
168 object_class->get_property = get_property;
171 signals[AUTHENTICATE] =
172 g_signal_new ("authenticate",
173 G_OBJECT_CLASS_TYPE (object_class),
175 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
177 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
184 signals[REAUTHENTICATE] =
185 g_signal_new ("reauthenticate",
186 G_OBJECT_CLASS_TYPE (object_class),
188 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
190 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
199 g_object_class_install_property (
200 object_class, PROP_PROXY_URI,
201 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
203 "The HTTP Proxy to use for this session",
205 g_object_class_install_property (
206 object_class, PROP_MAX_CONNS,
207 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
208 "Max Connection Count",
209 "The maximum number of connections that the session can open at once",
214 g_object_class_install_property (
215 object_class, PROP_MAX_CONNS_PER_HOST,
216 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
217 "Max Per-Host Connection Count",
218 "The maximum number of connections that the session can open at once to a given host",
223 g_object_class_install_property (
224 object_class, PROP_USE_NTLM,
225 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
227 "Whether or not to use NTLM authentication",
230 g_object_class_install_property (
231 object_class, PROP_SSL_CA_FILE,
232 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
234 "File containing SSL CA certificates",
239 SOUP_MAKE_TYPE (soup_session, SoupSession, class_init, init, PARENT_TYPE)
242 safe_uri_equal (const SoupUri *a, const SoupUri *b)
247 if ((a && !b) || (b && !a))
250 return soup_uri_equal (a, b);
254 set_property (GObject *object, guint prop_id,
255 const GValue *value, GParamSpec *pspec)
257 SoupSession *session = SOUP_SESSION (object);
259 gboolean need_abort = FALSE;
263 pval = g_value_get_pointer (value);
265 if (!safe_uri_equal (session->priv->proxy_uri, pval))
268 if (session->priv->proxy_uri)
269 soup_uri_free (session->priv->proxy_uri);
271 session->priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
274 soup_session_abort (session);
275 cleanup_hosts (session);
280 session->priv->max_conns = g_value_get_int (value);
282 case PROP_MAX_CONNS_PER_HOST:
283 session->priv->max_conns_per_host = g_value_get_int (value);
286 session->priv->use_ntlm = g_value_get_boolean (value);
288 case PROP_SSL_CA_FILE:
289 g_free (session->priv->ssl_ca_file);
290 session->priv->ssl_ca_file = g_strdup (g_value_get_string (value));
298 get_property (GObject *object, guint prop_id,
299 GValue *value, GParamSpec *pspec)
301 SoupSession *session = SOUP_SESSION (object);
305 g_value_set_pointer (value, session->priv->proxy_uri ?
306 soup_uri_copy (session->priv->proxy_uri) :
310 g_value_set_int (value, session->priv->max_conns);
312 case PROP_MAX_CONNS_PER_HOST:
313 g_value_set_int (value, session->priv->max_conns_per_host);
316 g_value_set_boolean (value, session->priv->use_ntlm);
318 case PROP_SSL_CA_FILE:
319 g_value_set_string (value, session->priv->ssl_ca_file);
329 host_uri_hash (gconstpointer key)
331 const SoupUri *uri = key;
333 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
337 host_uri_equal (gconstpointer v1, gconstpointer v2)
339 const SoupUri *one = v1;
340 const SoupUri *two = v2;
342 if (one->protocol != two->protocol)
344 if (one->port != two->port)
347 return strcmp (one->host, two->host) == 0;
350 static SoupSessionHost *
351 soup_session_host_new (SoupSession *session, const SoupUri *source_uri)
353 SoupSessionHost *host;
355 host = g_new0 (SoupSessionHost, 1);
356 host->root_uri = soup_uri_copy_root (source_uri);
358 if (host->root_uri->protocol == SOUP_PROTOCOL_HTTPS &&
359 !session->priv->ssl_creds) {
360 session->priv->ssl_creds =
361 soup_ssl_get_client_credentials (session->priv->ssl_ca_file);
367 /* Note: get_host_for_message doesn't lock the host_lock. The caller
368 * must do it itself if there's a chance the host doesn't already
371 static SoupSessionHost *
372 get_host_for_message (SoupSession *session, SoupMessage *msg)
374 SoupSessionHost *host;
375 const SoupUri *source = soup_message_get_uri (msg);
377 host = g_hash_table_lookup (session->priv->hosts, source);
381 host = soup_session_host_new (session, source);
382 g_hash_table_insert (session->priv->hosts, host->root_uri, host);
387 /* Note: get_proxy_host doesn't lock the host_lock. The caller must do
388 * it itself if there's a chance the host doesn't already exist.
390 static SoupSessionHost *
391 get_proxy_host (SoupSession *session)
393 if (session->priv->proxy_host || !session->priv->proxy_uri)
394 return session->priv->proxy_host;
396 session->priv->proxy_host =
397 soup_session_host_new (session, session->priv->proxy_uri);
398 return session->priv->proxy_host;
402 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
405 g_free (scheme_realm);
409 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
411 g_free (scheme_realm);
412 g_object_unref (auth);
416 free_host (SoupSessionHost *host, SoupSession *session)
418 while (host->connections) {
419 SoupConnection *conn = host->connections->data;
421 host->connections = g_slist_remove (host->connections, conn);
422 soup_connection_disconnect (conn);
425 if (host->auth_realms) {
426 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
427 g_hash_table_destroy (host->auth_realms);
430 g_hash_table_foreach (host->auths, free_auth, NULL);
431 g_hash_table_destroy (host->auths);
434 soup_uri_free (host->root_uri);
441 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
443 SoupSessionHost *host;
445 const char *realm, *const_path;
448 host = get_proxy_host (session);
451 host = get_host_for_message (session, msg);
452 const_path = soup_message_get_uri (msg)->path;
454 g_return_val_if_fail (host != NULL, NULL);
456 if (!host->auth_realms)
459 path = g_strdup (const_path);
462 realm = g_hash_table_lookup (host->auth_realms, path);
466 dir = strrchr (path, '/');
473 return g_hash_table_lookup (host->auths, realm);
479 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
484 realm = g_strdup_printf ("%s:%s",
485 soup_auth_get_scheme_name (auth),
486 soup_auth_get_realm (auth));
488 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
489 auth == (SoupAuth *)value) {
490 g_hash_table_remove (host->auths, realm);
492 g_object_unref (auth);
498 authenticate_auth (SoupSession *session, SoupAuth *auth,
499 SoupMessage *msg, gboolean prior_auth_failed,
503 char *username = NULL, *password = NULL;
506 uri = session->priv->proxy_uri;
508 uri = soup_message_get_uri (msg);
510 if (uri->passwd && !prior_auth_failed) {
511 soup_auth_authenticate (auth, uri->user, uri->passwd);
515 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
516 msg, soup_auth_get_scheme_name (auth),
517 soup_auth_get_realm (auth),
518 &username, &password);
519 if (username || password)
520 soup_auth_authenticate (auth, username, password);
524 memset (password, 0, strlen (password));
528 return soup_auth_is_authenticated (auth);
532 update_auth_internal (SoupSession *session, SoupMessage *msg,
533 const GSList *headers, gboolean proxy,
534 gboolean got_unauthorized)
536 SoupSessionHost *host;
537 SoupAuth *new_auth, *prior_auth, *old_auth;
538 gpointer old_path, old_realm;
539 const SoupUri *msg_uri;
543 gboolean prior_auth_failed = FALSE;
546 host = get_proxy_host (session);
548 host = get_host_for_message (session, msg);
550 g_return_val_if_fail (host != NULL, FALSE);
552 /* Try to construct a new auth from the headers; if we can't,
553 * there's no way we'll be able to authenticate.
555 msg_uri = soup_message_get_uri (msg);
556 new_auth = soup_auth_new_from_header_list (headers);
560 /* See if this auth is the same auth we used last time */
561 prior_auth = lookup_auth (session, msg, proxy);
563 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
564 !strcmp (soup_auth_get_realm (prior_auth),
565 soup_auth_get_realm (new_auth))) {
566 if (!got_unauthorized) {
567 /* The user is just trying to preauthenticate
568 * using information we already have, so
569 * there's nothing more that needs to be done.
571 g_object_unref (new_auth);
575 /* The server didn't like the username/password we
576 * provided before. Invalidate it and note this fact.
578 invalidate_auth (host, prior_auth);
579 prior_auth_failed = TRUE;
582 if (!host->auth_realms) {
583 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
584 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
587 /* Record where this auth realm is used */
588 realm = g_strdup_printf ("%s:%s",
589 soup_auth_get_scheme_name (new_auth),
590 soup_auth_get_realm (new_auth));
593 * RFC 2617 is somewhat unclear about the scope of protection
594 * spaces with regard to proxies. The only mention of it is
595 * as an aside in section 3.2.1, where it is defining the fields
596 * of a Digest challenge and says that the protection space is
597 * always the entire proxy. Is this the case for all authentication
598 * schemes or just Digest? Who knows, but we're assuming all.
601 pspace = g_slist_prepend (NULL, g_strdup (""));
603 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
605 for (p = pspace; p; p = p->next) {
607 if (g_hash_table_lookup_extended (host->auth_realms, path,
608 &old_path, &old_realm)) {
609 g_hash_table_remove (host->auth_realms, old_path);
614 g_hash_table_insert (host->auth_realms,
615 g_strdup (path), g_strdup (realm));
617 soup_auth_free_protection_space (new_auth, pspace);
619 /* Now, make sure the auth is recorded. (If there's a
620 * pre-existing auth, we keep that rather than the new one,
621 * since the old one might already be authenticated.)
623 old_auth = g_hash_table_lookup (host->auths, realm);
626 g_object_unref (new_auth);
629 g_hash_table_insert (host->auths, realm, new_auth);
631 /* If we need to authenticate, try to do it. */
632 if (!soup_auth_is_authenticated (new_auth)) {
633 return authenticate_auth (session, new_auth,
634 msg, prior_auth_failed, proxy);
637 /* Otherwise we're good. */
642 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
643 const char *auth_type, const char *auth_realm,
644 char **username, char **password, gpointer session)
646 g_signal_emit (session, signals[AUTHENTICATE], 0,
647 msg, auth_type, auth_realm, username, password);
651 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
652 const char *auth_type, const char *auth_realm,
653 char **username, char **password,
656 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
657 msg, auth_type, auth_realm, username, password);
662 authorize_handler (SoupMessage *msg, gpointer user_data)
664 SoupSession *session = user_data;
665 const GSList *headers;
668 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
669 headers = soup_message_get_header_list (msg->response_headers,
670 "Proxy-Authenticate");
673 headers = soup_message_get_header_list (msg->response_headers,
680 if (update_auth_internal (session, msg, headers, proxy, TRUE))
681 soup_session_requeue_message (session, msg);
685 redirect_handler (SoupMessage *msg, gpointer user_data)
687 SoupSession *session = user_data;
691 new_loc = soup_message_get_header (msg->response_headers, "Location");
694 new_uri = soup_uri_new (new_loc);
696 soup_message_set_status_full (msg,
697 SOUP_STATUS_MALFORMED,
698 "Invalid Redirect URL");
702 soup_message_set_uri (msg, new_uri);
703 soup_uri_free (new_uri);
705 soup_session_requeue_message (session, msg);
709 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
711 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
715 soup_message_remove_header (msg->request_headers, header);
717 auth = lookup_auth (session, msg, proxy);
720 if (!soup_auth_is_authenticated (auth) &&
721 !authenticate_auth (session, auth, msg, FALSE, proxy))
724 token = soup_auth_get_authorization (auth, msg);
726 soup_message_add_header (msg->request_headers, header, token);
732 soup_session_send_message_via (SoupSession *session, SoupMessage *msg,
733 SoupConnection *conn)
735 msg->status = SOUP_MESSAGE_STATUS_RUNNING;
737 add_auth (session, msg, FALSE);
738 if (session->priv->proxy_uri)
739 add_auth (session, msg, TRUE);
740 soup_connection_send_request (conn, msg);
744 find_oldest_connection (gpointer key, gpointer host, gpointer data)
746 SoupConnection *conn = key, **oldest = data;
748 /* Don't prune a connection that is currently in use, or
749 * hasn't been used yet.
751 if (soup_connection_is_in_use (conn) ||
752 soup_connection_last_used (conn) == 0)
755 if (!*oldest || (soup_connection_last_used (conn) <
756 soup_connection_last_used (*oldest)))
761 * soup_session_try_prune_connection:
762 * @session: a #SoupSession
764 * Finds the least-recently-used idle connection in @session and closes
767 * Return value: %TRUE if a connection was closed, %FALSE if there are
768 * no idle connections.
771 soup_session_try_prune_connection (SoupSession *session)
773 SoupConnection *oldest = NULL;
775 g_mutex_lock (session->priv->host_lock);
776 g_hash_table_foreach (session->priv->conns, find_oldest_connection,
779 /* Ref the connection before unlocking the mutex in
780 * case someone else tries to prune it too.
782 g_object_ref (oldest);
783 g_mutex_unlock (session->priv->host_lock);
784 soup_connection_disconnect (oldest);
785 g_object_unref (oldest);
788 g_mutex_unlock (session->priv->host_lock);
794 connection_disconnected (SoupConnection *conn, gpointer user_data)
796 SoupSession *session = user_data;
797 SoupSessionHost *host;
799 g_mutex_lock (session->priv->host_lock);
801 host = g_hash_table_lookup (session->priv->conns, conn);
803 g_hash_table_remove (session->priv->conns, conn);
804 host->connections = g_slist_remove (host->connections, conn);
808 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
809 session->priv->num_conns--;
811 g_mutex_unlock (session->priv->host_lock);
812 g_object_unref (conn);
816 connect_result (SoupConnection *conn, guint status, gpointer user_data)
818 SoupSession *session = user_data;
819 SoupSessionHost *host;
820 SoupMessageQueueIter iter;
823 g_mutex_lock (session->priv->host_lock);
825 host = g_hash_table_lookup (session->priv->conns, conn);
827 g_mutex_unlock (session->priv->host_lock);
831 if (status == SOUP_STATUS_OK) {
832 host->connections = g_slist_prepend (host->connections, conn);
833 g_mutex_unlock (session->priv->host_lock);
837 /* The connection failed. */
838 g_mutex_unlock (session->priv->host_lock);
839 connection_disconnected (conn, session);
841 if (host->connections) {
842 /* Something went wrong this time, but we have at
843 * least one open connection to this host. So just
844 * leave the message in the queue so it can use that
845 * connection once it's free.
850 /* It's hopeless. Cancel everything that was waiting for this host. */
851 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
852 if (get_host_for_message (session, msg) == host) {
853 soup_message_set_status (msg, status);
854 soup_session_cancel_message (session, msg);
860 * soup_session_get_connection:
861 * @session: a #SoupSession
862 * @msg: a #SoupMessage
863 * @try_pruning: on return, whether or not to try pruning a connection
864 * @is_new: on return, %TRUE if the returned connection is new and not
867 * Tries to find or create a connection for @msg. If there is an idle
868 * connection to the relevant host available, then it will be returned
869 * (with *@is_new set to %FALSE). Otherwise, if it is possible to
870 * create a new connection, one will be created and returned, with
871 * *@is_new set to %TRUE.
873 * If no connection can be made, it will return %NULL. If @session has
874 * the maximum number of open connections open, but does not have the
875 * maximum number of per-host connections open to the relevant host, then
876 * *@try_pruning will be set to %TRUE. In this case, the caller can
877 * call soup_session_try_prune_connection() to close an idle connection,
878 * and then try soup_session_get_connection() again. (If calling
879 * soup_session_try_prune_connection() wouldn't help, then *@try_pruning
880 * is left untouched; it is NOT set to %FALSE.)
882 * Return value: a #SoupConnection, or %NULL
885 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
886 gboolean *try_pruning, gboolean *is_new)
888 SoupConnection *conn;
889 SoupSessionHost *host;
892 g_mutex_lock (session->priv->host_lock);
894 host = get_host_for_message (session, msg);
895 for (conns = host->connections; conns; conns = conns->next) {
896 if (!soup_connection_is_in_use (conns->data)) {
897 soup_connection_reserve (conns->data);
898 g_mutex_unlock (session->priv->host_lock);
904 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
905 /* We already started a connection for this
906 * message, so don't start another one.
908 g_mutex_unlock (session->priv->host_lock);
912 if (host->num_conns >= session->priv->max_conns_per_host) {
913 g_mutex_unlock (session->priv->host_lock);
917 if (session->priv->num_conns >= session->priv->max_conns) {
919 g_mutex_unlock (session->priv->host_lock);
923 /* Make sure session->priv->proxy_host gets set now while
924 * we have the host_lock.
926 if (session->priv->proxy_uri)
927 get_proxy_host (session);
929 conn = g_object_new (
930 (session->priv->use_ntlm ?
931 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
932 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
933 SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
934 SOUP_CONNECTION_SSL_CREDENTIALS, session->priv->ssl_creds,
936 g_signal_connect (conn, "connect_result",
937 G_CALLBACK (connect_result),
939 g_signal_connect (conn, "disconnected",
940 G_CALLBACK (connection_disconnected),
942 g_signal_connect (conn, "authenticate",
943 G_CALLBACK (connection_authenticate),
945 g_signal_connect (conn, "reauthenticate",
946 G_CALLBACK (connection_reauthenticate),
949 g_hash_table_insert (session->priv->conns, conn, host);
951 /* We increment the connection counts so it counts against the
952 * totals, but we don't add it to the host's connection list
953 * yet, since it's not ready for use.
955 session->priv->num_conns++;
958 /* Mark the request as connecting, so we don't try to open
959 * another new connection for it while waiting for this one.
961 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
963 g_mutex_unlock (session->priv->host_lock);
969 message_finished (SoupMessage *msg, gpointer user_data)
971 SoupSession *session = user_data;
973 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
974 soup_message_queue_remove_message (session->queue, msg);
975 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
980 queue_message (SoupSession *session, SoupMessage *msg,
981 SoupMessageCallbackFn callback, gpointer user_data)
983 g_signal_connect_after (msg, "finished",
984 G_CALLBACK (message_finished), session);
986 soup_message_add_status_code_handler (msg, SOUP_STATUS_UNAUTHORIZED,
987 SOUP_HANDLER_POST_BODY,
988 authorize_handler, session);
989 soup_message_add_status_code_handler (msg,
990 SOUP_STATUS_PROXY_UNAUTHORIZED,
991 SOUP_HANDLER_POST_BODY,
992 authorize_handler, session);
994 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
995 soup_message_add_status_class_handler (
996 msg, SOUP_STATUS_CLASS_REDIRECT,
997 SOUP_HANDLER_POST_BODY,
998 redirect_handler, session);
1001 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1002 soup_message_queue_append (session->queue, msg);
1006 * soup_session_queue_message:
1007 * @session: a #SoupSession
1008 * @msg: the message to queue
1009 * @callback: a #SoupMessageCallbackFn which will be called after the
1010 * message completes or when an unrecoverable error occurs.
1011 * @user_data: a pointer passed to @callback.
1013 * Queues the message @msg for sending. All messages are processed
1014 * while the glib main loop runs. If @msg has been processed before,
1015 * any resources related to the time it was last sent are freed.
1017 * Upon message completion, the callback specified in @callback will
1018 * be invoked. If after returning from this callback the message has
1019 * not been requeued, @msg will be unreffed.
1022 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1023 SoupMessageCallbackFn callback, gpointer user_data)
1025 g_return_if_fail (SOUP_IS_SESSION (session));
1026 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1028 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1029 callback, user_data);
1033 requeue_message (SoupSession *session, SoupMessage *msg)
1035 msg->status = SOUP_MESSAGE_STATUS_QUEUED;
1039 * soup_session_requeue_message:
1040 * @session: a #SoupSession
1041 * @msg: the message to requeue
1043 * This causes @msg to be placed back on the queue to be attempted
1047 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1049 g_return_if_fail (SOUP_IS_SESSION (session));
1050 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1052 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1057 * soup_session_send_message:
1058 * @session: a #SoupSession
1059 * @msg: the message to send
1061 * Synchronously send @msg. This call will not return until the
1062 * transfer is finished successfully or there is an unrecoverable
1065 * @msg is not freed upon return.
1067 * Return value: the HTTP status code of the response
1070 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1072 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1073 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1075 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1080 cancel_message (SoupSession *session, SoupMessage *msg)
1082 soup_message_queue_remove_message (session->queue, msg);
1083 soup_message_finished (msg);
1087 * soup_session_cancel_message:
1088 * @session: a #SoupSession
1089 * @msg: the message to cancel
1091 * Causes @session to immediately finish processing @msg. You should
1092 * set a status code on @msg with soup_message_set_status() before
1093 * calling this function.
1096 soup_session_cancel_message (SoupSession *session, SoupMessage *msg)
1098 g_return_if_fail (SOUP_IS_SESSION (session));
1099 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1101 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg);
1105 * soup_session_abort:
1106 * @session: the session
1108 * Cancels all pending requests in @session.
1111 soup_session_abort (SoupSession *session)
1113 SoupMessageQueueIter iter;
1116 g_return_if_fail (SOUP_IS_SESSION (session));
1118 for (msg = soup_message_queue_first (session->queue, &iter); msg; msg = soup_message_queue_next (session->queue, &iter)) {
1119 soup_message_set_status (msg, SOUP_STATUS_CANCELLED);
1120 soup_session_cancel_message (session, msg);