Got rid of those Internal(1) warnings
[mono.git] / mcs / class / System.Web / System.Web / HttpCachePolicy.cs
1 // \r
2 // System.Web.HttpCachePolicy\r
3 //\r
4 // Authors:\r
5 //      Patrik Torstensson (Patrik.Torstensson@labs2.com)\r
6 //      Tim Coleman (tim@timcoleman.com)\r
7 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
8 //\r
9 using System;\r
10 using System.Collections;\r
11 using System.Text;\r
12 using System.Web.UI;\r
13 using System.Web.Util;\r
14 \r
15 namespace System.Web {\r
16 \r
17         class CacheabilityUpdatedEventArgs : EventArgs {\r
18 \r
19                 public readonly HttpCacheability Cacheability;\r
20 \r
21                 public CacheabilityUpdatedEventArgs (HttpCacheability cacheability)\r
22                 {\r
23                         Cacheability = cacheability;\r
24                 }\r
25         }\r
26         \r
27         internal delegate void CacheabilityUpdatedCallback (object sender, CacheabilityUpdatedEventArgs args);\r
28         \r
29         public sealed class HttpCachePolicy {\r
30 \r
31                 internal HttpCachePolicy ()\r
32                 {\r
33                 }\r
34 \r
35                 #region Fields\r
36 \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
42                 string etag;\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
50                 bool noStore;\r
51                 bool noTransforms;\r
52                 HttpCacheRevalidation revalidation;\r
53                 bool slidingSpiration;\r
54                 bool validUntilExpires;\r
55                 string varyByCustom;\r
56                 bool haveMaxAge;\r
57                 TimeSpan maxAge;\r
58                 bool haveProxyMaxAge;\r
59                 TimeSpan proxyMaxAge;\r
60                 ArrayList fields;\r
61                 bool slidingExpiration;\r
62                 int duration;\r
63                 \r
64                 #endregion\r
65 \r
66                 internal event CacheabilityUpdatedCallback CacheabilityUpdated;\r
67                 \r
68                 #region Properties\r
69                 \r
70                 public HttpCacheVaryByHeaders VaryByHeaders {\r
71                         get { return varyByHeaders; }\r
72                 }\r
73 \r
74                 public HttpCacheVaryByParams VaryByParams {\r
75                         get { return varyByParams; }\r
76                 }\r
77 \r
78                 internal DateTime Expires {\r
79                         get { return expireDate; }\r
80                 }\r
81 \r
82                 internal int Duration {\r
83                         get { return duration; }\r
84                         set { duration = value; }\r
85                 }\r
86 \r
87                 internal bool Sliding {\r
88                         get { return slidingExpiration; }\r
89                 }\r
90 \r
91                 #endregion // Properties\r
92 \r
93                 #region Methods\r
94 \r
95                 public void AddValidationCallback (HttpCacheValidateHandler handler, object data)\r
96                 {\r
97                         if (handler == null)\r
98                                 throw new ArgumentNullException ("handler");\r
99 \r
100                         if (validationCallbacks == null)\r
101                                 validationCallbacks = new ArrayList ();\r
102 \r
103                         validationCallbacks.Add (new Pair (handler, data));\r
104                 }\r
105 \r
106                 public void AppendCacheExtension (string extension)\r
107                 {\r
108                         if (extension == null)\r
109                                 throw new ArgumentNullException ("extension");\r
110 \r
111                         if (cacheExtension == null)\r
112                                 cacheExtension = new StringBuilder (extension);\r
113                         else\r
114                                 cacheExtension.Append (", " + extension);\r
115                 }\r
116 \r
117                 public void SetCacheability (HttpCacheability cacheability)\r
118                 {\r
119                         if (cacheability < HttpCacheability.NoCache || cacheability > HttpCacheability.Public)\r
120                                 throw new ArgumentOutOfRangeException ("cacheability");\r
121 \r
122                         if (this.cacheability > 0 && cacheability > this.cacheability)\r
123                                 return;\r
124                         \r
125                         this.cacheability = cacheability;\r
126 \r
127                         if (CacheabilityUpdated != null)\r
128                                 CacheabilityUpdated (this, new CacheabilityUpdatedEventArgs (cacheability));\r
129                 }\r
130 \r
131                 public void SetCacheability (HttpCacheability cacheability, string field)\r
132                 {\r
133                         if (field == null)\r
134                                 throw new ArgumentNullException ("field");\r
135 \r
136                         if (cacheability != HttpCacheability.Private &&\r
137                             cacheability != HttpCacheability.NoCache)\r
138                                 throw new ArgumentException ("Must be NoCache or Private", "cacheability");\r
139 \r
140                         if (fields == null)\r
141                                 fields = new ArrayList ();\r
142 \r
143                         fields.Add (new Pair (cacheability, field));\r
144                 }\r
145 \r
146                 public void SetETag (string etag)\r
147                 {\r
148                         if (etag == null)\r
149                                 throw new ArgumentNullException ("etag");\r
150 \r
151                         if (this.etag != null)\r
152                                 throw new InvalidOperationException ("The ETag header has already been set");\r
153 \r
154                         if (etagFromFileDependencies)\r
155                                 throw new InvalidOperationException ("SetEtagFromFileDependencies has already been called");\r
156 \r
157                         this.etag = etag;\r
158                 }\r
159 \r
160                 public void SetETagFromFileDependencies ()\r
161                 {\r
162                         if (this.etag != null)\r
163                                 throw new InvalidOperationException ("The ETag header has already been set");\r
164 \r
165                         etagFromFileDependencies = true;\r
166                 }\r
167 \r
168                 public void SetExpires (DateTime date)\r
169                 {\r
170                         if (haveExpireDate && date > expireDate)\r
171                                 return;\r
172 \r
173                         haveExpireDate = true;\r
174                         expireDate = date;\r
175                 }\r
176 \r
177                 public void SetLastModified (DateTime date)\r
178                 {\r
179                         if (date > DateTime.Now)\r
180                                 throw new ArgumentOutOfRangeException ("date");\r
181 \r
182                         if (haveLastModified && date < lastModified)\r
183                                 return;\r
184 \r
185                         haveLastModified = true;\r
186                         lastModified = date;\r
187                 }\r
188 \r
189                 public void SetLastModifiedFromFileDependencies ()\r
190                 {\r
191                         lastModifiedFromFileDependencies = true;\r
192                 }\r
193 \r
194                 public void SetMaxAge (TimeSpan date)\r
195                 {\r
196                         if (date < TimeSpan.Zero)\r
197                                 throw new ArgumentOutOfRangeException ("date");\r
198                         \r
199                         if (haveMaxAge && maxAge < date)\r
200                                 return;\r
201 \r
202                         maxAge = date;\r
203                         haveMaxAge = true;\r
204                 }\r
205 \r
206                 public void SetNoServerCaching ()\r
207                 {\r
208                         noServerCaching = true;\r
209                 }\r
210 \r
211                 public void SetNoStore ()\r
212                 {\r
213                         noStore = true;\r
214                 }\r
215 \r
216                 public void SetNoTransforms ()\r
217                 {\r
218                         noTransforms = true;\r
219                 }\r
220 \r
221                 public void SetProxyMaxAge (TimeSpan delta)\r
222                 {\r
223                         if (delta < TimeSpan.Zero)\r
224                                 throw new ArgumentOutOfRangeException ("delta");\r
225 \r
226                         if (haveProxyMaxAge && proxyMaxAge < delta)\r
227                                 return;\r
228 \r
229                         proxyMaxAge = delta;\r
230                 }\r
231 \r
232                 public void SetRevalidation (HttpCacheRevalidation revalidation)\r
233                 {\r
234                         if (revalidation < HttpCacheRevalidation.AllCaches ||\r
235                             revalidation > HttpCacheRevalidation.None)\r
236                                 throw new ArgumentOutOfRangeException ("revalidation");\r
237 \r
238                         if (this.revalidation > revalidation)\r
239                                 this.revalidation = revalidation;\r
240                 }\r
241 \r
242                 public void SetSlidingExpiration (bool slide)\r
243                 {\r
244                         slidingExpiration = slide;\r
245                 }\r
246 \r
247                 public void SetValidUntilExpires (bool validUntilExpires)\r
248                 {\r
249                         this.validUntilExpires = validUntilExpires;\r
250                 }\r
251 \r
252                 public void SetVaryByCustom (string custom)\r
253                 {\r
254                         if (custom == null)\r
255                                 throw new ArgumentNullException ("custom");\r
256 \r
257                         if (varyByCustom != null)\r
258                                 throw new InvalidOperationException ("VaryByCustom has already been set.");\r
259 \r
260                         varyByCustom = custom;\r
261                 }\r
262 \r
263                 internal string GetVaryByCustom ()\r
264                 {\r
265                         return varyByCustom;\r
266                 }\r
267 \r
268                 [MonoTODO]\r
269                 public void SetAllowResponseInBrowserHistory (bool allow)\r
270                 {\r
271                         throw new NotImplementedException ();\r
272                 }\r
273 \r
274                 internal void SetHeaders (HttpResponse response, ArrayList headers)\r
275                 {\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
280                         } else {\r
281                                 cc = "no-cache";\r
282                                 response.CacheControl = cc;\r
283                                 expires = "-1";\r
284                         }\r
285                         \r
286                         headers.Add (new HttpResponseHeader ("Cache-Control", cc));\r
287                         headers.Add (new HttpResponseHeader ("Expires", expires));\r
288                                                 \r
289                         if (etag != null)\r
290                                 headers.Add (new HttpResponseHeader ("ETag", etag));\r
291 \r
292                         if (haveLastModified)\r
293                                 headers.Add (new HttpResponseHeader ("Last-Modified",\r
294                                                              TimeUtil.ToUtcTimeString (lastModified)));\r
295 \r
296                         if (!varyByParams.IgnoreParams) {\r
297                                 HttpResponseHeader vb = varyByParams.GetResponseHeader ();\r
298                                 if (vb != null)\r
299                                         headers.Add (vb);\r
300                         }\r
301 \r
302                 }\r
303                 \r
304                 #endregion // Methods\r
305         }\r
306 }\r
307 \r