CachedVaryBy.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 class CachedVaryBy {
  35. private string[] prms;
  36. private string[] headers;
  37. private string custom;
  38. private string key;
  39. private ArrayList item_list;
  40. private bool wildCardParams;
  41. internal CachedVaryBy (HttpCachePolicy policy, string key)
  42. {
  43. prms = policy.VaryByParams.GetParamNames ();
  44. headers = policy.VaryByHeaders.GetHeaderNames ();
  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. builder.Append ("CachedRawResponse\n");
  62. builder.Append (file_path);
  63. builder.Append ('\n');
  64. builder.Append ("METHOD:" + request.HttpMethod);
  65. builder.Append ('\n');
  66. if (wildCardParams) {
  67. foreach (string p in request.QueryString) {
  68. // FIXME: QueryString might contain a null key if a page gets called like this: page.aspx?arg (w/out the "=")
  69. if (p == null) continue;
  70. builder.Append ("VPQ:");
  71. builder.Append (p.ToLower (CultureInfo.InvariantCulture));
  72. builder.Append ('=');
  73. builder.Append (request.QueryString [p]);
  74. builder.Append ('\n');
  75. }
  76. foreach (string p in request.Form) {
  77. // FIXME: can this be null, too?
  78. if (p == null) continue;
  79. builder.Append ("VPF:");
  80. builder.Append (p.ToLower (CultureInfo.InvariantCulture));
  81. builder.Append ('=');
  82. builder.Append (request.Form [p]);
  83. builder.Append ('\n');
  84. }
  85. } else if (prms != null) {
  86. for (int i=0; i<prms.Length; i++) {
  87. if (request.QueryString [prms [i]] != null) {
  88. builder.Append ("VPQ:");
  89. builder.Append (prms [i].ToLower (CultureInfo.InvariantCulture));
  90. builder.Append ('=');
  91. builder.Append (request.QueryString [prms [i]]);
  92. builder.Append ('\n');
  93. }
  94. if (request.Form [prms [i]] != null) {
  95. builder.Append ("VPF:");
  96. builder.Append (prms [i].ToLower (CultureInfo.InvariantCulture));
  97. builder.Append ('=');
  98. builder.Append (request.Form [prms [i]]);
  99. builder.Append ('\n');
  100. }
  101. }
  102. }
  103. if (headers != null) {
  104. for (int i=0; i<headers.Length; i++) {
  105. builder.Append ("VH:");
  106. builder.Append (headers [i].ToLower (CultureInfo.InvariantCulture));
  107. builder.Append ('=');
  108. builder.Append (request.Headers [headers [i]]);
  109. builder.Append ('\n');
  110. }
  111. }
  112. if (custom != null) {
  113. string s = app.GetVaryByCustomString (context, custom);
  114. builder.Append ("VC:");
  115. builder.Append (custom);
  116. builder.Append ('=');
  117. builder.Append (s != null ? s : "__null__");
  118. builder.Append ('\n');
  119. }
  120. return builder.ToString ();
  121. }
  122. }
  123. }