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;
60 SoupAuthManager *auth_manager;
61 SoupAuthManagerNTLM *ntlm_manager;
63 GHashTable *hosts; /* SoupURI -> SoupSessionHost */
64 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
67 /* Must hold the host_lock before potentially creating a
68 * new SoupSessionHost, or adding/removing a connection.
69 * Must not emit signals or destroy objects while holding it.
73 GMainContext *async_context;
75 /* Holds the timeout value for the connection, when
76 no response is received.
80 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
82 static void free_host (SoupSessionHost *host);
84 static void queue_message (SoupSession *session, SoupMessage *msg,
85 SoupSessionCallback callback, gpointer user_data);
86 static void requeue_message (SoupSession *session, SoupMessage *msg);
87 static void cancel_message (SoupSession *session, SoupMessage *msg,
90 /* temporary until we fix this to index hosts by SoupAddress */
91 extern guint soup_uri_host_hash (gconstpointer key);
92 extern gboolean soup_uri_host_equal (gconstpointer v1,
94 extern SoupURI *soup_uri_copy_root (SoupURI *uri);
96 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
97 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
99 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
101 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
109 static guint signals[LAST_SIGNAL] = { 0 };
116 PROP_MAX_CONNS_PER_HOST,
126 static void set_property (GObject *object, guint prop_id,
127 const GValue *value, GParamSpec *pspec);
128 static void get_property (GObject *object, guint prop_id,
129 GValue *value, GParamSpec *pspec);
132 soup_session_init (SoupSession *session)
134 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
136 priv->queue = soup_message_queue_new ();
138 priv->host_lock = g_mutex_new ();
139 priv->hosts = g_hash_table_new (soup_uri_host_hash,
140 soup_uri_host_equal);
141 priv->conns = g_hash_table_new (NULL, NULL);
143 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
144 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
148 priv->auth_manager = soup_auth_manager_new (session);
149 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
150 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
154 foreach_free_host (gpointer key, gpointer host, gpointer data)
161 cleanup_hosts (SoupSessionPrivate *priv)
163 GHashTable *old_hosts;
165 g_mutex_lock (priv->host_lock);
166 old_hosts = priv->hosts;
167 priv->hosts = g_hash_table_new (soup_uri_host_hash,
168 soup_uri_host_equal);
169 g_mutex_unlock (priv->host_lock);
171 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
172 g_hash_table_destroy (old_hosts);
176 dispose (GObject *object)
178 SoupSession *session = SOUP_SESSION (object);
179 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
181 soup_session_abort (session);
182 cleanup_hosts (priv);
184 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
188 finalize (GObject *object)
190 SoupSession *session = SOUP_SESSION (object);
191 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
193 soup_message_queue_destroy (priv->queue);
195 g_mutex_free (priv->host_lock);
196 g_hash_table_destroy (priv->hosts);
197 g_hash_table_destroy (priv->conns);
199 soup_auth_manager_free (priv->auth_manager);
200 if (priv->ntlm_manager)
201 soup_auth_manager_ntlm_free (priv->ntlm_manager);
204 soup_uri_free (priv->proxy_uri);
207 soup_ssl_free_client_credentials (priv->ssl_creds);
209 if (priv->async_context)
210 g_main_context_unref (priv->async_context);
212 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
216 soup_session_class_init (SoupSessionClass *session_class)
218 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
220 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
222 /* virtual method definition */
223 session_class->queue_message = queue_message;
224 session_class->requeue_message = requeue_message;
225 session_class->cancel_message = cancel_message;
227 /* virtual method override */
228 object_class->dispose = dispose;
229 object_class->finalize = finalize;
230 object_class->set_property = set_property;
231 object_class->get_property = get_property;
236 * SoupSession::request-started:
237 * @session: the session
238 * @msg: the request being sent
239 * @socket: the socket the request is being sent on
241 * Emitted just before a request is sent.
243 signals[REQUEST_STARTED] =
244 g_signal_new ("request-started",
245 G_OBJECT_CLASS_TYPE (object_class),
247 G_STRUCT_OFFSET (SoupSessionClass, request_started),
249 soup_marshal_NONE__OBJECT_OBJECT,
255 * SoupSession::authenticate:
256 * @session: the session
257 * @msg: the #SoupMessage being sent
258 * @auth: the #SoupAuth to authenticate
259 * @retrying: %TRUE if this is the second (or later) attempt
261 * Emitted when the session requires authentication. If
262 * credentials are available call soup_auth_authenticate() on
263 * @auth. If these credentials fail, the signal will be
264 * emitted again, with @retrying set to %TRUE, which will
265 * continue until you return without calling
266 * soup_auth_authenticate() on @auth.
268 * Note that this may be emitted before @msg's body has been
271 * If you call soup_session_pause_message() on @msg before
272 * returning, then you can authenticate @auth asynchronously
273 * (as long as you g_object_ref() it to make sure it doesn't
274 * get destroyed), and then unpause @msg when you are ready
275 * for it to continue.
277 signals[AUTHENTICATE] =
278 g_signal_new ("authenticate",
279 G_OBJECT_CLASS_TYPE (object_class),
281 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
283 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
290 g_object_class_install_property (
291 object_class, PROP_PROXY_URI,
292 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
294 "The HTTP Proxy to use for this session",
297 g_object_class_install_property (
298 object_class, PROP_MAX_CONNS,
299 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
300 "Max Connection Count",
301 "The maximum number of connections that the session can open at once",
304 SOUP_SESSION_MAX_CONNS_DEFAULT,
306 g_object_class_install_property (
307 object_class, PROP_MAX_CONNS_PER_HOST,
308 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
309 "Max Per-Host Connection Count",
310 "The maximum number of connections that the session can open at once to a given host",
313 SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
315 g_object_class_install_property (
316 object_class, PROP_USE_NTLM,
317 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
319 "Whether or not to use NTLM authentication",
322 g_object_class_install_property (
323 object_class, PROP_SSL_CA_FILE,
324 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
326 "File containing SSL CA certificates",
329 g_object_class_install_property (
330 object_class, PROP_ASYNC_CONTEXT,
331 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
332 "Async GMainContext",
333 "The GMainContext to dispatch async I/O in",
334 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
335 g_object_class_install_property (
336 object_class, PROP_TIMEOUT,
337 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
339 "Value in seconds to timeout a blocking I/O",
342 g_object_class_install_property (
343 object_class, PROP_USER_AGENT,
344 g_param_spec_string (SOUP_SESSION_USER_AGENT,
352 safe_uri_equal (SoupURI *a, SoupURI *b)
357 if ((a && !b) || (b && !a))
360 return soup_uri_equal (a, b);
364 safe_str_equal (const char *a, const char *b)
369 if ((a && !b) || (b && !a))
372 return strcmp (a, b) == 0;
376 set_property (GObject *object, guint prop_id,
377 const GValue *value, GParamSpec *pspec)
379 SoupSession *session = SOUP_SESSION (object);
380 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
382 gboolean need_abort = FALSE;
383 gboolean ca_file_changed = FALSE;
384 const char *new_ca_file, *user_agent;
388 uri = g_value_get_boxed (value);
390 if (!safe_uri_equal (priv->proxy_uri, uri))
394 soup_uri_free (priv->proxy_uri);
396 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
399 soup_session_abort (session);
400 cleanup_hosts (priv);
405 priv->max_conns = g_value_get_int (value);
407 case PROP_MAX_CONNS_PER_HOST:
408 priv->max_conns_per_host = g_value_get_int (value);
411 if (g_value_get_boolean (value)) {
412 if (!priv->ntlm_manager)
413 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
415 if (priv->ntlm_manager) {
416 soup_auth_manager_ntlm_free (priv->ntlm_manager);
417 priv->ntlm_manager = NULL;
421 case PROP_SSL_CA_FILE:
422 new_ca_file = g_value_get_string (value);
424 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
425 ca_file_changed = TRUE;
427 g_free (priv->ssl_ca_file);
428 priv->ssl_ca_file = g_strdup (new_ca_file);
430 if (ca_file_changed) {
431 if (priv->ssl_creds) {
432 soup_ssl_free_client_credentials (priv->ssl_creds);
433 priv->ssl_creds = NULL;
436 cleanup_hosts (priv);
440 case PROP_ASYNC_CONTEXT:
441 priv->async_context = g_value_get_pointer (value);
442 if (priv->async_context)
443 g_main_context_ref (priv->async_context);
446 priv->timeout = g_value_get_uint (value);
448 case PROP_USER_AGENT:
449 g_free (priv->user_agent);
450 user_agent = g_value_get_string (value);
452 priv->user_agent = NULL;
453 else if (!*user_agent) {
455 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
456 } else if (g_str_has_suffix (user_agent, " ")) {
458 g_strdup_printf ("%s%s", user_agent,
459 SOUP_SESSION_USER_AGENT_BASE);
461 priv->user_agent = g_strdup (user_agent);
469 get_property (GObject *object, guint prop_id,
470 GValue *value, GParamSpec *pspec)
472 SoupSession *session = SOUP_SESSION (object);
473 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
477 g_value_set_boxed (value, priv->proxy_uri);
480 g_value_set_int (value, priv->max_conns);
482 case PROP_MAX_CONNS_PER_HOST:
483 g_value_set_int (value, priv->max_conns_per_host);
486 g_value_set_boolean (value, priv->ntlm_manager != NULL);
488 case PROP_SSL_CA_FILE:
489 g_value_set_string (value, priv->ssl_ca_file);
491 case PROP_ASYNC_CONTEXT:
492 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
495 g_value_set_uint (value, priv->timeout);
497 case PROP_USER_AGENT:
498 g_value_set_string (value, priv->user_agent);
507 * soup_session_get_async_context:
508 * @session: a #SoupSession
510 * Gets @session's async_context. This does not add a ref to the
511 * context, so you will need to ref it yourself if you want it to
512 * outlive its session.
514 * Return value: @session's #GMainContext, which may be %NULL
517 soup_session_get_async_context (SoupSession *session)
519 SoupSessionPrivate *priv;
521 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
522 priv = SOUP_SESSION_GET_PRIVATE (session);
524 return priv->async_context;
529 static SoupSessionHost *
530 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
532 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
533 SoupSessionHost *host;
535 host = g_slice_new0 (SoupSessionHost);
536 host->root_uri = soup_uri_copy_root (source_uri);
538 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
541 soup_ssl_get_client_credentials (priv->ssl_ca_file);
547 /* Note: get_host_for_message doesn't lock the host_lock. The caller
548 * must do it itself if there's a chance the host doesn't already
551 static SoupSessionHost *
552 get_host_for_message (SoupSession *session, SoupMessage *msg)
554 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
555 SoupSessionHost *host;
556 SoupURI *source = soup_message_get_uri (msg);
558 host = g_hash_table_lookup (priv->hosts, source);
562 host = soup_session_host_new (session, source);
563 g_hash_table_insert (priv->hosts, host->root_uri, host);
569 free_host (SoupSessionHost *host)
571 while (host->connections) {
572 SoupConnection *conn = host->connections->data;
574 host->connections = g_slist_remove (host->connections, conn);
575 soup_connection_disconnect (conn);
578 soup_uri_free (host->root_uri);
579 g_slice_free (SoupSessionHost, host);
583 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
584 SoupAuth *auth, gboolean retrying)
586 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
590 redirect_handler (SoupMessage *msg, gpointer user_data)
592 SoupSession *session = user_data;
596 new_loc = soup_message_headers_get (msg->response_headers, "Location");
597 g_return_if_fail (new_loc != NULL);
599 if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
600 msg->status_code == SOUP_STATUS_FOUND ||
601 msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
602 /* Don't redirect non-safe methods */
603 if (msg->method != SOUP_METHOD_GET &&
604 msg->method != SOUP_METHOD_HEAD &&
605 msg->method != SOUP_METHOD_OPTIONS)
607 } else if (msg->status_code == SOUP_STATUS_SEE_OTHER) {
608 /* Redirect using a GET */
610 SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
612 soup_message_set_request (msg, NULL,
613 SOUP_MEMORY_STATIC, NULL, 0);
614 soup_message_headers_set_encoding (msg->request_headers,
617 /* Three possibilities:
619 * 1) This was a non-3xx response that happened to
620 * have a "Location" header
621 * 2) It's a non-redirecty 3xx response (300, 304,
623 * 3) It's some newly-defined 3xx response (308+)
625 * We ignore all of these cases. In the first two,
626 * redirecting would be explicitly wrong, and in the
627 * last case, we have no clue if the 3xx response is
628 * supposed to be redirecty or non-redirecty. Plus,
629 * 2616 says unrecognized status codes should be
630 * treated as the equivalent to the x00 code, and we
631 * don't redirect on 300, so therefore we shouldn't
632 * redirect on 308+ either.
637 /* Location is supposed to be an absolute URI, but some sites
638 * are lame, so we use soup_uri_new_with_base().
640 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
642 soup_message_set_status_full (msg,
643 SOUP_STATUS_MALFORMED,
644 "Invalid Redirect URL");
648 soup_message_set_uri (msg, new_uri);
649 soup_uri_free (new_uri);
651 soup_session_requeue_message (session, msg);
655 connection_started_request (SoupConnection *conn, SoupMessage *msg,
658 SoupSession *session = data;
659 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
661 if (priv->user_agent) {
662 soup_message_headers_replace (msg->request_headers,
663 "User-Agent", priv->user_agent);
666 g_signal_emit (session, signals[REQUEST_STARTED], 0,
667 msg, soup_connection_get_socket (conn));
671 find_oldest_connection (gpointer key, gpointer host, gpointer data)
673 SoupConnection *conn = key, **oldest = data;
675 /* Don't prune a connection that is currently in use, or
676 * hasn't been used yet.
678 if (soup_connection_is_in_use (conn) ||
679 soup_connection_last_used (conn) == 0)
682 if (!*oldest || (soup_connection_last_used (conn) <
683 soup_connection_last_used (*oldest)))
688 * soup_session_try_prune_connection:
689 * @session: a #SoupSession
691 * Finds the least-recently-used idle connection in @session and closes
694 * Return value: %TRUE if a connection was closed, %FALSE if there are
695 * no idle connections.
698 soup_session_try_prune_connection (SoupSession *session)
700 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
701 SoupConnection *oldest = NULL;
703 g_mutex_lock (priv->host_lock);
704 g_hash_table_foreach (priv->conns, find_oldest_connection,
707 /* Ref the connection before unlocking the mutex in
708 * case someone else tries to prune it too.
710 g_object_ref (oldest);
711 g_mutex_unlock (priv->host_lock);
712 soup_connection_disconnect (oldest);
713 g_object_unref (oldest);
716 g_mutex_unlock (priv->host_lock);
722 connection_disconnected (SoupConnection *conn, gpointer user_data)
724 SoupSession *session = user_data;
725 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
726 SoupSessionHost *host;
728 g_mutex_lock (priv->host_lock);
730 host = g_hash_table_lookup (priv->conns, conn);
732 g_hash_table_remove (priv->conns, conn);
733 host->connections = g_slist_remove (host->connections, conn);
737 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
740 g_mutex_unlock (priv->host_lock);
741 g_object_unref (conn);
745 connect_result (SoupConnection *conn, guint status, gpointer user_data)
747 SoupSession *session = user_data;
748 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
749 SoupSessionHost *host;
750 SoupMessageQueueIter iter;
753 g_mutex_lock (priv->host_lock);
755 host = g_hash_table_lookup (priv->conns, conn);
757 g_mutex_unlock (priv->host_lock);
761 if (status == SOUP_STATUS_OK) {
762 soup_connection_reserve (conn);
763 host->connections = g_slist_prepend (host->connections, conn);
764 g_mutex_unlock (priv->host_lock);
768 /* The connection failed. */
769 g_mutex_unlock (priv->host_lock);
770 connection_disconnected (conn, session);
772 if (host->connections) {
773 /* Something went wrong this time, but we have at
774 * least one open connection to this host. So just
775 * leave the message in the queue so it can use that
776 * connection once it's free.
781 /* There are two possibilities: either status is
782 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
783 * will create a new connection (and all we need to do here
784 * is downgrade the message from CONNECTING to QUEUED); or
785 * status is something else, probably CANT_CONNECT or
786 * CANT_RESOLVE or the like, in which case we need to cancel
787 * any messages waiting for this host, since they're out
790 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
791 if (get_host_for_message (session, msg) == host) {
792 if (status == SOUP_STATUS_TRY_AGAIN) {
793 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
794 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
796 soup_session_cancel_message (session, msg,
804 * soup_session_get_connection:
805 * @session: a #SoupSession
806 * @msg: a #SoupMessage
807 * @try_pruning: on return, whether or not to try pruning a connection
808 * @is_new: on return, %TRUE if the returned connection is new and not
811 * Tries to find or create a connection for @msg; this is an internal
812 * method for #SoupSession subclasses.
814 * If there is an idle connection to the relevant host available, then
815 * that connection will be returned (with *@is_new set to %FALSE). The
816 * connection will be marked "reserved", so the caller must call
817 * soup_connection_release() if it ends up not using the connection
820 * If there is no idle connection available, but it is possible to
821 * create a new connection, then one will be created and returned,
822 * with *@is_new set to %TRUE. The caller MUST then call
823 * soup_connection_connect_sync() or soup_connection_connect_async()
824 * to connect it. If the connection attempt succeeds, the connection
825 * will be marked "reserved" and added to @session's connection pool
826 * once it connects. If the connection attempt fails, the connection
829 * If no connection is available and a new connection cannot be made,
830 * soup_session_get_connection() will return %NULL. If @session has
831 * the maximum number of open connections open, but does not have the
832 * maximum number of per-host connections open to the relevant host,
833 * then *@try_pruning will be set to %TRUE. In this case, the caller
834 * can call soup_session_try_prune_connection() to close an idle
835 * connection, and then try soup_session_get_connection() again. (If
836 * calling soup_session_try_prune_connection() wouldn't help, then
837 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
839 * Return value: a #SoupConnection, or %NULL
842 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
843 gboolean *try_pruning, gboolean *is_new)
845 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
846 SoupConnection *conn;
847 SoupSessionHost *host;
850 g_mutex_lock (priv->host_lock);
852 host = get_host_for_message (session, msg);
853 for (conns = host->connections; conns; conns = conns->next) {
854 if (!soup_connection_is_in_use (conns->data)) {
855 soup_connection_reserve (conns->data);
856 g_mutex_unlock (priv->host_lock);
862 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
863 /* We already started a connection for this
864 * message, so don't start another one.
866 g_mutex_unlock (priv->host_lock);
870 if (host->num_conns >= priv->max_conns_per_host) {
871 g_mutex_unlock (priv->host_lock);
875 if (priv->num_conns >= priv->max_conns) {
877 g_mutex_unlock (priv->host_lock);
881 conn = soup_connection_new (
882 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
883 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
884 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
885 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
886 SOUP_CONNECTION_TIMEOUT, priv->timeout,
888 g_signal_connect (conn, "connect_result",
889 G_CALLBACK (connect_result),
891 g_signal_connect (conn, "disconnected",
892 G_CALLBACK (connection_disconnected),
894 g_signal_connect (conn, "request_started",
895 G_CALLBACK (connection_started_request),
898 g_hash_table_insert (priv->conns, conn, host);
900 /* We increment the connection counts so it counts against the
901 * totals, but we don't add it to the host's connection list
902 * yet, since it's not ready for use.
907 /* Mark the request as connecting, so we don't try to open
908 * another new connection for it while waiting for this one.
910 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
912 g_mutex_unlock (priv->host_lock);
918 soup_session_get_queue (SoupSession *session)
920 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
926 message_finished (SoupMessage *msg, gpointer user_data)
928 SoupSession *session = user_data;
929 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
931 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
932 soup_message_queue_remove_message (priv->queue, msg);
933 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
938 queue_message (SoupSession *session, SoupMessage *msg,
939 SoupSessionCallback callback, gpointer user_data)
941 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
943 g_signal_connect_after (msg, "finished",
944 G_CALLBACK (message_finished), session);
946 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
947 soup_message_add_header_handler (
948 msg, "got_body", "Location",
949 G_CALLBACK (redirect_handler), session);
952 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
953 soup_message_queue_append (priv->queue, msg);
957 * soup_session_queue_message:
958 * @session: a #SoupSession
959 * @msg: the message to queue
960 * @callback: a #SoupSessionCallback which will be called after the
961 * message completes or when an unrecoverable error occurs.
962 * @user_data: a pointer passed to @callback.
964 * Queues the message @msg for sending. All messages are processed
965 * while the glib main loop runs. If @msg has been processed before,
966 * any resources related to the time it was last sent are freed.
968 * Upon message completion, the callback specified in @callback will
969 * be invoked (in the thread associated with @session's async
970 * context). If after returning from this callback the message has not
971 * been requeued, @msg will be unreffed.
974 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
975 SoupSessionCallback callback, gpointer user_data)
977 g_return_if_fail (SOUP_IS_SESSION (session));
978 g_return_if_fail (SOUP_IS_MESSAGE (msg));
980 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
981 callback, user_data);
985 requeue_message (SoupSession *session, SoupMessage *msg)
987 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
991 * soup_session_requeue_message:
992 * @session: a #SoupSession
993 * @msg: the message to requeue
995 * This causes @msg to be placed back on the queue to be attempted
999 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1001 g_return_if_fail (SOUP_IS_SESSION (session));
1002 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1004 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1009 * soup_session_send_message:
1010 * @session: a #SoupSession
1011 * @msg: the message to send
1013 * Synchronously send @msg. This call will not return until the
1014 * transfer is finished successfully or there is an unrecoverable
1017 * @msg is not freed upon return.
1019 * Return value: the HTTP status code of the response
1022 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1024 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1025 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1027 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1032 * soup_session_pause_message:
1033 * @session: a #SoupSession
1034 * @msg: a #SoupMessage currently running on @session
1036 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1040 soup_session_pause_message (SoupSession *session,
1043 g_return_if_fail (SOUP_IS_SESSION (session));
1044 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1046 soup_message_io_pause (msg);
1050 * soup_session_unpause_message:
1051 * @session: a #SoupSession
1052 * @msg: a #SoupMessage currently running on @session
1054 * Resumes HTTP I/O on @msg. Use this to resume after calling
1055 * soup_sessino_pause_message().
1057 * If @msg is being sent via blocking I/O, this will resume reading or
1058 * writing immediately. If @msg is using non-blocking I/O, then
1059 * reading or writing won't resume until you return to the main loop.
1062 soup_session_unpause_message (SoupSession *session,
1065 g_return_if_fail (SOUP_IS_SESSION (session));
1066 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1068 soup_message_io_unpause (msg);
1073 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1075 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1077 soup_message_queue_remove_message (priv->queue, msg);
1078 soup_message_io_stop (msg);
1079 soup_message_set_status (msg, status_code);
1080 soup_message_finished (msg);
1084 * soup_session_cancel_message:
1085 * @session: a #SoupSession
1086 * @msg: the message to cancel
1087 * @status_code: status code to set on @msg (generally
1088 * %SOUP_STATUS_CANCELLED)
1090 * Causes @session to immediately finish processing @msg, with a final
1091 * status_code of @status_code. Depending on when you cancel it, the
1092 * response state may be incomplete or inconsistent.
1095 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, status_code);
1105 gather_conns (gpointer key, gpointer host, gpointer data)
1107 SoupConnection *conn = key;
1108 GSList **conns = data;
1110 *conns = g_slist_prepend (*conns, conn);
1114 * soup_session_abort:
1115 * @session: the session
1117 * Cancels all pending requests in @session.
1120 soup_session_abort (SoupSession *session)
1122 SoupSessionPrivate *priv;
1123 SoupMessageQueueIter iter;
1127 g_return_if_fail (SOUP_IS_SESSION (session));
1128 priv = SOUP_SESSION_GET_PRIVATE (session);
1130 for (msg = soup_message_queue_first (priv->queue, &iter);
1132 msg = soup_message_queue_next (priv->queue, &iter)) {
1133 soup_session_cancel_message (session, msg,
1134 SOUP_STATUS_CANCELLED);
1137 /* Close all connections */
1138 g_mutex_lock (priv->host_lock);
1140 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1142 for (c = conns; c; c = c->next)
1143 g_object_ref (c->data);
1144 g_mutex_unlock (priv->host_lock);
1145 for (c = conns; c; c = c->next) {
1146 soup_connection_disconnect (c->data);
1147 g_object_unref (c->data);
1150 g_slist_free (conns);