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-auth-basic.h"
18 #include "soup-auth-digest.h"
19 #include "soup-auth-manager.h"
20 #include "soup-connection.h"
21 #include "soup-connection-ntlm.h"
22 #include "soup-marshal.h"
23 #include "soup-message-private.h"
24 #include "soup-message-queue.h"
25 #include "soup-session.h"
26 #include "soup-session-private.h"
27 #include "soup-socket.h"
32 * SECTION:soup-session
33 * @short_description: Soup session state object
40 GSList *connections; /* CONTAINS: SoupConnection */
43 GHashTable *auth_realms; /* path -> scheme:realm */
44 GHashTable *auths; /* scheme:realm -> SoupAuth */
51 guint max_conns, max_conns_per_host;
55 SoupSSLCredentials *ssl_creds;
57 SoupMessageQueue *queue;
59 SoupAuthManager *auth_manager;
61 GHashTable *hosts; /* SoupURI -> SoupSessionHost */
62 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
65 /* Must hold the host_lock before potentially creating a
66 * new SoupSessionHost, or adding/removing a connection.
67 * Must not emit signals or destroy objects while holding it.
71 GMainContext *async_context;
73 /* Holds the timeout value for the connection, when
74 no response is received.
78 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
80 static void free_host (SoupSessionHost *host);
82 static void queue_message (SoupSession *session, SoupMessage *msg,
83 SoupSessionCallback callback, gpointer user_data);
84 static void requeue_message (SoupSession *session, SoupMessage *msg);
85 static void cancel_message (SoupSession *session, SoupMessage *msg,
88 /* temporary until we fix this to index hosts by SoupAddress */
89 extern guint soup_uri_host_hash (gconstpointer key);
90 extern gboolean soup_uri_host_equal (gconstpointer v1,
92 extern SoupURI *soup_uri_copy_root (SoupURI *uri);
94 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
95 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
97 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
105 static guint signals[LAST_SIGNAL] = { 0 };
112 PROP_MAX_CONNS_PER_HOST,
121 static void set_property (GObject *object, guint prop_id,
122 const GValue *value, GParamSpec *pspec);
123 static void get_property (GObject *object, guint prop_id,
124 GValue *value, GParamSpec *pspec);
127 soup_session_init (SoupSession *session)
129 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
131 priv->queue = soup_message_queue_new ();
133 priv->host_lock = g_mutex_new ();
134 priv->hosts = g_hash_table_new (soup_uri_host_hash,
135 soup_uri_host_equal);
136 priv->conns = g_hash_table_new (NULL, NULL);
138 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
139 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
143 priv->auth_manager = soup_auth_manager_new (session);
144 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
145 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
149 foreach_free_host (gpointer key, gpointer host, gpointer data)
156 cleanup_hosts (SoupSessionPrivate *priv)
158 GHashTable *old_hosts;
160 g_mutex_lock (priv->host_lock);
161 old_hosts = priv->hosts;
162 priv->hosts = g_hash_table_new (soup_uri_host_hash,
163 soup_uri_host_equal);
164 g_mutex_unlock (priv->host_lock);
166 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
167 g_hash_table_destroy (old_hosts);
171 dispose (GObject *object)
173 SoupSession *session = SOUP_SESSION (object);
174 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
176 soup_session_abort (session);
177 cleanup_hosts (priv);
179 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
183 finalize (GObject *object)
185 SoupSession *session = SOUP_SESSION (object);
186 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
188 soup_message_queue_destroy (priv->queue);
190 g_mutex_free (priv->host_lock);
191 g_hash_table_destroy (priv->hosts);
192 g_hash_table_destroy (priv->conns);
194 soup_auth_manager_free (priv->auth_manager);
197 soup_uri_free (priv->proxy_uri);
200 soup_ssl_free_client_credentials (priv->ssl_creds);
202 if (priv->async_context)
203 g_main_context_unref (priv->async_context);
205 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
209 soup_session_class_init (SoupSessionClass *session_class)
211 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
213 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
215 /* virtual method definition */
216 session_class->queue_message = queue_message;
217 session_class->requeue_message = requeue_message;
218 session_class->cancel_message = cancel_message;
220 /* virtual method override */
221 object_class->dispose = dispose;
222 object_class->finalize = finalize;
223 object_class->set_property = set_property;
224 object_class->get_property = get_property;
229 * SoupSession::request-started:
230 * @session: the session
231 * @msg: the request being sent
232 * @socket: the socket the request is being sent on
234 * Emitted just before a request is sent.
236 signals[REQUEST_STARTED] =
237 g_signal_new ("request-started",
238 G_OBJECT_CLASS_TYPE (object_class),
240 G_STRUCT_OFFSET (SoupSessionClass, request_started),
242 soup_marshal_NONE__OBJECT_OBJECT,
248 * SoupSession::authenticate:
249 * @session: the session
250 * @msg: the #SoupMessage being sent
251 * @auth: the #SoupAuth to authenticate
252 * @retrying: %TRUE if this is the second (or later) attempt
254 * Emitted when the session requires authentication. If
255 * credentials are available call soup_auth_authenticate() on
256 * @auth. If these credentials fail, the signal will be
257 * emitted again, with @retrying set to %TRUE, which will
258 * continue until you return without calling
259 * soup_auth_authenticate() on @auth.
261 signals[AUTHENTICATE] =
262 g_signal_new ("authenticate",
263 G_OBJECT_CLASS_TYPE (object_class),
265 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
267 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
274 g_object_class_install_property (
275 object_class, PROP_PROXY_URI,
276 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
278 "The HTTP Proxy to use for this session",
281 g_object_class_install_property (
282 object_class, PROP_MAX_CONNS,
283 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
284 "Max Connection Count",
285 "The maximum number of connections that the session can open at once",
290 g_object_class_install_property (
291 object_class, PROP_MAX_CONNS_PER_HOST,
292 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
293 "Max Per-Host Connection Count",
294 "The maximum number of connections that the session can open at once to a given host",
299 g_object_class_install_property (
300 object_class, PROP_USE_NTLM,
301 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
303 "Whether or not to use NTLM authentication",
306 g_object_class_install_property (
307 object_class, PROP_SSL_CA_FILE,
308 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
310 "File containing SSL CA certificates",
313 g_object_class_install_property (
314 object_class, PROP_ASYNC_CONTEXT,
315 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
316 "Async GMainContext",
317 "The GMainContext to dispatch async I/O in",
318 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
319 g_object_class_install_property (
320 object_class, PROP_TIMEOUT,
321 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
323 "Value in seconds to timeout a blocking I/O",
329 safe_uri_equal (SoupURI *a, SoupURI *b)
334 if ((a && !b) || (b && !a))
337 return soup_uri_equal (a, b);
341 safe_str_equal (const char *a, const char *b)
346 if ((a && !b) || (b && !a))
349 return strcmp (a, b) == 0;
353 set_property (GObject *object, guint prop_id,
354 const GValue *value, GParamSpec *pspec)
356 SoupSession *session = SOUP_SESSION (object);
357 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
359 gboolean need_abort = FALSE;
360 gboolean ca_file_changed = FALSE;
361 const char *new_ca_file;
365 uri = g_value_get_boxed (value);
367 if (!safe_uri_equal (priv->proxy_uri, uri))
371 soup_uri_free (priv->proxy_uri);
373 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
376 soup_session_abort (session);
377 cleanup_hosts (priv);
382 priv->max_conns = g_value_get_int (value);
384 case PROP_MAX_CONNS_PER_HOST:
385 priv->max_conns_per_host = g_value_get_int (value);
388 priv->use_ntlm = g_value_get_boolean (value);
390 case PROP_SSL_CA_FILE:
391 new_ca_file = g_value_get_string (value);
393 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
394 ca_file_changed = TRUE;
396 g_free (priv->ssl_ca_file);
397 priv->ssl_ca_file = g_strdup (new_ca_file);
399 if (ca_file_changed) {
400 if (priv->ssl_creds) {
401 soup_ssl_free_client_credentials (priv->ssl_creds);
402 priv->ssl_creds = NULL;
405 cleanup_hosts (priv);
409 case PROP_ASYNC_CONTEXT:
410 priv->async_context = g_value_get_pointer (value);
411 if (priv->async_context)
412 g_main_context_ref (priv->async_context);
415 priv->timeout = g_value_get_uint (value);
423 get_property (GObject *object, guint prop_id,
424 GValue *value, GParamSpec *pspec)
426 SoupSession *session = SOUP_SESSION (object);
427 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
431 g_value_set_boxed (value, priv->proxy_uri);
434 g_value_set_int (value, priv->max_conns);
436 case PROP_MAX_CONNS_PER_HOST:
437 g_value_set_int (value, priv->max_conns_per_host);
440 g_value_set_boolean (value, priv->use_ntlm);
442 case PROP_SSL_CA_FILE:
443 g_value_set_string (value, priv->ssl_ca_file);
445 case PROP_ASYNC_CONTEXT:
446 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
449 g_value_set_uint (value, priv->timeout);
458 * soup_session_get_async_context:
459 * @session: a #SoupSession
461 * Gets @session's async_context. This does not add a ref to the
462 * context, so you will need to ref it yourself if you want it to
463 * outlive its session.
465 * Return value: @session's #GMainContext, which may be %NULL
468 soup_session_get_async_context (SoupSession *session)
470 SoupSessionPrivate *priv;
472 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
473 priv = SOUP_SESSION_GET_PRIVATE (session);
475 return priv->async_context;
480 static SoupSessionHost *
481 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
483 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
484 SoupSessionHost *host;
486 host = g_slice_new0 (SoupSessionHost);
487 host->root_uri = soup_uri_copy_root (source_uri);
489 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
492 soup_ssl_get_client_credentials (priv->ssl_ca_file);
498 /* Note: get_host_for_message doesn't lock the host_lock. The caller
499 * must do it itself if there's a chance the host doesn't already
502 static SoupSessionHost *
503 get_host_for_message (SoupSession *session, SoupMessage *msg)
505 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
506 SoupSessionHost *host;
507 SoupURI *source = soup_message_get_uri (msg);
509 host = g_hash_table_lookup (priv->hosts, source);
513 host = soup_session_host_new (session, source);
514 g_hash_table_insert (priv->hosts, host->root_uri, host);
520 free_host (SoupSessionHost *host)
522 while (host->connections) {
523 SoupConnection *conn = host->connections->data;
525 host->connections = g_slist_remove (host->connections, conn);
526 soup_connection_disconnect (conn);
529 soup_uri_free (host->root_uri);
530 g_slice_free (SoupSessionHost, host);
534 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
535 SoupAuth *auth, gboolean retrying)
537 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
541 reemit_authenticate (SoupConnection *conn, SoupMessage *msg,
542 SoupAuth *auth, gboolean retrying, gpointer session)
544 soup_session_emit_authenticate (session, msg, auth, retrying);
548 redirect_handler (SoupMessage *msg, gpointer user_data)
550 SoupSession *session = user_data;
554 if (!SOUP_STATUS_IS_REDIRECTION (msg->status_code))
557 new_loc = soup_message_headers_get (msg->response_headers, "Location");
561 /* Location is supposed to be an absolute URI, but some sites
562 * are lame, so we use soup_uri_new_with_base().
564 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
566 soup_message_set_status_full (msg,
567 SOUP_STATUS_MALFORMED,
568 "Invalid Redirect URL");
572 soup_message_set_uri (msg, new_uri);
573 soup_uri_free (new_uri);
575 soup_session_requeue_message (session, msg);
579 connection_started_request (SoupConnection *conn, SoupMessage *msg,
582 SoupSession *session = data;
584 g_signal_emit (session, signals[REQUEST_STARTED], 0,
585 msg, soup_connection_get_socket (conn));
589 find_oldest_connection (gpointer key, gpointer host, gpointer data)
591 SoupConnection *conn = key, **oldest = data;
593 /* Don't prune a connection that is currently in use, or
594 * hasn't been used yet.
596 if (soup_connection_is_in_use (conn) ||
597 soup_connection_last_used (conn) == 0)
600 if (!*oldest || (soup_connection_last_used (conn) <
601 soup_connection_last_used (*oldest)))
606 * soup_session_try_prune_connection:
607 * @session: a #SoupSession
609 * Finds the least-recently-used idle connection in @session and closes
612 * Return value: %TRUE if a connection was closed, %FALSE if there are
613 * no idle connections.
616 soup_session_try_prune_connection (SoupSession *session)
618 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
619 SoupConnection *oldest = NULL;
621 g_mutex_lock (priv->host_lock);
622 g_hash_table_foreach (priv->conns, find_oldest_connection,
625 /* Ref the connection before unlocking the mutex in
626 * case someone else tries to prune it too.
628 g_object_ref (oldest);
629 g_mutex_unlock (priv->host_lock);
630 soup_connection_disconnect (oldest);
631 g_object_unref (oldest);
634 g_mutex_unlock (priv->host_lock);
640 connection_disconnected (SoupConnection *conn, gpointer user_data)
642 SoupSession *session = user_data;
643 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
644 SoupSessionHost *host;
646 g_mutex_lock (priv->host_lock);
648 host = g_hash_table_lookup (priv->conns, conn);
650 g_hash_table_remove (priv->conns, conn);
651 host->connections = g_slist_remove (host->connections, conn);
655 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
658 g_mutex_unlock (priv->host_lock);
659 g_object_unref (conn);
663 connect_result (SoupConnection *conn, guint status, gpointer user_data)
665 SoupSession *session = user_data;
666 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
667 SoupSessionHost *host;
668 SoupMessageQueueIter iter;
671 g_mutex_lock (priv->host_lock);
673 host = g_hash_table_lookup (priv->conns, conn);
675 g_mutex_unlock (priv->host_lock);
679 if (status == SOUP_STATUS_OK) {
680 soup_connection_reserve (conn);
681 host->connections = g_slist_prepend (host->connections, conn);
682 g_mutex_unlock (priv->host_lock);
686 /* The connection failed. */
687 g_mutex_unlock (priv->host_lock);
688 connection_disconnected (conn, session);
690 if (host->connections) {
691 /* Something went wrong this time, but we have at
692 * least one open connection to this host. So just
693 * leave the message in the queue so it can use that
694 * connection once it's free.
699 /* There are two possibilities: either status is
700 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
701 * will create a new connection (and all we need to do here
702 * is downgrade the message from CONNECTING to QUEUED); or
703 * status is something else, probably CANT_CONNECT or
704 * CANT_RESOLVE or the like, in which case we need to cancel
705 * any messages waiting for this host, since they're out
708 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
709 if (get_host_for_message (session, msg) == host) {
710 if (status == SOUP_STATUS_TRY_AGAIN) {
711 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
712 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
714 soup_session_cancel_message (session, msg,
722 * soup_session_get_connection:
723 * @session: a #SoupSession
724 * @msg: a #SoupMessage
725 * @try_pruning: on return, whether or not to try pruning a connection
726 * @is_new: on return, %TRUE if the returned connection is new and not
729 * Tries to find or create a connection for @msg; this is an internal
730 * method for #SoupSession subclasses.
732 * If there is an idle connection to the relevant host available, then
733 * that connection will be returned (with *@is_new set to %FALSE). The
734 * connection will be marked "reserved", so the caller must call
735 * soup_connection_release() if it ends up not using the connection
738 * If there is no idle connection available, but it is possible to
739 * create a new connection, then one will be created and returned,
740 * with *@is_new set to %TRUE. The caller MUST then call
741 * soup_connection_connect_sync() or soup_connection_connect_async()
742 * to connect it. If the connection attempt succeeds, the connection
743 * will be marked "reserved" and added to @session's connection pool
744 * once it connects. If the connection attempt fails, the connection
747 * If no connection is available and a new connection cannot be made,
748 * soup_session_get_connection() will return %NULL. If @session has
749 * the maximum number of open connections open, but does not have the
750 * maximum number of per-host connections open to the relevant host,
751 * then *@try_pruning will be set to %TRUE. In this case, the caller
752 * can call soup_session_try_prune_connection() to close an idle
753 * connection, and then try soup_session_get_connection() again. (If
754 * calling soup_session_try_prune_connection() wouldn't help, then
755 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
757 * Return value: a #SoupConnection, or %NULL
760 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
761 gboolean *try_pruning, gboolean *is_new)
763 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
764 SoupConnection *conn;
765 SoupSessionHost *host;
768 g_mutex_lock (priv->host_lock);
770 host = get_host_for_message (session, msg);
771 for (conns = host->connections; conns; conns = conns->next) {
772 if (!soup_connection_is_in_use (conns->data)) {
773 soup_connection_reserve (conns->data);
774 g_mutex_unlock (priv->host_lock);
780 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
781 /* We already started a connection for this
782 * message, so don't start another one.
784 g_mutex_unlock (priv->host_lock);
788 if (host->num_conns >= priv->max_conns_per_host) {
789 g_mutex_unlock (priv->host_lock);
793 if (priv->num_conns >= priv->max_conns) {
795 g_mutex_unlock (priv->host_lock);
799 conn = g_object_new (
801 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
802 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
803 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
804 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
805 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
806 SOUP_CONNECTION_TIMEOUT, priv->timeout,
808 g_signal_connect (conn, "connect_result",
809 G_CALLBACK (connect_result),
811 g_signal_connect (conn, "disconnected",
812 G_CALLBACK (connection_disconnected),
814 g_signal_connect (conn, "request_started",
815 G_CALLBACK (connection_started_request),
817 g_signal_connect (conn, "authenticate",
818 G_CALLBACK (reemit_authenticate),
821 g_hash_table_insert (priv->conns, conn, host);
823 /* We increment the connection counts so it counts against the
824 * totals, but we don't add it to the host's connection list
825 * yet, since it's not ready for use.
830 /* Mark the request as connecting, so we don't try to open
831 * another new connection for it while waiting for this one.
833 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
835 g_mutex_unlock (priv->host_lock);
841 soup_session_get_queue (SoupSession *session)
843 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
849 message_finished (SoupMessage *msg, gpointer user_data)
851 SoupSession *session = user_data;
852 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
854 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
855 soup_message_queue_remove_message (priv->queue, msg);
856 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
861 queue_message (SoupSession *session, SoupMessage *msg,
862 SoupSessionCallback callback, gpointer user_data)
864 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
866 g_signal_connect_after (msg, "finished",
867 G_CALLBACK (message_finished), session);
869 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
870 soup_message_add_header_handler (
871 msg, "got_body", "Location",
872 G_CALLBACK (redirect_handler), session);
875 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
876 soup_message_queue_append (priv->queue, msg);
880 * soup_session_queue_message:
881 * @session: a #SoupSession
882 * @msg: the message to queue
883 * @callback: a #SoupSessionCallback which will be called after the
884 * message completes or when an unrecoverable error occurs.
885 * @user_data: a pointer passed to @callback.
887 * Queues the message @msg for sending. All messages are processed
888 * while the glib main loop runs. If @msg has been processed before,
889 * any resources related to the time it was last sent are freed.
891 * Upon message completion, the callback specified in @callback will
892 * be invoked (in the thread associated with @session's async
893 * context). If after returning from this callback the message has not
894 * been requeued, @msg will be unreffed.
897 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
898 SoupSessionCallback callback, gpointer user_data)
900 g_return_if_fail (SOUP_IS_SESSION (session));
901 g_return_if_fail (SOUP_IS_MESSAGE (msg));
903 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
904 callback, user_data);
908 requeue_message (SoupSession *session, SoupMessage *msg)
910 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
914 * soup_session_requeue_message:
915 * @session: a #SoupSession
916 * @msg: the message to requeue
918 * This causes @msg to be placed back on the queue to be attempted
922 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
924 g_return_if_fail (SOUP_IS_SESSION (session));
925 g_return_if_fail (SOUP_IS_MESSAGE (msg));
927 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
932 * soup_session_send_message:
933 * @session: a #SoupSession
934 * @msg: the message to send
936 * Synchronously send @msg. This call will not return until the
937 * transfer is finished successfully or there is an unrecoverable
940 * @msg is not freed upon return.
942 * Return value: the HTTP status code of the response
945 soup_session_send_message (SoupSession *session, SoupMessage *msg)
947 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
948 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
950 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
955 * soup_session_pause_message:
956 * @session: a #SoupSession
957 * @msg: a #SoupMessage currently running on @session
959 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
963 soup_session_pause_message (SoupSession *session,
966 g_return_if_fail (SOUP_IS_SESSION (session));
967 g_return_if_fail (SOUP_IS_MESSAGE (msg));
969 soup_message_io_pause (msg);
973 * soup_session_unpause_message:
974 * @session: a #SoupSession
975 * @msg: a #SoupMessage currently running on @session
977 * Resumes HTTP I/O on @msg. Use this to resume after calling
978 * soup_sessino_pause_message().
980 * If @msg is being sent via blocking I/O, this will resume reading or
981 * writing immediately. If @msg is using non-blocking I/O, then
982 * reading or writing won't resume until you return to the main loop.
985 soup_session_unpause_message (SoupSession *session,
988 g_return_if_fail (SOUP_IS_SESSION (session));
989 g_return_if_fail (SOUP_IS_MESSAGE (msg));
991 soup_message_io_unpause (msg);
996 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
998 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1000 soup_message_queue_remove_message (priv->queue, msg);
1001 soup_message_io_stop (msg);
1002 soup_message_set_status (msg, status_code);
1003 soup_message_finished (msg);
1007 * soup_session_cancel_message:
1008 * @session: a #SoupSession
1009 * @msg: the message to cancel
1010 * @status_code: status code to set on @msg (generally
1011 * %SOUP_STATUS_CANCELLED)
1013 * Causes @session to immediately finish processing @msg, with a final
1014 * status_code of @status_code. Depending on when you cancel it, the
1015 * response state may be incomplete or inconsistent.
1018 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1021 g_return_if_fail (SOUP_IS_SESSION (session));
1022 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1024 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1028 gather_conns (gpointer key, gpointer host, gpointer data)
1030 SoupConnection *conn = key;
1031 GSList **conns = data;
1033 *conns = g_slist_prepend (*conns, conn);
1037 * soup_session_abort:
1038 * @session: the session
1040 * Cancels all pending requests in @session.
1043 soup_session_abort (SoupSession *session)
1045 SoupSessionPrivate *priv;
1046 SoupMessageQueueIter iter;
1050 g_return_if_fail (SOUP_IS_SESSION (session));
1051 priv = SOUP_SESSION_GET_PRIVATE (session);
1053 for (msg = soup_message_queue_first (priv->queue, &iter);
1055 msg = soup_message_queue_next (priv->queue, &iter)) {
1056 soup_session_cancel_message (session, msg,
1057 SOUP_STATUS_CANCELLED);
1060 /* Close all connections */
1061 g_mutex_lock (priv->host_lock);
1063 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1065 for (c = conns; c; c = c->next)
1066 g_object_ref (c->data);
1067 g_mutex_unlock (priv->host_lock);
1068 for (c = conns; c; c = c->next) {
1069 soup_connection_disconnect (c->data);
1070 g_object_unref (c->data);
1073 g_slist_free (conns);