merge -r 60439:60440
[mono.git] / mcs / class / System.Web / System.Web.Caching / OutputCacheModule.cs
1 //
2 // System.Web.Caching.OutputCacheModule
3 //
4 // Authors:
5 //  Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2003 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Web;
33 using System.Web.UI;
34 using System.Web.Util;
35 using System.Collections;
36
37 namespace System.Web.Caching {
38         
39         internal sealed class OutputCacheModule : IHttpModule {
40
41                 private CacheItemRemovedCallback response_removed;
42                 
43                 public OutputCacheModule ()
44                 {
45                 }
46
47                 public void Dispose ()
48                 {
49                 }
50
51                 public void Init (HttpApplication app)
52                 {
53                         app.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
54                         app.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
55  
56                         response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
57                 }
58
59                 void OnResolveRequestCache (object o, EventArgs args)
60                 {
61                         HttpApplication app = (HttpApplication) o;
62                         HttpContext context = app.Context;
63                         
64                         string vary_key = context.Request.FilePath;
65                         CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
66                         string key;
67                         CachedRawResponse c;
68
69                         if (varyby == null)
70                                 return;
71
72                         key = varyby.CreateKey (vary_key, context);
73                         c = context.Cache [key] as CachedRawResponse;
74
75                         if (c == null)
76                                 return;
77
78                         ArrayList callbacks = c.Policy.ValidationCallbacks;
79                         if (callbacks != null && callbacks.Count > 0) {
80                                 bool isValid = true;
81                                 bool isIgnored = false;
82
83                                 foreach (Pair p in callbacks) {
84                                         HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
85                                         object data = p.Second;
86                                         HttpValidationStatus status = HttpValidationStatus.Valid;
87
88                                         try {
89                                                 validate (context, data, ref status);
90                                         } catch {
91                                                 // MS.NET hides the exception
92                                                 isValid = false;
93                                                 break;
94                                         }
95
96                                         if (status == HttpValidationStatus.Invalid) {
97                                                 isValid = false;
98                                                 break;
99                                         } else if (status == HttpValidationStatus.IgnoreThisRequest) {
100                                                 isIgnored = true;
101                                         }
102                                 }
103
104                                 if (!isValid) {
105                                         OnRawResponseRemoved (key, c, CacheItemRemovedReason.Removed);
106                                         return;
107                                 } else if (isIgnored) {
108                                         return;
109                                 }
110                         }
111                         
112                         context.Response.ClearContent ();
113                         context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
114
115                         context.Response.ClearHeaders ();
116                         c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
117                         context.Response.SetCachedHeaders (c.Headers);
118
119                         context.Response.StatusCode = c.StatusCode;
120                         context.Response.StatusDescription = c.StatusDescription;
121                                 
122                         app.CompleteRequest ();
123                 }
124
125                 void OnUpdateRequestCache (object o, EventArgs args)
126                 {
127                         HttpApplication app = (HttpApplication) o;
128                         HttpContext context = app.Context;
129
130                         if (context.Response.IsCached && context.Response.StatusCode == 200 && 
131                             !context.Trace.IsEnabled)
132                                 DoCacheInsert (context);
133
134                 }
135
136                 private void DoCacheInsert (HttpContext context)
137                 {
138                         string vary_key = context.Request.FilePath;
139                         string key;
140                         CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
141                         CachedRawResponse prev = null;
142                         bool lookup = true;
143                         
144                         if (varyby == null) {
145                                 string path = context.Request.MapPath (vary_key);
146                                 string [] files = new string [] { path };
147                                 string [] keys = new string [0];
148                                 varyby = new CachedVaryBy (context.Response.Cache, vary_key);
149                                 context.Cache.InsertPrivate (vary_key, varyby,
150                                                 new CacheDependency (files, keys),
151                                                 Cache.NoAbsoluteExpiration,
152                                                 Cache.NoSlidingExpiration,
153                                                 CacheItemPriority.Normal, null);
154                                 lookup = false;
155                         } 
156                         
157                         key = varyby.CreateKey (vary_key, context);
158
159                         if (lookup)
160                                 prev = context.Cache [key] as CachedRawResponse;
161                         
162                         if (prev == null) {
163                                 CachedRawResponse c = context.Response.GetCachedResponse ();
164                                 string [] files = new string [] { };
165                                 string [] keys = new string [] { vary_key };
166                                 bool sliding = context.Response.Cache.Sliding;
167
168                                 context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
169                                                 (sliding ? Cache.NoAbsoluteExpiration :
170                                                                 context.Response.Cache.Expires),
171                                                 (sliding ? TimeSpan.FromSeconds (
172                                                         context.Response.Cache.Duration) :
173                                                                 Cache.NoSlidingExpiration),
174                                                 CacheItemPriority.Normal, response_removed);
175                                 c.VaryBy = varyby;
176                                 varyby.ItemList.Add (key);
177                         } 
178                 }
179
180                 private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
181                 {
182                         CachedRawResponse c = (CachedRawResponse) value;
183
184                         c.VaryBy.ItemList.Remove (key);                 
185                         if (c.VaryBy.ItemList.Count != 0)
186                                 return;
187                         
188                         Cache cache = HttpRuntime.Cache;
189                         cache.Remove (c.VaryBy.Key);
190                 }
191         }
192 }
193