CachedVaryBy.cs 4.3 KB

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