CachedVaryBy.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // System.Web.Caching.CachedVaryBy
  3. //
  4. // Author(s):
  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;
  30. using System.Globalization;
  31. using System.Text;
  32. using System.Collections;
  33. using System.Web.Util;
  34. namespace System.Web.Caching {
  35. internal sealed class CachedVaryBy {
  36. string[] prms;
  37. string[] headers;
  38. string custom;
  39. string key;
  40. ArrayList item_list;
  41. bool wildCardParams;
  42. internal CachedVaryBy (HttpCachePolicy policy, string key)
  43. {
  44. prms = policy.VaryByParams.GetParamNames ();
  45. headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
  46. custom = policy.GetVaryByCustom ();
  47. this.key = key;
  48. item_list = new ArrayList ();
  49. wildCardParams = policy.VaryByParams ["*"];
  50. }
  51. internal ArrayList ItemList {
  52. get { return item_list; }
  53. }
  54. internal string Key {
  55. get { return key; }
  56. }
  57. internal string CreateKey (string file_path, HttpContext context)
  58. {
  59. StringBuilder builder = new StringBuilder ();
  60. HttpApplication app = context.ApplicationInstance;
  61. HttpRequest request = context.Request;
  62. string newLine = Environment.NewLine;
  63. builder.Append ("CachedRawResponse" + newLine);
  64. builder.Append (file_path);
  65. builder.Append (newLine);
  66. builder.Append ("METHOD:" + request.HttpMethod);
  67. builder.Append (newLine);
  68. if (wildCardParams) {
  69. foreach (string p in request.QueryString) {
  70. // FIXME: QueryString might contain a null key if a page gets called like this: page.aspx?arg (w/out the "=")
  71. if (p == null) continue;
  72. builder.Append ("VPQ:");
  73. builder.Append (p.ToLower (Helpers.InvariantCulture));
  74. builder.Append ('=');
  75. builder.Append (request.QueryString [p]);
  76. builder.Append (newLine);
  77. }
  78. foreach (string p in request.Form) {
  79. // FIXME: can this be null, too?
  80. if (p == null) continue;
  81. builder.Append ("VPF:");
  82. builder.Append (p.ToLower (Helpers.InvariantCulture));
  83. builder.Append ('=');
  84. builder.Append (request.Form [p]);
  85. builder.Append (newLine);
  86. }
  87. } else if (prms != null) {
  88. for (int i=0; i<prms.Length; i++) {
  89. if (request.QueryString [prms [i]] != null) {
  90. builder.Append ("VPQ:");
  91. builder.Append (prms [i].ToLower (Helpers.InvariantCulture));
  92. builder.Append ('=');
  93. builder.Append (request.QueryString [prms [i]]);
  94. builder.Append (newLine);
  95. }
  96. if (request.Form [prms [i]] != null) {
  97. builder.Append ("VPF:");
  98. builder.Append (prms [i].ToLower (Helpers.InvariantCulture));
  99. builder.Append ('=');
  100. builder.Append (request.Form [prms [i]]);
  101. builder.Append (newLine);
  102. }
  103. }
  104. }
  105. if (headers != null) {
  106. for (int i=0; i<headers.Length; i++) {
  107. builder.Append ("VH:");
  108. builder.Append (headers [i].ToLower (Helpers.InvariantCulture));
  109. builder.Append ('=');
  110. builder.Append (request.Headers [headers [i]]);
  111. builder.Append (newLine);
  112. }
  113. }
  114. if (custom != null) {
  115. string s = app.GetVaryByCustomString (context, custom);
  116. builder.Append ("VC:");
  117. builder.Append (custom);
  118. builder.Append ('=');
  119. builder.Append (s != null ? s : "__null__");
  120. builder.Append (newLine);
  121. }
  122. return builder.ToString ();
  123. }
  124. }
  125. }