OutputCacheModule.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // System.Web.Caching.OutputCacheModule
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc (http://www.novell.com)
  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. using System.IO;
  30. using System.Web;
  31. using System.Web.UI;
  32. using System.Web.Util;
  33. using System.Collections;
  34. namespace System.Web.Caching {
  35. internal sealed class OutputCacheModule : IHttpModule {
  36. private CacheItemRemovedCallback response_removed;
  37. public OutputCacheModule ()
  38. {
  39. }
  40. public void Dispose ()
  41. {
  42. }
  43. public void Init (HttpApplication app)
  44. {
  45. app.ResolveRequestCache += new EventHandler(OnResolveRequestCache);
  46. app.UpdateRequestCache += new EventHandler(OnUpdateRequestCache);
  47. response_removed = new CacheItemRemovedCallback (OnRawResponseRemoved);
  48. }
  49. void OnResolveRequestCache (object o, EventArgs args)
  50. {
  51. HttpApplication app = (HttpApplication) o;
  52. HttpContext context = app.Context;
  53. string vary_key = context.Request.FilePath;
  54. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  55. string key;
  56. CachedRawResponse c;
  57. if (varyby == null)
  58. return;
  59. key = varyby.CreateKey (vary_key, context);
  60. c = context.Cache [key] as CachedRawResponse;
  61. if (c == null)
  62. return;
  63. ArrayList callbacks = c.Policy.ValidationCallbacks;
  64. if (callbacks != null && callbacks.Count > 0) {
  65. bool isValid = true;
  66. bool isIgnored = false;
  67. foreach (Pair p in callbacks) {
  68. HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
  69. object data = p.Second;
  70. HttpValidationStatus status = HttpValidationStatus.Valid;
  71. try {
  72. validate (context, data, ref status);
  73. } catch {
  74. // MS.NET hides the exception
  75. isValid = false;
  76. break;
  77. }
  78. if (status == HttpValidationStatus.Invalid) {
  79. isValid = false;
  80. break;
  81. } else if (status == HttpValidationStatus.IgnoreThisRequest) {
  82. isIgnored = true;
  83. }
  84. }
  85. if (!isValid) {
  86. OnRawResponseRemoved (key, c, CacheItemRemovedReason.Removed);
  87. return;
  88. } else if (isIgnored) {
  89. return;
  90. }
  91. }
  92. context.Response.ClearContent ();
  93. context.Response.BinaryWrite (c.GetData (), 0, c.ContentLength);
  94. context.Response.ClearHeaders ();
  95. context.Response.SetCachedHeaders (c.Headers);
  96. context.Response.StatusCode = c.StatusCode;
  97. context.Response.StatusDescription = c.StatusDescription;
  98. app.CompleteRequest ();
  99. }
  100. void OnUpdateRequestCache (object o, EventArgs args)
  101. {
  102. HttpApplication app = (HttpApplication) o;
  103. HttpContext context = app.Context;
  104. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  105. !context.Trace.IsEnabled)
  106. DoCacheInsert (context);
  107. }
  108. private void DoCacheInsert (HttpContext context)
  109. {
  110. string vary_key = context.Request.FilePath;
  111. string key;
  112. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  113. CachedRawResponse prev = null;
  114. bool lookup = true;
  115. if (varyby == null) {
  116. string path = context.Request.MapPath (vary_key);
  117. string [] files = new string [] { path };
  118. string [] keys = new string [0];
  119. varyby = new CachedVaryBy (context.Response.Cache, vary_key);
  120. context.InternalCache.Insert (vary_key, varyby,
  121. new CacheDependency (files, keys),
  122. Cache.NoAbsoluteExpiration,
  123. Cache.NoSlidingExpiration,
  124. CacheItemPriority.Normal, null);
  125. lookup = false;
  126. }
  127. key = varyby.CreateKey (vary_key, context);
  128. if (lookup)
  129. prev = context.Cache [key] as CachedRawResponse;
  130. if (prev == null) {
  131. CachedRawResponse c = context.Response.GetCachedResponse ();
  132. string [] files = new string [] { };
  133. string [] keys = new string [] { vary_key };
  134. bool sliding = context.Response.Cache.Sliding;
  135. context.InternalCache.Insert (key, c, new CacheDependency (files, keys),
  136. (sliding ? Cache.NoAbsoluteExpiration :
  137. context.Response.Cache.Expires),
  138. (sliding ? TimeSpan.FromSeconds (
  139. context.Response.Cache.Duration) :
  140. Cache.NoSlidingExpiration),
  141. CacheItemPriority.Normal, response_removed);
  142. c.VaryBy = varyby;
  143. varyby.ItemList.Add (key);
  144. }
  145. }
  146. private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  147. {
  148. CachedRawResponse c = (CachedRawResponse) value;
  149. c.VaryBy.ItemList.Remove (key);
  150. if (c.VaryBy.ItemList.Count != 0)
  151. return;
  152. Cache cache = HttpRuntime.Cache;
  153. cache.Remove (c.VaryBy.Key);
  154. }
  155. }
  156. }