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-auth-manager-ntlm.h"
21 #include "soup-connection.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;
54 SoupSSLCredentials *ssl_creds;
56 SoupMessageQueue *queue;
58 SoupAuthManager *auth_manager;
59 SoupAuthManagerNTLM *ntlm_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);
195 if (priv->ntlm_manager)
196 soup_auth_manager_ntlm_free (priv->ntlm_manager);
199 soup_uri_free (priv->proxy_uri);
202 soup_ssl_free_client_credentials (priv->ssl_creds);
204 if (priv->async_context)
205 g_main_context_unref (priv->async_context);
207 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
211 soup_session_class_init (SoupSessionClass *session_class)
213 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
215 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
217 /* virtual method definition */
218 session_class->queue_message = queue_message;
219 session_class->requeue_message = requeue_message;
220 session_class->cancel_message = cancel_message;
222 /* virtual method override */
223 object_class->dispose = dispose;
224 object_class->finalize = finalize;
225 object_class->set_property = set_property;
226 object_class->get_property = get_property;
231 * SoupSession::request-started:
232 * @session: the session
233 * @msg: the request being sent
234 * @socket: the socket the request is being sent on
236 * Emitted just before a request is sent.
238 signals[REQUEST_STARTED] =
239 g_signal_new ("request-started",
240 G_OBJECT_CLASS_TYPE (object_class),
242 G_STRUCT_OFFSET (SoupSessionClass, request_started),
244 soup_marshal_NONE__OBJECT_OBJECT,
250 * SoupSession::authenticate:
251 * @session: the session
252 * @msg: the #SoupMessage being sent
253 * @auth: the #SoupAuth to authenticate
254 * @retrying: %TRUE if this is the second (or later) attempt
256 * Emitted when the session requires authentication. If
257 * credentials are available call soup_auth_authenticate() on
258 * @auth. If these credentials fail, the signal will be
259 * emitted again, with @retrying set to %TRUE, which will
260 * continue until you return without calling
261 * soup_auth_authenticate() on @auth.
263 * Note that this may be emitted before @msg's body has been
266 * If you call soup_session_pause_message() on @msg before
267 * returning, then you can authenticate @auth asynchronously
268 * (as long as you g_object_ref() it to make sure it doesn't
269 * get destroyed), and then unpause @msg when you are ready
270 * for it to continue.
272 signals[AUTHENTICATE] =
273 g_signal_new ("authenticate",
274 G_OBJECT_CLASS_TYPE (object_class),
276 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
278 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
285 g_object_class_install_property (
286 object_class, PROP_PROXY_URI,
287 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
289 "The HTTP Proxy to use for this session",
292 g_object_class_install_property (
293 object_class, PROP_MAX_CONNS,
294 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
295 "Max Connection Count",
296 "The maximum number of connections that the session can open at once",
301 g_object_class_install_property (
302 object_class, PROP_MAX_CONNS_PER_HOST,
303 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
304 "Max Per-Host Connection Count",
305 "The maximum number of connections that the session can open at once to a given host",
310 g_object_class_install_property (
311 object_class, PROP_USE_NTLM,
312 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
314 "Whether or not to use NTLM authentication",
317 g_object_class_install_property (
318 object_class, PROP_SSL_CA_FILE,
319 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
321 "File containing SSL CA certificates",
324 g_object_class_install_property (
325 object_class, PROP_ASYNC_CONTEXT,
326 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
327 "Async GMainContext",
328 "The GMainContext to dispatch async I/O in",
329 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
330 g_object_class_install_property (
331 object_class, PROP_TIMEOUT,
332 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
334 "Value in seconds to timeout a blocking I/O",
340 safe_uri_equal (SoupURI *a, SoupURI *b)
345 if ((a && !b) || (b && !a))
348 return soup_uri_equal (a, b);
352 safe_str_equal (const char *a, const char *b)
357 if ((a && !b) || (b && !a))
360 return strcmp (a, b) == 0;
364 set_property (GObject *object, guint prop_id,
365 const GValue *value, GParamSpec *pspec)
367 SoupSession *session = SOUP_SESSION (object);
368 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
370 gboolean need_abort = FALSE;
371 gboolean ca_file_changed = FALSE;
372 const char *new_ca_file;
376 uri = g_value_get_boxed (value);
378 if (!safe_uri_equal (priv->proxy_uri, uri))
382 soup_uri_free (priv->proxy_uri);
384 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
387 soup_session_abort (session);
388 cleanup_hosts (priv);
393 priv->max_conns = g_value_get_int (value);
395 case PROP_MAX_CONNS_PER_HOST:
396 priv->max_conns_per_host = g_value_get_int (value);
399 if (g_value_get_boolean (value)) {
400 if (!priv->ntlm_manager)
401 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
403 if (priv->ntlm_manager) {
404 soup_auth_manager_ntlm_free (priv->ntlm_manager);
405 priv->ntlm_manager = NULL;
409 case PROP_SSL_CA_FILE:
410 new_ca_file = g_value_get_string (value);
412 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
413 ca_file_changed = TRUE;
415 g_free (priv->ssl_ca_file);
416 priv->ssl_ca_file = g_strdup (new_ca_file);
418 if (ca_file_changed) {
419 if (priv->ssl_creds) {
420 soup_ssl_free_client_credentials (priv->ssl_creds);
421 priv->ssl_creds = NULL;
424 cleanup_hosts (priv);
428 case PROP_ASYNC_CONTEXT:
429 priv->async_context = g_value_get_pointer (value);
430 if (priv->async_context)
431 g_main_context_ref (priv->async_context);
434 priv->timeout = g_value_get_uint (value);
442 get_property (GObject *object, guint prop_id,
443 GValue *value, GParamSpec *pspec)
445 SoupSession *session = SOUP_SESSION (object);
446 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
450 g_value_set_boxed (value, priv->proxy_uri);
453 g_value_set_int (value, priv->max_conns);
455 case PROP_MAX_CONNS_PER_HOST:
456 g_value_set_int (value, priv->max_conns_per_host);
459 g_value_set_boolean (value, priv->ntlm_manager != NULL);
461 case PROP_SSL_CA_FILE:
462 g_value_set_string (value, priv->ssl_ca_file);
464 case PROP_ASYNC_CONTEXT:
465 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
468 g_value_set_uint (value, priv->timeout);
477 * soup_session_get_async_context:
478 * @session: a #SoupSession
480 * Gets @session's async_context. This does not add a ref to the
481 * context, so you will need to ref it yourself if you want it to
482 * outlive its session.
484 * Return value: @session's #GMainContext, which may be %NULL
487 soup_session_get_async_context (SoupSession *session)
489 SoupSessionPrivate *priv;
491 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
492 priv = SOUP_SESSION_GET_PRIVATE (session);
494 return priv->async_context;
499 static SoupSessionHost *
500 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
502 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
503 SoupSessionHost *host;
505 host = g_slice_new0 (SoupSessionHost);
506 host->root_uri = soup_uri_copy_root (source_uri);
508 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
511 soup_ssl_get_client_credentials (priv->ssl_ca_file);
517 /* Note: get_host_for_message doesn't lock the host_lock. The caller
518 * must do it itself if there's a chance the host doesn't already
521 static SoupSessionHost *
522 get_host_for_message (SoupSession *session, SoupMessage *msg)
524 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
525 SoupSessionHost *host;
526 SoupURI *source = soup_message_get_uri (msg);
528 host = g_hash_table_lookup (priv->hosts, source);
532 host = soup_session_host_new (session, source);
533 g_hash_table_insert (priv->hosts, host->root_uri, host);
539 free_host (SoupSessionHost *host)
541 while (host->connections) {
542 SoupConnection *conn = host->connections->data;
544 host->connections = g_slist_remove (host->connections, conn);
545 soup_connection_disconnect (conn);
548 soup_uri_free (host->root_uri);
549 g_slice_free (SoupSessionHost, host);
553 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
554 SoupAuth *auth, gboolean retrying)
556 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
560 redirect_handler (SoupMessage *msg, gpointer user_data)
562 SoupSession *session = user_data;
566 if (!SOUP_STATUS_IS_REDIRECTION (msg->status_code))
569 new_loc = soup_message_headers_get (msg->response_headers, "Location");
573 /* Location is supposed to be an absolute URI, but some sites
574 * are lame, so we use soup_uri_new_with_base().
576 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
578 soup_message_set_status_full (msg,
579 SOUP_STATUS_MALFORMED,
580 "Invalid Redirect URL");
584 soup_message_set_uri (msg, new_uri);
585 soup_uri_free (new_uri);
587 soup_session_requeue_message (session, msg);
591 connection_started_request (SoupConnection *conn, SoupMessage *msg,
594 SoupSession *session = data;
596 g_signal_emit (session, signals[REQUEST_STARTED], 0,
597 msg, soup_connection_get_socket (conn));
601 find_oldest_connection (gpointer key, gpointer host, gpointer data)
603 SoupConnection *conn = key, **oldest = data;
605 /* Don't prune a connection that is currently in use, or
606 * hasn't been used yet.
608 if (soup_connection_is_in_use (conn) ||
609 soup_connection_last_used (conn) == 0)
612 if (!*oldest || (soup_connection_last_used (conn) <
613 soup_connection_last_used (*oldest)))
618 * soup_session_try_prune_connection:
619 * @session: a #SoupSession
621 * Finds the least-recently-used idle connection in @session and closes
624 * Return value: %TRUE if a connection was closed, %FALSE if there are
625 * no idle connections.
628 soup_session_try_prune_connection (SoupSession *session)
630 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
631 SoupConnection *oldest = NULL;
633 g_mutex_lock (priv->host_lock);
634 g_hash_table_foreach (priv->conns, find_oldest_connection,
637 /* Ref the connection before unlocking the mutex in
638 * case someone else tries to prune it too.
640 g_object_ref (oldest);
641 g_mutex_unlock (priv->host_lock);
642 soup_connection_disconnect (oldest);
643 g_object_unref (oldest);
646 g_mutex_unlock (priv->host_lock);
652 connection_disconnected (SoupConnection *conn, gpointer user_data)
654 SoupSession *session = user_data;
655 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
656 SoupSessionHost *host;
658 g_mutex_lock (priv->host_lock);
660 host = g_hash_table_lookup (priv->conns, conn);
662 g_hash_table_remove (priv->conns, conn);
663 host->connections = g_slist_remove (host->connections, conn);
667 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
670 g_mutex_unlock (priv->host_lock);
671 g_object_unref (conn);
675 connect_result (SoupConnection *conn, guint status, gpointer user_data)
677 SoupSession *session = user_data;
678 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
679 SoupSessionHost *host;
680 SoupMessageQueueIter iter;
683 g_mutex_lock (priv->host_lock);
685 host = g_hash_table_lookup (priv->conns, conn);
687 g_mutex_unlock (priv->host_lock);
691 if (status == SOUP_STATUS_OK) {
692 soup_connection_reserve (conn);
693 host->connections = g_slist_prepend (host->connections, conn);
694 g_mutex_unlock (priv->host_lock);
698 /* The connection failed. */
699 g_mutex_unlock (priv->host_lock);
700 connection_disconnected (conn, session);
702 if (host->connections) {
703 /* Something went wrong this time, but we have at
704 * least one open connection to this host. So just
705 * leave the message in the queue so it can use that
706 * connection once it's free.
711 /* There are two possibilities: either status is
712 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
713 * will create a new connection (and all we need to do here
714 * is downgrade the message from CONNECTING to QUEUED); or
715 * status is something else, probably CANT_CONNECT or
716 * CANT_RESOLVE or the like, in which case we need to cancel
717 * any messages waiting for this host, since they're out
720 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
721 if (get_host_for_message (session, msg) == host) {
722 if (status == SOUP_STATUS_TRY_AGAIN) {
723 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
724 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
726 soup_session_cancel_message (session, msg,
734 * soup_session_get_connection:
735 * @session: a #SoupSession
736 * @msg: a #SoupMessage
737 * @try_pruning: on return, whether or not to try pruning a connection
738 * @is_new: on return, %TRUE if the returned connection is new and not
741 * Tries to find or create a connection for @msg; this is an internal
742 * method for #SoupSession subclasses.
744 * If there is an idle connection to the relevant host available, then
745 * that connection will be returned (with *@is_new set to %FALSE). The
746 * connection will be marked "reserved", so the caller must call
747 * soup_connection_release() if it ends up not using the connection
750 * If there is no idle connection available, but it is possible to
751 * create a new connection, then one will be created and returned,
752 * with *@is_new set to %TRUE. The caller MUST then call
753 * soup_connection_connect_sync() or soup_connection_connect_async()
754 * to connect it. If the connection attempt succeeds, the connection
755 * will be marked "reserved" and added to @session's connection pool
756 * once it connects. If the connection attempt fails, the connection
759 * If no connection is available and a new connection cannot be made,
760 * soup_session_get_connection() will return %NULL. If @session has
761 * the maximum number of open connections open, but does not have the
762 * maximum number of per-host connections open to the relevant host,
763 * then *@try_pruning will be set to %TRUE. In this case, the caller
764 * can call soup_session_try_prune_connection() to close an idle
765 * connection, and then try soup_session_get_connection() again. (If
766 * calling soup_session_try_prune_connection() wouldn't help, then
767 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
769 * Return value: a #SoupConnection, or %NULL
772 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
773 gboolean *try_pruning, gboolean *is_new)
775 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
776 SoupConnection *conn;
777 SoupSessionHost *host;
780 g_mutex_lock (priv->host_lock);
782 host = get_host_for_message (session, msg);
783 for (conns = host->connections; conns; conns = conns->next) {
784 if (!soup_connection_is_in_use (conns->data)) {
785 soup_connection_reserve (conns->data);
786 g_mutex_unlock (priv->host_lock);
792 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
793 /* We already started a connection for this
794 * message, so don't start another one.
796 g_mutex_unlock (priv->host_lock);
800 if (host->num_conns >= priv->max_conns_per_host) {
801 g_mutex_unlock (priv->host_lock);
805 if (priv->num_conns >= priv->max_conns) {
807 g_mutex_unlock (priv->host_lock);
811 conn = soup_connection_new (
812 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
813 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
814 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
815 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
816 SOUP_CONNECTION_TIMEOUT, priv->timeout,
818 g_signal_connect (conn, "connect_result",
819 G_CALLBACK (connect_result),
821 g_signal_connect (conn, "disconnected",
822 G_CALLBACK (connection_disconnected),
824 g_signal_connect (conn, "request_started",
825 G_CALLBACK (connection_started_request),
828 g_hash_table_insert (priv->conns, conn, host);
830 /* We increment the connection counts so it counts against the
831 * totals, but we don't add it to the host's connection list
832 * yet, since it's not ready for use.
837 /* Mark the request as connecting, so we don't try to open
838 * another new connection for it while waiting for this one.
840 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
842 g_mutex_unlock (priv->host_lock);
848 soup_session_get_queue (SoupSession *session)
850 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
856 message_finished (SoupMessage *msg, gpointer user_data)
858 SoupSession *session = user_data;
859 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
861 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
862 soup_message_queue_remove_message (priv->queue, msg);
863 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
868 queue_message (SoupSession *session, SoupMessage *msg,
869 SoupSessionCallback callback, gpointer user_data)
871 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
873 g_signal_connect_after (msg, "finished",
874 G_CALLBACK (message_finished), session);
876 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
877 soup_message_add_header_handler (
878 msg, "got_body", "Location",
879 G_CALLBACK (redirect_handler), session);
882 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
883 soup_message_queue_append (priv->queue, msg);
887 * soup_session_queue_message:
888 * @session: a #SoupSession
889 * @msg: the message to queue
890 * @callback: a #SoupSessionCallback which will be called after the
891 * message completes or when an unrecoverable error occurs.
892 * @user_data: a pointer passed to @callback.
894 * Queues the message @msg for sending. All messages are processed
895 * while the glib main loop runs. If @msg has been processed before,
896 * any resources related to the time it was last sent are freed.
898 * Upon message completion, the callback specified in @callback will
899 * be invoked (in the thread associated with @session's async
900 * context). If after returning from this callback the message has not
901 * been requeued, @msg will be unreffed.
904 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
905 SoupSessionCallback callback, gpointer user_data)
907 g_return_if_fail (SOUP_IS_SESSION (session));
908 g_return_if_fail (SOUP_IS_MESSAGE (msg));
910 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
911 callback, user_data);
915 requeue_message (SoupSession *session, SoupMessage *msg)
917 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
921 * soup_session_requeue_message:
922 * @session: a #SoupSession
923 * @msg: the message to requeue
925 * This causes @msg to be placed back on the queue to be attempted
929 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
931 g_return_if_fail (SOUP_IS_SESSION (session));
932 g_return_if_fail (SOUP_IS_MESSAGE (msg));
934 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
939 * soup_session_send_message:
940 * @session: a #SoupSession
941 * @msg: the message to send
943 * Synchronously send @msg. This call will not return until the
944 * transfer is finished successfully or there is an unrecoverable
947 * @msg is not freed upon return.
949 * Return value: the HTTP status code of the response
952 soup_session_send_message (SoupSession *session, SoupMessage *msg)
954 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
955 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
957 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
962 * soup_session_pause_message:
963 * @session: a #SoupSession
964 * @msg: a #SoupMessage currently running on @session
966 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
970 soup_session_pause_message (SoupSession *session,
973 g_return_if_fail (SOUP_IS_SESSION (session));
974 g_return_if_fail (SOUP_IS_MESSAGE (msg));
976 soup_message_io_pause (msg);
980 * soup_session_unpause_message:
981 * @session: a #SoupSession
982 * @msg: a #SoupMessage currently running on @session
984 * Resumes HTTP I/O on @msg. Use this to resume after calling
985 * soup_sessino_pause_message().
987 * If @msg is being sent via blocking I/O, this will resume reading or
988 * writing immediately. If @msg is using non-blocking I/O, then
989 * reading or writing won't resume until you return to the main loop.
992 soup_session_unpause_message (SoupSession *session,
995 g_return_if_fail (SOUP_IS_SESSION (session));
996 g_return_if_fail (SOUP_IS_MESSAGE (msg));
998 soup_message_io_unpause (msg);
1003 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1005 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1007 soup_message_queue_remove_message (priv->queue, msg);
1008 soup_message_io_stop (msg);
1009 soup_message_set_status (msg, status_code);
1010 soup_message_finished (msg);
1014 * soup_session_cancel_message:
1015 * @session: a #SoupSession
1016 * @msg: the message to cancel
1017 * @status_code: status code to set on @msg (generally
1018 * %SOUP_STATUS_CANCELLED)
1020 * Causes @session to immediately finish processing @msg, with a final
1021 * status_code of @status_code. Depending on when you cancel it, the
1022 * response state may be incomplete or inconsistent.
1025 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1028 g_return_if_fail (SOUP_IS_SESSION (session));
1029 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1031 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1035 gather_conns (gpointer key, gpointer host, gpointer data)
1037 SoupConnection *conn = key;
1038 GSList **conns = data;
1040 *conns = g_slist_prepend (*conns, conn);
1044 * soup_session_abort:
1045 * @session: the session
1047 * Cancels all pending requests in @session.
1050 soup_session_abort (SoupSession *session)
1052 SoupSessionPrivate *priv;
1053 SoupMessageQueueIter iter;
1057 g_return_if_fail (SOUP_IS_SESSION (session));
1058 priv = SOUP_SESSION_GET_PRIVATE (session);
1060 for (msg = soup_message_queue_first (priv->queue, &iter);
1062 msg = soup_message_queue_next (priv->queue, &iter)) {
1063 soup_session_cancel_message (session, msg,
1064 SOUP_STATUS_CANCELLED);
1067 /* Close all connections */
1068 g_mutex_lock (priv->host_lock);
1070 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1072 for (c = conns; c; c = c->next)
1073 g_object_ref (c->data);
1074 g_mutex_unlock (priv->host_lock);
1075 for (c = conns; c; c = c->next) {
1076 soup_connection_disconnect (c->data);
1077 g_object_unref (c->data);
1080 g_slist_free (conns);