f7e61e084fd9992a703d3ab6276ab479489b31ce
[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                                 if (value)
169                                         flags |= CookieFlags.Secure;
170                                 else
171                                         flags &= ~CookieFlags.Secure;
172                         }
173                 }
174
175                 public string Value {
176                         get {
177                                 return values.ToString ();
178                         }
179                         set {
180                                 values.Clear ();
181                                 
182                                 if (value != null && value != "") {
183                                         string [] components = value.Split ('&');
184                                         foreach (string kv in components){
185                                                 int pos = kv.IndexOf ('=');
186                                                 if (pos == -1){
187                                                         values.Add (null, kv);
188                                                 } else {
189                                                         string key = kv.Substring (0, pos);
190                                                         string val = kv.Substring (pos+1);
191                                                         
192                                                         values.Add (key, val);
193                                                 }
194                                         }
195                                 }
196                         }
197                 }
198
199                 public NameValueCollection Values {
200                         get {
201                                 return values;
202                         }
203                 }
204
205 #if NET_2_0
206                 public bool HttpOnly {
207                         get {
208                                 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
209                         }
210
211                         set {
212                                 flags |= CookieFlags.HttpOnly;
213                         }
214                 }
215 #endif
216
217                 /*
218                  * simple utility class that just overrides ToString
219                  * to get the desired behavior for
220                  * HttpCookie.Values
221                  */
222                 class CookieNVC : NameValueCollection
223                 {
224                         public override string ToString ()
225                         {
226                                 StringBuilder builder = new StringBuilder ("");
227
228                                 bool first_key = true;
229                                 foreach (string key in Keys) {
230                                         if (!first_key)
231                                                 builder.Append ("&");
232
233                                         bool first_val = true;
234                                         foreach (string v in GetValues (key)) {
235                                                 if (!first_val)
236                                                         builder.Append ("&");
237
238                                                 if (key != null) {
239                                                         builder.Append (key);
240                                                         builder.Append ("=");
241                                                 }
242                                                 builder.Append (v);
243                                                 first_val = false;
244                                         }
245                                         first_key = false;
246                                 }
247
248                                 return builder.ToString();
249                         }
250
251                         /* MS's implementation has the interesting quirk that if you do:
252                          * cookie.Values[null] = "foo"
253                          * it clears out the rest of the values.
254                          */
255                         public override void Set (string name, string value)
256                         {
257                                 if (this.IsReadOnly)
258                                         throw new NotSupportedException ("Collection is read-only");
259
260                                 if (name == null)
261                                         Clear();
262
263                                 base.Set (name, value);
264                         }
265                 }
266         }
267
268 }