2 // System.Web.HttpCookie.cs
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Specialized;
33 using System.Security.Permissions;
38 internal enum CookieFlags : byte {
43 // CAS - no InheritanceDemand here as the class is sealed
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public sealed class HttpCookie {
49 DateTime expires = DateTime.MinValue;
51 CookieFlags flags = 0;
52 NameValueCollection values;
55 internal HttpCookie (string name, string value, string path, DateTime expires)
58 this.values = new CookieNVC();
61 this.expires = expires;
64 public HttpCookie (string name)
67 values = new CookieNVC();
71 public HttpCookie (string name, string value)
77 internal string GetCookieHeaderValue ()
79 StringBuilder builder = new StringBuilder ();
81 builder.Append (name);
83 builder.Append (Value);
86 builder.Append ("; domain=");
87 builder.Append (domain);
91 builder.Append ("; path=");
92 builder.Append (path);
95 if (expires != DateTime.MinValue) {
96 builder.Append ("; expires=");
97 builder.Append (expires.ToUniversalTime().ToString("r"));
100 if ((flags & CookieFlags.Secure) != 0) {
101 builder.Append ("; secure");
104 if ((flags & CookieFlags.HttpOnly) != 0){
105 builder.Append ("; HttpOnly");
108 return builder.ToString ();
111 public string Domain {
120 public DateTime Expires {
129 public bool HasKeys {
131 return values.HasKeys();
136 public string this [ string key ] {
138 return values [ key ];
141 values [ key ] = value;
165 return (flags & CookieFlags.Secure) == CookieFlags.Secure;
169 flags |= CookieFlags.Secure;
171 flags &= ~CookieFlags.Secure;
175 public string Value {
177 return HttpUtility.UrlDecode(values.ToString ());
182 if (value != null && value != "") {
183 string [] components = value.Split ('&');
184 foreach (string kv in components){
185 int pos = kv.IndexOf ('=');
187 values.Add (null, kv);
189 string key = kv.Substring (0, pos);
190 string val = kv.Substring (pos+1);
192 values.Add (key, val);
199 public NameValueCollection Values {
205 public bool HttpOnly {
207 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
212 flags |= CookieFlags.HttpOnly;
214 flags &= ~CookieFlags.HttpOnly;
219 * simple utility class that just overrides ToString
220 * to get the desired behavior for
224 sealed class CookieNVC : NameValueCollection
227 : base (StringComparer.OrdinalIgnoreCase)
231 public override string ToString ()
233 StringBuilder builder = new StringBuilder ("");
235 bool first_key = true;
236 foreach (string key in Keys) {
238 builder.Append ("&");
240 string[] vals = GetValues (key);
242 vals = new string[1] {String.Empty};
244 bool first_val = true;
245 foreach (string v in vals) {
247 builder.Append ("&");
249 if (key != null && key.Length > 0) {
250 builder.Append (HttpUtility.UrlEncode(key));
251 builder.Append ("=");
253 if(v != null && v.Length > 0)
254 builder.Append (HttpUtility.UrlEncode(v));
261 return builder.ToString();
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.
268 public override void Set (string name, string value)
271 throw new NotSupportedException ("Collection is read-only");
277 // if (value == null)
278 // value = string.Empty;
280 base.Set (name, value);