added implementation of CredentialCache
[mono.git] / mcs / class / System / System.Net / CredentialCache.cs
1 //\r
2 // System.Net.CredentialCache\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Runtime.Serialization;\r
11 \r
12 namespace System.Net \r
13 {\r
14         [Serializable]\r
15         public class CredentialCache : ICredentials, IEnumerable\r
16         {\r
17                 // Fields\r
18                 private Hashtable cache;\r
19                 \r
20                 // Constructors         \r
21                 public CredentialCache () \r
22                 {\r
23                         cache = new Hashtable ();\r
24                 }\r
25                 \r
26                 // Properties\r
27                 \r
28                 [MonoTODO ("Need EnvironmentPermission implementation first")]\r
29                 public static ICredentials DefaultCredentials {\r
30                         get {\r
31                                 throw new NotImplementedException ();\r
32                         }\r
33                 }\r
34                 \r
35 \r
36                 // ICredentials\r
37 \r
38                 public NetworkCredential GetCredential (Uri uriPrefix, string authType)\r
39                 {\r
40                         int longestPrefix = -1;\r
41                         NetworkCredential result = null;\r
42                         \r
43                         if (uriPrefix == null || authType == null)\r
44                                 return null;\r
45                                 \r
46                         string absPath = uriPrefix.AbsolutePath;\r
47                         absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));\r
48                         \r
49                         IDictionaryEnumerator e = cache.GetEnumerator ();\r
50                         while (e.MoveNext ()) {\r
51                                 CredentialCacheKey key = e.Key as CredentialCacheKey;\r
52                                 \r
53                                 if (key.Length <= longestPrefix) \r
54                                         continue;\r
55                                 \r
56                                 if (String.Compare (key.AuthType, authType, true) != 0)\r
57                                         continue;\r
58                                 \r
59                                 Uri cachedUri = key.UriPrefix;\r
60                                 \r
61                                 if (cachedUri.Scheme != uriPrefix.Scheme)\r
62                                         continue;\r
63                                         \r
64                                 if (cachedUri.Port != uriPrefix.Port)\r
65                                         continue;\r
66                                         \r
67                                 if (cachedUri.Host != uriPrefix.Host)\r
68                                         continue;\r
69                                                                 \r
70                                 if (!absPath.StartsWith (key.AbsPath))\r
71                                         continue;\r
72                                         \r
73                                 longestPrefix = key.Length;\r
74                                 result = (NetworkCredential) e.Value;\r
75                         }\r
76                         \r
77                         return result;\r
78                 }\r
79 \r
80                 // IEnumerable\r
81 \r
82                 public IEnumerator GetEnumerator ()\r
83                 {\r
84                         return cache.Values.GetEnumerator ();\r
85                 }               \r
86                 \r
87                 // Methods\r
88                 \r
89                 public void Add (Uri uriPrefix, string authType, NetworkCredential cred)\r
90                 {\r
91                         if (uriPrefix == null) \r
92                                 throw new ArgumentNullException ("uriPrefix");\r
93 \r
94                         if (authType == null) \r
95                                 throw new ArgumentNullException ("authType");\r
96                         \r
97                         // throws ArgumentException when same key already exists.\r
98                         cache.Add (new CredentialCacheKey (uriPrefix, authType), cred);\r
99                 }\r
100                 \r
101                 public void Remove (Uri uriPrefix, string authType)\r
102                 {\r
103                         if (uriPrefix == null) \r
104                                 throw new ArgumentNullException ("uriPrefix");\r
105 \r
106                         if (authType == null) \r
107                                 throw new ArgumentNullException ("authType");\r
108 \r
109                         cache.Remove (new CredentialCacheKey (uriPrefix, authType));\r
110                 }\r
111                 \r
112                 // Inner Classes\r
113                 \r
114                 internal class CredentialCacheKey\r
115                 {\r
116                         private Uri uriPrefix;\r
117                         private string authType;\r
118                         \r
119                         private string absPath;\r
120                         private int len;\r
121                         private int hash;\r
122                         \r
123                         internal CredentialCacheKey (Uri uriPrefix, string authType)\r
124                         {\r
125                                 this.uriPrefix = uriPrefix;\r
126                                 this.authType = authType;\r
127                                 \r
128                                 this.absPath = uriPrefix.AbsolutePath;\r
129                                 this.absPath = absPath.Substring (0, absPath.LastIndexOf ('/'));                                \r
130 \r
131                                 this.len = uriPrefix.AbsoluteUri.Length;\r
132                                 this.hash = uriPrefix.GetHashCode () \r
133                                           + authType.ToString ().GetHashCode ();\r
134                         }\r
135                         \r
136                         public int Length {\r
137                                 get { return len; }\r
138                         }                       \r
139                         \r
140                         public string AbsPath {\r
141                                 get { return absPath; }\r
142                         }\r
143                         \r
144                         public Uri UriPrefix {\r
145                                 get { return uriPrefix; }\r
146                         }\r
147                         \r
148                         public string AuthType {\r
149                                 get { return authType; }\r
150                         }\r
151                         \r
152                         public override int GetHashCode ()\r
153                         {\r
154                                 return hash;\r
155                         }\r
156                         \r
157                         public override bool Equals (object obj)\r
158                         {\r
159                                 CredentialCacheKey key = obj as CredentialCacheKey;\r
160                                 return ((key != null) && (this.hash == key.hash));\r
161                         }\r
162                         \r
163                         public override string ToString ()\r
164                         {\r
165                                 return absPath + " : " + authType + " : len=" + len;\r
166                         }\r
167                 }\r
168         } \r
169\r
170 \r