Commit 6a9937d2166023c370489d8e6654d01f6ec52f44 is not correct. WebClient.Proxy shoul...
[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                                 int c1 = x.Name.Length + x.Value.Length;
54                                 int c2 = y.Name.Length + y.Value.Length;
55
56                                 return (c1 - c2);
57                         }
58                 }
59
60                 static CookieCollectionComparer Comparer = new CookieCollectionComparer ();
61
62                 List<Cookie> list = new List<Cookie> ();
63
64                 internal IList<Cookie> List {
65                         get { return list; }
66                 }
67                 // ICollection
68                 public int Count {
69                         get { return list.Count; }
70                 }
71
72                 public bool IsSynchronized {
73                         get { return false; }
74                 }
75
76                 public Object SyncRoot {
77                         get { return this; }
78                 }
79
80                 public void CopyTo (Array array, int index)
81                 {
82                         (list as IList).CopyTo (array, index);
83                 }
84
85                 public void CopyTo (Cookie [] array, int index)
86                 {
87                         list.CopyTo (array, index);
88                 }
89
90                 // IEnumerable
91                 public IEnumerator GetEnumerator ()
92                 {
93                         return list.GetEnumerator ();
94                 }
95
96                 // This
97
98                 // LAMESPEC: So how is one supposed to create a writable CookieCollection 
99                 // instance?? We simply ignore this property, as this collection is always
100                 // writable.
101                 public bool IsReadOnly {
102                         get { return true; }
103                 }               
104
105                 public void Add (Cookie cookie) 
106                 {
107                         if (cookie == null)
108                                 throw new ArgumentNullException ("cookie");
109
110                         int pos = SearchCookie (cookie);
111                         if (pos == -1)
112                                 list.Add (cookie);
113                         else
114                                 list [pos] = cookie;
115                 }
116
117                 internal void Sort ()
118                 {
119                         if (list.Count > 0)
120                                 list.Sort (Comparer);
121                 }
122                 
123                 int SearchCookie (Cookie cookie)
124                 {
125                         string name = cookie.Name;
126                         string domain = cookie.Domain;
127                         string path = cookie.Path;
128
129                         for (int i = list.Count - 1; i >= 0; i--) {
130                                 Cookie c = list [i];
131                                 if (c.Version != cookie.Version)
132                                         continue;
133
134                                 if (0 != String.Compare (domain, c.Domain, true, CultureInfo.InvariantCulture))
135                                         continue;
136
137                                 if (0 != String.Compare (name, c.Name, true, CultureInfo.InvariantCulture))
138                                         continue;
139
140                                 if (0 != String.Compare (path, c.Path, true, CultureInfo.InvariantCulture))
141                                         continue;
142
143                                 return i;
144                         }
145
146                         return -1;
147                 }
148
149                 public void Add (CookieCollection cookies) 
150                 {
151                         if (cookies == null)
152                                 throw new ArgumentNullException ("cookies");
153
154                         foreach (Cookie c in cookies)
155                                 Add (c);
156                 }
157
158                 public Cookie this [int index] {
159                         get {
160                                 if (index < 0 || index >= list.Count)
161                                         throw new ArgumentOutOfRangeException ("index");
162
163                                 return list [index];
164                         }
165                 }
166
167                 public Cookie this [string name] {
168                         get {
169                                 foreach (Cookie c in list) {
170                                         if (0 == String.Compare (c.Name, name, true, CultureInfo.InvariantCulture))
171                                                 return c;
172                                 }
173                                 return null;
174                         }
175                 }
176
177         } // CookieCollection
178 } // System.Net
179