OutputCacheModule.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. c.DateHeader.Value = TimeUtil.ToUtcTimeString (DateTime.Now);
  96. context.Response.SetCachedHeaders (c.Headers);
  97. context.Response.StatusCode = c.StatusCode;
  98. context.Response.StatusDescription = c.StatusDescription;
  99. app.CompleteRequest ();
  100. }
  101. void OnUpdateRequestCache (object o, EventArgs args)
  102. {
  103. HttpApplication app = (HttpApplication) o;
  104. HttpContext context = app.Context;
  105. if (context.Response.IsCached && context.Response.StatusCode == 200 &&
  106. !context.Trace.IsEnabled)
  107. DoCacheInsert (context);
  108. }
  109. private void DoCacheInsert (HttpContext context)
  110. {
  111. string vary_key = context.Request.FilePath;
  112. string key;
  113. CachedVaryBy varyby = context.Cache [vary_key] as CachedVaryBy;
  114. CachedRawResponse prev = null;
  115. bool lookup = true;
  116. if (varyby == null) {
  117. string path = context.Request.MapPath (vary_key);
  118. string [] files = new string [] { path };
  119. string [] keys = new string [0];
  120. varyby = new CachedVaryBy (context.Response.Cache, vary_key);
  121. context.Cache.InsertPrivate (vary_key, varyby,
  122. new CacheDependency (files, keys),
  123. Cache.NoAbsoluteExpiration,
  124. Cache.NoSlidingExpiration,
  125. CacheItemPriority.Normal, null);
  126. lookup = false;
  127. }
  128. key = varyby.CreateKey (vary_key, context);
  129. if (lookup)
  130. prev = context.Cache [key] as CachedRawResponse;
  131. if (prev == null) {
  132. CachedRawResponse c = context.Response.GetCachedResponse ();
  133. string [] files = new string [] { };
  134. string [] keys = new string [] { vary_key };
  135. bool sliding = context.Response.Cache.Sliding;
  136. context.Cache.InsertPrivate (key, c, new CacheDependency (files, keys),
  137. (sliding ? Cache.NoAbsoluteExpiration :
  138. context.Response.Cache.Expires),
  139. (sliding ? TimeSpan.FromSeconds (
  140. context.Response.Cache.Duration) :
  141. Cache.NoSlidingExpiration),
  142. CacheItemPriority.Normal, response_removed);
  143. c.VaryBy = varyby;
  144. varyby.ItemList.Add (key);
  145. }
  146. }
  147. private void OnRawResponseRemoved (string key, object value, CacheItemRemovedReason reason)
  148. {
  149. CachedRawResponse c = (CachedRawResponse) value;
  150. c.VaryBy.ItemList.Remove (key);
  151. if (c.VaryBy.ItemList.Count != 0)
  152. return;
  153. Cache cache = HttpRuntime.Cache;
  154. cache.Remove (c.VaryBy.Key);
  155. }
  156. }
  157. }