2 // System.Web.HttpCachePolicy
\r
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
\r
6 // Tim Coleman (tim@timcoleman.com)
\r
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
10 using System.Collections;
\r
12 using System.Web.UI;
\r
13 using System.Web.Util;
\r
15 namespace System.Web {
\r
17 class CacheabilityUpdatedEventArgs : EventArgs {
\r
19 public readonly HttpCacheability Cacheability;
\r
21 public CacheabilityUpdatedEventArgs (HttpCacheability cacheability)
\r
23 Cacheability = cacheability;
\r
27 internal delegate void CacheabilityUpdatedCallback (object sender, CacheabilityUpdatedEventArgs args);
\r
29 public sealed class HttpCachePolicy {
\r
31 internal HttpCachePolicy ()
\r
37 HttpCacheVaryByHeaders varyByHeaders = new HttpCacheVaryByHeaders ();
\r
38 HttpCacheVaryByParams varyByParams = new HttpCacheVaryByParams ();
\r
39 ArrayList validationCallbacks;
\r
40 StringBuilder cacheExtension;
\r
41 HttpCacheability cacheability;
\r
43 bool etagFromFileDependencies;
\r
44 bool haveExpireDate;
\r
45 DateTime expireDate;
\r
46 bool haveLastModified;
\r
47 DateTime lastModified;
\r
48 bool lastModifiedFromFileDependencies;
\r
49 bool noServerCaching;
\r
52 HttpCacheRevalidation revalidation;
\r
53 bool slidingSpiration;
\r
54 bool validUntilExpires;
\r
55 string varyByCustom;
\r
58 bool haveProxyMaxAge;
\r
59 TimeSpan proxyMaxAge;
\r
61 bool slidingExpiration;
\r
66 internal event CacheabilityUpdatedCallback CacheabilityUpdated;
\r
70 public HttpCacheVaryByHeaders VaryByHeaders {
\r
71 get { return varyByHeaders; }
\r
74 public HttpCacheVaryByParams VaryByParams {
\r
75 get { return varyByParams; }
\r
78 internal DateTime Expires {
\r
79 get { return expireDate; }
\r
82 internal int Duration {
\r
83 get { return duration; }
\r
84 set { duration = value; }
\r
87 internal bool Sliding {
\r
88 get { return slidingExpiration; }
\r
91 #endregion // Properties
\r
95 public void AddValidationCallback (HttpCacheValidateHandler handler, object data)
\r
97 if (handler == null)
\r
98 throw new ArgumentNullException ("handler");
\r
100 if (validationCallbacks == null)
\r
101 validationCallbacks = new ArrayList ();
\r
103 validationCallbacks.Add (new Pair (handler, data));
\r
106 public void AppendCacheExtension (string extension)
\r
108 if (extension == null)
\r
109 throw new ArgumentNullException ("extension");
\r
111 if (cacheExtension == null)
\r
112 cacheExtension = new StringBuilder (extension);
\r
114 cacheExtension.Append (", " + extension);
\r
117 public void SetCacheability (HttpCacheability cacheability)
\r
119 if (cacheability < HttpCacheability.NoCache || cacheability > HttpCacheability.Public)
\r
120 throw new ArgumentOutOfRangeException ("cacheability");
\r
122 if (this.cacheability > 0 && cacheability > this.cacheability)
\r
125 this.cacheability = cacheability;
\r
127 if (CacheabilityUpdated != null)
\r
128 CacheabilityUpdated (this, new CacheabilityUpdatedEventArgs (cacheability));
\r
131 public void SetCacheability (HttpCacheability cacheability, string field)
\r
134 throw new ArgumentNullException ("field");
\r
136 if (cacheability != HttpCacheability.Private &&
\r
137 cacheability != HttpCacheability.NoCache)
\r
138 throw new ArgumentException ("Must be NoCache or Private", "cacheability");
\r
140 if (fields == null)
\r
141 fields = new ArrayList ();
\r
143 fields.Add (new Pair (cacheability, field));
\r
146 public void SetETag (string etag)
\r
149 throw new ArgumentNullException ("etag");
\r
151 if (this.etag != null)
\r
152 throw new InvalidOperationException ("The ETag header has already been set");
\r
154 if (etagFromFileDependencies)
\r
155 throw new InvalidOperationException ("SetEtagFromFileDependencies has already been called");
\r
160 public void SetETagFromFileDependencies ()
\r
162 if (this.etag != null)
\r
163 throw new InvalidOperationException ("The ETag header has already been set");
\r
165 etagFromFileDependencies = true;
\r
168 public void SetExpires (DateTime date)
\r
170 if (haveExpireDate && date > expireDate)
\r
173 haveExpireDate = true;
\r
177 public void SetLastModified (DateTime date)
\r
179 if (date > DateTime.Now)
\r
180 throw new ArgumentOutOfRangeException ("date");
\r
182 if (haveLastModified && date < lastModified)
\r
185 haveLastModified = true;
\r
186 lastModified = date;
\r
189 public void SetLastModifiedFromFileDependencies ()
\r
191 lastModifiedFromFileDependencies = true;
\r
194 public void SetMaxAge (TimeSpan date)
\r
196 if (date < TimeSpan.Zero)
\r
197 throw new ArgumentOutOfRangeException ("date");
\r
199 if (haveMaxAge && maxAge < date)
\r
206 public void SetNoServerCaching ()
\r
208 noServerCaching = true;
\r
211 public void SetNoStore ()
\r
216 public void SetNoTransforms ()
\r
218 noTransforms = true;
\r
221 public void SetProxyMaxAge (TimeSpan delta)
\r
223 if (delta < TimeSpan.Zero)
\r
224 throw new ArgumentOutOfRangeException ("delta");
\r
226 if (haveProxyMaxAge && proxyMaxAge < delta)
\r
229 proxyMaxAge = delta;
\r
232 public void SetRevalidation (HttpCacheRevalidation revalidation)
\r
234 if (revalidation < HttpCacheRevalidation.AllCaches ||
\r
235 revalidation > HttpCacheRevalidation.None)
\r
236 throw new ArgumentOutOfRangeException ("revalidation");
\r
238 if (this.revalidation > revalidation)
\r
239 this.revalidation = revalidation;
\r
242 public void SetSlidingExpiration (bool slide)
\r
244 slidingExpiration = slide;
\r
247 public void SetValidUntilExpires (bool validUntilExpires)
\r
249 this.validUntilExpires = validUntilExpires;
\r
252 public void SetVaryByCustom (string custom)
\r
254 if (custom == null)
\r
255 throw new ArgumentNullException ("custom");
\r
257 if (varyByCustom != null)
\r
258 throw new InvalidOperationException ("VaryByCustom has already been set.");
\r
260 varyByCustom = custom;
\r
263 internal string GetVaryByCustom ()
\r
265 return varyByCustom;
\r
269 public void SetAllowResponseInBrowserHistory (bool allow)
\r
271 throw new NotImplementedException ();
\r
274 internal void SetHeaders (HttpResponse response, ArrayList headers)
\r
276 string cc, expires;
\r
277 if (cacheability > HttpCacheability.NoCache) {
\r
278 cc = String.Format ("{0}, max-age={1}", cacheability, (long) maxAge.TotalSeconds);
\r
279 expires = TimeUtil.ToUtcTimeString (expireDate);
\r
282 response.CacheControl = cc;
\r
286 headers.Add (new HttpResponseHeader ("Cache-Control", cc));
\r
287 headers.Add (new HttpResponseHeader ("Expires", expires));
\r
290 headers.Add (new HttpResponseHeader ("ETag", etag));
\r
292 if (haveLastModified)
\r
293 headers.Add (new HttpResponseHeader ("Last-Modified",
\r
294 TimeUtil.ToUtcTimeString (lastModified)));
\r
296 if (!varyByParams.IgnoreParams) {
\r
297 HttpResponseHeader vb = varyByParams.GetResponseHeader ();
\r
304 #endregion // Methods
\r