CookieCollection.cs 4.1 KB

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