2005-08-17 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpCookieCollection.cs
1 //
2 // System.Web.HttpCookieCollection.cs 
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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
31 using System.Collections.Specialized;
32
33 namespace System.Web {
34
35         public sealed class HttpCookieCollection : NameObjectCollectionBase {
36
37                 private bool auto_fill = false;
38
39                 [Obsolete ("Don't use this constructor, use the (bool, bool) one, as it's more clear what it does")]
40                 internal HttpCookieCollection (HttpResponse Response, bool ReadOnly)
41                 {
42                         auto_fill = Response != null;
43                         IsReadOnly = ReadOnly;
44                 }
45
46                 internal HttpCookieCollection (bool auto_fill, bool read_only)
47                 {
48                         this.auto_fill = auto_fill;
49                         IsReadOnly = read_only;
50                 }
51
52                 internal HttpCookieCollection (string cookies)
53                 {
54                         if (cookies == null || cookies == "")
55                                 return;
56
57                         string[] cookie_components = cookies.Split (';');
58                         foreach (string kv in cookie_components) {
59                                 int pos = kv.IndexOf ('=');
60                                 if (pos == -1) {
61                                         /* XXX ugh */
62                                         continue;
63                                 }
64                                 else {
65                                         string key = kv.Substring (0, pos);
66                                         string val = kv.Substring (pos+1);
67
68                                         Add (new HttpCookie (key.Trim (), val.Trim()));
69                                 }
70                         }
71                 }
72
73                 public HttpCookieCollection ()
74                 {
75                 }
76
77                 public void Add (HttpCookie cookie)
78                 {
79                         if (BaseGet (cookie.Name) != null)
80                                 return;
81
82                         BaseAdd (cookie.Name, cookie);
83                 }
84
85                 public void Clear ()
86                 {
87                         BaseClear ();
88                 }
89
90                 public void CopyTo (Array array, int index)
91                 {
92                         /* XXX this is kind of gross and inefficient
93                          * since it makes a copy of the superclass's
94                          * list */
95                         object[] values = BaseGetAllValues();
96                         values.CopyTo (array, index);
97                 }
98
99                 public string GetKey (int index)
100                 {
101                         HttpCookie cookie = (HttpCookie)BaseGet (index);
102                         if (cookie == null)
103                                 return null;
104                         else
105                                 return cookie.Name;
106                 }
107
108                 public void Remove (string name)
109                 {
110                         BaseRemove (name);
111                 }
112
113                 public void Set (HttpCookie cookie)
114                 {
115                         BaseSet (cookie.Name, cookie);
116                 }
117
118                 public HttpCookie Get (int index)
119                 {
120                         return (HttpCookie)BaseGet (index);
121                 }
122
123                 public HttpCookie Get (string name)
124                 {
125                         return (HttpCookie)BaseGet (name);
126                 }
127
128                 public HttpCookie this [int index]
129                 {
130                         get {
131                                 return (HttpCookie)BaseGet (index);
132                         }
133                 }
134
135                 public HttpCookie this [string name]
136                 {
137                         get {
138                                 HttpCookie cookie = (HttpCookie)BaseGet (name);
139                                 if (!IsReadOnly && auto_fill && cookie == null) {
140                                         cookie = new HttpCookie (name);
141                                         BaseAdd (name, cookie);
142                                 }
143                                 return cookie;
144                         }
145                 }
146
147                 public string[] AllKeys {
148                         get {
149                                 /* XXX another inefficient copy due to
150                                  * lack of exposure from the base
151                                  * class */
152                                 string[] keys = new string [Keys.Count];
153                                 for (int i = 0; i < Keys.Count; i ++)
154                                         keys[i] = Keys[i];
155                                 
156                                 return keys;
157                         }
158                 }
159         }
160 }
161