Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System / System.Net / CookieCollection.cs
1 //
2 // System.Net.CookieCollection
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Globalization;
36 using System.Runtime.Serialization;
37
38 namespace System.Net 
39 {
40         [Serializable]
41 #if NET_2_1
42         public sealed class CookieCollection : ICollection, IEnumerable {
43 #else
44         public class CookieCollection : ICollection, IEnumerable {
45 #endif
46                 // not 100% identical to MS implementation
47                 sealed class CookieCollectionComparer : IComparer<Cookie> {
48                         public int Compare (Cookie x, Cookie y)
49                         {
50                                 if (x == null || y == null)
51                                         return 0;
52
53                                 var ydomain = y.Domain.Length - (y.Domain[0] == '.' ? 1 : 0);
54                                 var xdomain = x.Domain.Length - (x.Domain[0] == '.' ? 1 : 0);
55
56                                 int result = ydomain - xdomain;
57                                 return result == 0 ? y.Path.Length - x.Path.Length : result;
58                         }
59                 }
60
61                 static CookieCollectionComparer Comparer = new CookieCollectionComparer ();
62
63                 List<Cookie> list = new List<Cookie> ();
64
65                 internal IList<Cookie> List {
66                         get { return list; }
67                 }
68                 // ICollection
69                 public int Count {
70                         get { return list.Count; }
71                 }
72
73                 public bool IsSynchronized {
74                         get { return false; }
75                 }
76
77                 public Object SyncRoot {
78                         get { return this; }
79                 }
80
81                 public void CopyTo (Array array, int index)
82                 {
83                         (list as IList).CopyTo (array, index);
84                 }
85
86                 public void CopyTo (Cookie [] array, int index)
87                 {
88                         list.CopyTo (array, index);
89                 }
90
91                 // IEnumerable
92                 public IEnumerator GetEnumerator ()
93                 {
94                         return list.GetEnumerator ();
95                 }
96
97                 // This
98
99                 // LAMESPEC: So how is one supposed to create a writable CookieCollection 
100                 // instance?? We simply ignore this property, as this collection is always
101                 // writable.
102                 public bool IsReadOnly {
103                         get { return true; }
104                 }               
105
106                 public void Add (Cookie cookie) 
107                 {
108                         if (cookie == null)
109                                 throw new ArgumentNullException ("cookie");
110
111                         int pos = SearchCookie (cookie);
112                         if (pos == -1)
113                                 list.Add (cookie);
114                         else
115                                 list [pos] = cookie;
116                 }
117
118                 internal void Sort ()
119                 {
120                         if (list.Count > 0)
121                                 list.Sort (Comparer);
122                 }
123                 
124                 int SearchCookie (Cookie cookie)
125                 {
126                         string name = cookie.Name;
127                         string domain = cookie.Domain;
128                         string path = cookie.Path;
129
130                         for (int i = list.Count - 1; i >= 0; i--) {
131                                 Cookie c = list [i];
132                                 if (c.Version != cookie.Version)
133                                         continue;
134
135                                 if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
136                                         continue;
137
138                                 if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
139                                         continue;
140
141                                 if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
142                                         continue;
143
144                                 return i;
145                         }
146
147                         return -1;
148                 }
149
150                 public void Add (CookieCollection cookies) 
151                 {
152                         if (cookies == null)
153                                 throw new ArgumentNullException ("cookies");
154
155                         foreach (Cookie c in cookies)
156                                 Add (c);
157                 }
158
159                 public Cookie this [int index] {
160                         get {
161                                 if (index < 0 || index >= list.Count)
162                                         throw new ArgumentOutOfRangeException ("index");
163
164                                 return list [index];
165                         }
166                 }
167
168                 public Cookie this [string name] {
169                         get {
170                                 foreach (Cookie c in list) {
171                                         if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
172                                                 return c;
173                                 }
174                                 return null;
175                         }
176                 }
177
178         } // CookieCollection
179 } // System.Net
180