merge -r 58060:58217
[mono.git] / mcs / class / System.Web / System.Web / HttpCachePolicy.cs
1 // 
2 // System.Web.HttpCachePolicy
3 //
4 // Authors:
5 //      Tim Coleman (tim@timcoleman.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Globalization;
32 using System.Security.Permissions;
33 using System.Text;
34 using System.Web.UI;
35 using System.Web.Util;
36
37 namespace System.Web {
38
39         class CacheabilityUpdatedEventArgs : EventArgs {
40
41                 public readonly HttpCacheability Cacheability;
42
43                 public CacheabilityUpdatedEventArgs (HttpCacheability cacheability)
44                 {
45                         Cacheability = cacheability;
46                 }
47         }
48         
49         internal delegate void CacheabilityUpdatedCallback (object sender, CacheabilityUpdatedEventArgs args);
50         
51         // CAS - no InheritanceDemand here as the class is sealed
52         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
53         public sealed class HttpCachePolicy {
54
55                 internal HttpCachePolicy ()
56                 {
57                 }
58
59 #region Fields
60
61                 HttpCacheVaryByHeaders vary_by_headers = new HttpCacheVaryByHeaders ();
62                 HttpCacheVaryByParams vary_by_params = new HttpCacheVaryByParams ();
63                 ArrayList validation_callbacks;
64                 StringBuilder cache_extension;
65                 internal HttpCacheability Cacheability;
66                 string etag;
67                 bool etag_from_file_dependencies;
68
69                 //
70                 // Used externally
71                 //
72                 internal bool have_expire_date;
73                 internal DateTime expire_date;
74                 internal bool have_last_modified;
75                 internal DateTime last_modified;
76                 
77                 //bool LastModifiedFromFileDependencies;
78                 HttpCacheRevalidation revalidation;
79                 string vary_by_custom;
80                 bool HaveMaxAge;
81                 TimeSpan MaxAge;
82                 bool HaveProxyMaxAge;
83                 TimeSpan ProxyMaxAge;
84                 ArrayList fields;
85                 bool sliding_expiration;
86                 int duration;
87                 bool allow_response_in_browser_history;
88                 
89 #endregion
90
91                 internal event CacheabilityUpdatedCallback CacheabilityUpdated;
92                 
93 #region Properties
94                 
95                 public HttpCacheVaryByHeaders VaryByHeaders {
96                         get { return vary_by_headers; }
97                 }
98
99                 public HttpCacheVaryByParams VaryByParams {
100                         get { return vary_by_params; }
101                 }
102
103                 internal int Duration {
104                         get { return duration; }
105                         set { duration = value; }
106                 }
107
108                 internal bool Sliding {
109                         get { return sliding_expiration; }
110                 }
111                 
112                 internal DateTime Expires {
113                         get { return expire_date; }
114                 }
115
116 #endregion // Properties
117
118 #region Methods
119
120                 internal int ExpireMinutes ()
121                 {
122                         if (!have_expire_date)
123                                 return 0;
124                         
125                         return (expire_date - DateTime.Now).Minutes;
126                 }
127                 
128                 public void AddValidationCallback (HttpCacheValidateHandler handler, object data)
129                 {
130                         if (handler == null)
131                                 throw new ArgumentNullException ("handler");
132
133                         if (validation_callbacks == null)
134                                 validation_callbacks = new ArrayList ();
135
136                         validation_callbacks.Add (new Pair (handler, data));
137                 }
138
139                 public void AppendCacheExtension (string extension)
140                 {
141                         if (extension == null)
142                                 throw new ArgumentNullException ("extension");
143
144                         if (cache_extension == null)
145                                 cache_extension = new StringBuilder (extension);
146                         else
147                                 cache_extension.Append (", " + extension);
148                 }
149
150                 //
151                 // This one now allows the full range of Cacheabilities.
152                 //
153                 public void SetCacheability (HttpCacheability cacheability)
154                 {
155                         if (cacheability < HttpCacheability.NoCache || cacheability > HttpCacheability.ServerAndPrivate)
156                                 throw new ArgumentOutOfRangeException ("cacheability");
157
158                         if (Cacheability > 0 && cacheability > Cacheability)
159                                 return;
160                         
161                         Cacheability = cacheability;
162
163                         if (CacheabilityUpdated != null)
164                                 CacheabilityUpdated (this, new CacheabilityUpdatedEventArgs (cacheability));
165                 }
166
167                 public void SetCacheability (HttpCacheability cacheability, string field)
168                 {
169                         if (field == null)
170                                 throw new ArgumentNullException ("field");
171
172                         if (cacheability != HttpCacheability.NoCache && cacheability != HttpCacheability.Private)
173                                 throw new ArgumentException ("Must be NoCache or Private", "cacheability");
174
175                         if (fields == null)
176                                 fields = new ArrayList ();
177
178                         fields.Add (new Pair (cacheability, field));
179                 }
180
181                 public void SetETag (string etag)
182                 {
183                         if (etag == null)
184                                 throw new ArgumentNullException ("etag");
185
186                         if (this.etag != null)
187                                 throw new InvalidOperationException ("The ETag header has already been set");
188
189                         if (etag_from_file_dependencies)
190                                 throw new InvalidOperationException ("SetEtagFromFileDependencies has already been called");
191
192                         this.etag = etag;
193                 }
194
195                 public void SetETagFromFileDependencies ()
196                 {
197                         if (this.etag != null)
198                                 throw new InvalidOperationException ("The ETag header has already been set");
199
200                         etag_from_file_dependencies = true;
201                 }
202
203                 public void SetExpires (DateTime date)
204                 {
205                         if (have_expire_date && date > expire_date)
206                                 return;
207
208                         have_expire_date = true;
209                         expire_date = date;
210                 }
211
212                 public void SetLastModified (DateTime date)
213                 {
214                         if (date > DateTime.Now)
215                                 throw new ArgumentOutOfRangeException ("date");
216
217                         if (have_last_modified && date < last_modified)
218                                 return;
219
220                         have_last_modified = true;
221                         last_modified = date;
222                 }
223
224                 [MonoTODO]
225                 public void SetLastModifiedFromFileDependencies ()
226                 {
227                         throw new NotImplementedException (); 
228                 }
229
230                 public void SetMaxAge (TimeSpan date)
231                 {
232                         if (date < TimeSpan.Zero)
233                                 throw new ArgumentOutOfRangeException ("date");
234                         
235                         if (HaveMaxAge && MaxAge < date)
236                                 return;
237
238                         MaxAge = date;
239                         HaveMaxAge = true;
240                 }
241
242                 [MonoTODO]
243                 public void SetNoServerCaching ()
244                 {
245                         throw new NotImplementedException (); 
246                 }
247
248                 [MonoTODO]
249                 public void SetNoStore ()
250                 {
251                         throw new NotImplementedException (); 
252                 }
253
254                 [MonoTODO]
255                 public void SetNoTransforms ()
256                 {
257                         throw new NotImplementedException (); 
258                 }
259
260                 public void SetProxyMaxAge (TimeSpan delta)
261                 {
262                         if (delta < TimeSpan.Zero)
263                                 throw new ArgumentOutOfRangeException ("delta");
264
265                         if (HaveProxyMaxAge && ProxyMaxAge < delta)
266                                 return;
267
268                         ProxyMaxAge = delta;
269                 }
270
271                 public void SetRevalidation (HttpCacheRevalidation revalidation)
272                 {
273                         if (revalidation < HttpCacheRevalidation.AllCaches ||
274                             revalidation > HttpCacheRevalidation.None)
275                                 throw new ArgumentOutOfRangeException ("revalidation");
276
277                         if (this.revalidation > revalidation)
278                                 this.revalidation = revalidation;
279                 }
280
281                 public void SetSlidingExpiration (bool slide)
282                 {
283                         sliding_expiration = slide;
284                 }
285
286                 [MonoTODO]
287                 public void SetValidUntilExpires (bool validUntilExpires)
288                 {
289                         throw new NotImplementedException (); 
290                 }
291
292                 public void SetVaryByCustom (string custom)
293                 {
294                         if (custom == null)
295                                 throw new ArgumentNullException ("custom");
296
297                         if (vary_by_custom != null)
298                                 throw new InvalidOperationException ("VaryByCustom has already been set.");
299
300                         vary_by_custom = custom;
301                 }
302
303                 internal string GetVaryByCustom ()
304                 {
305                         return vary_by_custom;
306                 }
307
308                 public void SetAllowResponseInBrowserHistory (bool allow)
309                 {
310                         if (Cacheability == HttpCacheability.NoCache || Cacheability == HttpCacheability.ServerAndNoCache) 
311                                 allow_response_in_browser_history = allow;
312                 }
313
314                 internal void SetHeaders (HttpResponse response, ArrayList headers)
315                 {
316                         string cc, expires;
317                         if (Cacheability > HttpCacheability.NoCache) {
318                                 string c = Cacheability.ToString ().ToLower (CultureInfo.InvariantCulture);
319                                 
320                                 if (MaxAge.TotalSeconds != 0)
321                                         cc = String.Format ("{0}, max-age={1}", c, (long) MaxAge.TotalSeconds);
322                                 else
323                                         cc = c;
324                                 
325                                 expires = TimeUtil.ToUtcTimeString (expire_date);
326                                 headers.Add (new UnknownResponseHeader ("Expires", expires));
327                         } else {
328                                 cc = "no-cache";
329                                 response.CacheControl = cc;
330                                 if (!allow_response_in_browser_history) {
331                                         expires = "-1";
332                                         headers.Add (new UnknownResponseHeader ("Expires", expires));
333                                 }
334                         }
335                         
336                         headers.Add (new UnknownResponseHeader ("Cache-Control", cc));
337                                                 
338                         if (etag != null)
339                                 headers.Add (new UnknownResponseHeader ("ETag", etag));
340
341                         if (have_last_modified)
342                                 headers.Add (new UnknownResponseHeader ("Last-Modified",
343                                                              TimeUtil.ToUtcTimeString (last_modified)));
344
345                         if (!vary_by_params.IgnoreParams) {
346                                 BaseResponseHeader vb = vary_by_params.GetResponseHeader ();
347                                 if (vb != null)
348                                         headers.Add (vb);
349                         }
350
351                 }
352 #if NET_2_0
353                 [MonoTODO]
354                 public void SetOmitVaryStar (bool omit)
355                 {
356                         throw new NotImplementedException (); 
357                 }
358 #endif
359                 
360 #endregion // Methods
361         }
362 }
363