Got rid of those Internal(1) warnings
[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 using System;
8 using System.Text;
9 using System.Web;
10 using System.Collections.Specialized;
11
12 namespace System.Web
13 {
14         public sealed class HttpCookie
15         {
16                 string _Name;
17                 string _Value;
18                 string _Domain;
19                 DateTime _Expires;
20                 bool _ExpiresSet;
21                 string _Path;  
22                 bool _Secure = false;
23
24                 HttpValueCollection _Values;
25
26                 internal HttpCookie ()
27                 {
28                         _Path = "/";
29                 }
30
31                 public HttpCookie (string name)
32                 {
33                         _Path = "/";
34                         _Name = name;
35                 }
36
37                 public HttpCookie (string name, string value)
38                 {
39                         _Name = name;
40                         _Value = value;
41                         _Path = "/";
42                 }
43
44                 internal HttpCookie (string name, string value, string path, DateTime expires)
45                 {
46                         _Name = name;
47                         _Value = value;
48                         _Path = path;
49                         if (expires != DateTime.MinValue)
50                                 Expires = expires;
51                 }
52                 
53                 internal HttpResponseHeader GetCookieHeader ()
54                 {
55                         StringBuilder oSetCookie = new StringBuilder ();
56
57                         if (null != _Name && _Name.Length > 0) {
58                                 oSetCookie.Append (_Name);
59                                 oSetCookie.Append ("=");
60                         }
61
62                         if (null != _Values) {
63                                 oSetCookie.Append (_Values.ToString (false));
64                         } else if (null != _Value) {
65                                 oSetCookie.Append (_Value);
66                         }
67
68                         if (null != _Domain && _Domain.Length > 0) {
69                                 oSetCookie.Append ("; domain=");
70                                 oSetCookie.Append (_Domain);
71                         }
72
73                         if (null != _Path && Path.Length > 0) {
74                                 oSetCookie.Append ("; path=");
75                                 oSetCookie.Append (_Path);
76                         }
77
78                         if (_ExpiresSet && _Expires != DateTime.MinValue) {
79                                 oSetCookie.Append ("; expires=");
80                                 oSetCookie.Append (_Expires.ToUniversalTime ().ToString ("ddd, dd-MMM-yyyy HH':'mm':'ss '+0000'"));
81                         }
82
83                         if (_Secure)
84                                 oSetCookie.Append ("; secure");
85
86                         return new HttpResponseHeader (HttpWorkerRequest.HeaderSetCookie, oSetCookie.ToString());
87                 }
88
89                 public string Domain
90                 {
91                         get { return _Domain; }
92                         set { _Domain = value; }
93                 }
94
95                 public DateTime Expires
96                 {
97                         get {
98                                 if (!_ExpiresSet)
99                                         return DateTime.MinValue;
100
101                                 return _Expires;
102                         }
103
104                         set {
105                                 _ExpiresSet = true;
106                                 _Expires = value;
107                         }
108                 }
109
110                 public bool HasKeys
111                 {
112                         get {
113                                 return Values.HasKeys ();
114                         }
115                 }
116
117                 public string this [string key]
118                 {
119                         get { return Values [key]; }
120                         set { Values [key] = value; }
121                 }
122
123                 public string Name
124                 {
125                         get { return _Name; }
126                         set { _Name = value; }
127                 }
128
129                 public string Path
130                 {
131                         get { return _Path; }
132                         set { _Path = value; }
133                 }
134
135                 public bool Secure
136                 {
137                         get { return _Secure; }
138                         set { _Secure = value; }
139                 }
140
141                 public string Value
142                 {
143                         get {
144                                 if (null != _Values)
145                                         return _Values.ToString (false);
146
147                                 return _Value;
148                         }
149
150                         set {
151                                 if (null != _Values) {
152                                         _Values.Reset ();
153                                         _Values.Add (null, value);
154                                         return;
155                                 }
156
157                                 _Value = value;
158                         }
159                 }
160
161                 public NameValueCollection Values
162                 {
163                         get {
164                                 if (null == _Values) {
165                                         _Values = new HttpValueCollection ();
166                                         if (null != _Value) {
167                                                 // Do we have multiple keys
168                                                 if (_Value.IndexOf ("&") >= 0 || _Value.IndexOf ("=") >= 0) {
169                                                         _Values.FillFromCookieString (_Value);
170                                                 } else {
171                                                         _Values.Add (null, _Value);
172                                                 }
173
174                                                 _Value = null;
175                                         }
176                                 }
177
178                                 return _Values;
179                         }
180                 }
181         }
182 }
183