CookieCollection.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ArrayList list = new ArrayList (4);
  40. internal ArrayList List {
  41. get { return list; }
  42. }
  43. // ICollection
  44. public int Count {
  45. get { return list.Count; }
  46. }
  47. public bool IsSynchronized {
  48. get { return false; }
  49. }
  50. public Object SyncRoot {
  51. get { return this; }
  52. }
  53. public void CopyTo (Array array, int arrayIndex)
  54. {
  55. list.CopyTo (array, arrayIndex);
  56. }
  57. // IEnumerable
  58. public IEnumerator GetEnumerator ()
  59. {
  60. return list.GetEnumerator ();
  61. }
  62. // This
  63. // LAMESPEC: So how is one supposed to create a writable CookieCollection
  64. // instance?? We simply ignore this property, as this collection is always
  65. // writable.
  66. public bool IsReadOnly {
  67. get { return true; }
  68. }
  69. public void Add (Cookie cookie)
  70. {
  71. if (cookie == null)
  72. throw new ArgumentNullException ("cookie");
  73. int pos = SearchCookie (cookie);
  74. if (pos == -1)
  75. list.Add (cookie);
  76. else
  77. list [pos] = cookie;
  78. }
  79. int SearchCookie (Cookie cookie)
  80. {
  81. string name = cookie.Name;
  82. string domain = cookie.Domain;
  83. string path = cookie.Path;
  84. for (int i = list.Count - 1; i >= 0; i--) {
  85. Cookie c = (Cookie) list [i];
  86. if (c.Version != cookie.Version)
  87. continue;
  88. if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
  89. continue;
  90. if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
  91. continue;
  92. if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
  93. continue;
  94. return i;
  95. }
  96. return -1;
  97. }
  98. public void Add (CookieCollection cookies)
  99. {
  100. if (cookies == null)
  101. throw new ArgumentNullException ("cookies");
  102. foreach (Cookie c in cookies)
  103. Add (c);
  104. }
  105. public Cookie this [int index] {
  106. get {
  107. if (index < 0 || index >= list.Count)
  108. throw new ArgumentOutOfRangeException ("index");
  109. return (Cookie) list [index];
  110. }
  111. }
  112. public Cookie this [string name] {
  113. get {
  114. foreach (Cookie c in list) {
  115. if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
  116. return c;
  117. }
  118. return null;
  119. }
  120. }
  121. } // CookieCollection
  122. } // System.Net