* NetworkAccess.cs: Only mark as flags enum on 2.0.
[mono.git] / mcs / class / System / System.Net / DigestClient.cs
index 43b03b6350791b4768deaa6211e1c160d5c48ee8..ef7438277218e42d1b9353a98e009f801a8afd77 100644 (file)
 // http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Collections;
 using System.Collections.Specialized;
@@ -40,7 +61,6 @@ namespace System.Net
                int length;
                int pos;
                static string [] keywords = { "realm", "opaque", "nonce", "algorithm", "qop" };
-               static char [] endSeparator = new char[] { '"', ',' };
                string [] values = new string [keywords.Length];
 
                public DigestHeaderParser (string header)
@@ -109,15 +129,6 @@ namespace System.Net
                        pos--;
                }
                
-               void SkipNonWhitespace ()
-               {
-                       char c = 'a';
-                       while (pos < length && c != ' ' && c != '\t' && c != '\r' && c != '\n') {
-                               c = header [pos++];
-                       }
-                       pos--;
-               }
-               
                string GetKey ()
                {
                        SkipWhitespace ();
@@ -268,7 +279,7 @@ namespace System.Net
 
                private string HA2 (HttpWebRequest webRequest) 
                {
-                       string ha2 = String.Format ("{0}:{1}", webRequest.Method, webRequest.RequestUri.AbsolutePath);
+                       string ha2 = String.Format ("{0}:{1}", webRequest.Method, webRequest.RequestUri.PathAndQuery);
                        if (QOP == "auth-int") {
                                // TODO
                                // ha2 += String.Format (":{0}", hentity);
@@ -280,7 +291,7 @@ namespace System.Net
                {
                        string response = String.Format ("{0}:{1}:", HA1 (username, password), Nonce);
                        if (QOP != null)
-                               response += String.Format ("{0}:{1}:{2}:", _nc.ToString ("x8"), CNonce, QOP);
+                               response += String.Format ("{0}:{1}:{2}:", _nc.ToString ("X8"), CNonce, QOP);
                        response += HA2 (webRequest);
                        return HashToHexString (response);
                }
@@ -296,6 +307,9 @@ namespace System.Net
        
                        lastUse = DateTime.Now;
                        NetworkCredential cred = credentials.GetCredential (request.RequestUri, "digest");
+                       if (cred == null)
+                               return null;
+
                        string userName = cred.UserName;
                        if (userName == null || userName == "")
                                return null;
@@ -315,7 +329,7 @@ namespace System.Net
                        auth.AppendFormat ("response=\"{0}\", ", Response (userName, password, request));
 
                        if (QOP != null) { // quality of protection (server decision)
-                               auth.AppendFormat ("qop={0}, ", QOP);
+                               auth.AppendFormat ("qop=\"{0}\", ", QOP);
                        }
 
                        lock (this) {
@@ -346,21 +360,15 @@ namespace System.Net
        class DigestClient : IAuthenticationModule
        {
 
-               static Hashtable cache;
-
-               public DigestClient () {}
-
+               static readonly Hashtable cache = Hashtable.Synchronized (new Hashtable ());
+               
                static Hashtable Cache {
                        get {
-                               lock (typeof (DigestClient)) {
-                                       if (cache == null) {
-                                               cache = Hashtable.Synchronized (new Hashtable ());
-                                       } else {
-                                               CheckExpired (cache.Count);
-                                       }
-
-                                       return cache;
+                               lock (cache.SyncRoot) {
+                                       CheckExpired (cache.Count);
                                }
+                               
+                               return cache;
                        }
                }