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
35 * #SoupSession is the object that controls client-side HTTP. A
36 * #SoupSession encapsulates all of the state that libsoup is keeping
37 * on behalf of your program; cached HTTP connections, authentication
40 * Most applications will only need a single #SoupSession; the primary
41 * reason you might need multiple sessions is if you need to have
42 * multiple independent authentication contexts. (Eg, you are
43 * connecting to a server and authenticating as two different users at
44 * different times; the easiest way to ensure that each #SoupMessage
45 * is sent with the authentication information you intended is to use
46 * one session for the first user, and a second session for the other
49 * #SoupSession itself is an abstract class, with two subclasses. If
50 * you are using the glib main loop, you will generally want to use
51 * #SoupSessionAsync, which uses non-blocking I/O and callbacks. On
52 * the other hand, if your application is threaded and you want to do
53 * synchronous I/O in a separate thread from the UI, use
60 GSList *connections; /* CONTAINS: SoupConnection */
63 GHashTable *auth_realms; /* path -> scheme:realm */
64 GHashTable *auths; /* scheme:realm -> SoupAuth */
72 SoupSSLCredentials *ssl_creds;
74 SoupMessageQueue *queue;
78 SoupAuthManager *auth_manager;
79 SoupAuthManagerNTLM *ntlm_manager;
81 GHashTable *hosts; /* SoupURI -> SoupSessionHost */
82 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
84 guint max_conns, max_conns_per_host;
85 guint io_timeout, idle_timeout;
87 /* Must hold the host_lock before potentially creating a
88 * new SoupSessionHost, or adding/removing a connection.
89 * Must not emit signals or destroy objects while holding it.
93 GMainContext *async_context;
95 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
97 static void free_host (SoupSessionHost *host);
99 static void queue_message (SoupSession *session, SoupMessage *msg,
100 SoupSessionCallback callback, gpointer user_data);
101 static void requeue_message (SoupSession *session, SoupMessage *msg);
102 static void cancel_message (SoupSession *session, SoupMessage *msg,
105 /* temporary until we fix this to index hosts by SoupAddress */
106 extern guint soup_uri_host_hash (gconstpointer key);
107 extern gboolean soup_uri_host_equal (gconstpointer v1,
109 extern SoupURI *soup_uri_copy_root (SoupURI *uri);
111 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
112 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
114 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
116 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
126 static guint signals[LAST_SIGNAL] = { 0 };
133 PROP_MAX_CONNS_PER_HOST,
144 static void set_property (GObject *object, guint prop_id,
145 const GValue *value, GParamSpec *pspec);
146 static void get_property (GObject *object, guint prop_id,
147 GValue *value, GParamSpec *pspec);
150 soup_session_init (SoupSession *session)
152 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
154 priv->queue = soup_message_queue_new ();
156 priv->host_lock = g_mutex_new ();
157 priv->hosts = g_hash_table_new (soup_uri_host_hash,
158 soup_uri_host_equal);
159 priv->conns = g_hash_table_new (NULL, NULL);
161 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
162 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
164 priv->auth_manager = soup_auth_manager_new (session);
165 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
166 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
170 foreach_free_host (gpointer key, gpointer host, gpointer data)
177 cleanup_hosts (SoupSessionPrivate *priv)
179 GHashTable *old_hosts;
181 g_mutex_lock (priv->host_lock);
182 old_hosts = priv->hosts;
183 priv->hosts = g_hash_table_new (soup_uri_host_hash,
184 soup_uri_host_equal);
185 g_mutex_unlock (priv->host_lock);
187 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
188 g_hash_table_destroy (old_hosts);
192 dispose (GObject *object)
194 SoupSession *session = SOUP_SESSION (object);
195 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
197 soup_session_abort (session);
198 cleanup_hosts (priv);
200 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
204 finalize (GObject *object)
206 SoupSession *session = SOUP_SESSION (object);
207 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
209 soup_message_queue_destroy (priv->queue);
211 g_mutex_free (priv->host_lock);
212 g_hash_table_destroy (priv->hosts);
213 g_hash_table_destroy (priv->conns);
215 g_free (priv->user_agent);
217 soup_auth_manager_free (priv->auth_manager);
218 if (priv->ntlm_manager)
219 soup_auth_manager_ntlm_free (priv->ntlm_manager);
222 soup_uri_free (priv->proxy_uri);
225 soup_ssl_free_client_credentials (priv->ssl_creds);
227 if (priv->async_context)
228 g_main_context_unref (priv->async_context);
230 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
234 soup_session_class_init (SoupSessionClass *session_class)
236 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
238 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
240 /* virtual method definition */
241 session_class->queue_message = queue_message;
242 session_class->requeue_message = requeue_message;
243 session_class->cancel_message = cancel_message;
245 /* virtual method override */
246 object_class->dispose = dispose;
247 object_class->finalize = finalize;
248 object_class->set_property = set_property;
249 object_class->get_property = get_property;
254 * SoupSession::request-queued:
255 * @session: the session
256 * @msg: the request that was queued
258 * Emitted when a request is queued on @session. (Note that
259 * "queued" doesn't just mean soup_session_queue_message();
260 * soup_session_send_message() implicitly queues the message
263 * When sending a request, first #SoupSession::request_queued
264 * is emitted, indicating that the session has become aware of
267 * Once a connection is available to send the request on, the
268 * session emits #SoupSession::request_started. Then, various
269 * #SoupMessage signals are emitted as the message is
270 * processed. If the message is requeued, it will emit
271 * #SoupMessage::restarted, which will then be followed by
272 * another #SoupSession::request_started and another set of
273 * #SoupMessage signals when the message is re-sent.
275 * Eventually, the message will emit #SoupMessage::finished.
276 * Normally, this signals the completion of message
277 * processing. However, it is possible that the application
278 * will requeue the message from the "finished" handler (or
279 * equivalently, from the soup_session_queue_message()
280 * callback). In that case, the process will loop back to
281 * #SoupSession::request_started.
283 * Eventually, a message will reach "finished" and not be
284 * requeued. At that point, the session will emit
285 * #SoupSession::request_unqueued to indicate that it is done
288 * To sum up: #SoupSession::request_queued and
289 * #SoupSession::request_unqueued are guaranteed to be emitted
290 * exactly once, but #SoupSession::request_started and
291 * #SoupMessage::finished (and all of the other #SoupMessage
292 * signals) may be invoked multiple times for a given message.
294 signals[REQUEST_QUEUED] =
295 g_signal_new ("request-queued",
296 G_OBJECT_CLASS_TYPE (object_class),
300 soup_marshal_NONE__OBJECT,
305 * SoupSession::request-started:
306 * @session: the session
307 * @msg: the request being sent
308 * @socket: the socket the request is being sent on
310 * Emitted just before a request is sent. See
311 * #SoupSession::request_queued for a detailed description of
312 * the message lifecycle within a session.
314 signals[REQUEST_STARTED] =
315 g_signal_new ("request-started",
316 G_OBJECT_CLASS_TYPE (object_class),
318 G_STRUCT_OFFSET (SoupSessionClass, request_started),
320 soup_marshal_NONE__OBJECT_OBJECT,
326 * SoupSession::request-unqueued:
327 * @session: the session
328 * @msg: the request that was unqueued
330 * Emitted when a request is removed from @session's queue,
331 * indicating that @session is done with it. See
332 * #SoupSession::request_queued for a detailed description of the
333 * message lifecycle within a session.
335 signals[REQUEST_UNQUEUED] =
336 g_signal_new ("request-unqueued",
337 G_OBJECT_CLASS_TYPE (object_class),
341 soup_marshal_NONE__OBJECT,
346 * SoupSession::authenticate:
347 * @session: the session
348 * @msg: the #SoupMessage being sent
349 * @auth: the #SoupAuth to authenticate
350 * @retrying: %TRUE if this is the second (or later) attempt
352 * Emitted when the session requires authentication. If
353 * credentials are available call soup_auth_authenticate() on
354 * @auth. If these credentials fail, the signal will be
355 * emitted again, with @retrying set to %TRUE, which will
356 * continue until you return without calling
357 * soup_auth_authenticate() on @auth.
359 * Note that this may be emitted before @msg's body has been
362 * If you call soup_session_pause_message() on @msg before
363 * returning, then you can authenticate @auth asynchronously
364 * (as long as you g_object_ref() it to make sure it doesn't
365 * get destroyed), and then unpause @msg when you are ready
366 * for it to continue.
368 signals[AUTHENTICATE] =
369 g_signal_new ("authenticate",
370 G_OBJECT_CLASS_TYPE (object_class),
372 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
374 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
381 g_object_class_install_property (
382 object_class, PROP_PROXY_URI,
383 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
385 "The HTTP Proxy to use for this session",
388 g_object_class_install_property (
389 object_class, PROP_MAX_CONNS,
390 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
391 "Max Connection Count",
392 "The maximum number of connections that the session can open at once",
395 SOUP_SESSION_MAX_CONNS_DEFAULT,
397 g_object_class_install_property (
398 object_class, PROP_MAX_CONNS_PER_HOST,
399 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
400 "Max Per-Host Connection Count",
401 "The maximum number of connections that the session can open at once to a given host",
404 SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
406 g_object_class_install_property (
407 object_class, PROP_IDLE_TIMEOUT,
408 g_param_spec_uint (SOUP_SESSION_IDLE_TIMEOUT,
410 "Connection lifetime when idle",
413 g_object_class_install_property (
414 object_class, PROP_USE_NTLM,
415 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
417 "Whether or not to use NTLM authentication",
420 g_object_class_install_property (
421 object_class, PROP_SSL_CA_FILE,
422 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
424 "File containing SSL CA certificates",
427 g_object_class_install_property (
428 object_class, PROP_ASYNC_CONTEXT,
429 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
430 "Async GMainContext",
431 "The GMainContext to dispatch async I/O in",
432 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
433 g_object_class_install_property (
434 object_class, PROP_TIMEOUT,
435 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
437 "Value in seconds to timeout a blocking I/O",
442 * SoupSession:user-agent:
444 * If non-%NULL, the value to use for the "User-Agent" header
445 * on #SoupMessage<!-- -->s sent from this session.
447 * RFC 2616 says: "The User-Agent request-header field
448 * contains information about the user agent originating the
449 * request. This is for statistical purposes, the tracing of
450 * protocol violations, and automated recognition of user
451 * agents for the sake of tailoring responses to avoid
452 * particular user agent limitations. User agents SHOULD
453 * include this field with requests."
455 * The User-Agent header contains a list of one or more
456 * product tokens, separated by whitespace, with the most
457 * significant product token coming first. The tokens must be
458 * brief, ASCII, and mostly alphanumeric (although "-", "_",
459 * and "." are also allowed), and may optionally include a "/"
460 * followed by a version string. You may also put comments,
461 * enclosed in parentheses, between or after the tokens.
463 * If you set a %user_agent property that has trailing
464 * whitespace, #SoupSession will append its own product token
465 * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
468 g_object_class_install_property (
469 object_class, PROP_USER_AGENT,
470 g_param_spec_string (SOUP_SESSION_USER_AGENT,
478 safe_uri_equal (SoupURI *a, SoupURI *b)
483 if ((a && !b) || (b && !a))
486 return soup_uri_equal (a, b);
490 safe_str_equal (const char *a, const char *b)
495 if ((a && !b) || (b && !a))
498 return strcmp (a, b) == 0;
502 set_property (GObject *object, guint prop_id,
503 const GValue *value, GParamSpec *pspec)
505 SoupSession *session = SOUP_SESSION (object);
506 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
508 gboolean need_abort = FALSE;
509 gboolean ca_file_changed = FALSE;
510 const char *new_ca_file, *user_agent;
514 uri = g_value_get_boxed (value);
516 if (!safe_uri_equal (priv->proxy_uri, uri))
520 soup_uri_free (priv->proxy_uri);
522 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
525 soup_session_abort (session);
526 cleanup_hosts (priv);
531 priv->max_conns = g_value_get_int (value);
533 case PROP_MAX_CONNS_PER_HOST:
534 priv->max_conns_per_host = g_value_get_int (value);
537 if (g_value_get_boolean (value)) {
538 if (!priv->ntlm_manager)
539 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
541 if (priv->ntlm_manager) {
542 soup_auth_manager_ntlm_free (priv->ntlm_manager);
543 priv->ntlm_manager = NULL;
547 case PROP_SSL_CA_FILE:
548 new_ca_file = g_value_get_string (value);
550 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
551 ca_file_changed = TRUE;
553 g_free (priv->ssl_ca_file);
554 priv->ssl_ca_file = g_strdup (new_ca_file);
556 if (ca_file_changed) {
557 if (priv->ssl_creds) {
558 soup_ssl_free_client_credentials (priv->ssl_creds);
559 priv->ssl_creds = NULL;
562 cleanup_hosts (priv);
566 case PROP_ASYNC_CONTEXT:
567 priv->async_context = g_value_get_pointer (value);
568 if (priv->async_context)
569 g_main_context_ref (priv->async_context);
572 priv->io_timeout = g_value_get_uint (value);
574 case PROP_USER_AGENT:
575 g_free (priv->user_agent);
576 user_agent = g_value_get_string (value);
578 priv->user_agent = NULL;
579 else if (!*user_agent) {
581 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
582 } else if (g_str_has_suffix (user_agent, " ")) {
584 g_strdup_printf ("%s%s", user_agent,
585 SOUP_SESSION_USER_AGENT_BASE);
587 priv->user_agent = g_strdup (user_agent);
589 case PROP_IDLE_TIMEOUT:
590 priv->idle_timeout = g_value_get_uint (value);
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599 get_property (GObject *object, guint prop_id,
600 GValue *value, GParamSpec *pspec)
602 SoupSession *session = SOUP_SESSION (object);
603 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
607 g_value_set_boxed (value, priv->proxy_uri);
610 g_value_set_int (value, priv->max_conns);
612 case PROP_MAX_CONNS_PER_HOST:
613 g_value_set_int (value, priv->max_conns_per_host);
616 g_value_set_boolean (value, priv->ntlm_manager != NULL);
618 case PROP_SSL_CA_FILE:
619 g_value_set_string (value, priv->ssl_ca_file);
621 case PROP_ASYNC_CONTEXT:
622 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
625 g_value_set_uint (value, priv->io_timeout);
627 case PROP_USER_AGENT:
628 g_value_set_string (value, priv->user_agent);
630 case PROP_IDLE_TIMEOUT:
631 g_value_set_uint (value, priv->idle_timeout);
634 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
641 * soup_session_get_async_context:
642 * @session: a #SoupSession
644 * Gets @session's async_context. This does not add a ref to the
645 * context, so you will need to ref it yourself if you want it to
646 * outlive its session.
648 * Return value: @session's #GMainContext, which may be %NULL
651 soup_session_get_async_context (SoupSession *session)
653 SoupSessionPrivate *priv;
655 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
656 priv = SOUP_SESSION_GET_PRIVATE (session);
658 return priv->async_context;
663 static SoupSessionHost *
664 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
666 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
667 SoupSessionHost *host;
669 host = g_slice_new0 (SoupSessionHost);
670 host->root_uri = soup_uri_copy_root (source_uri);
672 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
675 soup_ssl_get_client_credentials (priv->ssl_ca_file);
681 /* Note: get_host_for_message doesn't lock the host_lock. The caller
682 * must do it itself if there's a chance the host doesn't already
685 static SoupSessionHost *
686 get_host_for_message (SoupSession *session, SoupMessage *msg)
688 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
689 SoupSessionHost *host;
690 SoupURI *source = soup_message_get_uri (msg);
692 host = g_hash_table_lookup (priv->hosts, source);
696 host = soup_session_host_new (session, source);
697 g_hash_table_insert (priv->hosts, host->root_uri, host);
703 free_host (SoupSessionHost *host)
705 while (host->connections) {
706 SoupConnection *conn = host->connections->data;
708 host->connections = g_slist_remove (host->connections, conn);
709 soup_connection_disconnect (conn);
712 soup_uri_free (host->root_uri);
713 g_slice_free (SoupSessionHost, host);
717 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
718 SoupAuth *auth, gboolean retrying)
720 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
724 redirect_handler (SoupMessage *msg, gpointer user_data)
726 SoupSession *session = user_data;
730 new_loc = soup_message_headers_get (msg->response_headers, "Location");
731 g_return_if_fail (new_loc != NULL);
733 if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
734 msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
735 /* Don't redirect non-safe methods */
736 if (msg->method != SOUP_METHOD_GET &&
737 msg->method != SOUP_METHOD_HEAD &&
738 msg->method != SOUP_METHOD_OPTIONS &&
739 msg->method != SOUP_METHOD_PROPFIND)
741 } else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
742 msg->status_code == SOUP_STATUS_FOUND) {
743 /* Redirect using a GET */
745 SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
747 soup_message_set_request (msg, NULL,
748 SOUP_MEMORY_STATIC, NULL, 0);
749 soup_message_headers_set_encoding (msg->request_headers,
752 /* Three possibilities:
754 * 1) This was a non-3xx response that happened to
755 * have a "Location" header
756 * 2) It's a non-redirecty 3xx response (300, 304,
758 * 3) It's some newly-defined 3xx response (308+)
760 * We ignore all of these cases. In the first two,
761 * redirecting would be explicitly wrong, and in the
762 * last case, we have no clue if the 3xx response is
763 * supposed to be redirecty or non-redirecty. Plus,
764 * 2616 says unrecognized status codes should be
765 * treated as the equivalent to the x00 code, and we
766 * don't redirect on 300, so therefore we shouldn't
767 * redirect on 308+ either.
772 /* Location is supposed to be an absolute URI, but some sites
773 * are lame, so we use soup_uri_new_with_base().
775 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
777 soup_message_set_status_full (msg,
778 SOUP_STATUS_MALFORMED,
779 "Invalid Redirect URL");
783 soup_message_set_uri (msg, new_uri);
784 soup_uri_free (new_uri);
786 soup_session_requeue_message (session, msg);
790 connection_started_request (SoupConnection *conn, SoupMessage *msg,
793 SoupSession *session = data;
794 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
796 if (priv->user_agent) {
797 soup_message_headers_replace (msg->request_headers,
798 "User-Agent", priv->user_agent);
801 /* Kludge to deal with the fact that CONNECT msgs come from the
802 * SoupConnection rather than being queued normally.
804 if (msg->method == SOUP_METHOD_CONNECT)
805 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
807 g_signal_emit (session, signals[REQUEST_STARTED], 0,
808 msg, soup_connection_get_socket (conn));
812 find_oldest_connection (gpointer key, gpointer host, gpointer data)
814 SoupConnection *conn = key, **oldest = data;
816 /* Don't prune a connection that is currently in use, or
817 * hasn't been used yet.
819 if (soup_connection_is_in_use (conn) ||
820 soup_connection_last_used (conn) == 0)
823 if (!*oldest || (soup_connection_last_used (conn) <
824 soup_connection_last_used (*oldest)))
829 * soup_session_try_prune_connection:
830 * @session: a #SoupSession
832 * Finds the least-recently-used idle connection in @session and closes
835 * Return value: %TRUE if a connection was closed, %FALSE if there are
836 * no idle connections.
839 soup_session_try_prune_connection (SoupSession *session)
841 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
842 SoupConnection *oldest = NULL;
844 g_mutex_lock (priv->host_lock);
845 g_hash_table_foreach (priv->conns, find_oldest_connection,
848 /* Ref the connection before unlocking the mutex in
849 * case someone else tries to prune it too.
851 g_object_ref (oldest);
852 g_mutex_unlock (priv->host_lock);
853 soup_connection_disconnect (oldest);
854 g_object_unref (oldest);
857 g_mutex_unlock (priv->host_lock);
863 connection_disconnected (SoupConnection *conn, gpointer user_data)
865 SoupSession *session = user_data;
866 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
867 SoupSessionHost *host;
869 g_mutex_lock (priv->host_lock);
871 host = g_hash_table_lookup (priv->conns, conn);
873 g_hash_table_remove (priv->conns, conn);
874 host->connections = g_slist_remove (host->connections, conn);
878 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
881 g_mutex_unlock (priv->host_lock);
882 g_object_unref (conn);
886 connect_result (SoupConnection *conn, guint status, gpointer user_data)
888 SoupSession *session = user_data;
889 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
890 SoupSessionHost *host;
891 SoupMessageQueueIter iter;
894 g_mutex_lock (priv->host_lock);
896 host = g_hash_table_lookup (priv->conns, conn);
898 g_mutex_unlock (priv->host_lock);
902 if (status == SOUP_STATUS_OK) {
903 soup_connection_reserve (conn);
904 host->connections = g_slist_prepend (host->connections, conn);
905 g_mutex_unlock (priv->host_lock);
909 /* The connection failed. */
910 g_mutex_unlock (priv->host_lock);
911 connection_disconnected (conn, session);
913 if (host->connections) {
914 /* Something went wrong this time, but we have at
915 * least one open connection to this host. So just
916 * leave the message in the queue so it can use that
917 * connection once it's free.
922 /* There are two possibilities: either status is
923 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
924 * will create a new connection (and all we need to do here
925 * is downgrade the message from CONNECTING to QUEUED); or
926 * status is something else, probably CANT_CONNECT or
927 * CANT_RESOLVE or the like, in which case we need to cancel
928 * any messages waiting for this host, since they're out
931 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
932 if (get_host_for_message (session, msg) == host) {
933 if (status == SOUP_STATUS_TRY_AGAIN) {
934 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
935 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
937 soup_session_cancel_message (session, msg,
945 * soup_session_get_connection:
946 * @session: a #SoupSession
947 * @msg: a #SoupMessage
948 * @try_pruning: on return, whether or not to try pruning a connection
949 * @is_new: on return, %TRUE if the returned connection is new and not
952 * Tries to find or create a connection for @msg; this is an internal
953 * method for #SoupSession subclasses.
955 * If there is an idle connection to the relevant host available, then
956 * that connection will be returned (with *@is_new set to %FALSE). The
957 * connection will be marked "reserved", so the caller must call
958 * soup_connection_release() if it ends up not using the connection
961 * If there is no idle connection available, but it is possible to
962 * create a new connection, then one will be created and returned,
963 * with *@is_new set to %TRUE. The caller MUST then call
964 * soup_connection_connect_sync() or soup_connection_connect_async()
965 * to connect it. If the connection attempt succeeds, the connection
966 * will be marked "reserved" and added to @session's connection pool
967 * once it connects. If the connection attempt fails, the connection
970 * If no connection is available and a new connection cannot be made,
971 * soup_session_get_connection() will return %NULL. If @session has
972 * the maximum number of open connections open, but does not have the
973 * maximum number of per-host connections open to the relevant host,
974 * then *@try_pruning will be set to %TRUE. In this case, the caller
975 * can call soup_session_try_prune_connection() to close an idle
976 * connection, and then try soup_session_get_connection() again. (If
977 * calling soup_session_try_prune_connection() wouldn't help, then
978 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
980 * Return value: a #SoupConnection, or %NULL
983 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
984 gboolean *try_pruning, gboolean *is_new)
986 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
987 SoupConnection *conn;
988 SoupSessionHost *host;
991 g_mutex_lock (priv->host_lock);
993 host = get_host_for_message (session, msg);
994 for (conns = host->connections; conns; conns = conns->next) {
995 if (!soup_connection_is_in_use (conns->data)) {
996 soup_connection_reserve (conns->data);
997 g_mutex_unlock (priv->host_lock);
1003 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
1004 /* We already started a connection for this
1005 * message, so don't start another one.
1007 g_mutex_unlock (priv->host_lock);
1011 if (host->num_conns >= priv->max_conns_per_host) {
1012 g_mutex_unlock (priv->host_lock);
1016 if (priv->num_conns >= priv->max_conns) {
1017 *try_pruning = TRUE;
1018 g_mutex_unlock (priv->host_lock);
1022 conn = soup_connection_new (
1023 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1024 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1025 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1026 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1027 SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
1028 SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
1030 g_signal_connect (conn, "connect_result",
1031 G_CALLBACK (connect_result),
1033 g_signal_connect (conn, "disconnected",
1034 G_CALLBACK (connection_disconnected),
1036 g_signal_connect (conn, "request_started",
1037 G_CALLBACK (connection_started_request),
1040 g_hash_table_insert (priv->conns, conn, host);
1042 /* We increment the connection counts so it counts against the
1043 * totals, but we don't add it to the host's connection list
1044 * yet, since it's not ready for use.
1049 /* Mark the request as connecting, so we don't try to open
1050 * another new connection for it while waiting for this one.
1052 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
1054 g_mutex_unlock (priv->host_lock);
1060 soup_session_get_queue (SoupSession *session)
1062 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1068 message_finished (SoupMessage *msg, gpointer user_data)
1070 SoupSession *session = user_data;
1071 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1073 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1074 soup_message_queue_remove_message (priv->queue, msg);
1075 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1076 g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
1077 g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
1082 queue_message (SoupSession *session, SoupMessage *msg,
1083 SoupSessionCallback callback, gpointer user_data)
1085 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1087 g_signal_connect_after (msg, "finished",
1088 G_CALLBACK (message_finished), session);
1090 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1091 soup_message_add_header_handler (
1092 msg, "got_body", "Location",
1093 G_CALLBACK (redirect_handler), session);
1096 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1097 soup_message_queue_append (priv->queue, msg);
1099 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
1103 * SoupSessionCallback:
1104 * @session: the session
1105 * @msg: the message that has finished
1106 * @user_data: the data passed to soup_session_queue_message
1108 * Prototype for the callback passed to soup_session_queue_message(),
1113 * soup_session_queue_message:
1114 * @session: a #SoupSession
1115 * @msg: the message to queue
1116 * @callback: a #SoupSessionCallback which will be called after the
1117 * message completes or when an unrecoverable error occurs.
1118 * @user_data: a pointer passed to @callback.
1120 * Queues the message @msg for sending. All messages are processed
1121 * while the glib main loop runs. If @msg has been processed before,
1122 * any resources related to the time it was last sent are freed.
1124 * Upon message completion, the callback specified in @callback will
1125 * be invoked (in the thread associated with @session's async
1126 * context). If after returning from this callback the message has not
1127 * been requeued, @msg will be unreffed.
1130 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1131 SoupSessionCallback callback, gpointer user_data)
1133 g_return_if_fail (SOUP_IS_SESSION (session));
1134 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1136 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1137 callback, user_data);
1141 requeue_message (SoupSession *session, SoupMessage *msg)
1143 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1147 * soup_session_requeue_message:
1148 * @session: a #SoupSession
1149 * @msg: the message to requeue
1151 * This causes @msg to be placed back on the queue to be attempted
1155 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1157 g_return_if_fail (SOUP_IS_SESSION (session));
1158 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1160 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1165 * soup_session_send_message:
1166 * @session: a #SoupSession
1167 * @msg: the message to send
1169 * Synchronously send @msg. This call will not return until the
1170 * transfer is finished successfully or there is an unrecoverable
1173 * @msg is not freed upon return.
1175 * Return value: the HTTP status code of the response
1178 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1180 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1181 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1183 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1188 * soup_session_pause_message:
1189 * @session: a #SoupSession
1190 * @msg: a #SoupMessage currently running on @session
1192 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1196 soup_session_pause_message (SoupSession *session,
1199 g_return_if_fail (SOUP_IS_SESSION (session));
1200 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1202 soup_message_io_pause (msg);
1206 * soup_session_unpause_message:
1207 * @session: a #SoupSession
1208 * @msg: a #SoupMessage currently running on @session
1210 * Resumes HTTP I/O on @msg. Use this to resume after calling
1211 * soup_sessino_pause_message().
1213 * If @msg is being sent via blocking I/O, this will resume reading or
1214 * writing immediately. If @msg is using non-blocking I/O, then
1215 * reading or writing won't resume until you return to the main loop.
1218 soup_session_unpause_message (SoupSession *session,
1221 g_return_if_fail (SOUP_IS_SESSION (session));
1222 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1224 soup_message_io_unpause (msg);
1229 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1231 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1233 soup_message_queue_remove_message (priv->queue, msg);
1234 soup_message_io_stop (msg);
1235 soup_message_set_status (msg, status_code);
1236 soup_message_finished (msg);
1240 * soup_session_cancel_message:
1241 * @session: a #SoupSession
1242 * @msg: the message to cancel
1243 * @status_code: status code to set on @msg (generally
1244 * %SOUP_STATUS_CANCELLED)
1246 * Causes @session to immediately finish processing @msg, with a final
1247 * status_code of @status_code. Depending on when you cancel it, the
1248 * response state may be incomplete or inconsistent.
1251 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1254 g_return_if_fail (SOUP_IS_SESSION (session));
1255 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1257 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1261 gather_conns (gpointer key, gpointer host, gpointer data)
1263 SoupConnection *conn = key;
1264 GSList **conns = data;
1266 *conns = g_slist_prepend (*conns, conn);
1270 * soup_session_abort:
1271 * @session: the session
1273 * Cancels all pending requests in @session.
1276 soup_session_abort (SoupSession *session)
1278 SoupSessionPrivate *priv;
1279 SoupMessageQueueIter iter;
1283 g_return_if_fail (SOUP_IS_SESSION (session));
1284 priv = SOUP_SESSION_GET_PRIVATE (session);
1286 for (msg = soup_message_queue_first (priv->queue, &iter);
1288 msg = soup_message_queue_next (priv->queue, &iter)) {
1289 soup_session_cancel_message (session, msg,
1290 SOUP_STATUS_CANCELLED);
1293 /* Close all connections */
1294 g_mutex_lock (priv->host_lock);
1296 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1298 for (c = conns; c; c = c->next)
1299 g_object_ref (c->data);
1300 g_mutex_unlock (priv->host_lock);
1301 for (c = conns; c; c = c->next) {
1302 soup_connection_disconnect (c->data);
1303 g_object_unref (c->data);
1306 g_slist_free (conns);