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)
129 static guint signals[LAST_SIGNAL] = { 0 };
136 PROP_MAX_CONNS_PER_HOST,
146 static void set_property (GObject *object, guint prop_id,
147 const GValue *value, GParamSpec *pspec);
148 static void get_property (GObject *object, guint prop_id,
149 GValue *value, GParamSpec *pspec);
152 soup_session_init (SoupSession *session)
154 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
156 priv->queue = soup_message_queue_new ();
158 priv->host_lock = g_mutex_new ();
159 priv->hosts = g_hash_table_new (soup_uri_host_hash,
160 soup_uri_host_equal);
161 priv->conns = g_hash_table_new (NULL, NULL);
163 priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
164 priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
168 priv->auth_manager = soup_auth_manager_new (session);
169 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
170 soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
174 foreach_free_host (gpointer key, gpointer host, gpointer data)
181 cleanup_hosts (SoupSessionPrivate *priv)
183 GHashTable *old_hosts;
185 g_mutex_lock (priv->host_lock);
186 old_hosts = priv->hosts;
187 priv->hosts = g_hash_table_new (soup_uri_host_hash,
188 soup_uri_host_equal);
189 g_mutex_unlock (priv->host_lock);
191 g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
192 g_hash_table_destroy (old_hosts);
196 dispose (GObject *object)
198 SoupSession *session = SOUP_SESSION (object);
199 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
201 soup_session_abort (session);
202 cleanup_hosts (priv);
204 G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
208 finalize (GObject *object)
210 SoupSession *session = SOUP_SESSION (object);
211 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
213 soup_message_queue_destroy (priv->queue);
215 g_mutex_free (priv->host_lock);
216 g_hash_table_destroy (priv->hosts);
217 g_hash_table_destroy (priv->conns);
219 g_free (priv->user_agent);
221 soup_auth_manager_free (priv->auth_manager);
222 if (priv->ntlm_manager)
223 soup_auth_manager_ntlm_free (priv->ntlm_manager);
226 soup_uri_free (priv->proxy_uri);
229 soup_ssl_free_client_credentials (priv->ssl_creds);
231 if (priv->async_context)
232 g_main_context_unref (priv->async_context);
234 G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
238 soup_session_class_init (SoupSessionClass *session_class)
240 GObjectClass *object_class = G_OBJECT_CLASS (session_class);
242 g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
244 /* virtual method definition */
245 session_class->queue_message = queue_message;
246 session_class->requeue_message = requeue_message;
247 session_class->cancel_message = cancel_message;
249 /* virtual method override */
250 object_class->dispose = dispose;
251 object_class->finalize = finalize;
252 object_class->set_property = set_property;
253 object_class->get_property = get_property;
258 * SoupSession::request-started:
259 * @session: the session
260 * @msg: the request being sent
261 * @socket: the socket the request is being sent on
263 * Emitted just before a request is sent.
265 signals[REQUEST_STARTED] =
266 g_signal_new ("request-started",
267 G_OBJECT_CLASS_TYPE (object_class),
269 G_STRUCT_OFFSET (SoupSessionClass, request_started),
271 soup_marshal_NONE__OBJECT_OBJECT,
277 * SoupSession::authenticate:
278 * @session: the session
279 * @msg: the #SoupMessage being sent
280 * @auth: the #SoupAuth to authenticate
281 * @retrying: %TRUE if this is the second (or later) attempt
283 * Emitted when the session requires authentication. If
284 * credentials are available call soup_auth_authenticate() on
285 * @auth. If these credentials fail, the signal will be
286 * emitted again, with @retrying set to %TRUE, which will
287 * continue until you return without calling
288 * soup_auth_authenticate() on @auth.
290 * Note that this may be emitted before @msg's body has been
293 * If you call soup_session_pause_message() on @msg before
294 * returning, then you can authenticate @auth asynchronously
295 * (as long as you g_object_ref() it to make sure it doesn't
296 * get destroyed), and then unpause @msg when you are ready
297 * for it to continue.
299 signals[AUTHENTICATE] =
300 g_signal_new ("authenticate",
301 G_OBJECT_CLASS_TYPE (object_class),
303 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
305 soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
312 g_object_class_install_property (
313 object_class, PROP_PROXY_URI,
314 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
316 "The HTTP Proxy to use for this session",
319 g_object_class_install_property (
320 object_class, PROP_MAX_CONNS,
321 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
322 "Max Connection Count",
323 "The maximum number of connections that the session can open at once",
326 SOUP_SESSION_MAX_CONNS_DEFAULT,
328 g_object_class_install_property (
329 object_class, PROP_MAX_CONNS_PER_HOST,
330 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
331 "Max Per-Host Connection Count",
332 "The maximum number of connections that the session can open at once to a given host",
335 SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
337 g_object_class_install_property (
338 object_class, PROP_USE_NTLM,
339 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
341 "Whether or not to use NTLM authentication",
344 g_object_class_install_property (
345 object_class, PROP_SSL_CA_FILE,
346 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
348 "File containing SSL CA certificates",
351 g_object_class_install_property (
352 object_class, PROP_ASYNC_CONTEXT,
353 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
354 "Async GMainContext",
355 "The GMainContext to dispatch async I/O in",
356 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
357 g_object_class_install_property (
358 object_class, PROP_TIMEOUT,
359 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
361 "Value in seconds to timeout a blocking I/O",
366 * SoupSession:user-agent:
368 * If non-%NULL, the value to use for the "User-Agent" header
369 * on #SoupMessage<!-- -->s sent from this session.
371 * RFC 2616 says: "The User-Agent request-header field
372 * contains information about the user agent originating the
373 * request. This is for statistical purposes, the tracing of
374 * protocol violations, and automated recognition of user
375 * agents for the sake of tailoring responses to avoid
376 * particular user agent limitations. User agents SHOULD
377 * include this field with requests."
379 * The User-Agent header contains a list of one or more
380 * product tokens, separated by whitespace, with the most
381 * significant product token coming first. The tokens must be
382 * brief, ASCII, and mostly alphanumeric (although "-", "_",
383 * and "." are also allowed), and may optionally include a "/"
384 * followed by a version string. You may also put comments,
385 * enclosed in parentheses, between or after the tokens.
387 * If you set a %user_agent property that has trailing
388 * whitespace, #SoupSession will append its own product token
389 * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
392 g_object_class_install_property (
393 object_class, PROP_USER_AGENT,
394 g_param_spec_string (SOUP_SESSION_USER_AGENT,
402 safe_uri_equal (SoupURI *a, SoupURI *b)
407 if ((a && !b) || (b && !a))
410 return soup_uri_equal (a, b);
414 safe_str_equal (const char *a, const char *b)
419 if ((a && !b) || (b && !a))
422 return strcmp (a, b) == 0;
426 set_property (GObject *object, guint prop_id,
427 const GValue *value, GParamSpec *pspec)
429 SoupSession *session = SOUP_SESSION (object);
430 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
432 gboolean need_abort = FALSE;
433 gboolean ca_file_changed = FALSE;
434 const char *new_ca_file, *user_agent;
438 uri = g_value_get_boxed (value);
440 if (!safe_uri_equal (priv->proxy_uri, uri))
444 soup_uri_free (priv->proxy_uri);
446 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
449 soup_session_abort (session);
450 cleanup_hosts (priv);
455 priv->max_conns = g_value_get_int (value);
457 case PROP_MAX_CONNS_PER_HOST:
458 priv->max_conns_per_host = g_value_get_int (value);
461 if (g_value_get_boolean (value)) {
462 if (!priv->ntlm_manager)
463 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
465 if (priv->ntlm_manager) {
466 soup_auth_manager_ntlm_free (priv->ntlm_manager);
467 priv->ntlm_manager = NULL;
471 case PROP_SSL_CA_FILE:
472 new_ca_file = g_value_get_string (value);
474 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
475 ca_file_changed = TRUE;
477 g_free (priv->ssl_ca_file);
478 priv->ssl_ca_file = g_strdup (new_ca_file);
480 if (ca_file_changed) {
481 if (priv->ssl_creds) {
482 soup_ssl_free_client_credentials (priv->ssl_creds);
483 priv->ssl_creds = NULL;
486 cleanup_hosts (priv);
490 case PROP_ASYNC_CONTEXT:
491 priv->async_context = g_value_get_pointer (value);
492 if (priv->async_context)
493 g_main_context_ref (priv->async_context);
496 priv->timeout = g_value_get_uint (value);
498 case PROP_USER_AGENT:
499 g_free (priv->user_agent);
500 user_agent = g_value_get_string (value);
502 priv->user_agent = NULL;
503 else if (!*user_agent) {
505 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
506 } else if (g_str_has_suffix (user_agent, " ")) {
508 g_strdup_printf ("%s%s", user_agent,
509 SOUP_SESSION_USER_AGENT_BASE);
511 priv->user_agent = g_strdup (user_agent);
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
520 get_property (GObject *object, guint prop_id,
521 GValue *value, GParamSpec *pspec)
523 SoupSession *session = SOUP_SESSION (object);
524 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
528 g_value_set_boxed (value, priv->proxy_uri);
531 g_value_set_int (value, priv->max_conns);
533 case PROP_MAX_CONNS_PER_HOST:
534 g_value_set_int (value, priv->max_conns_per_host);
537 g_value_set_boolean (value, priv->ntlm_manager != NULL);
539 case PROP_SSL_CA_FILE:
540 g_value_set_string (value, priv->ssl_ca_file);
542 case PROP_ASYNC_CONTEXT:
543 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
546 g_value_set_uint (value, priv->timeout);
548 case PROP_USER_AGENT:
549 g_value_set_string (value, priv->user_agent);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559 * soup_session_get_async_context:
560 * @session: a #SoupSession
562 * Gets @session's async_context. This does not add a ref to the
563 * context, so you will need to ref it yourself if you want it to
564 * outlive its session.
566 * Return value: @session's #GMainContext, which may be %NULL
569 soup_session_get_async_context (SoupSession *session)
571 SoupSessionPrivate *priv;
573 g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
574 priv = SOUP_SESSION_GET_PRIVATE (session);
576 return priv->async_context;
581 static SoupSessionHost *
582 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
584 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
585 SoupSessionHost *host;
587 host = g_slice_new0 (SoupSessionHost);
588 host->root_uri = soup_uri_copy_root (source_uri);
590 if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
593 soup_ssl_get_client_credentials (priv->ssl_ca_file);
599 /* Note: get_host_for_message doesn't lock the host_lock. The caller
600 * must do it itself if there's a chance the host doesn't already
603 static SoupSessionHost *
604 get_host_for_message (SoupSession *session, SoupMessage *msg)
606 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
607 SoupSessionHost *host;
608 SoupURI *source = soup_message_get_uri (msg);
610 host = g_hash_table_lookup (priv->hosts, source);
614 host = soup_session_host_new (session, source);
615 g_hash_table_insert (priv->hosts, host->root_uri, host);
621 free_host (SoupSessionHost *host)
623 while (host->connections) {
624 SoupConnection *conn = host->connections->data;
626 host->connections = g_slist_remove (host->connections, conn);
627 soup_connection_disconnect (conn);
630 soup_uri_free (host->root_uri);
631 g_slice_free (SoupSessionHost, host);
635 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
636 SoupAuth *auth, gboolean retrying)
638 g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
642 redirect_handler (SoupMessage *msg, gpointer user_data)
644 SoupSession *session = user_data;
648 new_loc = soup_message_headers_get (msg->response_headers, "Location");
649 g_return_if_fail (new_loc != NULL);
651 if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
652 msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
653 /* Don't redirect non-safe methods */
654 if (msg->method != SOUP_METHOD_GET &&
655 msg->method != SOUP_METHOD_HEAD &&
656 msg->method != SOUP_METHOD_OPTIONS &&
657 msg->method != SOUP_METHOD_PROPFIND)
659 } else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
660 msg->status_code == SOUP_STATUS_FOUND) {
661 /* Redirect using a GET */
663 SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
665 soup_message_set_request (msg, NULL,
666 SOUP_MEMORY_STATIC, NULL, 0);
667 soup_message_headers_set_encoding (msg->request_headers,
670 /* Three possibilities:
672 * 1) This was a non-3xx response that happened to
673 * have a "Location" header
674 * 2) It's a non-redirecty 3xx response (300, 304,
676 * 3) It's some newly-defined 3xx response (308+)
678 * We ignore all of these cases. In the first two,
679 * redirecting would be explicitly wrong, and in the
680 * last case, we have no clue if the 3xx response is
681 * supposed to be redirecty or non-redirecty. Plus,
682 * 2616 says unrecognized status codes should be
683 * treated as the equivalent to the x00 code, and we
684 * don't redirect on 300, so therefore we shouldn't
685 * redirect on 308+ either.
690 /* Location is supposed to be an absolute URI, but some sites
691 * are lame, so we use soup_uri_new_with_base().
693 new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
695 soup_message_set_status_full (msg,
696 SOUP_STATUS_MALFORMED,
697 "Invalid Redirect URL");
701 soup_message_set_uri (msg, new_uri);
702 soup_uri_free (new_uri);
704 soup_session_requeue_message (session, msg);
708 connection_started_request (SoupConnection *conn, SoupMessage *msg,
711 SoupSession *session = data;
712 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
714 if (priv->user_agent) {
715 soup_message_headers_replace (msg->request_headers,
716 "User-Agent", priv->user_agent);
719 g_signal_emit (session, signals[REQUEST_STARTED], 0,
720 msg, soup_connection_get_socket (conn));
724 find_oldest_connection (gpointer key, gpointer host, gpointer data)
726 SoupConnection *conn = key, **oldest = data;
728 /* Don't prune a connection that is currently in use, or
729 * hasn't been used yet.
731 if (soup_connection_is_in_use (conn) ||
732 soup_connection_last_used (conn) == 0)
735 if (!*oldest || (soup_connection_last_used (conn) <
736 soup_connection_last_used (*oldest)))
741 * soup_session_try_prune_connection:
742 * @session: a #SoupSession
744 * Finds the least-recently-used idle connection in @session and closes
747 * Return value: %TRUE if a connection was closed, %FALSE if there are
748 * no idle connections.
751 soup_session_try_prune_connection (SoupSession *session)
753 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
754 SoupConnection *oldest = NULL;
756 g_mutex_lock (priv->host_lock);
757 g_hash_table_foreach (priv->conns, find_oldest_connection,
760 /* Ref the connection before unlocking the mutex in
761 * case someone else tries to prune it too.
763 g_object_ref (oldest);
764 g_mutex_unlock (priv->host_lock);
765 soup_connection_disconnect (oldest);
766 g_object_unref (oldest);
769 g_mutex_unlock (priv->host_lock);
775 connection_disconnected (SoupConnection *conn, gpointer user_data)
777 SoupSession *session = user_data;
778 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
779 SoupSessionHost *host;
781 g_mutex_lock (priv->host_lock);
783 host = g_hash_table_lookup (priv->conns, conn);
785 g_hash_table_remove (priv->conns, conn);
786 host->connections = g_slist_remove (host->connections, conn);
790 g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
793 g_mutex_unlock (priv->host_lock);
794 g_object_unref (conn);
798 connect_result (SoupConnection *conn, guint status, gpointer user_data)
800 SoupSession *session = user_data;
801 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
802 SoupSessionHost *host;
803 SoupMessageQueueIter iter;
806 g_mutex_lock (priv->host_lock);
808 host = g_hash_table_lookup (priv->conns, conn);
810 g_mutex_unlock (priv->host_lock);
814 if (status == SOUP_STATUS_OK) {
815 soup_connection_reserve (conn);
816 host->connections = g_slist_prepend (host->connections, conn);
817 g_mutex_unlock (priv->host_lock);
821 /* The connection failed. */
822 g_mutex_unlock (priv->host_lock);
823 connection_disconnected (conn, session);
825 if (host->connections) {
826 /* Something went wrong this time, but we have at
827 * least one open connection to this host. So just
828 * leave the message in the queue so it can use that
829 * connection once it's free.
834 /* There are two possibilities: either status is
835 * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
836 * will create a new connection (and all we need to do here
837 * is downgrade the message from CONNECTING to QUEUED); or
838 * status is something else, probably CANT_CONNECT or
839 * CANT_RESOLVE or the like, in which case we need to cancel
840 * any messages waiting for this host, since they're out
843 for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
844 if (get_host_for_message (session, msg) == host) {
845 if (status == SOUP_STATUS_TRY_AGAIN) {
846 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
847 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
849 soup_session_cancel_message (session, msg,
857 * soup_session_get_connection:
858 * @session: a #SoupSession
859 * @msg: a #SoupMessage
860 * @try_pruning: on return, whether or not to try pruning a connection
861 * @is_new: on return, %TRUE if the returned connection is new and not
864 * Tries to find or create a connection for @msg; this is an internal
865 * method for #SoupSession subclasses.
867 * If there is an idle connection to the relevant host available, then
868 * that connection will be returned (with *@is_new set to %FALSE). The
869 * connection will be marked "reserved", so the caller must call
870 * soup_connection_release() if it ends up not using the connection
873 * If there is no idle connection available, but it is possible to
874 * create a new connection, then one will be created and returned,
875 * with *@is_new set to %TRUE. The caller MUST then call
876 * soup_connection_connect_sync() or soup_connection_connect_async()
877 * to connect it. If the connection attempt succeeds, the connection
878 * will be marked "reserved" and added to @session's connection pool
879 * once it connects. If the connection attempt fails, the connection
882 * If no connection is available and a new connection cannot be made,
883 * soup_session_get_connection() will return %NULL. If @session has
884 * the maximum number of open connections open, but does not have the
885 * maximum number of per-host connections open to the relevant host,
886 * then *@try_pruning will be set to %TRUE. In this case, the caller
887 * can call soup_session_try_prune_connection() to close an idle
888 * connection, and then try soup_session_get_connection() again. (If
889 * calling soup_session_try_prune_connection() wouldn't help, then
890 * *@try_pruning is left untouched; it is NOT set to %FALSE.)
892 * Return value: a #SoupConnection, or %NULL
895 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
896 gboolean *try_pruning, gboolean *is_new)
898 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
899 SoupConnection *conn;
900 SoupSessionHost *host;
903 g_mutex_lock (priv->host_lock);
905 host = get_host_for_message (session, msg);
906 for (conns = host->connections; conns; conns = conns->next) {
907 if (!soup_connection_is_in_use (conns->data)) {
908 soup_connection_reserve (conns->data);
909 g_mutex_unlock (priv->host_lock);
915 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
916 /* We already started a connection for this
917 * message, so don't start another one.
919 g_mutex_unlock (priv->host_lock);
923 if (host->num_conns >= priv->max_conns_per_host) {
924 g_mutex_unlock (priv->host_lock);
928 if (priv->num_conns >= priv->max_conns) {
930 g_mutex_unlock (priv->host_lock);
934 conn = soup_connection_new (
935 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
936 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
937 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
938 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
939 SOUP_CONNECTION_TIMEOUT, priv->timeout,
941 g_signal_connect (conn, "connect_result",
942 G_CALLBACK (connect_result),
944 g_signal_connect (conn, "disconnected",
945 G_CALLBACK (connection_disconnected),
947 g_signal_connect (conn, "request_started",
948 G_CALLBACK (connection_started_request),
951 g_hash_table_insert (priv->conns, conn, host);
953 /* We increment the connection counts so it counts against the
954 * totals, but we don't add it to the host's connection list
955 * yet, since it's not ready for use.
960 /* Mark the request as connecting, so we don't try to open
961 * another new connection for it while waiting for this one.
963 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
965 g_mutex_unlock (priv->host_lock);
971 soup_session_get_queue (SoupSession *session)
973 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
979 message_finished (SoupMessage *msg, gpointer user_data)
981 SoupSession *session = user_data;
982 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
984 if (!SOUP_MESSAGE_IS_STARTING (msg)) {
985 soup_message_queue_remove_message (priv->queue, msg);
986 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
991 queue_message (SoupSession *session, SoupMessage *msg,
992 SoupSessionCallback callback, gpointer user_data)
994 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
996 g_signal_connect_after (msg, "finished",
997 G_CALLBACK (message_finished), session);
999 if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
1000 soup_message_add_header_handler (
1001 msg, "got_body", "Location",
1002 G_CALLBACK (redirect_handler), session);
1005 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1006 soup_message_queue_append (priv->queue, msg);
1010 * SoupSessionCallback:
1011 * @session: the session
1012 * @msg: the message that has finished
1013 * @user_data: the data passed to soup_session_queue_message
1015 * Prototype for the callback passed to soup_session_queue_message(),
1020 * soup_session_queue_message:
1021 * @session: a #SoupSession
1022 * @msg: the message to queue
1023 * @callback: a #SoupSessionCallback which will be called after the
1024 * message completes or when an unrecoverable error occurs.
1025 * @user_data: a pointer passed to @callback.
1027 * Queues the message @msg for sending. All messages are processed
1028 * while the glib main loop runs. If @msg has been processed before,
1029 * any resources related to the time it was last sent are freed.
1031 * Upon message completion, the callback specified in @callback will
1032 * be invoked (in the thread associated with @session's async
1033 * context). If after returning from this callback the message has not
1034 * been requeued, @msg will be unreffed.
1037 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1038 SoupSessionCallback callback, gpointer user_data)
1040 g_return_if_fail (SOUP_IS_SESSION (session));
1041 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1043 SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1044 callback, user_data);
1048 requeue_message (SoupSession *session, SoupMessage *msg)
1050 soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1054 * soup_session_requeue_message:
1055 * @session: a #SoupSession
1056 * @msg: the message to requeue
1058 * This causes @msg to be placed back on the queue to be attempted
1062 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1064 g_return_if_fail (SOUP_IS_SESSION (session));
1065 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1067 SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1072 * soup_session_send_message:
1073 * @session: a #SoupSession
1074 * @msg: the message to send
1076 * Synchronously send @msg. This call will not return until the
1077 * transfer is finished successfully or there is an unrecoverable
1080 * @msg is not freed upon return.
1082 * Return value: the HTTP status code of the response
1085 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1087 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1088 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1090 return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1095 * soup_session_pause_message:
1096 * @session: a #SoupSession
1097 * @msg: a #SoupMessage currently running on @session
1099 * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1103 soup_session_pause_message (SoupSession *session,
1106 g_return_if_fail (SOUP_IS_SESSION (session));
1107 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1109 soup_message_io_pause (msg);
1113 * soup_session_unpause_message:
1114 * @session: a #SoupSession
1115 * @msg: a #SoupMessage currently running on @session
1117 * Resumes HTTP I/O on @msg. Use this to resume after calling
1118 * soup_sessino_pause_message().
1120 * If @msg is being sent via blocking I/O, this will resume reading or
1121 * writing immediately. If @msg is using non-blocking I/O, then
1122 * reading or writing won't resume until you return to the main loop.
1125 soup_session_unpause_message (SoupSession *session,
1128 g_return_if_fail (SOUP_IS_SESSION (session));
1129 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1131 soup_message_io_unpause (msg);
1136 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1138 SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1140 soup_message_queue_remove_message (priv->queue, msg);
1141 soup_message_io_stop (msg);
1142 soup_message_set_status (msg, status_code);
1143 soup_message_finished (msg);
1147 * soup_session_cancel_message:
1148 * @session: a #SoupSession
1149 * @msg: the message to cancel
1150 * @status_code: status code to set on @msg (generally
1151 * %SOUP_STATUS_CANCELLED)
1153 * Causes @session to immediately finish processing @msg, with a final
1154 * status_code of @status_code. Depending on when you cancel it, the
1155 * response state may be incomplete or inconsistent.
1158 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1161 g_return_if_fail (SOUP_IS_SESSION (session));
1162 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1164 SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1168 gather_conns (gpointer key, gpointer host, gpointer data)
1170 SoupConnection *conn = key;
1171 GSList **conns = data;
1173 *conns = g_slist_prepend (*conns, conn);
1177 * soup_session_abort:
1178 * @session: the session
1180 * Cancels all pending requests in @session.
1183 soup_session_abort (SoupSession *session)
1185 SoupSessionPrivate *priv;
1186 SoupMessageQueueIter iter;
1190 g_return_if_fail (SOUP_IS_SESSION (session));
1191 priv = SOUP_SESSION_GET_PRIVATE (session);
1193 for (msg = soup_message_queue_first (priv->queue, &iter);
1195 msg = soup_message_queue_next (priv->queue, &iter)) {
1196 soup_session_cancel_message (session, msg,
1197 SOUP_STATUS_CANCELLED);
1200 /* Close all connections */
1201 g_mutex_lock (priv->host_lock);
1203 g_hash_table_foreach (priv->conns, gather_conns, &conns);
1205 for (c = conns; c; c = c->next)
1206 g_object_ref (c->data);
1207 g_mutex_unlock (priv->host_lock);
1208 for (c = conns; c; c = c->next) {
1209 soup_connection_disconnect (c->data);
1210 g_object_unref (c->data);
1213 g_slist_free (conns);