CookieCollection.cs 3.4 KB

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