c121772be1b1ff510550e4645140f8a256fd62e5
[mono.git] / mcs / class / System.Web / System.Web / HttpCookie.cs
1 // 
2 // System.Web.HttpCookie
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //
7
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.
27 //
28 using System;
29 using System.Globalization;
30 using System.Text;
31 using System.Web;
32 using System.Collections.Specialized;
33
34 namespace System.Web
35 {
36         public sealed class HttpCookie
37         {
38                 string _Name;
39                 string _Value;
40                 string _Domain;
41                 DateTime _Expires;
42                 bool _ExpiresSet;
43                 string _Path;  
44                 bool _Secure = false;
45
46                 HttpValueCollection _Values;
47
48                 internal HttpCookie ()
49                 {
50                         _Path = "/";
51                 }
52
53                 public HttpCookie (string name)
54                 {
55                         _Path = "/";
56                         _Name = name;
57                 }
58
59                 public HttpCookie (string name, string value)
60                 {
61                         _Name = name;
62                         _Value = value;
63                         _Path = "/";
64                 }
65
66                 internal HttpCookie (string name, string value, string path, DateTime expires)
67                 {
68                         _Name = name;
69                         _Value = value;
70                         _Path = path;
71                         if (expires != DateTime.MinValue)
72                                 Expires = expires;
73                 }
74                 
75                 internal HttpResponseHeader GetCookieHeader ()
76                 {
77                         StringBuilder oSetCookie = new StringBuilder ();
78
79                         if (null != _Name && _Name.Length > 0) {
80                                 oSetCookie.Append (_Name);
81                                 oSetCookie.Append ("=");
82                         }
83
84                         if (null != _Values) {
85                                 oSetCookie.Append (_Values.ToString (false));
86                         } else if (null != _Value) {
87                                 oSetCookie.Append (_Value);
88                         }
89
90                         if (null != _Domain && _Domain.Length > 0) {
91                                 oSetCookie.Append ("; domain=");
92                                 oSetCookie.Append (_Domain);
93                         }
94
95                         if (null != _Path && Path.Length > 0) {
96                                 oSetCookie.Append ("; path=");
97                                 oSetCookie.Append (_Path);
98                         }
99
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));
105                         }
106
107                         if (_Secure)
108                                 oSetCookie.Append ("; secure");
109
110                         return new HttpResponseHeader (HttpWorkerRequest.HeaderSetCookie, oSetCookie.ToString());
111                 }
112
113                 public string Domain
114                 {
115                         get { return _Domain; }
116                         set { _Domain = value; }
117                 }
118
119                 public DateTime Expires
120                 {
121                         get {
122                                 if (!_ExpiresSet)
123                                         return DateTime.MinValue;
124
125                                 return _Expires;
126                         }
127
128                         set {
129                                 _ExpiresSet = true;
130                                 _Expires = value;
131                         }
132                 }
133
134                 public bool HasKeys
135                 {
136                         get {
137                                 return Values.HasKeys ();
138                         }
139                 }
140
141                 public string this [string key]
142                 {
143                         get { return Values [key]; }
144                         set { Values [key] = value; }
145                 }
146
147                 public string Name
148                 {
149                         get { return _Name; }
150                         set { _Name = value; }
151                 }
152
153                 public string Path
154                 {
155                         get { return _Path; }
156                         set { _Path = value; }
157                 }
158
159                 public bool Secure
160                 {
161                         get { return _Secure; }
162                         set { _Secure = value; }
163                 }
164
165                 public string Value
166                 {
167                         get {
168                                 if (null != _Values)
169                                         return _Values.ToString (false);
170
171                                 return _Value;
172                         }
173
174                         set {
175                                 if (null != _Values) {
176                                         _Values.Reset ();
177                                         _Values.Add (null, value);
178                                         return;
179                                 }
180
181                                 _Value = value;
182                         }
183                 }
184
185                 public NameValueCollection Values
186                 {
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);
196                                                 }
197
198                                                 _Value = null;
199                                         }
200                                 }
201
202                                 return _Values;
203                         }
204                 }
205         }
206 }
207