2 // System.Web.Caching.OutputCacheModule
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2003 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Web.Util;
35 using System.Collections;
37 namespace System.Web.Caching {
39 internal sealed class OutputCacheModule : IHttpModule {
41 private CacheItemRemovedCallback response_removed;
43 public OutputCacheModule ()
47 public void Dispose ()
51 public void Init (HttpApplication app)
53 app.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
54 app.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
56 response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
59 void OnResolveRequestCache (object o, EventArgs args)
61 HttpApplication app = (HttpApplication) o;
62 HttpContext context = app.Context;
64 string vary_key = context.Request.FilePath;
65 CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
72 key = varyby.CreateKey (vary_key, context);
73 c = context.Cache [key] as CachedRawResponse;
78 ArrayList callbacks = c.Policy.ValidationCallbacks;
79 if (callbacks != null && callbacks.Count > 0) {
81 bool isIgnored = false;
83 foreach (Pair p in callbacks) {
84 HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
85 object data = p.Second;
86 HttpValidationStatus status = HttpValidationStatus.Valid;
89 validate (context, data, ref status);
91 // MS.NET hides the exception
96 if (status == HttpValidationStatus.Invalid) {
99 } else if (status == HttpValidationStatus.IgnoreThisRequest) {
105 OnRawResponseRemoved (key, c, CacheItemRemovedReason.Removed);
107 } else if (isIgnored) {
112 context.Response.ClearContent ();
113 context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
115 context.Response.ClearHeaders ();
116 c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
117 context.Response.SetCachedHeaders (c.Headers);
119 context.Response.StatusCode = c.StatusCode;
120 context.Response.StatusDescription = c.StatusDescription;
122 app.CompleteRequest ();
125 void OnUpdateRequestCache (object o, EventArgs args)
127 HttpApplication app = (HttpApplication) o;
128 HttpContext context = app.Context;
130 if (context.Response.IsCached && context.Response.StatusCode == 200 &&
131 !context.Trace.IsEnabled)
132 DoCacheInsert (context);
136 private void DoCacheInsert (HttpContext context)
138 string vary_key = context.Request.FilePath;
140 CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
141 CachedRawResponse prev = null;
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);
157 key = varyby.CreateKey (vary_key, context);
160 prev = context.Cache [key] as CachedRawResponse;
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;
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);
176 varyby.ItemList.Add (key);
180 private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
182 CachedRawResponse c = (CachedRawResponse) value;
184 c.VaryBy.ItemList.Remove (key);
185 if (c.VaryBy.ItemList.Count != 0)
188 Cache cache = HttpRuntime.Cache;
189 cache.Remove (c.VaryBy.Key);