**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web / HttpCookie.cs
blobc121772be1b1ff510550e4645140f8a256fd62e5
1 //
2 // System.Web.HttpCookie
3 //
4 // Author:
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.Globalization;
30 using System.Text;
31 using System.Web;
32 using System.Collections.Specialized;
34 namespace System.Web
36 public sealed class HttpCookie
38 string _Name;
39 string _Value;
40 string _Domain;
41 DateTime _Expires;
42 bool _ExpiresSet;
43 string _Path;
44 bool _Secure = false;
46 HttpValueCollection _Values;
48 internal HttpCookie ()
50 _Path = "/";
53 public HttpCookie (string name)
55 _Path = "/";
56 _Name = name;
59 public HttpCookie (string name, string value)
61 _Name = name;
62 _Value = value;
63 _Path = "/";
66 internal HttpCookie (string name, string value, string path, DateTime expires)
68 _Name = name;
69 _Value = value;
70 _Path = path;
71 if (expires != DateTime.MinValue)
72 Expires = expires;
75 internal HttpResponseHeader GetCookieHeader ()
77 StringBuilder oSetCookie = new StringBuilder ();
79 if (null != _Name && _Name.Length > 0) {
80 oSetCookie.Append (_Name);
81 oSetCookie.Append ("=");
84 if (null != _Values) {
85 oSetCookie.Append (_Values.ToString (false));
86 } else if (null != _Value) {
87 oSetCookie.Append (_Value);
90 if (null != _Domain && _Domain.Length > 0) {
91 oSetCookie.Append ("; domain=");
92 oSetCookie.Append (_Domain);
95 if (null != _Path && Path.Length > 0) {
96 oSetCookie.Append ("; path=");
97 oSetCookie.Append (_Path);
100 if (_ExpiresSet && _Expires != DateTime.MinValue) {
101 oSetCookie.Append ("; expires=");
102 DateTime ut = _Expires.ToUniversalTime ();
103 oSetCookie.Append (ut.ToString ("ddd, dd-MMM-yyyy HH':'mm':'ss 'GMT'",
104 DateTimeFormatInfo.InvariantInfo));
107 if (_Secure)
108 oSetCookie.Append ("; secure");
110 return new HttpResponseHeader (HttpWorkerRequest.HeaderSetCookie, oSetCookie.ToString());
113 public string Domain
115 get { return _Domain; }
116 set { _Domain = value; }
119 public DateTime Expires
121 get {
122 if (!_ExpiresSet)
123 return DateTime.MinValue;
125 return _Expires;
128 set {
129 _ExpiresSet = true;
130 _Expires = value;
134 public bool HasKeys
136 get {
137 return Values.HasKeys ();
141 public string this [string key]
143 get { return Values [key]; }
144 set { Values [key] = value; }
147 public string Name
149 get { return _Name; }
150 set { _Name = value; }
153 public string Path
155 get { return _Path; }
156 set { _Path = value; }
159 public bool Secure
161 get { return _Secure; }
162 set { _Secure = value; }
165 public string Value
167 get {
168 if (null != _Values)
169 return _Values.ToString (false);
171 return _Value;
174 set {
175 if (null != _Values) {
176 _Values.Reset ();
177 _Values.Add (null, value);
178 return;
181 _Value = value;
185 public NameValueCollection Values
187 get {
188 if (null == _Values) {
189 _Values = new HttpValueCollection ();
190 if (null != _Value) {
191 // Do we have multiple keys
192 if (_Value.IndexOf ("&") >= 0 || _Value.IndexOf ("=") >= 0) {
193 _Values.FillFromCookieString (_Value);
194 } else {
195 _Values.Add (null, _Value);
198 _Value = null;
202 return _Values;