CacheControlHeaderValue.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // CacheControlHeaderValue.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Generic;
  29. namespace System.Net.Http.Headers
  30. {
  31. public class CacheControlHeaderValue : ICloneable
  32. {
  33. List<NameValueHeaderValue> extensions;
  34. List<string> no_cache_headers, private_headers;
  35. public ICollection<NameValueHeaderValue> Extensions {
  36. get {
  37. return extensions ?? (extensions = new List<NameValueHeaderValue> ());
  38. }
  39. }
  40. public TimeSpan? MaxAge { get; set; }
  41. public bool MaxStale { get; set; }
  42. public TimeSpan? MaxStaleLimit { get; set; }
  43. public TimeSpan? MinFresh { get; set; }
  44. public bool MustRevalidate { get; set; }
  45. public bool NoCache { get; set; }
  46. public ICollection<string> NoCacheHeaders {
  47. get {
  48. return no_cache_headers ?? (no_cache_headers = new List<string> ());
  49. }
  50. }
  51. public bool NoStore { get; set; }
  52. public bool NoTransform { get; set; }
  53. public bool OnlyIfCached { get; set; }
  54. public bool Private { get; set; }
  55. public ICollection<string> PrivateHeaders {
  56. get {
  57. return private_headers ?? (private_headers = new List<string> ());
  58. }
  59. }
  60. public bool ProxyRevalidate { get; set; }
  61. public bool Public { get; set; }
  62. public TimeSpan? SharedMaxAge { get; set; }
  63. object ICloneable.Clone ()
  64. {
  65. var copy = (CacheControlHeaderValue) MemberwiseClone ();
  66. if (extensions != null) {
  67. copy.extensions = new List<NameValueHeaderValue> ();
  68. foreach (var entry in extensions) {
  69. copy.extensions.Add (entry);
  70. }
  71. }
  72. if (no_cache_headers != null) {
  73. copy.no_cache_headers = new List<string> ();
  74. foreach (var entry in no_cache_headers) {
  75. copy.no_cache_headers.Add (entry);
  76. }
  77. }
  78. if (private_headers != null) {
  79. copy.private_headers = new List<string> ();
  80. foreach (var entry in private_headers) {
  81. copy.private_headers.Add (entry);
  82. }
  83. }
  84. return copy;
  85. }
  86. public override bool Equals (object obj)
  87. {
  88. var source = obj as CacheControlHeaderValue;
  89. if (source == null)
  90. return false;
  91. if (MaxAge != source.MaxAge || MaxStale != source.MaxStale || MaxStaleLimit != source.MaxStaleLimit ||
  92. MinFresh != source.MinFresh || MustRevalidate != source.MustRevalidate || NoCache != source.NoCache ||
  93. NoStore != source.NoStore || NoTransform != source.NoTransform || OnlyIfCached != source.OnlyIfCached ||
  94. Private != source.Private || ProxyRevalidate != source.ProxyRevalidate || Public != source.Public ||
  95. SharedMaxAge != source.SharedMaxAge)
  96. return false;
  97. return extensions.SequenceEqual (source.extensions) &&
  98. no_cache_headers.SequenceEqual (source.no_cache_headers) &&
  99. private_headers.SequenceEqual (source.private_headers);
  100. }
  101. public override int GetHashCode ()
  102. {
  103. int hc = 29;
  104. unchecked {
  105. hc = hc * 29 + HashCodeCalculator.Calculate (extensions);
  106. hc = hc * 29 + MaxAge.GetHashCode ();
  107. hc = hc * 29 + MaxStale.GetHashCode ();
  108. hc = hc * 29 + MaxStaleLimit.GetHashCode ();
  109. hc = hc * 29 + MinFresh.GetHashCode ();
  110. hc = hc * 29 + MustRevalidate.GetHashCode ();
  111. hc = hc * 29 + HashCodeCalculator.Calculate (no_cache_headers);
  112. hc = hc * 29 + NoCache.GetHashCode ();
  113. hc = hc * 29 + NoStore.GetHashCode ();
  114. hc = hc * 29 + NoTransform.GetHashCode ();
  115. hc = hc * 29 + OnlyIfCached.GetHashCode ();
  116. hc = hc * 29 + Private.GetHashCode ();
  117. hc = hc * 29 + HashCodeCalculator.Calculate (private_headers);
  118. hc = hc * 29 + ProxyRevalidate.GetHashCode ();
  119. hc = hc * 29 + Public.GetHashCode ();
  120. hc = hc * 29 + SharedMaxAge.GetHashCode ();
  121. }
  122. return hc;
  123. }
  124. public static CacheControlHeaderValue Parse (string input)
  125. {
  126. CacheControlHeaderValue value;
  127. if (TryParse (input, out value))
  128. return value;
  129. throw new FormatException (input);
  130. }
  131. public static bool TryParse (string input, out CacheControlHeaderValue parsedValue)
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. }
  136. }