CookieCollection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Net.CookieCollection
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Runtime.Serialization;
  30. namespace System.Net
  31. {
  32. [Serializable]
  33. public class CookieCollection : ICollection, IEnumerable
  34. {
  35. private ArrayList list = new ArrayList ();
  36. // ctor
  37. public CookieCollection ()
  38. {
  39. }
  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. // LAMESPEC: Which exception should we throw when the read only
  67. // property is set to true??
  68. public void Add (Cookie cookie)
  69. {
  70. if (cookie == null)
  71. throw new ArgumentNullException ("cookie");
  72. int pos = list.IndexOf (cookie);
  73. if (pos == -1)
  74. list.Add (cookie);
  75. else
  76. list [pos] = cookie;
  77. }
  78. // LAMESPEC: Which exception should we throw when the read only
  79. // property is set to true??
  80. public void Add (CookieCollection cookies)
  81. {
  82. if (cookies == null)
  83. throw new ArgumentNullException ("cookies");
  84. IEnumerator enumerator = cookies.list.GetEnumerator ();
  85. while (enumerator.MoveNext ())
  86. Add ((Cookie) enumerator.Current);
  87. }
  88. public Cookie this [int index] {
  89. get {
  90. if (index < 0 || index >= list.Count)
  91. throw new ArgumentOutOfRangeException ("index");
  92. return (Cookie) list [index];
  93. }
  94. }
  95. public Cookie this [string name] {
  96. get {
  97. lock (this) {
  98. IEnumerator enumerator = list.GetEnumerator ();
  99. while (enumerator.MoveNext ())
  100. if (String.Compare (((Cookie) enumerator.Current).Name, name, true) == 0)
  101. return (Cookie) enumerator.Current;
  102. }
  103. return null;
  104. }
  105. }
  106. } // CookieCollection
  107. } // System.Net