HttpCachePolicy.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.Web.HttpCachePolicy
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // Tim Coleman ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Text;
  12. using System.Web.UI;
  13. namespace System.Web {
  14. public sealed class HttpCachePolicy {
  15. internal HttpCachePolicy ()
  16. {
  17. }
  18. #region Fields
  19. HttpCacheVaryByHeaders varyByHeaders = new HttpCacheVaryByHeaders ();
  20. HttpCacheVaryByParams varyByParams = new HttpCacheVaryByParams ();
  21. ArrayList validationCallbacks;
  22. StringBuilder cacheExtension;
  23. HttpCacheability cacheability;
  24. string etag;
  25. bool etagFromFileDependencies;
  26. bool haveExpireDate;
  27. DateTime expireDate;
  28. bool haveLastModified;
  29. DateTime lastModified;
  30. bool lastModifiedFromFileDependencies;
  31. bool noServerCaching;
  32. bool noStore;
  33. bool noTransforms;
  34. HttpCacheRevalidation revalidation;
  35. bool slidingSpiration;
  36. bool validUntilExpires;
  37. string varyByCustom;
  38. bool haveMaxAge;
  39. TimeSpan maxAge;
  40. bool haveProxyMaxAge;
  41. TimeSpan proxyMaxAge;
  42. ArrayList fields;
  43. bool slidingExpiration;
  44. #endregion
  45. #region Properties
  46. public HttpCacheVaryByHeaders VaryByHeaders {
  47. get { return varyByHeaders; }
  48. }
  49. public HttpCacheVaryByParams VaryByParams {
  50. get { return varyByParams; }
  51. }
  52. #endregion // Properties
  53. #region Methods
  54. public void AddValidationCallback (HttpCacheValidateHandler handler, object data)
  55. {
  56. if (handler == null)
  57. throw new ArgumentNullException ("handler");
  58. if (validationCallbacks == null)
  59. validationCallbacks = new ArrayList ();
  60. validationCallbacks.Add (new Pair (handler, data));
  61. }
  62. public void AppendCacheExtension (string extension)
  63. {
  64. if (extension == null)
  65. throw new ArgumentNullException ("extension");
  66. if (cacheExtension == null)
  67. cacheExtension = new StringBuilder (extension);
  68. else
  69. cacheExtension.Append (", " + extension);
  70. }
  71. public void SetCacheability (HttpCacheability cacheability)
  72. {
  73. if (cacheability < HttpCacheability.NoCache || cacheability > HttpCacheability.Public)
  74. throw new ArgumentOutOfRangeException ("cacheability");
  75. if (cacheability > this.cacheability)
  76. return;
  77. this.cacheability = cacheability;
  78. }
  79. public void SetCacheability (HttpCacheability cacheability, string field)
  80. {
  81. if (field == null)
  82. throw new ArgumentNullException ("field");
  83. if (cacheability != HttpCacheability.Private &&
  84. cacheability != HttpCacheability.NoCache)
  85. throw new ArgumentException ("Must be NoCache or Private", "cacheability");
  86. if (fields == null)
  87. fields = new ArrayList ();
  88. fields.Add (new Pair (cacheability, field));
  89. }
  90. public void SetETag (string etag)
  91. {
  92. if (etag == null)
  93. throw new ArgumentNullException ("etag");
  94. if (this.etag != null)
  95. throw new InvalidOperationException ("The ETag header has already been set");
  96. if (etagFromFileDependencies)
  97. throw new InvalidOperationException ("SetEtagFromFileDependencies has already been called");
  98. this.etag = etag;
  99. }
  100. public void SetETagFromFileDependencies ()
  101. {
  102. if (this.etag != null)
  103. throw new InvalidOperationException ("The ETag header has already been set");
  104. etagFromFileDependencies = true;
  105. }
  106. public void SetExpires (DateTime date)
  107. {
  108. if (haveExpireDate && date > expireDate)
  109. return;
  110. haveExpireDate = true;
  111. expireDate = date;
  112. }
  113. public void SetLastModified (DateTime date)
  114. {
  115. if (date > DateTime.Now)
  116. throw new ArgumentOutOfRangeException ("date");
  117. if (haveLastModified && date < lastModified)
  118. return;
  119. haveLastModified = true;
  120. lastModified = date;
  121. }
  122. public void SetLastModifiedFromFileDependencies ()
  123. {
  124. lastModifiedFromFileDependencies = true;
  125. }
  126. public void SetMaxAge (TimeSpan date)
  127. {
  128. if (date < TimeSpan.Zero)
  129. throw new ArgumentOutOfRangeException ("date");
  130. if (haveMaxAge && maxAge < date)
  131. return;
  132. proxyMaxAge = date;
  133. }
  134. public void SetNoServerCaching ()
  135. {
  136. noServerCaching = true;
  137. }
  138. public void SetNoStore ()
  139. {
  140. noStore = true;
  141. }
  142. public void SetNoTransforms ()
  143. {
  144. noTransforms = true;
  145. }
  146. public void SetProxyMaxAge (TimeSpan delta)
  147. {
  148. if (delta < TimeSpan.Zero)
  149. throw new ArgumentOutOfRangeException ("delta");
  150. if (haveProxyMaxAge && proxyMaxAge < delta)
  151. return;
  152. proxyMaxAge = delta;
  153. }
  154. public void SetRevalidation (HttpCacheRevalidation revalidation)
  155. {
  156. if (revalidation < HttpCacheRevalidation.AllCaches ||
  157. revalidation > HttpCacheRevalidation.None)
  158. throw new ArgumentOutOfRangeException ("revalidation");
  159. if (this.revalidation > revalidation)
  160. this.revalidation = revalidation;
  161. }
  162. public void SetSlidingExpiration (bool slide)
  163. {
  164. slidingExpiration = slide;
  165. }
  166. public void SetValidUntilExpires (bool validUntilExpires)
  167. {
  168. this.validUntilExpires = validUntilExpires;
  169. }
  170. public void SetVaryByCustom (string custom)
  171. {
  172. if (custom == null)
  173. throw new ArgumentNullException ("custom");
  174. if (varyByCustom != null)
  175. throw new InvalidOperationException ("VaryByCustom has already been set.");
  176. varyByCustom = custom;
  177. }
  178. #endregion // Methods
  179. }
  180. }