[System]: WebRequest.GetSystemProxy(): Return custom proxy for monodroid.
[mono.git] / mcs / class / System / System.Net / CredentialCache.cs
index cc2ec8029dff262c8bd3cc01e283d65de4ceba81..9978daa46d73a1caee1e21f8f0706712daeeafdc 100644 (file)
@@ -31,17 +31,16 @@ using System.Collections;
 using System.Runtime.Serialization;
 
 namespace System.Net {
-       public class CredentialCache : ICredentials, IEnumerable
-#if NET_2_0
-                                    , ICredentialsByHost
-#endif
+       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")]
@@ -52,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)
                {
@@ -130,25 +127,56 @@ namespace System.Net {
                        cache.Remove (new CredentialCacheKey (uriPrefix, authType));
                }
                
-#if NET_2_0
-               [MonoNotSupported ("")]
-               public void Add (string host, int port, string authenticationType, NetworkCredential credential)
+               public NetworkCredential GetCredential (string host, int port, string authenticationType)
                {
-                       throw new NotImplementedException ();
+                       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;
                }
 
-               [MonoNotSupported ("")]
-               public NetworkCredential GetCredential (string host, int port, string authenticationType)
+               public void Add (string host, int port, string authenticationType, NetworkCredential credential)
                {
-                       throw new NotImplementedException ();
+                       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);
                }
 
-               [MonoNotSupported ("")]
                public void Remove (string host, int port, string authenticationType)
                {
-                       throw new NotImplementedException ();
+                       if (host == null)
+                               return;
+
+                       if (authenticationType == null)
+                               return;
+
+                       cacheForHost.Remove (new CredentialCacheForHostKey (host, port, authenticationType));
                }
-#endif
 
                class CredentialCacheKey {
                        Uri uriPrefix;
@@ -202,7 +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;
+               }
+       }
+}
 }