Merge pull request #1508 from slluis/fix-20966
[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-2009 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 string GetCookieHeaderValue ()
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 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 HttpUtility.UrlDecode(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                 public bool HttpOnly {
206                         get {
207                                 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
208                         }
209
210                         set {
211                                 if (value)
212                                         flags |= CookieFlags.HttpOnly;
213                                 else
214                                         flags &= ~CookieFlags.HttpOnly;
215                         }
216                 }
217
218                 /*
219                  * simple utility class that just overrides ToString
220                  * to get the desired behavior for
221                  * HttpCookie.Values
222                  */
223                 [Serializable]
224                 sealed class CookieNVC : NameValueCollection
225                 {
226                         public CookieNVC ()
227                                 : base (StringComparer.OrdinalIgnoreCase)
228                         {
229                         }
230
231                         public override string ToString ()
232                         {
233                                 StringBuilder builder = new StringBuilder ("");
234
235                                 bool first_key = true;
236                                 foreach (string key in Keys) {
237                                         if (!first_key)
238                                                 builder.Append ("&");
239
240                                        string[] vals = GetValues (key);
241                                        if(vals == null)
242                                                vals = new string[1] {String.Empty};
243
244                                        bool first_val = true;
245                                        foreach (string v in vals) {
246                                                if (!first_val)
247                                                        builder.Append ("&");
248                                                
249                                                if (key != null && key.Length > 0) {
250                                                        builder.Append (HttpUtility.UrlEncode(key));
251                                                        builder.Append ("=");
252                                                }
253                                                if(v != null && v.Length > 0)
254                                                        builder.Append (HttpUtility.UrlEncode(v));
255                                                
256                                                first_val = false;
257                                         }
258                                         first_key = false;
259                                 }
260
261                                 return builder.ToString();
262                         }
263
264                         /* MS's implementation has the interesting quirk that if you do:
265                          * cookie.Values[null] = "foo"
266                          * it clears out the rest of the values.
267                          */
268                         public override void Set (string name, string value)
269                         {
270                                 if (this.IsReadOnly)
271                                         throw new NotSupportedException ("Collection is read-only");
272
273                                 if (name == null) {
274                                         Clear();
275                                         name = string.Empty;
276                                 }
277 //                              if (value == null)
278 //                                      value = string.Empty;
279
280                                 base.Set (name, value);
281                         }
282                 }
283         }
284 }