[System] Fixes leak in NtlmClient which never released cached WebRequest. Fixes ...
authorMarek Safar <marek.safar@gmail.com>
Mon, 7 Dec 2015 16:59:46 +0000 (17:59 +0100)
committerMarek Safar <marek.safar@gmail.com>
Mon, 7 Dec 2015 16:59:46 +0000 (17:59 +0100)
mcs/class/System/Mono.Http/NtlmClient.cs

index 54e85db013edb1ab9dc4d3c2fa9ebb1a8b601a2c..b40d601b4b1c5544c56f443ecec9cf0ab96cfafd 100644 (file)
@@ -40,6 +40,7 @@ using MonoSecurity::Mono.Security.Protocol.Ntlm;
 using System;
 using System.Collections;
 using System.Net;
+using System.Runtime.CompilerServices;
 
 namespace Mono.Http
 {
@@ -122,14 +123,8 @@ namespace Mono.Http
 
        class NtlmClient : IAuthenticationModule
        {
-               static Hashtable cache;
-
-               static NtlmClient () 
-               {
-                       cache = new Hashtable ();
-               }
-       
-               public NtlmClient () {}
+               static readonly ConditionalWeakTable<HttpWebRequest, NtlmSession> cache =
+                       new ConditionalWeakTable<HttpWebRequest, NtlmSession> ();
        
                public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
                {
@@ -153,12 +148,7 @@ namespace Mono.Http
                                return null;
 
                        lock (cache) {
-                               NtlmSession ds = (NtlmSession) cache [request];
-                               if (ds == null) {
-                                       ds = new NtlmSession ();
-                                       cache.Add (request, ds);
-                               }
-
+                               var ds = cache.GetOrCreateValue (request);
                                return ds.Authenticate (header, webRequest, credentials);
                        }
                }