1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2000-2003, Ximian, Inc.
16 #include "soup-session.h"
17 #include "soup-connection.h"
18 #include "soup-connection-ntlm.h"
19 #include "soup-marshal.h"
20 #include "soup-message-queue.h"
21 #include "soup-private.h"
27 GSList *connections; /* CONTAINS: SoupConnection */
30 GHashTable *auth_realms; /* path -> scheme:realm */
31 GHashTable *auths; /* scheme:realm -> SoupAuth */
34 struct SoupSessionPrivate {
36 guint max_conns, max_conns_per_host;
39 SoupMessageQueue *queue;
41 GHashTable *hosts; /* SoupUri -> SoupSessionHost */
42 GHashTable *conns; /* SoupConnection -> SoupSessionHost */
45 SoupSessionHost *proxy_host;
48 static guint host_uri_hash (gconstpointer key);
49 static gboolean host_uri_equal (gconstpointer v1, gconstpointer v2);
50 static void free_host (SoupSessionHost *host, SoupSession *session);
52 static gboolean run_queue (SoupSession *session, gboolean try_pruning);
54 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
55 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 4
57 #define PARENT_TYPE G_TYPE_OBJECT
58 static GObjectClass *parent_class;
66 static guint signals[LAST_SIGNAL] = { 0 };
73 PROP_MAX_CONNS_PER_HOST,
79 static void set_property (GObject *object, guint prop_id,
80 const GValue *value, GParamSpec *pspec);
81 static void get_property (GObject *object, guint prop_id,
82 GValue *value, GParamSpec *pspec);
85 init (GObject *object)
87 SoupSession *session = SOUP_SESSION (object);
89 session->priv = g_new0 (SoupSessionPrivate, 1);
90 session->priv->queue = soup_message_queue_new ();
91 session->priv->hosts = g_hash_table_new (host_uri_hash,
93 session->priv->conns = g_hash_table_new (NULL, NULL);
95 session->priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
96 session->priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
100 foreach_free_host (gpointer key, gpointer host, gpointer session)
102 free_host (host, session);
107 cleanup_hosts (SoupSession *session)
109 g_hash_table_foreach_remove (session->priv->hosts,
110 foreach_free_host, session);
114 dispose (GObject *object)
116 SoupSession *session = SOUP_SESSION (object);
118 soup_session_abort (session);
119 cleanup_hosts (session);
121 G_OBJECT_CLASS (parent_class)->dispose (object);
125 finalize (GObject *object)
127 SoupSession *session = SOUP_SESSION (object);
129 soup_message_queue_destroy (session->priv->queue);
130 g_hash_table_destroy (session->priv->hosts);
131 g_hash_table_destroy (session->priv->conns);
132 g_free (session->priv);
134 G_OBJECT_CLASS (parent_class)->finalize (object);
138 class_init (GObjectClass *object_class)
140 parent_class = g_type_class_ref (PARENT_TYPE);
142 /* virtual method override */
143 object_class->dispose = dispose;
144 object_class->finalize = finalize;
145 object_class->set_property = set_property;
146 object_class->get_property = get_property;
149 signals[AUTHENTICATE] =
150 g_signal_new ("authenticate",
151 G_OBJECT_CLASS_TYPE (object_class),
153 G_STRUCT_OFFSET (SoupSessionClass, authenticate),
155 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
162 signals[REAUTHENTICATE] =
163 g_signal_new ("reauthenticate",
164 G_OBJECT_CLASS_TYPE (object_class),
166 G_STRUCT_OFFSET (SoupSessionClass, reauthenticate),
168 soup_marshal_NONE__OBJECT_STRING_STRING_POINTER_POINTER,
177 g_object_class_install_property (
178 object_class, PROP_PROXY_URI,
179 g_param_spec_pointer (SOUP_SESSION_PROXY_URI,
181 "The HTTP Proxy to use for this session",
183 g_object_class_install_property (
184 object_class, PROP_MAX_CONNS,
185 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
186 "Max Connection Count",
187 "The maximum number of connections that the session can open at once",
192 g_object_class_install_property (
193 object_class, PROP_MAX_CONNS_PER_HOST,
194 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
195 "Max Per-Host Connection Count",
196 "The maximum number of connections that the session can open at once to a given host",
201 g_object_class_install_property (
202 object_class, PROP_USE_NTLM,
203 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
205 "Whether or not to use NTLM authentication",
210 SOUP_MAKE_TYPE (soup_session, SoupSession, class_init, init, PARENT_TYPE)
213 soup_session_new (void)
215 return g_object_new (SOUP_TYPE_SESSION, NULL);
219 soup_session_new_with_options (const char *optname1, ...)
221 SoupSession *session;
224 va_start (ap, optname1);
225 session = (SoupSession *)g_object_new_valist (SOUP_TYPE_SESSION, optname1, ap);
232 set_property (GObject *object, guint prop_id,
233 const GValue *value, GParamSpec *pspec)
235 SoupSession *session = SOUP_SESSION (object);
240 if (session->priv->proxy_uri)
241 soup_uri_free (session->priv->proxy_uri);
242 pval = g_value_get_pointer (value);
243 session->priv->proxy_uri = pval ? soup_uri_copy (pval) : NULL;
245 soup_session_abort (session);
246 cleanup_hosts (session);
249 session->priv->max_conns = g_value_get_int (value);
251 case PROP_MAX_CONNS_PER_HOST:
252 session->priv->max_conns_per_host = g_value_get_int (value);
255 session->priv->use_ntlm = g_value_get_boolean (value);
263 get_property (GObject *object, guint prop_id,
264 GValue *value, GParamSpec *pspec)
266 SoupSession *session = SOUP_SESSION (object);
270 g_value_set_pointer (value, session->priv->proxy_uri ?
271 soup_uri_copy (session->priv->proxy_uri) :
275 g_value_set_int (value, session->priv->max_conns);
277 case PROP_MAX_CONNS_PER_HOST:
278 g_value_set_int (value, session->priv->max_conns_per_host);
281 g_value_set_boolean (value, session->priv->use_ntlm);
291 host_uri_hash (gconstpointer key)
293 const SoupUri *uri = key;
295 return (uri->protocol << 16) + uri->port + g_str_hash (uri->host);
299 host_uri_equal (gconstpointer v1, gconstpointer v2)
301 const SoupUri *one = v1;
302 const SoupUri *two = v2;
304 if (one->protocol != two->protocol)
306 if (one->port != two->port)
309 return strcmp (one->host, two->host) == 0;
312 static SoupSessionHost *
313 get_host_for_message (SoupSession *session, SoupMessage *msg)
315 SoupSessionHost *host;
316 const SoupUri *source = soup_message_get_uri (msg);
318 host = g_hash_table_lookup (session->priv->hosts, source);
322 host = g_new0 (SoupSessionHost, 1);
323 host->root_uri = soup_uri_copy_root (source);
325 g_hash_table_insert (session->priv->hosts, host->root_uri, host);
330 free_realm (gpointer path, gpointer scheme_realm, gpointer data)
333 g_free (scheme_realm);
337 free_auth (gpointer scheme_realm, gpointer auth, gpointer data)
339 g_free (scheme_realm);
340 g_object_unref (auth);
344 free_host (SoupSessionHost *host, SoupSession *session)
346 while (host->connections) {
347 SoupConnection *conn = host->connections->data;
349 host->connections = g_slist_remove (host->connections, conn);
350 soup_connection_disconnect (conn);
353 if (host->auth_realms) {
354 g_hash_table_foreach (host->auth_realms, free_realm, NULL);
355 g_hash_table_destroy (host->auth_realms);
358 g_hash_table_foreach (host->auths, free_auth, NULL);
359 g_hash_table_destroy (host->auths);
362 soup_uri_free (host->root_uri);
368 lookup_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
370 SoupSessionHost *host;
372 const char *realm, *const_path;
375 host = session->priv->proxy_host;
378 host = get_host_for_message (session, msg);
379 const_path = soup_message_get_uri (msg)->path;
381 g_return_val_if_fail (host != NULL, NULL);
383 if (!host->auth_realms)
386 path = g_strdup (const_path);
389 realm = g_hash_table_lookup (host->auth_realms, path);
393 dir = strrchr (path, '/');
400 return g_hash_table_lookup (host->auths, realm);
406 invalidate_auth (SoupSessionHost *host, SoupAuth *auth)
411 realm = g_strdup_printf ("%s:%s",
412 soup_auth_get_scheme_name (auth),
413 soup_auth_get_realm (auth));
415 if (g_hash_table_lookup_extended (host->auths, realm, &key, &value) &&
416 auth == (SoupAuth *)value) {
417 g_hash_table_remove (host->auths, realm);
419 g_object_unref (auth);
425 authenticate_auth (SoupSession *session, SoupAuth *auth,
426 SoupMessage *msg, gboolean prior_auth_failed)
428 const SoupUri *uri = soup_message_get_uri (msg);
429 char *username = NULL, *password = NULL;
431 if (uri->passwd && !prior_auth_failed) {
432 soup_auth_authenticate (auth, uri->user, uri->passwd);
436 g_signal_emit (session, signals[prior_auth_failed ? REAUTHENTICATE : AUTHENTICATE], 0,
437 msg, soup_auth_get_scheme_name (auth),
438 soup_auth_get_realm (auth),
439 &username, &password);
440 if (username || password)
441 soup_auth_authenticate (auth, username, password);
445 memset (password, 0, strlen (password));
449 return soup_auth_is_authenticated (auth);
453 update_auth_internal (SoupSession *session, SoupMessage *msg,
454 const GSList *headers, gboolean proxy,
455 gboolean got_unauthorized)
457 SoupSessionHost *host;
458 SoupAuth *new_auth, *prior_auth, *old_auth;
459 gpointer old_path, old_realm;
460 const SoupUri *msg_uri;
464 gboolean prior_auth_failed = FALSE;
466 host = get_host_for_message (session, msg);
467 g_return_val_if_fail (host != NULL, FALSE);
469 /* Try to construct a new auth from the headers; if we can't,
470 * there's no way we'll be able to authenticate.
472 msg_uri = soup_message_get_uri (msg);
473 new_auth = soup_auth_new_from_header_list (headers);
477 /* See if this auth is the same auth we used last time */
478 prior_auth = lookup_auth (session, msg, proxy);
480 G_OBJECT_TYPE (prior_auth) == G_OBJECT_TYPE (new_auth) &&
481 !strcmp (soup_auth_get_realm (prior_auth),
482 soup_auth_get_realm (new_auth))) {
483 if (!got_unauthorized) {
484 /* The user is just trying to preauthenticate
485 * using information we already have, so
486 * there's nothing more that needs to be done.
488 g_object_unref (new_auth);
492 /* The server didn't like the username/password we
493 * provided before. Invalidate it and note this fact.
495 invalidate_auth (host, prior_auth);
496 prior_auth_failed = TRUE;
499 if (!host->auth_realms) {
500 host->auth_realms = g_hash_table_new (g_str_hash, g_str_equal);
501 host->auths = g_hash_table_new (g_str_hash, g_str_equal);
504 /* Record where this auth realm is used */
505 realm = g_strdup_printf ("%s:%s",
506 soup_auth_get_scheme_name (new_auth),
507 soup_auth_get_realm (new_auth));
508 pspace = soup_auth_get_protection_space (new_auth, msg_uri);
509 for (p = pspace; p; p = p->next) {
511 if (g_hash_table_lookup_extended (host->auth_realms, path,
512 &old_path, &old_realm)) {
513 g_hash_table_remove (host->auth_realms, old_path);
518 g_hash_table_insert (host->auth_realms,
519 g_strdup (path), g_strdup (realm));
521 soup_auth_free_protection_space (new_auth, pspace);
523 /* Now, make sure the auth is recorded. (If there's a
524 * pre-existing auth, we keep that rather than the new one,
525 * since the old one might already be authenticated.)
527 old_auth = g_hash_table_lookup (host->auths, realm);
530 g_object_unref (new_auth);
533 g_hash_table_insert (host->auths, realm, new_auth);
535 /* If we need to authenticate, try to do it. */
536 if (!soup_auth_is_authenticated (new_auth)) {
537 return authenticate_auth (session, new_auth,
538 msg, prior_auth_failed);
541 /* Otherwise we're good. */
546 connection_authenticate (SoupConnection *conn, SoupMessage *msg,
547 const char *auth_type, const char *auth_realm,
548 char **username, char **password, gpointer session)
550 g_signal_emit (session, signals[AUTHENTICATE], 0,
551 msg, auth_type, auth_realm, username, password);
555 connection_reauthenticate (SoupConnection *conn, SoupMessage *msg,
556 const char *auth_type, const char *auth_realm,
557 char **username, char **password,
560 g_signal_emit (conn, signals[REAUTHENTICATE], 0,
561 msg, auth_type, auth_realm, username, password);
566 authorize_handler (SoupMessage *msg, gpointer user_data)
568 SoupSession *session = user_data;
569 const GSList *headers;
572 if (msg->status_code == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
573 headers = soup_message_get_header_list (msg->response_headers,
574 "Proxy-Authenticate");
577 headers = soup_message_get_header_list (msg->response_headers,
584 if (update_auth_internal (session, msg, headers, proxy, TRUE))
585 soup_session_requeue_message (session, msg);
589 redirect_handler (SoupMessage *msg, gpointer user_data)
591 SoupSession *session = user_data;
595 new_loc = soup_message_get_header (msg->response_headers, "Location");
598 new_uri = soup_uri_new (new_loc);
600 goto INVALID_REDIRECT;
602 soup_message_set_uri (msg, new_uri);
603 soup_uri_free (new_uri);
605 soup_session_requeue_message (session, msg);
609 soup_message_set_status_full (msg,
610 SOUP_STATUS_MALFORMED,
611 "Invalid Redirect URL");
615 request_finished (SoupMessage *req, gpointer user_data)
617 if (!SOUP_MESSAGE_IS_STARTING (req))
618 req->status = SOUP_MESSAGE_STATUS_FINISHED;
622 final_finished (SoupMessage *req, gpointer user_data)
624 SoupSession *session = user_data;
626 if (!SOUP_MESSAGE_IS_STARTING (req)) {
627 soup_message_queue_remove_message (session->priv->queue, req);
629 g_signal_handlers_disconnect_by_func (req, request_finished, session);
630 g_signal_handlers_disconnect_by_func (req, final_finished, session);
631 g_object_unref (req);
634 run_queue (session, FALSE);
638 add_auth (SoupSession *session, SoupMessage *msg, gboolean proxy)
640 const char *header = proxy ? "Proxy-Authorization" : "Authorization";
644 soup_message_remove_header (msg->request_headers, header);
646 auth = lookup_auth (session, msg, proxy);
649 if (!soup_auth_is_authenticated (auth) &&
650 !authenticate_auth (session, auth, msg, FALSE))
653 token = soup_auth_get_authorization (auth, msg);
655 soup_message_add_header (msg->request_headers, header, token);
661 send_request (SoupSession *session, SoupMessage *req, SoupConnection *conn)
663 req->status = SOUP_MESSAGE_STATUS_RUNNING;
665 add_auth (session, req, FALSE);
666 if (session->priv->proxy_uri)
667 add_auth (session, req, TRUE);
668 soup_connection_send_request (conn, req);
672 find_oldest_connection (gpointer key, gpointer host, gpointer data)
674 SoupConnection *conn = key, **oldest = data;
676 /* Don't prune a connection that hasn't even been used yet. */
677 if (soup_connection_last_used (conn) == 0)
680 if (!*oldest || (soup_connection_last_used (conn) <
681 soup_connection_last_used (*oldest)))
686 try_prune_connection (SoupSession *session)
688 SoupConnection *oldest = NULL;
690 g_hash_table_foreach (session->priv->conns, find_oldest_connection,
693 soup_connection_disconnect (oldest);
694 g_object_unref (oldest);
700 static void connection_closed (SoupConnection *conn, SoupSession *session);
703 cleanup_connection (SoupSession *session, SoupConnection *conn)
705 SoupSessionHost *host =
706 g_hash_table_lookup (session->priv->conns, conn);
708 g_return_if_fail (host != NULL);
710 g_hash_table_remove (session->priv->conns, conn);
711 g_signal_handlers_disconnect_by_func (conn, connection_closed, session);
712 session->priv->num_conns--;
714 host->connections = g_slist_remove (host->connections, conn);
719 connection_closed (SoupConnection *conn, SoupSession *session)
721 cleanup_connection (session, conn);
723 /* Run the queue in case anyone was waiting for a connection
726 run_queue (session, FALSE);
730 got_connection (SoupConnection *conn, guint status, gpointer user_data)
732 SoupSession *session = user_data;
733 SoupSessionHost *host = g_hash_table_lookup (session->priv->conns, conn);
735 g_return_if_fail (host != NULL);
737 if (status == SOUP_STATUS_OK) {
738 host->connections = g_slist_prepend (host->connections, conn);
739 run_queue (session, FALSE);
744 cleanup_connection (session, conn);
745 g_object_unref (conn);
747 if (host->connections) {
748 /* Something went wrong this time, but we have at
749 * least one open connection to this host. So just
750 * leave the message in the queue so it can use that
751 * connection once it's free.
756 /* Flush any queued messages for this host */
757 host->error = status;
758 run_queue (session, FALSE);
760 if (status != SOUP_STATUS_CANT_RESOLVE &&
761 status != SOUP_STATUS_CANT_RESOLVE_PROXY) {
762 /* If the error was "can't resolve", then it's not likely
763 * to improve. But if it was something else, it may have
764 * been transient, so we clear the error so the user can
772 run_queue (SoupSession *session, gboolean try_pruning)
774 SoupMessageQueueIter iter;
776 SoupConnection *conn;
777 SoupSessionHost *host;
778 gboolean skipped_any = FALSE, started_any = FALSE;
781 /* FIXME: prefer CONNECTING messages */
784 for (msg = soup_message_queue_first (session->priv->queue, &iter); msg; msg = soup_message_queue_next (session->priv->queue, &iter)) {
786 if (!SOUP_MESSAGE_IS_STARTING (msg))
789 host = get_host_for_message (session, msg);
791 /* If the hostname is known to be bad, fail right away */
793 soup_message_set_status (msg, host->error);
794 soup_message_finished (msg);
797 /* If there is an idle connection, use it */
798 for (conns = host->connections; conns; conns = conns->next) {
799 if (!soup_connection_is_in_use (conns->data))
803 send_request (session, msg, conns->data);
808 if (msg->status == SOUP_MESSAGE_STATUS_CONNECTING) {
809 /* We already started a connection for this
810 * message, so don't start another one.
815 /* If we have the max number of per-host connections
816 * or total connections open, we'll have to wait.
818 if (host->num_conns >= session->priv->max_conns_per_host)
820 else if (session->priv->num_conns >= session->priv->max_conns) {
821 /* In this case, closing an idle connection
822 * somewhere else would let us open one here.
828 /* Otherwise, open a new connection */
829 conn = g_object_new (
830 (session->priv->use_ntlm ?
831 SOUP_TYPE_CONNECTION_NTLM : SOUP_TYPE_CONNECTION),
832 SOUP_CONNECTION_DEST_URI, host->root_uri,
833 SOUP_CONNECTION_PROXY_URI, session->priv->proxy_uri,
835 g_signal_connect (conn, "authenticate",
836 G_CALLBACK (connection_authenticate),
838 g_signal_connect (conn, "reauthenticate",
839 G_CALLBACK (connection_reauthenticate),
842 soup_connection_connect_async (conn, got_connection, session);
843 g_signal_connect (conn, "disconnected",
844 G_CALLBACK (connection_closed), session);
845 g_hash_table_insert (session->priv->conns, conn, host);
846 session->priv->num_conns++;
848 /* Increment the host's connection count, but don't add
849 * this connection to the list yet, since it's not ready.
853 /* Mark the request as connecting, so we don't try to
854 * open another new connection for it next time around.
856 msg->status = SOUP_MESSAGE_STATUS_CONNECTING;
861 if (try_pruning && skipped_any && !started_any) {
862 /* We didn't manage to start any message, but there is
863 * at least one message in the queue that could be
864 * sent if we pruned an idle connection from some
867 if (try_prune_connection (session)) {
877 queue_message (SoupSession *session, SoupMessage *req, gboolean requeue)
879 req->status = SOUP_MESSAGE_STATUS_QUEUED;
881 soup_message_queue_append (session->priv->queue, req);
882 run_queue (session, TRUE);
887 * soup_session_queue_message:
888 * @session: a #SoupSession
889 * @req: the message to queue
890 * @callback: a #SoupMessageCallbackFn which will be called after the
891 * message completes or when an unrecoverable error occurs.
892 * @user_data: a pointer passed to @callback.
894 * Queues the message @req for sending. All messages are processed
895 * while the glib main loop runs. If @req has been processed before,
896 * any resources related to the time it was last sent are freed.
898 * Upon message completion, the callback specified in @callback will
899 * be invoked. If after returning from this callback the message has
900 * not been requeued, @req will be unreffed.
903 soup_session_queue_message (SoupSession *session, SoupMessage *req,
904 SoupMessageCallbackFn callback, gpointer user_data)
906 g_return_if_fail (SOUP_IS_SESSION (session));
907 g_return_if_fail (SOUP_IS_MESSAGE (req));
909 g_signal_connect (req, "finished",
910 G_CALLBACK (request_finished), session);
912 g_signal_connect (req, "finished",
913 G_CALLBACK (callback), user_data);
915 g_signal_connect_after (req, "finished",
916 G_CALLBACK (final_finished), session);
918 soup_message_add_status_code_handler (req, SOUP_STATUS_UNAUTHORIZED,
919 SOUP_HANDLER_POST_BODY,
920 authorize_handler, session);
921 soup_message_add_status_code_handler (req,
922 SOUP_STATUS_PROXY_UNAUTHORIZED,
923 SOUP_HANDLER_POST_BODY,
924 authorize_handler, session);
926 if (!(soup_message_get_flags (req) & SOUP_MESSAGE_NO_REDIRECT)) {
927 soup_message_add_status_class_handler (
928 req, SOUP_STATUS_CLASS_REDIRECT,
929 SOUP_HANDLER_POST_BODY,
930 redirect_handler, session);
933 queue_message (session, req, FALSE);
937 * soup_session_requeue_message:
938 * @session: a #SoupSession
939 * @req: the message to requeue
941 * This causes @req to be placed back on the queue to be attempted
945 soup_session_requeue_message (SoupSession *session, SoupMessage *req)
947 g_return_if_fail (SOUP_IS_SESSION (session));
948 g_return_if_fail (SOUP_IS_MESSAGE (req));
950 queue_message (session, req, TRUE);
955 * soup_session_send_message:
956 * @session: a #SoupSession
957 * @req: the message to send
959 * Synchronously send @req. This call will not return until the
960 * transfer is finished successfully or there is an unrecoverable
963 * @req is not freed upon return.
965 * Return value: the HTTP status code of the response
968 soup_session_send_message (SoupSession *session, SoupMessage *req)
970 g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
971 g_return_val_if_fail (SOUP_IS_MESSAGE (req), SOUP_STATUS_MALFORMED);
973 /* Balance out the unref that final_finished will do */
976 soup_session_queue_message (session, req, NULL, NULL);
978 while (req->status != SOUP_MESSAGE_STATUS_FINISHED &&
979 !SOUP_STATUS_IS_TRANSPORT_ERROR (req->status_code))
980 g_main_iteration (TRUE);
982 return req->status_code;
986 * soup_session_abort:
987 * @session: the session
989 * Cancels all pending requests in @session.
992 soup_session_abort (SoupSession *session)
994 SoupMessageQueueIter iter;
997 for (msg = soup_message_queue_first (session->priv->queue, &iter); msg; msg = soup_message_queue_next (session->priv->queue, &iter)) {
998 soup_message_queue_remove (session->priv->queue, &iter);
999 soup_message_cancel (msg);