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 */
71 guint max_conns, max_conns_per_host;
74 SoupSSLCredentials *ssl_creds;
76 SoupMessageQueue *queue;
80 SoupAuthManager *auth_manager;
81 SoupAuthManagerNTLM *ntlm_manager;
83 GHashTable *hosts; /* SoupURI -> SoupSessionHost */
84 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
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 /* Holds the timeout value for the connection, when
96 no response is received.
100 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
102 static void free_host (SoupSessionHost *host);
104 static void queue_message (SoupSession *session, SoupMessage *msg,
105 SoupSessionCallback callback, gpointer user_data);
106 static void requeue_message (SoupSession *session, SoupMessage *msg);
107 static void cancel_message (SoupSession *session, SoupMessage *msg,
110 /* temporary until we fix this to index hosts by SoupAddress */
111 extern guint soup_uri_host_hash (gconstpointer key);
112 extern gboolean soup_uri_host_equal (gconstpointer v1,
114 extern SoupURI *soup_uri_copy_root (SoupURI *uri);
116 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
117 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
119 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
121 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
131 static guint signals[LAST_SIGNAL] = { 0 };
138 PROP_MAX_CONNS_PER_HOST,
148 static void set_property (GObject *object, guint prop_id,
149 const GValue *value, GParamSpec *pspec);
150 static void get_property (GObject *object, guint prop_id,
151 GValue *value, GParamSpec *pspec);
154 soup_session_init (SoupSession *session)
156 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
158 priv->queue = soup_message_queue_new ();
160 priv->host_lock = g_mutex_new ();
161 priv->hosts = g_hash_table_new (soup_uri_host_hash,
162 soup_uri_host_equal);
163 priv->conns = g_hash_table_new (NULL, NULL);
165 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
166 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
170 priv->auth_manager = soup_auth_manager_new (session);
171 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
172 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
176 foreach_free_host (gpointer key, gpointer host, gpointer data)
183 cleanup_hosts (SoupSessionPrivate *priv)
185 GHashTable *old_hosts;
187 g_mutex_lock (priv->host_lock);
188 old_hosts = priv->hosts;
189 priv->hosts = g_hash_table_new (soup_uri_host_hash,
190 soup_uri_host_equal);
191 g_mutex_unlock (priv->host_lock);
193 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
194 g_hash_table_destroy (old_hosts);
198 dispose (GObject *object)
200 SoupSession *session = SOUP_SESSION (object);
201 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
203 soup_session_abort (session);
204 cleanup_hosts (priv);
206 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
210 finalize (GObject *object)
212 SoupSession *session = SOUP_SESSION (object);
213 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
215 soup_message_queue_destroy (priv->queue);
217 g_mutex_free (priv->host_lock);
218 g_hash_table_destroy (priv->hosts);
219 g_hash_table_destroy (priv->conns);
221 g_free (priv->user_agent);
223 soup_auth_manager_free (priv->auth_manager);
224 if (priv->ntlm_manager)
225 soup_auth_manager_ntlm_free (priv->ntlm_manager);
228 soup_uri_free (priv->proxy_uri);
231 soup_ssl_free_client_credentials (priv->ssl_creds);
233 if (priv->async_context)
234 g_main_context_unref (priv->async_context);
236 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
240 soup_session_class_init (SoupSessionClass *session_class)
242 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
244 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
246 /* virtual method definition */
247 session_class->queue_message = queue_message;
248 session_class->requeue_message = requeue_message;
249 session_class->cancel_message = cancel_message;
251 /* virtual method override */
252 object_class->dispose = dispose;
253 object_class->finalize = finalize;
254 object_class->set_property = set_property;
255 object_class->get_property = get_property;
260 * SoupSession::request-queued:
261 * @session: the session
262 * @msg: the request that was queued
264 * Emitted when a request is queued on @session. (Note that
265 * "queued" doesn't just mean soup_session_queue_message();
266 * soup_session_send_message() implicitly queues the message
269 * When sending a request, first #SoupSession::request_queued
270 * is emitted, indicating that the session has become aware of
273 * Once a connection is available to send the request on, the
274 * session emits #SoupSession::request_started. Then, various
275 * #SoupMessage signals are emitted as the message is
276 * processed. If the message is requeued, it will emit
277 * #SoupMessage::restarted, which will then be followed by
278 * another #SoupSession::request_started and another set of
279 * #SoupMessage signals when the message is re-sent.
281 * Eventually, the message will emit #SoupMessage::finished.
282 * Normally, this signals the completion of message
283 * processing. However, it is possible that the application
284 * will requeue the message from the "finished" handler (or
285 * equivalently, from the soup_session_queue_message()
286 * callback). In that case, the process will loop back to
287 * #SoupSession::request_started.
289 * Eventually, a message will reach "finished" and not be
290 * requeued. At that point, the session will emit
291 * #SoupSession::request_unqueued to indicate that it is done
294 * To sum up: #SoupSession::request_queued and
295 * #SoupSession::request_unqueued are guaranteed to be emitted
296 * exactly once, but #SoupSession::request_started and
297 * #SoupMessage::finished (and all of the other #SoupMessage
298 * signals) may be invoked multiple times for a given message.
300 signals[REQUEST_QUEUED] =
301 g_signal_new ("request-queued",
302 G_OBJECT_CLASS_TYPE (object_class),
306 soup_marshal_NONE__OBJECT,
311 * SoupSession::request-started:
312 * @session: the session
313 * @msg: the request being sent
314 * @socket: the socket the request is being sent on
316 * Emitted just before a request is sent. See
317 * #SoupSession::request_queued for a detailed description of
318 * the message lifecycle within a session.
320 signals[REQUEST_STARTED] =
321 g_signal_new ("request-started",
322 G_OBJECT_CLASS_TYPE (object_class),
324 G_STRUCT_OFFSET (SoupSessionClass, request_started),
326 soup_marshal_NONE__OBJECT_OBJECT,
332 * SoupSession::request-unqueued:
333 * @session: the session
334 * @msg: the request that was unqueued
336 * Emitted when a request is removed from @session's queue,
337 * indicating that @session is done with it. See
338 * #SoupSession::request_queued for a detailed description of the
339 * message lifecycle within a session.
341 signals[REQUEST_UNQUEUED] =
342 g_signal_new ("request-unqueued",
343 G_OBJECT_CLASS_TYPE (object_class),
347 soup_marshal_NONE__OBJECT,
352 * SoupSession::authenticate:
353 * @session: the session
354 * @msg: the #SoupMessage being sent
355 * @auth: the #SoupAuth to authenticate
356 * @retrying: %TRUE if this is the second (or later) attempt
358 * Emitted when the session requires authentication. If
359 * credentials are available call soup_auth_authenticate() on
360 * @auth. If these credentials fail, the signal will be
361 * emitted again, with @retrying set to %TRUE, which will
362 * continue until you return without calling
363 * soup_auth_authenticate() on @auth.
365 * Note that this may be emitted before @msg's body has been
368 * If you call soup_session_pause_message() on @msg before
369 * returning, then you can authenticate @auth asynchronously
370 * (as long as you g_object_ref() it to make sure it doesn't
371 * get destroyed), and then unpause @msg when you are ready
372 * for it to continue.
374 signals[AUTHENTICATE] =
375 g_signal_new ("authenticate",
376 G_OBJECT_CLASS_TYPE (object_class),
378 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
380 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
387 g_object_class_install_property (
388 object_class, PROP_PROXY_URI,
389 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
391 "The HTTP Proxy to use for this session",
394 g_object_class_install_property (
395 object_class, PROP_MAX_CONNS,
396 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
397 "Max Connection Count",
398 "The maximum number of connections that the session can open at once",
401 SOUP_SESSION_MAX_CONNS_DEFAULT,
403 g_object_class_install_property (
404 object_class, PROP_MAX_CONNS_PER_HOST,
405 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
406 "Max Per-Host Connection Count",
407 "The maximum number of connections that the session can open at once to a given host",
410 SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
412 g_object_class_install_property (
413 object_class, PROP_USE_NTLM,
414 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
416 "Whether or not to use NTLM authentication",
419 g_object_class_install_property (
420 object_class, PROP_SSL_CA_FILE,
421 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
423 "File containing SSL CA certificates",
426 g_object_class_install_property (
427 object_class, PROP_ASYNC_CONTEXT,
428 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
429 "Async GMainContext",
430 "The GMainContext to dispatch async I/O in",
431 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
432 g_object_class_install_property (
433 object_class, PROP_TIMEOUT,
434 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
436 "Value in seconds to timeout a blocking I/O",
441 * SoupSession:user-agent:
443 * If non-%NULL, the value to use for the "User-Agent" header
444 * on #SoupMessage<!-- -->s sent from this session.
446 * RFC 2616 says: "The User-Agent request-header field
447 * contains information about the user agent originating the
448 * request. This is for statistical purposes, the tracing of
449 * protocol violations, and automated recognition of user
450 * agents for the sake of tailoring responses to avoid
451 * particular user agent limitations. User agents SHOULD
452 * include this field with requests."
454 * The User-Agent header contains a list of one or more
455 * product tokens, separated by whitespace, with the most
456 * significant product token coming first. The tokens must be
457 * brief, ASCII, and mostly alphanumeric (although "-", "_",
458 * and "." are also allowed), and may optionally include a "/"
459 * followed by a version string. You may also put comments,
460 * enclosed in parentheses, between or after the tokens.
462 * If you set a %user_agent property that has trailing
463 * whitespace, #SoupSession will append its own product token
464 * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
467 g_object_class_install_property (
468 object_class, PROP_USER_AGENT,
469 g_param_spec_string (SOUP_SESSION_USER_AGENT,
477 safe_uri_equal (SoupURI *a, SoupURI *b)
482 if ((a && !b) || (b && !a))
485 return soup_uri_equal (a, b);
489 safe_str_equal (const char *a, const char *b)
494 if ((a && !b) || (b && !a))
497 return strcmp (a, b) == 0;
501 set_property (GObject *object, guint prop_id,
502 const GValue *value, GParamSpec *pspec)
504 SoupSession *session = SOUP_SESSION (object);
505 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
507 gboolean need_abort = FALSE;
508 gboolean ca_file_changed = FALSE;
509 const char *new_ca_file, *user_agent;
513 uri = g_value_get_boxed (value);
515 if (!safe_uri_equal (priv->proxy_uri, uri))
519 soup_uri_free (priv->proxy_uri);
521 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
524 soup_session_abort (session);
525 cleanup_hosts (priv);
530 priv->max_conns = g_value_get_int (value);
532 case PROP_MAX_CONNS_PER_HOST:
533 priv->max_conns_per_host = g_value_get_int (value);
536 if (g_value_get_boolean (value)) {
537 if (!priv->ntlm_manager)
538 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
540 if (priv->ntlm_manager) {
541 soup_auth_manager_ntlm_free (priv->ntlm_manager);
542 priv->ntlm_manager = NULL;
546 case PROP_SSL_CA_FILE:
547 new_ca_file = g_value_get_string (value);
549 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
550 ca_file_changed = TRUE;
552 g_free (priv->ssl_ca_file);
553 priv->ssl_ca_file = g_strdup (new_ca_file);
555 if (ca_file_changed) {
556 if (priv->ssl_creds) {
557 soup_ssl_free_client_credentials (priv->ssl_creds);
558 priv->ssl_creds = NULL;
561 cleanup_hosts (priv);
565 case PROP_ASYNC_CONTEXT:
566 priv->async_context = g_value_get_pointer (value);
567 if (priv->async_context)
568 g_main_context_ref (priv->async_context);
571 priv->timeout = g_value_get_uint (value);
573 case PROP_USER_AGENT:
574 g_free (priv->user_agent);
575 user_agent = g_value_get_string (value);
577 priv->user_agent = NULL;
578 else if (!*user_agent) {
580 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
581 } else if (g_str_has_suffix (user_agent, " ")) {
583 g_strdup_printf ("%s%s", user_agent,
584 SOUP_SESSION_USER_AGENT_BASE);
586 priv->user_agent = g_strdup (user_agent);
589 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595 get_property (GObject *object, guint prop_id,
596 GValue *value, GParamSpec *pspec)
598 SoupSession *session = SOUP_SESSION (object);
599 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
603 g_value_set_boxed (value, priv->proxy_uri);
606 g_value_set_int (value, priv->max_conns);
608 case PROP_MAX_CONNS_PER_HOST:
609 g_value_set_int (value, priv->max_conns_per_host);
612 g_value_set_boolean (value, priv->ntlm_manager != NULL);
614 case PROP_SSL_CA_FILE:
615 g_value_set_string (value, priv->ssl_ca_file);
617 case PROP_ASYNC_CONTEXT:
618 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
621 g_value_set_uint (value, priv->timeout);
623 case PROP_USER_AGENT:
624 g_value_set_string (value, priv->user_agent);
627 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634 * soup_session_get_async_context:
635 * @session: a #SoupSession
637 * Gets @session's async_context. This does not add a ref to the
638 * context, so you will need to ref it yourself if you want it to
639 * outlive its session.
641 * Return value: @session's #GMainContext, which may be %NULL
644 soup_session_get_async_context (SoupSession *session)
646 SoupSessionPrivate *priv;
648 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
649 priv = SOUP_SESSION_GET_PRIVATE (session);
651 return priv->async_context;
656 static SoupSessionHost *
657 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
659 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
660 SoupSessionHost *host;
662 host = g_slice_new0 (SoupSessionHost);
663 host->root_uri = soup_uri_copy_root (source_uri);
665 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
668 soup_ssl_get_client_credentials (priv->ssl_ca_file);
674 /* Note: get_host_for_message doesn't lock the host_lock. The caller
675 * must do it itself if there's a chance the host doesn't already
678 static SoupSessionHost *
679 get_host_for_message (SoupSession *session, SoupMessage *msg)
681 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
682 SoupSessionHost *host;
683 SoupURI *source = soup_message_get_uri (msg);
685 host = g_hash_table_lookup (priv->hosts, source);
689 host = soup_session_host_new (session, source);
690 g_hash_table_insert (priv->hosts, host->root_uri, host);
696 free_host (SoupSessionHost *host)
698 while (host->connections) {
699 SoupConnection *conn = host->connections->data;
701 host->connections = g_slist_remove (host->connections, conn);
702 soup_connection_disconnect (conn);
705 soup_uri_free (host->root_uri);
706 g_slice_free (SoupSessionHost, host);
710 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
711 SoupAuth *auth, gboolean retrying)
713 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
717 redirect_handler (SoupMessage *msg, gpointer user_data)
719 SoupSession *session = user_data;
723 new_loc = soup_message_headers_get (msg->response_headers, "Location");
724 g_return_if_fail (new_loc != NULL);
726 if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
727 msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
728 /* Don't redirect non-safe methods */
729 if (msg->method != SOUP_METHOD_GET &&
730 msg->method != SOUP_METHOD_HEAD &&
731 msg->method != SOUP_METHOD_OPTIONS &&
732 msg->method != SOUP_METHOD_PROPFIND)
734 } else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
735 msg->status_code == SOUP_STATUS_FOUND) {
736 /* Redirect using a GET */
738 SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
740 soup_message_set_request (msg, NULL,
741 SOUP_MEMORY_STATIC, NULL, 0);
742 soup_message_headers_set_encoding (msg->request_headers,
745 /* Three possibilities:
747 * 1) This was a non-3xx response that happened to
748 * have a "Location" header
749 * 2) It's a non-redirecty 3xx response (300, 304,
751 * 3) It's some newly-defined 3xx response (308+)
753 * We ignore all of these cases. In the first two,
754 * redirecting would be explicitly wrong, and in the
755 * last case, we have no clue if the 3xx response is
756 * supposed to be redirecty or non-redirecty. Plus,
757 * 2616 says unrecognized status codes should be
758 * treated as the equivalent to the x00 code, and we
759 * don't redirect on 300, so therefore we shouldn't
760 * redirect on 308+ either.
765 /* Location is supposed to be an absolute URI, but some sites
766 * are lame, so we use soup_uri_new_with_base().
768 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
770 soup_message_set_status_full (msg,
771 SOUP_STATUS_MALFORMED,
772 "Invalid Redirect URL");
776 soup_message_set_uri (msg, new_uri);
777 soup_uri_free (new_uri);
779 soup_session_requeue_message (session, msg);
783 connection_started_request (SoupConnection *conn, SoupMessage *msg,
786 SoupSession *session = data;
787 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
789 if (priv->user_agent) {
790 soup_message_headers_replace (msg->request_headers,
791 "User-Agent", priv->user_agent);
794 /* Kludge to deal with the fact that CONNECT msgs come from the
795 * SoupConnection rather than being queued normally.
797 if (msg->method == SOUP_METHOD_CONNECT)
798 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
800 g_signal_emit (session, signals[REQUEST_STARTED], 0,
801 msg, soup_connection_get_socket (conn));
805 find_oldest_connection (gpointer key, gpointer host, gpointer data)
807 SoupConnection *conn = key, **oldest = data;
809 /* Don't prune a connection that is currently in use, or
810 * hasn't been used yet.
812 if (soup_connection_is_in_use (conn) ||
813 soup_connection_last_used (conn) == 0)
816 if (!*oldest || (soup_connection_last_used (conn) <
817 soup_connection_last_used (*oldest)))
822 * soup_session_try_prune_connection:
823 * @session: a #SoupSession
825 * Finds the least-recently-used idle connection in @session and closes
828 * Return value: %TRUE if a connection was closed, %FALSE if there are
829 * no idle connections.
832 soup_session_try_prune_connection (SoupSession *session)
834 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
835 SoupConnection *oldest = NULL;
837 g_mutex_lock (priv->host_lock);
838 g_hash_table_foreach (priv->conns, find_oldest_connection,
841 /* Ref the connection before unlocking the mutex in
842 * case someone else tries to prune it too.
844 g_object_ref (oldest);
845 g_mutex_unlock (priv->host_lock);
846 soup_connection_disconnect (oldest);
847 g_object_unref (oldest);
850 g_mutex_unlock (priv->host_lock);
856 connection_disconnected (SoupConnection *conn, gpointer user_data)
858 SoupSession *session = user_data;
859 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
860 SoupSessionHost *host;
862 g_mutex_lock (priv->host_lock);
864 host = g_hash_table_lookup (priv->conns, conn);
866 g_hash_table_remove (priv->conns, conn);
867 host->connections = g_slist_remove (host->connections, conn);
871 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
874 g_mutex_unlock (priv->host_lock);
875 g_object_unref (conn);
879 connect_result (SoupConnection *conn, guint status, gpointer user_data)
881 SoupSession *session = user_data;
882 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
883 SoupSessionHost *host;
884 SoupMessageQueueIter iter;
887 g_mutex_lock (priv->host_lock);
889 host = g_hash_table_lookup (priv->conns, conn);
891 g_mutex_unlock (priv->host_lock);
895 if (status == SOUP_STATUS_OK) {
896 soup_connection_reserve (conn);
897 host->connections = g_slist_prepend (host->connections, conn);
898 g_mutex_unlock (priv->host_lock);
902 /* The connection failed. */
903 g_mutex_unlock (priv->host_lock);
904 connection_disconnected (conn, session);
906 if (host->connections) {
907 /* Something went wrong this time, but we have at
908 * least one open connection to this host. So just
909 * leave the message in the queue so it can use that
910 * connection once it's free.
915 /* There are two possibilities: either status is
916 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
917 * will create a new connection (and all we need to do here
918 * is downgrade the message from CONNECTING to QUEUED); or
919 * status is something else, probably CANT_CONNECT or
920 * CANT_RESOLVE or the like, in which case we need to cancel
921 * any messages waiting for this host, since they're out
924 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
925 if (get_host_for_message (session, msg) == host) {
926 if (status == SOUP_STATUS_TRY_AGAIN) {
927 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
928 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
930 soup_session_cancel_message (session, msg,
938 * soup_session_get_connection:
939 * @session: a #SoupSession
940 * @msg: a #SoupMessage
941 * @try_pruning: on return, whether or not to try pruning a connection
942 * @is_new: on return, %TRUE if the returned connection is new and not
945 * Tries to find or create a connection for @msg; this is an internal
946 * method for #SoupSession subclasses.
948 * If there is an idle connection to the relevant host available, then
949 * that connection will be returned (with *@is_new set to %FALSE). The
950 * connection will be marked "reserved", so the caller must call
951 * soup_connection_release() if it ends up not using the connection
954 * If there is no idle connection available, but it is possible to
955 * create a new connection, then one will be created and returned,
956 * with *@is_new set to %TRUE. The caller MUST then call
957 * soup_connection_connect_sync() or soup_connection_connect_async()
958 * to connect it. If the connection attempt succeeds, the connection
959 * will be marked "reserved" and added to @session's connection pool
960 * once it connects. If the connection attempt fails, the connection
963 * If no connection is available and a new connection cannot be made,
964 * soup_session_get_connection() will return %NULL. If @session has
965 * the maximum number of open connections open, but does not have the
966 * maximum number of per-host connections open to the relevant host,
967 * then *@try_pruning will be set to %TRUE. In this case, the caller
968 * can call soup_session_try_prune_connection() to close an idle
969 * connection, and then try soup_session_get_connection() again. (If
970 * calling soup_session_try_prune_connection() wouldn't help, then
971 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
973 * Return value: a #SoupConnection, or %NULL
976 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
977 gboolean *try_pruning, gboolean *is_new)
979 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
980 SoupConnection *conn;
981 SoupSessionHost *host;
984 g_mutex_lock (priv->host_lock);
986 host = get_host_for_message (session, msg);
987 for (conns = host->connections; conns; conns = conns->next) {
988 if (!soup_connection_is_in_use (conns->data)) {
989 soup_connection_reserve (conns->data);
990 g_mutex_unlock (priv->host_lock);
996 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
997 /* We already started a connection for this
998 * message, so don't start another one.
1000 g_mutex_unlock (priv->host_lock);
1004 if (host->num_conns >= priv->max_conns_per_host) {
1005 g_mutex_unlock (priv->host_lock);
1009 if (priv->num_conns >= priv->max_conns) {
1010 *try_pruning = TRUE;
1011 g_mutex_unlock (priv->host_lock);
1015 conn = soup_connection_new (
1016 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
1017 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
1018 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
1019 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
1020 SOUP_CONNECTION_TIMEOUT, priv->timeout,
1022 g_signal_connect (conn, "connect_result",
1023 G_CALLBACK (connect_result),
1025 g_signal_connect (conn, "disconnected",
1026 G_CALLBACK (connection_disconnected),
1028 g_signal_connect (conn, "request_started",
1029 G_CALLBACK (connection_started_request),
1032 g_hash_table_insert (priv->conns, conn, host);
1034 /* We increment the connection counts so it counts against the
1035 * totals, but we don't add it to the host's connection list
1036 * yet, since it's not ready for use.
1041 /* Mark the request as connecting, so we don't try to open
1042 * another new connection for it while waiting for this one.
1044 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
1046 g_mutex_unlock (priv->host_lock);
1052 soup_session_get_queue (SoupSession *session)
1054 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1060 message_finished (SoupMessage *msg, gpointer user_data)
1062 SoupSession *session = user_data;
1063 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1065 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
1066 soup_message_queue_remove_message (priv->queue, msg);
1067 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
1068 g_signal_handlers_disconnect_by_func (msg, redirect_handler, session);
1069 g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
1074 queue_message (SoupSession *session, SoupMessage *msg,
1075 SoupSessionCallback callback, gpointer user_data)
1077 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1079 g_signal_connect_after (msg, "finished",
1080 G_CALLBACK (message_finished), session);
1082 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1083 soup_message_add_header_handler (
1084 msg, "got_body", "Location",
1085 G_CALLBACK (redirect_handler), session);
1088 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1089 soup_message_queue_append (priv->queue, msg);
1091 g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
1095 * SoupSessionCallback:
1096 * @session: the session
1097 * @msg: the message that has finished
1098 * @user_data: the data passed to soup_session_queue_message
1100 * Prototype for the callback passed to soup_session_queue_message(),
1105 * soup_session_queue_message:
1106 * @session: a #SoupSession
1107 * @msg: the message to queue
1108 * @callback: a #SoupSessionCallback which will be called after the
1109 * message completes or when an unrecoverable error occurs.
1110 * @user_data: a pointer passed to @callback.
1112 * Queues the message @msg for sending. All messages are processed
1113 * while the glib main loop runs. If @msg has been processed before,
1114 * any resources related to the time it was last sent are freed.
1116 * Upon message completion, the callback specified in @callback will
1117 * be invoked (in the thread associated with @session's async
1118 * context). If after returning from this callback the message has not
1119 * been requeued, @msg will be unreffed.
1122 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1123 SoupSessionCallback callback, gpointer user_data)
1125 g_return_if_fail (SOUP_IS_SESSION (session));
1126 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1128 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1129 callback, user_data);
1133 requeue_message (SoupSession *session, SoupMessage *msg)
1135 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1139 * soup_session_requeue_message:
1140 * @session: a #SoupSession
1141 * @msg: the message to requeue
1143 * This causes @msg to be placed back on the queue to be attempted
1147 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1149 g_return_if_fail (SOUP_IS_SESSION (session));
1150 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1152 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1157 * soup_session_send_message:
1158 * @session: a #SoupSession
1159 * @msg: the message to send
1161 * Synchronously send @msg. This call will not return until the
1162 * transfer is finished successfully or there is an unrecoverable
1165 * @msg is not freed upon return.
1167 * Return value: the HTTP status code of the response
1170 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1172 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1173 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1175 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1180 * soup_session_pause_message:
1181 * @session: a #SoupSession
1182 * @msg: a #SoupMessage currently running on @session
1184 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1188 soup_session_pause_message (SoupSession *session,
1191 g_return_if_fail (SOUP_IS_SESSION (session));
1192 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1194 soup_message_io_pause (msg);
1198 * soup_session_unpause_message:
1199 * @session: a #SoupSession
1200 * @msg: a #SoupMessage currently running on @session
1202 * Resumes HTTP I/O on @msg. Use this to resume after calling
1203 * soup_sessino_pause_message().
1205 * If @msg is being sent via blocking I/O, this will resume reading or
1206 * writing immediately. If @msg is using non-blocking I/O, then
1207 * reading or writing won't resume until you return to the main loop.
1210 soup_session_unpause_message (SoupSession *session,
1213 g_return_if_fail (SOUP_IS_SESSION (session));
1214 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1216 soup_message_io_unpause (msg);
1221 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1223 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1225 soup_message_queue_remove_message (priv->queue, msg);
1226 soup_message_io_stop (msg);
1227 soup_message_set_status (msg, status_code);
1228 soup_message_finished (msg);
1232 * soup_session_cancel_message:
1233 * @session: a #SoupSession
1234 * @msg: the message to cancel
1235 * @status_code: status code to set on @msg (generally
1236 * %SOUP_STATUS_CANCELLED)
1238 * Causes @session to immediately finish processing @msg, with a final
1239 * status_code of @status_code. Depending on when you cancel it, the
1240 * response state may be incomplete or inconsistent.
1243 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1246 g_return_if_fail (SOUP_IS_SESSION (session));
1247 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1249 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1253 gather_conns (gpointer key, gpointer host, gpointer data)
1255 SoupConnection *conn = key;
1256 GSList **conns = data;
1258 *conns = g_slist_prepend (*conns, conn);
1262 * soup_session_abort:
1263 * @session: the session
1265 * Cancels all pending requests in @session.
1268 soup_session_abort (SoupSession *session)
1270 SoupSessionPrivate *priv;
1271 SoupMessageQueueIter iter;
1275 g_return_if_fail (SOUP_IS_SESSION (session));
1276 priv = SOUP_SESSION_GET_PRIVATE (session);
1278 for (msg = soup_message_queue_first (priv->queue, &iter);
1280 msg = soup_message_queue_next (priv->queue, &iter)) {
1281 soup_session_cancel_message (session, msg,
1282 SOUP_STATUS_CANCELLED);
1285 /* Close all connections */
1286 g_mutex_lock (priv->host_lock);
1288 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1290 for (c = conns; c; c = c->next)
1291 g_object_ref (c->data);
1292 g_mutex_unlock (priv->host_lock);
1293 for (c = conns; c; c = c->next) {
1294 soup_connection_disconnect (c->data);
1295 g_object_unref (c->data);
1298 g_slist_free (conns);