* NetworkAccess.cs: Only mark as flags enum on 2.0.
[mono.git] / mcs / class / System / System.Net / CookieContainer.cs
index 0ee5678ea21ffb6b06e80987746287ca6b8ad641..de818005de60c91938304b49a1a6c7137709f4fc 100644 (file)
@@ -132,15 +132,40 @@ namespace System.Net
 
                void AddCookie (Cookie cookie)
                {
-                       lock (this) {
-                               if (cookies == null)
-                                       cookies = new CookieCollection ();
+                       if (cookies == null)
+                               cookies = new CookieCollection ();
+
+                       if (count + 1 > capacity)
+                               throw new CookieException ("Capacity exceeded");
+
+                       cookies.Add (cookie);
+                       count = cookies.Count;
+                       CheckExpiration ();
+
+               }
+
+               // Only needs to be called from AddCookie (Cookie) and GetCookies (Uri)
+               void CheckExpiration ()
+               {
+                       if (cookies == null)
+                               return;
 
-                               if (count + 1 > capacity)
-                                       throw new CookieException ("Capacity exceeded");
+                       ArrayList removed = null;
+                       for (int i = cookies.Count - 1; i >= 0; i--) {
+                               Cookie cookie = cookies [i];
+                               if (cookie.Expired) {
+                                       if (removed == null)
+                                               removed = new ArrayList ();
+                                       removed.Add (i);
+                               }
+                       }
 
-                               cookies.Add (cookie);
-                               count = cookies.Count;
+                       if (removed != null) {
+                               // We went backwards above, so this works.
+                               ArrayList list = cookies.List;
+                               foreach (int n in removed) {
+                                       list.RemoveAt (n);
+                               }
                        }
                }
 
@@ -164,7 +189,7 @@ namespace System.Net
                        if (uri != null && cookie.Domain == "")
                                cookie.Domain = uri.Host;
 
-                       if (cookie.Path == null || cookie.Path == "") {
+                       if (cookie.Version == 0 && (cookie.Path == null || cookie.Path == "")) {
                                if (uri != null) {
                                        cookie.Path = uri.AbsolutePath;
                                } else {
@@ -213,7 +238,7 @@ namespace System.Net
                                return "";
 
                        StringBuilder result = new StringBuilder ();
-                       foreach (Cookie cookie in cookies) {
+                       foreach (Cookie cookie in coll) {
                                result.Append (cookie.ToString ());
                                result.Append (';');
                        }
@@ -232,8 +257,11 @@ namespace System.Net
                        int dot = host.IndexOf ('.');
                        if (dot == -1)
                                return (String.Compare (host, domain, true, CultureInfo.InvariantCulture) == 0);
-                       
-                       string subdomain = host.Substring (dot);
+
+                       if (host.Length < domain.Length)
+                               return false;
+
+                       string subdomain = host.Substring (host.Length - domain.Length);
                        return (String.Compare (subdomain, domain, true, CultureInfo.InvariantCulture) == 0);
                }
 
@@ -241,11 +269,12 @@ namespace System.Net
                {
                        if (uri == null)
                                throw new ArgumentNullException ("uri");
-                       
+
+                       CheckExpiration ();
                        CookieCollection coll = new CookieCollection ();
                        if (cookies == null)
                                return coll;
-                       
+
                        foreach (Cookie cookie in cookies) {
                                string domain = cookie.Domain;
                                string host = uri.Host;
@@ -270,9 +299,13 @@ namespace System.Net
                                        }
                                }
 
+                               if (cookie.Secure && uri.Scheme != "https")
+                                       continue;
+
                                coll.Add (cookie);
                        }
-                       
+
+                       coll.SortByPath ();
                        return coll;
                }