2
0

CachedVaryBy.cs 4.8 KB

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