2 // System.Net.CookieCollection
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Globalization;
36 using System.Runtime.Serialization;
42 public sealed class CookieCollection : ICollection, IEnumerable {
44 public class CookieCollection : ICollection, IEnumerable {
46 // not 100% identical to MS implementation
47 sealed class CookieCollectionComparer : IComparer<Cookie> {
48 public int Compare (Cookie x, Cookie y)
50 if (x == null || y == null)
53 int c1 = x.Name.Length + x.Value.Length;
54 int c2 = y.Name.Length + y.Value.Length;
60 static CookieCollectionComparer Comparer = new CookieCollectionComparer ();
62 List<Cookie> list = new List<Cookie> ();
64 internal IList<Cookie> List {
69 get { return list.Count; }
72 public bool IsSynchronized {
76 public Object SyncRoot {
80 public void CopyTo (Array array, int index)
82 (list as IList).CopyTo (array, index);
85 public void CopyTo (Cookie [] array, int index)
87 list.CopyTo (array, index);
91 public IEnumerator GetEnumerator ()
93 return list.GetEnumerator ();
98 // LAMESPEC: So how is one supposed to create a writable CookieCollection
99 // instance?? We simply ignore this property, as this collection is always
101 public bool IsReadOnly {
105 public void Add (Cookie cookie)
108 throw new ArgumentNullException ("cookie");
110 int pos = SearchCookie (cookie);
117 internal void Sort ()
120 list.Sort (Comparer);
123 int SearchCookie (Cookie cookie)
125 string name = cookie.Name;
126 string domain = cookie.Domain;
127 string path = cookie.Path;
129 for (int i = list.Count - 1; i >= 0; i--) {
131 if (c.Version != cookie.Version)
134 if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
137 if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
140 if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
149 public void Add (CookieCollection cookies)
152 throw new ArgumentNullException ("cookies");
154 foreach (Cookie c in cookies)
158 public Cookie this [int index] {
160 if (index < 0 || index >= list.Count)
161 throw new ArgumentOutOfRangeException ("index");
167 public Cookie this [string name] {
169 foreach (Cookie c in list) {
170 if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
177 } // CookieCollection