[libsoup/carlosgc/thread-safe: 5/19] auth-manager: Add a mutex to protect accessing the auth hosts




commit b40e7a9b7bb88d8ac04d238173e63fd1d97a1b00
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Mon Apr 11 13:09:37 2022 +0200

    auth-manager: Add a mutex to protect accessing the auth hosts

 libsoup/auth/soup-auth-manager.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/libsoup/auth/soup-auth-manager.c b/libsoup/auth/soup-auth-manager.c
index 89b83bb5..dc19dfe4 100644
--- a/libsoup/auth/soup-auth-manager.c
+++ b/libsoup/auth/soup-auth-manager.c
@@ -61,6 +61,7 @@ typedef struct {
        gboolean auto_ntlm;
 
        SoupAuth *proxy_auth;
+        GMutex mutex;
        GHashTable *auth_hosts;
 } SoupAuthManagerPrivate;
 
@@ -90,6 +91,7 @@ soup_auth_manager_init (SoupAuthManager *manager)
                                                  soup_uri_host_equal,
                                                  NULL,
                                                  (GDestroyNotify)soup_auth_host_free);
+        g_mutex_init (&priv->mutex);
 }
 
 static void
@@ -103,6 +105,8 @@ soup_auth_manager_finalize (GObject *object)
 
        g_clear_object (&priv->proxy_auth);
 
+        g_mutex_clear (&priv->mutex);
+
        G_OBJECT_CLASS (soup_auth_manager_parent_class)->finalize (object);
 }
 
@@ -647,6 +651,8 @@ auth_got_headers (SoupMessage *msg, gpointer manager)
        SoupAuth *auth, *prior_auth;
        gboolean prior_auth_failed = FALSE;
 
+        g_mutex_lock (&priv->mutex);
+
        /* See if we used auth last time */
        prior_auth = soup_message_get_auth (msg);
        if (prior_auth && check_auth (msg, prior_auth)) {
@@ -655,8 +661,10 @@ auth_got_headers (SoupMessage *msg, gpointer manager)
                        prior_auth_failed = TRUE;
        } else {
                auth = create_auth (priv, msg);
-               if (!auth)
+               if (!auth) {
+                        g_mutex_unlock (&priv->mutex);
                        return;
+                }
        }
 
        if (!soup_message_query_flags (msg, SOUP_MESSAGE_DO_NOT_USE_AUTH_CACHE)) {
@@ -668,6 +676,8 @@ auth_got_headers (SoupMessage *msg, gpointer manager)
                auth = g_object_ref (new_auth);
        }
 
+        g_mutex_unlock (&priv->mutex);
+
        /* If we need to authenticate, try to do it. */
        authenticate_auth (manager, auth, msg,
                           prior_auth_failed, FALSE, TRUE);
@@ -681,6 +691,8 @@ auth_got_body (SoupMessage *msg, gpointer manager)
         SoupAuthManagerPrivate *priv = soup_auth_manager_get_instance_private (manager);
        SoupAuth *auth;
 
+        g_mutex_lock (&priv->mutex);
+
        auth = lookup_auth (priv, msg);
        if (auth && soup_auth_is_ready (auth, msg)) {
                if (SOUP_IS_CONNECTION_AUTH (auth))
@@ -694,6 +706,8 @@ auth_got_body (SoupMessage *msg, gpointer manager)
 
                soup_session_requeue_message (priv->session, msg);
        }
+
+        g_mutex_unlock (&priv->mutex);
 }
 
 static void
@@ -703,6 +717,8 @@ proxy_auth_got_headers (SoupMessage *msg, gpointer manager)
        SoupAuth *auth = NULL, *prior_auth;
        gboolean prior_auth_failed = FALSE;
 
+        g_mutex_lock (&priv->mutex);
+
        /* See if we used auth last time */
        prior_auth = soup_message_get_proxy_auth (msg);
        if (prior_auth && check_auth (msg, prior_auth)) {
@@ -715,13 +731,17 @@ proxy_auth_got_headers (SoupMessage *msg, gpointer manager)
 
        if (!auth) {
                auth = create_auth (priv, msg);
-               if (!auth)
+               if (!auth) {
+                        g_mutex_unlock (&priv->mutex);
                        return;
+                }
 
                if (!soup_message_query_flags (msg, SOUP_MESSAGE_DO_NOT_USE_AUTH_CACHE))
                        priv->proxy_auth = g_object_ref (auth);
        }
 
+        g_mutex_unlock (&priv->mutex);
+
        /* If we need to authenticate, try to do it. */
        authenticate_auth (manager, auth, msg,
                           prior_auth_failed, TRUE, TRUE);
@@ -735,6 +755,8 @@ proxy_auth_got_body (SoupMessage *msg, gpointer manager)
         SoupAuthManagerPrivate *priv = soup_auth_manager_get_instance_private (manager);
        SoupAuth *auth;
 
+        g_mutex_lock (&priv->mutex);
+
        auth = lookup_proxy_auth (priv, msg);
        if (auth && soup_auth_is_ready (auth, msg)) {
                /* When not using cached credentials, update the Authorization header
@@ -744,6 +766,8 @@ proxy_auth_got_body (SoupMessage *msg, gpointer manager)
                        update_authorization_header (msg, auth, TRUE);
                soup_session_requeue_message (priv->session, msg);
        }
+
+        g_mutex_unlock (&priv->mutex);
 }
 
 static void
@@ -755,6 +779,8 @@ auth_msg_starting (SoupMessage *msg, gpointer manager)
        if (soup_message_query_flags (msg, SOUP_MESSAGE_DO_NOT_USE_AUTH_CACHE))
                return;
 
+        g_mutex_lock (&priv->mutex);
+
        if (soup_message_get_method (msg) != SOUP_METHOD_CONNECT) {
                auth = lookup_auth (priv, msg);
                if (auth) {
@@ -774,6 +800,8 @@ auth_msg_starting (SoupMessage *msg, gpointer manager)
        }
        soup_message_set_proxy_auth (msg, auth);
        update_authorization_header (msg, auth, TRUE);
+
+        g_mutex_unlock (&priv->mutex);
 }
 
 static void
@@ -831,7 +859,9 @@ soup_auth_manager_use_auth (SoupAuthManager *manager,
 {
         SoupAuthManagerPrivate *priv = soup_auth_manager_get_instance_private (manager);
 
+        g_mutex_lock (&priv->mutex);
        record_auth_for_uri (priv, uri, auth, FALSE);
+        g_mutex_unlock (&priv->mutex);
 }
 
 /**
@@ -848,7 +878,9 @@ soup_auth_manager_clear_cached_credentials (SoupAuthManager *manager)
 
        g_return_if_fail (SOUP_IS_AUTH_MANAGER (manager));
 
+        g_mutex_lock (&priv->mutex);
        g_hash_table_remove_all (priv->auth_hosts);
+        g_mutex_unlock (&priv->mutex);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]