Browse Source

* HttpCachePolicy.cs: Make sure cacheability and maxage get
set. Add method to set Http response header data
* HttpCacheVaryByParams.cs: Add method to create a response header.
* HttpCacheability.cs: Add ServerAndPrivate and ServerAndNoCache.
* HttpResponse.cs: Set cache headers.

svn path=/trunk/mcs/; revision=19971

Jackson Harper 22 years ago
parent
commit
02b656b2de

+ 8 - 0
mcs/class/System.Web/System.Web/ChangeLog

@@ -1,3 +1,11 @@
+2003-11-13  Jackson Harper  <[email protected]>
+
+	* HttpCachePolicy.cs: Make sure cacheability and maxage get
+	set. Add method to set Http response header data
+	* HttpCacheVaryByParams.cs: Add method to create a response header.
+	* HttpCacheability.cs: Add ServerAndPrivate and ServerAndNoCache.
+	* HttpResponse.cs: Set cache headers.
+	
 2003-11-11 Ben Maurer  <[email protected]>
 
 	* HttpModuleCollection.cs (GetKey): Recursion, again!

+ 36 - 7
mcs/class/System.Web/System.Web/HttpCachePolicy.cs

@@ -2,14 +2,15 @@
 // System.Web.HttpCachePolicy
 //
 // Authors:
-// 	Patrik Torstensson ([email protected])
-// 	Tim Coleman ([email protected])
+//	Patrik Torstensson ([email protected])
+//	Tim Coleman ([email protected])
 //	Gonzalo Paniagua Javier ([email protected])
 //
 using System;
 using System.Collections;
 using System.Text;
 using System.Web.UI;
+using System.Web.Util;
 
 namespace System.Web {
 	public sealed class HttpCachePolicy {
@@ -45,7 +46,7 @@ namespace System.Web {
 		TimeSpan proxyMaxAge;
 		ArrayList fields;
 		bool slidingExpiration;
-
+		
 		#endregion
 
 		#region Properties
@@ -89,9 +90,9 @@ namespace System.Web {
 			if (cacheability < HttpCacheability.NoCache || cacheability > HttpCacheability.Public)
 				throw new ArgumentOutOfRangeException ("cacheability");
 
-			if (cacheability > this.cacheability)
+			if (this.cacheability > 0 && cacheability > this.cacheability)
 				return;
-
+			
 			this.cacheability = cacheability;
 		}
 
@@ -162,11 +163,12 @@ namespace System.Web {
 		{
 			if (date < TimeSpan.Zero)
 				throw new ArgumentOutOfRangeException ("date");
-
+			
 			if (haveMaxAge && maxAge < date)
 				return;
 
-			proxyMaxAge = date;
+			maxAge = date;
+			haveMaxAge = true;
 		}
 
 		public void SetNoServerCaching ()
@@ -226,6 +228,33 @@ namespace System.Web {
 			varyByCustom = custom;
 		}
 
+		internal void SetHeaders (HttpResponse response, ArrayList headers)
+		{
+			string cc, expires;
+			if (cacheability > HttpCacheability.NoCache) {
+				cc = String.Format ("{0}, max-age={1}", cacheability, (long) maxAge.TotalSeconds);
+				expires = TimeUtil.ToUtcTimeString (expireDate);
+			} else {
+				cc = "no-cache";
+				response.CacheControl = cc;
+				expires = "-1";
+			}
+			
+			headers.Add (new HttpResponseHeader ("Cache-Control", cc));
+			headers.Add (new HttpResponseHeader ("Expires", expires));
+						
+			if (etag != null)
+				headers.Add (new HttpResponseHeader ("ETag", etag));
+
+			if (haveLastModified)
+				headers.Add (new HttpResponseHeader ("Last-Modified",
+							     TimeUtil.ToUtcTimeString (lastModified)));
+
+			if (varyByParams.IgnoreParams)
+				headers.Add (varyByParams.GetResponseHeader ());
+
+		}
+		
 		#endregion // Methods
 	}
 }

+ 20 - 1
mcs/class/System.Web/System.Web/HttpCacheVaryByParams.cs

@@ -5,6 +5,7 @@
 //   Patrik Torstensson ([email protected])
 //
 using System;
+using System.Text;
 using System.Collections;
 
 namespace System.Web {
@@ -33,7 +34,25 @@ namespace System.Web {
             _IgnoreParams = value;
          }
       }
-      
+
+      internal HttpResponseHeader GetResponseHeader ()
+      {
+	      if (_IgnoreParams)
+		      throw new Exception ("Can not get VaryByParams Header when params are ignored.");
+
+	      if (_Wildcard)
+		      return new HttpResponseHeader ("Vary", "*");
+
+	      StringBuilder builder = new StringBuilder ();
+	      foreach (string item in _Items.Keys) {
+		      if (!(bool) _Items [item])
+			      continue;
+		      builder.Append (item + ";");
+	      }
+
+	      return new HttpResponseHeader ("Vary", builder.ToString ());
+      }
+           
       public bool this[string header] {
          get {
             if (null == header) {

+ 2 - 0
mcs/class/System.Web/System.Web/HttpCacheability.cs

@@ -14,5 +14,7 @@ namespace System.Web {
 		Private,
 		Server,
 		Public,
+		ServerAndPrivate,
+		ServerAndNoCache = 0x3
 	}
 }

+ 4 - 1
mcs/class/System.Web/System.Web/HttpResponse.cs

@@ -159,7 +159,10 @@ namespace System.Web
 				oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentType,
 								      _sContentType));
 			}
-
+     			
+			if (_CachePolicy != null)
+				_CachePolicy.SetHeaders (this, oHeaders);
+			
 			if (_sCacheControl != null) {
 				oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderPragma,
 								      _sCacheControl));