CookieCollection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // CookieCollection.cs
  3. // Copied from System.Net.CookieCollection
  4. //
  5. // Authors:
  6. // Lawrence Pit (loz@cable.a2000.nl)
  7. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  8. // Sebastien Pouliot <sebastien@ximian.com>
  9. //
  10. // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
  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;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.Globalization;
  35. using System.Net;
  36. using System.Runtime.Serialization;
  37. namespace WebSocketSharp.Net {
  38. [Serializable]
  39. public class CookieCollection : ICollection, IEnumerable
  40. {
  41. // not 100% identical to MS implementation
  42. sealed class CookieCollectionComparer : IComparer<Cookie>
  43. {
  44. public int Compare (Cookie x, Cookie y)
  45. {
  46. if (x == null || y == null)
  47. return 0;
  48. int c1 = x.Name.Length + x.Value.Length;
  49. int c2 = y.Name.Length + y.Value.Length;
  50. return (c1 - c2);
  51. }
  52. }
  53. static CookieCollectionComparer Comparer = new CookieCollectionComparer ();
  54. List<Cookie> list = new List<Cookie> ();
  55. internal IList<Cookie> List {
  56. get { return list; }
  57. }
  58. // ICollection
  59. public int Count {
  60. get { return list.Count; }
  61. }
  62. public bool IsSynchronized {
  63. get { return false; }
  64. }
  65. public Object SyncRoot {
  66. get { return this; }
  67. }
  68. public void CopyTo (Array array, int index)
  69. {
  70. (list as IList).CopyTo (array, index);
  71. }
  72. public void CopyTo (Cookie [] array, int index)
  73. {
  74. list.CopyTo (array, index);
  75. }
  76. // IEnumerable
  77. public IEnumerator GetEnumerator ()
  78. {
  79. return list.GetEnumerator ();
  80. }
  81. // This
  82. // LAMESPEC: So how is one supposed to create a writable CookieCollection
  83. // instance?? We simply ignore this property, as this collection is always
  84. // writable.
  85. public bool IsReadOnly {
  86. get { return true; }
  87. }
  88. public void Add (Cookie cookie)
  89. {
  90. if (cookie == null)
  91. throw new ArgumentNullException ("cookie");
  92. int pos = SearchCookie (cookie);
  93. if (pos == -1)
  94. list.Add (cookie);
  95. else
  96. list [pos] = cookie;
  97. }
  98. internal void Sort ()
  99. {
  100. if (list.Count > 0)
  101. list.Sort (Comparer);
  102. }
  103. int SearchCookie (Cookie cookie)
  104. {
  105. string name = cookie.Name;
  106. string domain = cookie.Domain;
  107. string path = cookie.Path;
  108. for (int i = list.Count - 1; i >= 0; i--) {
  109. Cookie c = list [i];
  110. if (c.Version != cookie.Version)
  111. continue;
  112. if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
  113. continue;
  114. if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
  115. continue;
  116. if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
  117. continue;
  118. return i;
  119. }
  120. return -1;
  121. }
  122. public void Add (CookieCollection cookies)
  123. {
  124. if (cookies == null)
  125. throw new ArgumentNullException ("cookies");
  126. foreach (Cookie c in cookies)
  127. Add (c);
  128. }
  129. public Cookie this [int index] {
  130. get {
  131. if (index < 0 || index >= list.Count)
  132. throw new ArgumentOutOfRangeException ("index");
  133. return list [index];
  134. }
  135. }
  136. public Cookie this [string name] {
  137. get {
  138. foreach (Cookie c in list) {
  139. if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
  140. return c;
  141. }
  142. return null;
  143. }
  144. }
  145. }
  146. }