HttpCacheVaryByParams.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // System.Web.HttpCacheVaryByParams
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. //
  7. using System;
  8. using System.Text;
  9. using System.Collections;
  10. namespace System.Web {
  11. public sealed class HttpCacheVaryByParams {
  12. private Hashtable _Items;
  13. private bool _IgnoreParams;
  14. private bool _Wildcard;
  15. private bool _Dirty;
  16. // TODO: We need internal methods here to communicate with CachePolicy
  17. internal HttpCacheVaryByParams() {
  18. }
  19. public bool IgnoreParams {
  20. get {
  21. return _IgnoreParams;
  22. }
  23. set {
  24. if (_Wildcard || null != _Items) {
  25. return;
  26. }
  27. _Dirty = true;
  28. _IgnoreParams = value;
  29. }
  30. }
  31. internal HttpResponseHeader GetResponseHeader ()
  32. {
  33. if (_IgnoreParams)
  34. throw new Exception ("Can not get VaryByParams Header when params are ignored.");
  35. if (_Wildcard)
  36. return new HttpResponseHeader ("Vary", "*");
  37. StringBuilder builder = new StringBuilder ();
  38. foreach (string item in _Items.Keys) {
  39. if (!(bool) _Items [item])
  40. continue;
  41. builder.Append (item + ";");
  42. }
  43. return new HttpResponseHeader ("Vary", builder.ToString ());
  44. }
  45. public bool this[string header] {
  46. get {
  47. if (null == header) {
  48. throw new ArgumentNullException("header");
  49. }
  50. if (header == "*") {
  51. return _Wildcard;
  52. }
  53. if (null != _Items) {
  54. return _Items.ContainsKey(header);
  55. }
  56. return false;
  57. }
  58. set {
  59. if (null == header) {
  60. throw new ArgumentNullException("header");
  61. }
  62. if (!(value)) {
  63. return;
  64. }
  65. _Dirty = true;
  66. if (header == "*") {
  67. _Wildcard = true;
  68. _Items = null;
  69. return;
  70. }
  71. if (!_Wildcard) {
  72. if (null == _Items) {
  73. _Items = new Hashtable();
  74. }
  75. _Items[header] = true;
  76. }
  77. }
  78. }
  79. }
  80. }