Sorry, nothing to see here
[mono.git] / mcs / class / System / System.Net / CookieContainer.cs
index b8eada3c36e1eb1f0174752c41955a194fa23c2e..f1ca4c8a451ddcf82753c67cb45e710e9c36624f 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 ();
+
+               }
 
-                               if (count + 1 > capacity)
-                                       throw new CookieException ("Capacity exceeded");
+               // Only needs to be called from AddCookie (Cookie) and GetCookies (Uri)
+               void CheckExpiration ()
+               {
+                       if (cookies == null)
+                               return;
 
-                               cookies.Add (cookie);
-                               count = cookies.Count;
+                       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);
+                               }
+                       }
+
+                       if (removed != null) {
+                               // We went backwards above, so this works.
+                               ArrayList list = cookies.List;
+                               foreach (int n in removed) {
+                                       list.RemoveAt (n);
+                               }
                        }
                }
 
@@ -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 (';');
                        }
@@ -244,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;
@@ -273,6 +299,9 @@ namespace System.Net
                                        }
                                }
 
+                               if (cookie.Secure && uri.Scheme != "https")
+                                       continue;
+
                                coll.Add (cookie);
                        }