2 // System.Net.Cookie.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Daniel Nauck (dna(at)mono-project(dot)de)
9 // (c) Copyright 2004 Novell, Inc. (http://www.ximian.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
36 using System.Collections;
38 namespace System.Net {
40 // Supported cookie formats are:
41 // Netscape: http://home.netscape.com/newsref/std/cookie_spec.html
42 // RFC 2109: http://www.ietf.org/rfc/rfc2109.txt
43 // RFC 2965: http://www.ietf.org/rfc/rfc2965.txt
45 public sealed class Cookie
65 static char [] reservedCharsName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
66 static char [] portSeparators = new char [] {'"', ','};
67 static string tspecials = "()<>@,;:\\\"/[]?={} \t"; // from RFC 2965, 2068
71 expires = DateTime.MinValue;
72 timestamp = DateTime.Now;
80 public Cookie (string name, string value)
87 public Cookie (string name, string value, string path)
93 public Cookie (string name, string value, string path, string domain)
94 : this (name, value, path)
99 public string Comment {
100 get { return comment; }
101 set { comment = value == null ? String.Empty : value; }
104 public Uri CommentUri {
105 get { return commentUri; }
106 set { commentUri = value; }
109 public bool Discard {
110 get { return discard; }
111 set { discard = value; }
114 public string Domain {
115 get { return domain; }
116 set { domain = value == null ? String.Empty : value; }
119 public bool Expired {
121 return expires <= DateTime.Now &&
122 expires != DateTime.MinValue;
126 expires = DateTime.Now;
130 public DateTime Expires {
131 get { return expires; }
132 set { expires = value; }
138 get { return httpOnly; }
139 set { httpOnly = value; }
146 if (value == null || value.Length == 0) {
147 throw new CookieException ("Name cannot be empty");
150 if (value [0] == '$' || value.IndexOfAny (reservedCharsName) != -1) {
151 // see CookieTest, according to MS implementation
152 // the name value changes even though it's incorrect
154 throw new CookieException ("Name contains invalid characters");
162 get { return (path == null || path == "") ? String.Empty : path; }
163 set { path = (value == null) ? String.Empty : value; }
169 if (value == null || value.Length == 0) {
173 if (value [0] != '"' || value [value.Length - 1] != '"') {
174 throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Port must be enclosed by double quotes.");
177 string [] values = port.Split (portSeparators);
178 ports = new int[values.Length];
179 for (int i = 0; i < ports.Length; i++) {
180 ports [i] = Int32.MinValue;
181 if (values [i].Length == 0)
184 ports [i] = Int32.Parse (values [i]);
185 } catch (Exception e) {
186 throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Invalid value: " + values [i], e);
193 internal int [] Ports {
194 get { return ports; }
198 get { return secure; }
199 set { secure = value; }
202 public DateTime TimeStamp {
203 get { return timestamp; }
206 public string Value {
214 // LAMESPEC: According to .Net specs the Value property should not accept
215 // the semicolon and comma characters, yet it does. For now we'll follow
216 // the behaviour of MS.Net instead of the specs.
218 if (value.IndexOfAny(reservedCharsValue) != -1)
219 throw new CookieException("Invalid value. Value cannot contain semicolon or comma characters.");
227 get { return version; }
229 if ((value < 0) || (value > 10))
236 public override bool Equals (Object obj)
238 System.Net.Cookie c = obj as System.Net.Cookie;
241 String.Compare (this.name, c.name, true, CultureInfo.InvariantCulture) == 0 &&
242 String.Compare (this.val, c.val, false, CultureInfo.InvariantCulture) == 0 &&
243 String.Compare (this.Path, c.Path, false, CultureInfo.InvariantCulture) == 0 &&
244 String.Compare (this.domain, c.domain, true, CultureInfo.InvariantCulture) == 0 &&
245 this.version == c.version;
248 public override int GetHashCode ()
250 return hash(CaseInsensitiveHashCodeProvider.DefaultInvariant.GetHashCode(name),
253 CaseInsensitiveHashCodeProvider.DefaultInvariant.GetHashCode (domain),
257 private static int hash (int i, int j, int k, int l, int m)
259 return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25) ^ (m << 20 | m >> 12);
262 // returns a string that can be used to send a cookie to an Origin Server
263 // i.e., only used for clients
264 // see para 4.2.2 of RFC 2109 and para 3.3.4 of RFC 2965
265 // see also bug #316017
266 public override string ToString ()
268 if (name.Length == 0)
271 StringBuilder result = new StringBuilder (64);
274 result.Append ("$Version=").Append (version).Append ("; ");
276 result.Append (name).Append ("=").Append (val);
279 return result.ToString ();
281 if (path != null && path.Length != 0)
282 result.Append ("; $Path=").Append (path);
284 if (domain != null && domain.Length != 0)
285 result.Append ("; $Domain=").Append (domain);
287 if (port != null && port.Length != 0)
288 result.Append ("; $Port=").Append (port);
290 return result.ToString ();
293 internal string ToClientString ()
295 if (name.Length == 0)
298 StringBuilder result = new StringBuilder (64);
301 result.Append ("Version=").Append (version).Append (";");
303 result.Append (name).Append ("=").Append (val);
305 if (path != null && path.Length != 0)
306 result.Append (";Path=").Append (QuotedString (path));
308 if (domain != null && domain.Length != 0)
309 result.Append (";Domain=").Append (QuotedString (domain));
311 if (port != null && port.Length != 0)
312 result.Append (";Port=").Append (port);
314 return result.ToString ();
317 // See par 3.6 of RFC 2616
318 string QuotedString (string value)
320 if (version == 0 || IsToken (value))
323 return "\"" + value.Replace("\"", "\\\"") + "\"";
326 bool IsToken (string value)
328 int len = value.Length;
329 for (int i = 0; i < len; i++) {
331 if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)