2 // System.Web.HttpCookie.cs
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005 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;
35 namespace System.Web {
38 internal enum CookieFlags : byte {
43 // CAS - no InheritanceDemand here as the class is sealed
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 // Cookies must be serializable to be saved in the session for J2EE portal
49 public sealed class HttpCookie {
53 DateTime expires = DateTime.MinValue;
55 CookieFlags flags = 0;
56 NameValueCollection values;
59 internal HttpCookie (string name, string value, string path, DateTime expires)
62 this.values = new CookieNVC();
65 this.expires = expires;
68 public HttpCookie (string name)
71 values = new CookieNVC();
75 public HttpCookie (string name, string value)
81 internal BaseResponseHeader GetCookieHeader ()
83 StringBuilder builder = new StringBuilder ("");
85 builder.Append (name);
87 builder.Append (Value);
90 builder.Append ("; domain=");
91 builder.Append (domain);
95 builder.Append ("; path=");
96 builder.Append (path);
99 if (expires != DateTime.MinValue) {
100 builder.Append ("; expires=");
101 builder.Append (expires.ToUniversalTime().ToString("r"));
104 if ((flags & CookieFlags.Secure) != 0) {
105 builder.Append ("; secure");
108 if ((flags & CookieFlags.HttpOnly) != 0){
109 builder.Append ("; HttpOnly");
112 return new UnknownResponseHeader ("Set-Cookie", builder.ToString());
115 public string Domain {
124 public DateTime Expires {
133 public bool HasKeys {
135 return values.HasKeys();
140 public string this [ string key ] {
142 return values [ key ];
145 values [ key ] = value;
169 return (flags & CookieFlags.Secure) == CookieFlags.Secure;
173 flags |= CookieFlags.Secure;
175 flags &= ~CookieFlags.Secure;
179 public string Value {
181 return HttpUtility.UrlDecode(values.ToString ());
186 if (value != null && value != "") {
187 string [] components = value.Split ('&');
188 foreach (string kv in components){
189 int pos = kv.IndexOf ('=');
191 values.Add (null, kv);
193 string key = kv.Substring (0, pos);
194 string val = kv.Substring (pos+1);
196 values.Add (key, val);
203 public NameValueCollection Values {
210 public bool HttpOnly {
212 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
216 flags |= CookieFlags.HttpOnly;
222 * simple utility class that just overrides ToString
223 * to get the desired behavior for
226 class CookieNVC : NameValueCollection
230 : base (StringComparer.OrdinalIgnoreCase)
235 public override string ToString ()
237 StringBuilder builder = new StringBuilder ("");
239 bool first_key = true;
240 foreach (string key in Keys) {
242 builder.Append ("&");
244 string[] vals = GetValues (key);
246 vals = new string[0];
248 bool first_val = true;
249 foreach (string v in vals) {
251 builder.Append ("&");
253 if (key != null && key.Length > 0) {
254 builder.Append (HttpUtility.UrlEncode(key));
255 builder.Append ("=");
257 if(v != null && v.Length > 0)
258 builder.Append (HttpUtility.UrlEncode(v));
265 return builder.ToString();
268 /* MS's implementation has the interesting quirk that if you do:
269 * cookie.Values[null] = "foo"
270 * it clears out the rest of the values.
272 public override void Set (string name, string value)
275 throw new NotSupportedException ("Collection is read-only");
282 value = string.Empty;
284 base.Set (name, value);