Browse Source

2006-08-03 Gonzalo Paniagua Javier <[email protected]>

	* CookieCollection.cs:
	* CookieContainer.cs: remove expired cookies.


svn path=/trunk/mcs/; revision=63290
Gonzalo Paniagua Javier 19 years ago
parent
commit
dafa708ed0

+ 5 - 0
mcs/class/System/System.Net/ChangeLog

@@ -1,3 +1,8 @@
+2006-08-03 Gonzalo Paniagua Javier <[email protected]>
+
+	* CookieCollection.cs:
+	* CookieContainer.cs: remove expired cookies.
+
 2006-07-07  Atsushi Enomoto  <[email protected]>
 
 	* Dns.cs : (GetHostEntry) when passed IP addresses, it does not issue

+ 4 - 1
mcs/class/System/System.Net/CookieCollection.cs

@@ -41,6 +41,9 @@ namespace System.Net
 	{
 		ArrayList list = new ArrayList (4);
 
+		internal ArrayList List {
+			get { return list; }
+		}
 		// ICollection
 		public int Count {
 			get { return list.Count; }
@@ -133,7 +136,7 @@ namespace System.Net
 		public Cookie this [string name] {
 			get {
 				foreach (Cookie c in list) {
-					if (0 == String.Compare (c.Name, name, true))
+					if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
 						return c;
 				}
 				return null;

+ 36 - 10
mcs/class/System/System.Net/CookieContainer.cs

@@ -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);
+				}
 			}
 		}
 
@@ -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,7 +299,7 @@ namespace System.Net
 					}
 				}
 
-				if (uri.Scheme == "https" && !cookie.Secure)
+				if (cookie.Secure && uri.Scheme != "https")
 					continue;
 
 				coll.Add (cookie);