Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Cookie.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 30ee979fef11abca8adc718c7fb197f61498f73c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// System.Net.Cookie.cs
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Text;

namespace System.Net {

	// Supported cookie formats are:
	// Netscape: http://home.netscape.com/newsref/std/cookie_spec.html
	// RFC 2109: http://www.ietf.org/rfc/rfc2109.txt
	// RFC 2965: http://www.ietf.org/rfc/rfc2965.txt
	[Serializable]
	public sealed class Cookie 
	{
		private string comment;
		private Uri commentUri;
		private bool discard;
		private string domain;
		private bool expired;
		private DateTime expires;
		private string name;
		private string path;
		private string port;
		private int [] ports;
		private bool secure;
		private DateTime timestamp;
		private string val;
		private int version;
		
		private static char [] reservedCharsName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
		private static char [] reservedCharsValue = new char [] {';', ','};
		private static char [] portSeparators = new char [] {'"', ','};
                private static string tspecials = "()<>@,;:\\\"/[]?={} \t";   // from RFC 2965, 2068

		public Cookie ()
		{
			expires = DateTime.MinValue;
			timestamp = DateTime.Now;
			domain = "";
			name = "";
			val = "";
		}

		public Cookie (string name, string value)
			: this ()
		{
			Name = name;
			Value = value;
		}

		public Cookie (string name, string value, string path) 
			: this (name, value) 
		{
			Path = path;
		}
		
		public Cookie (string name, string value, string path, string domain)
			: this (name, value, path)
		{
			Domain = domain;
		}
		
		public string Comment {
			get { return comment; }
			set { comment = value == null ? String.Empty : value; }
		}
		
		public Uri CommentUri {
			get { return commentUri; }
			set { commentUri = value; }
		}

		public bool Discard {
			get { return discard; }
			set { discard = value; }
		}
		
		public string Domain {
			get { return domain; }
			set { domain = value == null ? String.Empty : value; }
		}

		public bool Expired {
			get { 
				return expires <= DateTime.Now && 
				       expires != DateTime.MinValue;
			}
			set { 
				expired = value; 
				if (expired) {
					expires = DateTime.Now;
				}
			}
		}

		public DateTime Expires {
			get { return expires; }
			set { expires = value; }
		}

		public string Name {
			get { return name; }
			set { 
				if (value == null || value.Length == 0) {
					throw new CookieException ("Name cannot be empty");
				}			
				
				if (value [0] == '$' || value.IndexOfAny (reservedCharsName) != -1) {
					// see CookieTest, according to MS implementation
					// the name value changes even though it's incorrect
					name = String.Empty;
					throw new CookieException ("Name contains invalid characters");
				}
					
				name = value; 
			}
		}

		public string Path {
			get { return (path == null) ? "/" : path; }
			set { path = (value == null) ? String.Empty : value; }
		}

		public string Port {
			get { return port; }
			set { 
				if (value == null || value.Length == 0) {
					port = String.Empty;
					return;
				}
				if (value [0] != '"' || value [value.Length - 1] != '"') {
					throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Port must be enclosed by double quotes.");
				}
				port = value; 
				string [] values = port.Split (portSeparators);
				ports = new int[values.Length];
				for (int i = 0; i < ports.Length; i++) {
					ports [i] = Int32.MinValue;
					if (values [i].Length == 0)
						continue;
					try {						
						ports [i] = Int32.Parse (values [i]);
					} catch (Exception e) {
						throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Invalid value: " + values [i], e);
					}
				}
			}
		}
		
		int[] Ports {
			get { return ports; }
		}

		public bool Secure {
			get { return secure; }
			set { secure = value; }
		}

		public DateTime TimeStamp {
			get { return timestamp; }
		}

		public string Value {
			get { return val; }
			set { 
				if (value == null) {
					val = String.Empty;
					return;
				}
				
				// LAMESPEC: According to .Net specs the Value property should not accept 
				// the semicolon and comma characters, yet it does. For now we'll follow
				// the behaviour of MS.Net instead of the specs.
				/*
				if (value.IndexOfAny(reservedCharsValue) != -1)
					throw new CookieException("Invalid value. Value cannot contain semicolon or comma characters.");
				*/
				
				val = value; 
			}
		}
		
		public int Version {
			get { return version; }
			set { 
				if ((value < 0) || (value > 10)) 
					version = 0;
				else 
					version = value; 
			}
		}
		
		public override bool Equals (Object obj) 
		{
			System.Net.Cookie c = obj as System.Net.Cookie;			
			
			return c != null &&
			       String.Compare (this.name, c.name, true) == 0 &&
			       String.Compare (this.val, c.val, false) == 0 &&
			       String.Compare (this.path, c.path, false) == 0 &&
			       String.Compare (this.domain, c.domain, true) == 0 &&
			       this.version == c.version;
		}
		
		public override int GetHashCode ()
		{
			return hash(name.ToLower ().GetHashCode (),
			            val.GetHashCode (),
			            path.GetHashCode (),
			            domain.ToLower ().GetHashCode (),
			            version);
		}
		
		private static int hash (int i, int j, int k, int l, int m) 
		{
			return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25) ^ (m << 20 | m >> 12);
		}
		
		// returns a string that can be used to send a cookie to an Origin Server
		// i.e., only used for clients
		// see also para 3.3.4 of RFC 1965
		public override string ToString () 
		{
			if (name.Length == 0) 
				return String.Empty;

			StringBuilder result = new StringBuilder (64);
	
			if (version > 0) {
				result.Append ("$Version=").Append (version).Append (";");
            		}				
				
			result.Append (name).Append ("=").Append (val);

			// in the MS.Net implementation path and domain don't show up in
			// the result, I guess that's a bug in their implementation...
			if (path != null && path.Length != 0)
				result.Append (";$Path=").Append (QuotedString (path));
				
			if (domain != null && domain.Length != 0)
				result.Append (";$Domain=").Append (QuotedString (domain));			
	
			if (port != null && port.Length != 0)
				result.Append (";$Port=").Append (port);	
						
			return result.ToString ();
		}
				
		// See par 3.6 of RFC 2616
  	    	private string QuotedString (string value)
	    	{
			if (version == 0 || IsToken (value))
				return value;
			else 
				return "\"" + value.Replace("\"", "\\\"") + "\"";
	    	}			    	    

	    	private bool IsToken (string value) 
	    	{
			int len = value.Length;
			for (int i = 0; i < len; i++) {
			    	char c = value [i];
				if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)
			      		return false;
			}
			return true;
	    	}	    

	}
}