2005-11-02 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpCookie.cs
1 //
2 // System.Web.HttpCookie.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.Text;
32 using System.Collections.Specialized;
33 using System.Security.Permissions;
34
35 namespace System.Web {
36
37         [Flags]
38         internal enum CookieFlags : byte {
39                 Secure = 1,
40                 HttpOnly = 2
41         }
42         
43         // CAS - no InheritanceDemand here as the class is sealed
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public sealed class HttpCookie {
46
47                 string path = "/";
48                 string domain;
49                 DateTime expires = DateTime.MinValue;
50                 string name;
51                 CookieFlags flags = 0;
52                 NameValueCollection values;
53
54                 [Obsolete]
55                 internal HttpCookie (string name, string value, string path, DateTime expires)
56                 {
57                         this.name = name;
58                         this.values = new CookieNVC();
59                         this.Value = value;
60                         this.path = path;
61                         this.expires = expires;
62                 }
63
64                 public HttpCookie (string name)
65                 {
66                         this.name = name;
67                         values = new CookieNVC();
68                         Value = "";
69                 }
70
71                 public HttpCookie (string name, string value)
72                   : this (name)
73                 {
74                         Value = value;
75                 }
76
77                 internal BaseResponseHeader GetCookieHeader ()
78                 {
79                         StringBuilder builder = new StringBuilder ("");
80
81                         builder.Append (name);
82                         builder.Append ("=");
83                         builder.Append (Value);
84
85                         if (domain != null) {
86                                 builder.Append ("; domain=");
87                                 builder.Append (domain);
88                         }
89                
90                         if (path != null) {
91                                 builder.Append ("; path=");
92                                 builder.Append (path);
93                         }
94
95                         if (expires != DateTime.MinValue) {
96                                 builder.Append ("; expires=");
97                                 builder.Append (expires.ToUniversalTime().ToString("r"));
98                         }
99
100                         if ((flags & CookieFlags.Secure) != 0) {
101                                 builder.Append ("; secure");
102                         }
103
104                         if ((flags & CookieFlags.HttpOnly) != 0){
105                                 builder.Append ("; HttpOnly");
106                         }
107
108                         return new UnknownResponseHeader ("Set-Cookie", builder.ToString());
109                 }
110
111                 public string Domain {
112                         get {
113                                 return domain;
114                         }
115                         set {
116                                 domain = value;
117                         }
118                 }
119
120                 public DateTime Expires {
121                         get {
122                                 return expires;
123                         }
124                         set {
125                                 expires = value;
126                         }
127                 }
128
129                 public bool HasKeys {
130                         get {
131                                 return values.HasKeys();
132                         }
133                 }
134
135
136                 public string this [ string key ] {
137                         get {
138                                 return values [ key ];
139                         }
140                         set {
141                                 values [ key ] = value;
142                         }
143                 }
144
145                 public string Name {
146                         get {
147                                 return name;
148                         }
149                         set {
150                                 name = value;
151                         }
152                 }
153
154                 public string Path {
155                         get {
156                                 return path;
157                         }
158                         set {
159                                 path = value;
160                         }
161                 }
162
163                 public bool Secure {
164                         get {
165                                 return (flags & CookieFlags.Secure) == CookieFlags.Secure;
166                         }
167                         set {
168                                 flags |= CookieFlags.Secure;
169                         }
170                 }
171
172                 public string Value {
173                         get {
174                                 return values.ToString ();
175                         }
176                         set {
177                                 values.Clear ();
178                                 
179                                 if (value != null && value != "") {
180                                         string [] components = value.Split ('&');
181                                         foreach (string kv in components){
182                                                 int pos = kv.IndexOf ('=');
183                                                 if (pos == -1){
184                                                         values.Add (null, kv);
185                                                 } else {
186                                                         string key = kv.Substring (0, pos);
187                                                         string val = kv.Substring (pos+1);
188                                                         
189                                                         values.Add (key, val);
190                                                 }
191                                         }
192                                 }
193                         }
194                 }
195
196                 public NameValueCollection Values {
197                         get {
198                                 return values;
199                         }
200                 }
201
202 #if NET_2_0
203                 public bool HttpOnly {
204                         get {
205                                 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
206                         }
207
208                         set {
209                                 flags |= CookieFlags.HttpOnly;
210                         }
211                 }
212 #endif
213
214                 /*
215                  * simple utility class that just overrides ToString
216                  * to get the desired behavior for
217                  * HttpCookie.Values
218                  */
219                 class CookieNVC : NameValueCollection
220                 {
221                         public override string ToString ()
222                         {
223                                 StringBuilder builder = new StringBuilder ("");
224
225                                 bool first_key = true;
226                                 foreach (string key in Keys) {
227                                         if (!first_key)
228                                                 builder.Append ("&");
229
230                                         bool first_val = true;
231                                         foreach (string v in GetValues (key)) {
232                                                 if (!first_val)
233                                                         builder.Append ("&");
234
235                                                 if (key != null) {
236                                                         builder.Append (key);
237                                                         builder.Append ("=");
238                                                 }
239                                                 builder.Append (v);
240                                                 first_val = false;
241                                         }
242                                         first_key = false;
243                                 }
244
245                                 return builder.ToString();
246                         }
247
248                         /* MS's implementation has the interesting quirk that if you do:
249                          * cookie.Values[null] = "foo"
250                          * it clears out the rest of the values.
251                          */
252                         public override void Set (string name, string value)
253                         {
254                                 if (this.IsReadOnly)
255                                         throw new NotSupportedException ("Collection is read-only");
256
257                                 if (name == null)
258                                         Clear();
259
260                                 base.Set (name, value);
261                         }
262                 }
263         }
264
265 }