CookieCollection.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // System.Net.CookieCollection
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Globalization;
  34. using System.Runtime.Serialization;
  35. namespace System.Net
  36. {
  37. [Serializable]
  38. #if NET_2_1
  39. public sealed class CookieCollection : ICollection, IEnumerable {
  40. #else
  41. public class CookieCollection : ICollection, IEnumerable {
  42. #endif
  43. // not 100% identical to MS implementation
  44. sealed class CookieCollectionComparer : IComparer<Cookie> {
  45. public int Compare (Cookie x, Cookie y)
  46. {
  47. if (x == null || y == null)
  48. return 0;
  49. var ydomain = y.Domain.Length - (y.Domain[0] == '.' ? 1 : 0);
  50. var xdomain = x.Domain.Length - (x.Domain[0] == '.' ? 1 : 0);
  51. int result = ydomain - xdomain;
  52. return result == 0 ? y.Path.Length - x.Path.Length : result;
  53. }
  54. }
  55. static CookieCollectionComparer Comparer = new CookieCollectionComparer ();
  56. List<Cookie> list = new List<Cookie> ();
  57. internal IList<Cookie> List {
  58. get { return list; }
  59. }
  60. // ICollection
  61. public int Count {
  62. get { return list.Count; }
  63. }
  64. public bool IsSynchronized {
  65. get { return false; }
  66. }
  67. public Object SyncRoot {
  68. get { return this; }
  69. }
  70. public void CopyTo (Array array, int index)
  71. {
  72. (list as IList).CopyTo (array, index);
  73. }
  74. public void CopyTo (Cookie [] array, int index)
  75. {
  76. list.CopyTo (array, index);
  77. }
  78. // IEnumerable
  79. public IEnumerator GetEnumerator ()
  80. {
  81. return list.GetEnumerator ();
  82. }
  83. // This
  84. // LAMESPEC: So how is one supposed to create a writable CookieCollection
  85. // instance?? We simply ignore this property, as this collection is always
  86. // writable.
  87. public bool IsReadOnly {
  88. get { return true; }
  89. }
  90. public void Add (Cookie cookie)
  91. {
  92. if (cookie == null)
  93. throw new ArgumentNullException ("cookie");
  94. int pos = SearchCookie (cookie);
  95. if (pos == -1)
  96. list.Add (cookie);
  97. else
  98. list [pos] = cookie;
  99. }
  100. internal void Sort ()
  101. {
  102. if (list.Count > 0)
  103. list.Sort (Comparer);
  104. }
  105. int SearchCookie (Cookie cookie)
  106. {
  107. string name = cookie.Name;
  108. string domain = cookie.Domain;
  109. string path = cookie.Path;
  110. for (int i = list.Count - 1; i >= 0; i--) {
  111. Cookie c = list [i];
  112. if (c.Version != cookie.Version)
  113. continue;
  114. if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
  115. continue;
  116. if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
  117. continue;
  118. if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
  119. continue;
  120. return i;
  121. }
  122. return -1;
  123. }
  124. public void Add (CookieCollection cookies)
  125. {
  126. if (cookies == null)
  127. throw new ArgumentNullException ("cookies");
  128. foreach (Cookie c in cookies)
  129. Add (c);
  130. }
  131. public Cookie this [int index] {
  132. get {
  133. if (index < 0 || index >= list.Count)
  134. throw new ArgumentOutOfRangeException ("index");
  135. return list [index];
  136. }
  137. }
  138. public Cookie this [string name] {
  139. get {
  140. foreach (Cookie c in list) {
  141. if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
  142. return c;
  143. }
  144. return null;
  145. }
  146. }
  147. } // CookieCollection
  148. } // System.Net