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)]
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 BaseResponseHeader GetCookieHeader ()
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 new UnknownResponseHeader ("Set-Cookie", 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 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 {
206 public bool HttpOnly {
208 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
212 flags |= CookieFlags.HttpOnly;
218 * simple utility class that just overrides ToString
219 * to get the desired behavior for
222 class CookieNVC : NameValueCollection
224 public override string ToString ()
226 StringBuilder builder = new StringBuilder ("");
228 bool first_key = true;
229 foreach (string key in Keys) {
231 builder.Append ("&");
233 bool first_val = true;
234 foreach (string v in GetValues (key)) {
236 builder.Append ("&");
239 builder.Append (key);
240 builder.Append ("=");
248 return builder.ToString();
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.
255 public override void Set (string name, string value)
258 throw new NotSupportedException ("Collection is read-only");
263 base.Set (name, value);