CachedVaryBy.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // System.Web.Caching.CachedVaryBy
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. // Marek Habersack <[email protected]>
  7. //
  8. // (C) 2003-2010 Novell, Inc (http://www.novell.com)
  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. using System;
  31. using System.Globalization;
  32. using System.Text;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Web.Util;
  36. namespace System.Web.Caching
  37. {
  38. #if NET_4_0
  39. [Serializable]
  40. #endif
  41. sealed class CachedVaryBy
  42. {
  43. string[] prms;
  44. string[] headers;
  45. string custom;
  46. string key;
  47. List <string> item_list;
  48. bool wildCardParams;
  49. internal CachedVaryBy (HttpCachePolicy policy, string key)
  50. {
  51. prms = policy.VaryByParams.GetParamNames ();
  52. headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
  53. custom = policy.GetVaryByCustom ();
  54. this.key = key;
  55. item_list = new List <string> ();
  56. wildCardParams = policy.VaryByParams ["*"];
  57. }
  58. internal List <string> ItemList {
  59. get { return item_list; }
  60. }
  61. internal string Key {
  62. get { return key; }
  63. }
  64. internal string CreateKey (string file_path, HttpContext context)
  65. {
  66. if (String.IsNullOrEmpty (file_path))
  67. throw new ArgumentNullException ("file_path");
  68. StringBuilder builder = new StringBuilder ("vbk"); // VaryBy Key
  69. HttpRequest request = context != null ? context.Request : null;
  70. string name, value;
  71. builder.Append (file_path);
  72. if (request == null)
  73. return builder.ToString ();
  74. builder.Append (request.HttpMethod);
  75. if (wildCardParams) {
  76. builder.Append ("WQ"); // Wildcard, Query
  77. foreach (string p in request.QueryString) {
  78. if (p == null)
  79. continue;
  80. builder.Append ('N'); // Name
  81. builder.Append (p.ToLowerInvariant ());
  82. value = request.QueryString [p];
  83. if (String.IsNullOrEmpty (value))
  84. continue;
  85. builder.Append ('V'); // Value
  86. builder.Append (value);
  87. }
  88. builder.Append ('F'); // Form
  89. foreach (string p in request.Form) {
  90. if (p == null)
  91. continue;
  92. builder.Append ('N'); // Name
  93. builder.Append (p.ToLowerInvariant ());
  94. value = request.Form [p];
  95. if (String.IsNullOrEmpty (value))
  96. continue;
  97. builder.Append ('V'); // Value
  98. builder.Append (value);
  99. }
  100. } else if (prms != null) {
  101. StringBuilder fprms = null;
  102. builder.Append ("SQ"); // Specified, Query
  103. for (int i = 0; i < prms.Length; i++) {
  104. name = prms [i];
  105. if (String.IsNullOrEmpty (name))
  106. continue;
  107. value = request.QueryString [name];
  108. if (value != null) {
  109. builder.Append ('N'); // Name
  110. builder.Append (name.ToLowerInvariant ());
  111. if (value.Length > 0) {
  112. builder.Append ('V'); // Value
  113. builder.Append (value);
  114. }
  115. }
  116. value = request.Form [name];
  117. if (value != null) {
  118. if (fprms == null)
  119. fprms = new StringBuilder ('F'); // Form
  120. builder.Append ('N'); // Name
  121. builder.Append (name.ToLowerInvariant ());
  122. if (value.Length > 0) {
  123. builder.Append ('V'); // Value
  124. builder.Append (value);
  125. }
  126. }
  127. }
  128. if (fprms != null)
  129. builder.Append (fprms.ToString ());
  130. }
  131. if (headers != null) {
  132. builder.Append ('H'); // Headers
  133. for (int i=0; i < headers.Length; i++) {
  134. builder.Append ('N'); // Name
  135. name = headers [i];
  136. builder.Append (name.ToLowerInvariant ());
  137. value = request.Headers [name];
  138. if (String.IsNullOrEmpty (value))
  139. continue;
  140. builder.Append ('V'); // Value
  141. builder.Append (value);
  142. }
  143. }
  144. if (custom != null) {
  145. builder.Append ('C'); // Custom
  146. string s = context.ApplicationInstance.GetVaryByCustomString (context, custom);
  147. builder.Append ('N'); // Name
  148. builder.Append (custom);
  149. builder.Append ('V'); // Value
  150. builder.Append (s != null ? s : "__null__");
  151. }
  152. return builder.ToString ();
  153. }
  154. }
  155. }