2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web / HttpCookieCollection.cs
1 // 
2 // System.Web.HttpCookieCollection
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   (First impl Bob Smith <bob@thestuff.net>)
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Web;
32 using System.Collections.Specialized;
33
34 namespace System.Web
35 {
36         public sealed class HttpCookieCollection : NameObjectCollectionBase
37         {
38                 HttpCookie [] _AllCookies;
39                 string [] _AllKeys;
40
41                 HttpResponse _Response;
42
43                 internal HttpCookieCollection(HttpResponse Response, bool ReadOnly) : base()
44                 {
45                         _Response = Response;
46                         IsReadOnly = ReadOnly;
47                 }
48
49                 public HttpCookieCollection () { }
50
51                 public string [] AllKeys
52                 {
53                         get {
54                                 if (null == _AllKeys)
55                                         _AllKeys = BaseGetAllKeys ();
56
57                                 return _AllKeys;
58                         }
59                 }
60
61                 public HttpCookie this [int index]
62                 {
63                         get { return Get (index); }
64                 }
65
66                 public HttpCookie this [string name]
67                 {
68                         get { return Get (name); }
69                 }
70
71                 public void Add (HttpCookie cookie)
72                 {
73                         if (null != _Response)
74                                 _Response.GoingToChangeCookieColl ();
75
76                         // empy performance cache
77                         _AllCookies = null;
78                         _AllKeys = null;
79
80                         BaseAdd (cookie.Name, cookie);
81
82                         if (null != _Response)
83                                 _Response.OnCookieAdd (cookie);
84                 }
85
86                 public void Clear ()
87                 {
88                         _AllCookies = null;
89                         _AllKeys = null;
90                         BaseClear ();
91                 }
92
93                 public void CopyTo (Array dest, int index)
94                 {
95                         if (null == _AllCookies) {
96                                 _AllCookies = new HttpCookie [Count];
97
98                                 for (int i = 0; i != Count; i++)
99                                         _AllCookies [i] = Get (i);
100                         }
101
102                         _AllCookies.CopyTo (dest, index);
103                 }
104
105                 public HttpCookie Get (int index)
106                 {
107                         return BaseGet (index) as HttpCookie;
108                 }
109
110                 public HttpCookie Get (string name)
111                 {
112                         HttpCookie oRet = BaseGet (name) as HttpCookie;
113                         if (null == oRet && _Response != null) {
114                                 _AllCookies = null;
115                                 _AllKeys = null;
116
117                                 _Response.GoingToChangeCookieColl ();
118
119                                 oRet = new HttpCookie (name);
120                                 BaseAdd (name, oRet);
121
122                                 _Response.OnCookieAdd (oRet);
123                         }
124
125                         return oRet;
126                 }
127
128                 public string GetKey (int index)
129                 {
130                         return BaseGetKey (index);
131                 }
132
133                 public void Remove (string name)
134                 {
135                         if (null != _Response)
136                                 _Response.GoingToChangeCookieColl ();
137
138                         _AllCookies = null;
139                         _AllKeys = null;
140                         BaseRemove (name);
141
142                         if (null != _Response)
143                                 _Response.ChangedCookieColl ();
144                 }
145
146                 public void Set (HttpCookie cookie)
147                 {
148                         if (null != _Response)
149                                 _Response.GoingToChangeCookieColl ();
150
151                         _AllCookies = null;
152                         _AllKeys = null;
153                         BaseSet (cookie.Name, cookie);
154
155                         if (null != _Response)
156                                 _Response.ChangedCookieColl();
157                 }
158
159                 internal void MakeCookieExpire (string name, string path)
160                 {
161                         DateTime expirationTime = new DateTime (1999, 10, 12); // This is the date MS sends!
162                         HttpCookie willExpire = new HttpCookie (name, String.Empty, path, expirationTime);
163                         Remove (name);
164                         Add (willExpire);
165                 }
166         }
167 }
168