[System]: WebRequest.GetSystemProxy(): Return custom proxy for monodroid.
[mono.git] / mcs / class / System / System.Net / CredentialCache.cs
index 9b2b9f5b51b6158ad4e566f9a24bd89052b2f5e4..9978daa46d73a1caee1e21f8f0706712daeeafdc 100644 (file)
@@ -31,13 +31,16 @@ using System.Collections;
 using System.Runtime.Serialization;
 
 namespace System.Net {
-       public class CredentialCache : ICredentials, IEnumerable {
-               static NetworkCredential empty = new NetworkCredential ("", "", "");
+       public class CredentialCache : ICredentials, IEnumerable, ICredentialsByHost
+       {
+               static NetworkCredential empty = new NetworkCredential (String.Empty, String.Empty, String.Empty);
                Hashtable cache;
+               Hashtable cacheForHost;
 
                public CredentialCache () 
                {
                        cache = new Hashtable ();
+                       cacheForHost = new Hashtable ();
                }
                
                [MonoTODO ("Need EnvironmentPermission implementation first")]
@@ -48,13 +51,11 @@ namespace System.Net {
                        }
                }
 
-#if NET_2_0
                // MS does might return a special ICredentials which does not allow getting the
                // username/password information out of it for non-internal classes.
                public static NetworkCredential DefaultNetworkCredentials {
                        get { return empty; }
                }
-#endif
 
                public NetworkCredential GetCredential (Uri uriPrefix, string authType)
                {
@@ -126,6 +127,57 @@ namespace System.Net {
                        cache.Remove (new CredentialCacheKey (uriPrefix, authType));
                }
                
+               public NetworkCredential GetCredential (string host, int port, string authenticationType)
+               {
+                       NetworkCredential result = null;
+                       
+                       if (host == null || port < 0 || authenticationType == null)
+                               return null;
+
+                       IDictionaryEnumerator e = cacheForHost.GetEnumerator ();
+                       while (e.MoveNext ()) {
+                               CredentialCacheForHostKey key = e.Key as CredentialCacheForHostKey;
+                               
+                               if (String.Compare (key.AuthType, authenticationType, true) != 0)
+                                       continue;
+                               
+                               if (key.Host != host)
+                                       continue;
+                               
+                               if (key.Port != port)
+                                       continue;
+                               
+                               result = (NetworkCredential) e.Value;
+                       }
+                       
+                       return result;
+               }
+
+               public void Add (string host, int port, string authenticationType, NetworkCredential credential)
+               {
+                       if (host == null)
+                               throw new ArgumentNullException("host");
+
+                       if (port < 0)
+                               throw new ArgumentOutOfRangeException("port");
+
+                       if (authenticationType == null)
+                               throw new ArgumentOutOfRangeException("authenticationType");
+
+                       cacheForHost.Add (new CredentialCacheForHostKey (host, port, authenticationType), credential);
+               }
+
+               public void Remove (string host, int port, string authenticationType)
+               {
+                       if (host == null)
+                               return;
+
+                       if (authenticationType == null)
+                               return;
+
+                       cacheForHost.Remove (new CredentialCacheForHostKey (host, port, authenticationType));
+               }
+
                class CredentialCacheKey {
                        Uri uriPrefix;
                        string authType;
@@ -143,7 +195,7 @@ namespace System.Net {
 
                                this.len = uriPrefix.AbsoluteUri.Length;
                                this.hash = uriPrefix.GetHashCode () 
-                                         + authType.ToString ().GetHashCode ();
+                                         + authType.GetHashCode ();
                        }
                        
                        public int Length {
@@ -178,6 +230,53 @@ namespace System.Net {
                                return absPath + " : " + authType + " : len=" + len;
                        }
                }
-       } 
+
+               class CredentialCacheForHostKey {
+                       string host;
+                       int port;
+                       string authType;
+                       int hash;
+
+                       internal CredentialCacheForHostKey (string host, int port, string authType)
+                       {
+                               this.host = host;
+                               this.port = port;
+                               this.authType = authType;
+
+                               this.hash = host.GetHashCode ()
+                                       + port.GetHashCode ()
+                                       + authType.GetHashCode ();
+                       }
+
+                       public string Host {
+                               get { return host; }
+                       }
+
+                       public int Port {
+                               get { return port; }
+                       }
+
+                       public string AuthType {
+                               get { return authType; }
+                       }
+
+               public override int GetHashCode ()
+               {
+                       return hash;
+               }
+
+               public override bool Equals (object obj)
+               {
+                       CredentialCacheForHostKey key = obj as CredentialCacheForHostKey;
+                       return ((key != null) && (this.hash == key.hash));
+               }
+
+               public override string ToString ()
+               {
+                       return host + " : " + authType;
+               }
+       }
+}
 }
 
+