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;
34 namespace System.Web {
37 internal enum CookieFlags : byte {
42 public sealed class HttpCookie {
46 DateTime expires = DateTime.MinValue;
48 CookieFlags flags = 0;
49 NameValueCollection values;
52 internal HttpCookie (string name, string value, string path, DateTime expires)
55 this.values = new CookieNVC();
58 this.expires = expires;
61 public HttpCookie (string name)
64 values = new CookieNVC();
68 public HttpCookie (string name, string value)
74 internal BaseResponseHeader GetCookieHeader ()
76 StringBuilder builder = new StringBuilder ("");
78 builder.Append (name);
80 builder.Append (Value);
83 builder.Append ("; domain=");
84 builder.Append (domain);
88 builder.Append ("; path=");
89 builder.Append (path);
92 if (expires != DateTime.MinValue) {
93 builder.Append ("; expires=");
94 builder.Append (expires.ToUniversalTime().ToString("r"));
97 if ((flags & CookieFlags.Secure) != 0) {
98 builder.Append ("; secure");
101 if ((flags & CookieFlags.HttpOnly) != 0){
102 builder.Append ("; HttpOnly");
105 return new UnknownResponseHeader ("Set-Cookie", builder.ToString());
108 public string Domain {
117 public DateTime Expires {
126 public bool HasKeys {
128 return values.HasKeys();
133 public string this [ string key ] {
135 return values [ key ];
138 values [ key ] = value;
162 return (flags & CookieFlags.Secure) == CookieFlags.Secure;
165 flags |= CookieFlags.Secure;
169 public string Value {
171 return values.ToString ();
176 if (value != null && value != "") {
177 string [] components = value.Split ('&');
178 foreach (string kv in components){
179 int pos = kv.IndexOf ('=');
181 values.Add (null, kv);
183 string key = kv.Substring (0, pos);
184 string val = kv.Substring (pos+1);
186 values.Add (key, val);
193 public NameValueCollection Values {
200 public bool HttpOnly {
202 return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
206 flags |= CookieFlags.HttpOnly;
212 * simple utility class that just overrides ToString
213 * to get the desired behavior for
216 class CookieNVC : NameValueCollection
218 public override string ToString ()
220 StringBuilder builder = new StringBuilder ("");
222 bool first_key = true;
223 foreach (string key in Keys) {
225 builder.Append ("&");
227 bool first_val = true;
228 foreach (string v in GetValues (key)) {
230 builder.Append ("&");
233 builder.Append (key);
234 builder.Append ("=");
242 return builder.ToString();
245 /* MS's implementation has the interesting quirk that if you do:
246 * cookie.Values[null] = "foo"
247 * it clears out the rest of the values.
249 public override void Set (string name, string value)
252 throw new NotSupportedException ("Collection is read-only");
257 base.Set (name, value);