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)
8 // Sebastien Pouliot <sebastien@ximian.com>
10 // Copyright (C) 2004,2009 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Globalization;
37 using System.Collections;
39 namespace System.Net {
41 // Supported cookie formats are:
42 // Netscape: http://home.netscape.com/newsref/std/cookie_spec.html
43 // RFC 2109: http://www.ietf.org/rfc/rfc2109.txt
44 // RFC 2965: http://www.ietf.org/rfc/rfc2965.txt
46 public sealed class Cookie
63 static char [] reservedCharsName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
64 static char [] portSeparators = new char [] {'"', ','};
65 static string tspecials = "()<>@,;:\\\"/[]?={} \t"; // from RFC 2965, 2068
69 expires = DateTime.MinValue;
70 timestamp = DateTime.Now;
71 domain = String.Empty;
74 comment = String.Empty;
78 public Cookie (string name, string value)
85 public Cookie (string name, string value, string path)
91 public Cookie (string name, string value, string path, string domain)
92 : this (name, value, path)
97 public string Comment {
98 get { return comment; }
99 set { comment = value == null ? String.Empty : value; }
102 public Uri CommentUri {
103 get { return commentUri; }
104 set { commentUri = value; }
107 public bool Discard {
108 get { return discard; }
109 set { discard = value; }
112 public string Domain {
113 get { return domain; }
115 if (String.IsNullOrEmpty (value)) {
116 domain = String.Empty;
121 if (IPAddress.TryParse (value, out test))
129 internal bool ExactDomain { get; set; }
131 public bool Expired {
133 return expires <= DateTime.Now &&
134 expires != DateTime.MinValue;
138 expires = DateTime.Now;
142 public DateTime Expires {
143 get { return expires; }
144 set { expires = value; }
147 public bool HttpOnly {
148 get { return httpOnly; }
149 set { httpOnly = value; }
155 if (String.IsNullOrEmpty (value))
156 throw new CookieException ("Name cannot be empty");
158 if (value [0] == '$' || value.IndexOfAny (reservedCharsName) != -1) {
159 // see CookieTest, according to MS implementation
160 // the name value changes even though it's incorrect
162 throw new CookieException ("Name contains invalid characters");
170 get { return (path == null) ? String.Empty : path; }
171 set { path = (value == null) ? String.Empty : value; }
177 if (String.IsNullOrEmpty (value)) {
181 if (value [0] != '"' || value [value.Length - 1] != '"') {
182 throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Port must be enclosed by double quotes.");
185 string [] values = port.Split (portSeparators);
186 ports = new int[values.Length];
187 for (int i = 0; i < ports.Length; i++) {
188 ports [i] = Int32.MinValue;
189 if (values [i].Length == 0)
192 ports [i] = Int32.Parse (values [i]);
193 } catch (Exception e) {
194 throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Invalid value: " + values [i], e);
201 internal int [] Ports {
202 get { return ports; }
203 set { ports = value; }
207 get { return secure; }
208 set { secure = value; }
211 public DateTime TimeStamp {
212 get { return timestamp; }
215 public string Value {
223 // LAMESPEC: According to .Net specs the Value property should not accept
224 // the semicolon and comma characters, yet it does. For now we'll follow
225 // the behaviour of MS.Net instead of the specs.
227 if (value.IndexOfAny(reservedCharsValue) != -1)
228 throw new CookieException("Invalid value. Value cannot contain semicolon or comma characters.");
236 get { return version; }
238 if ((value < 0) || (value > 10))
245 public override bool Equals (Object comparand)
247 System.Net.Cookie c = comparand as System.Net.Cookie;
250 String.Compare (this.name, c.name, true, CultureInfo.InvariantCulture) == 0 &&
251 String.Compare (this.val, c.val, false, CultureInfo.InvariantCulture) == 0 &&
252 String.Compare (this.Path, c.Path, false, CultureInfo.InvariantCulture) == 0 &&
253 String.Compare (this.domain, c.domain, true, CultureInfo.InvariantCulture) == 0 &&
254 this.version == c.version;
257 public override int GetHashCode ()
259 return hash(CaseInsensitiveHashCodeProvider.DefaultInvariant.GetHashCode(name),
262 CaseInsensitiveHashCodeProvider.DefaultInvariant.GetHashCode (domain),
266 private static int hash (int i, int j, int k, int l, int m)
268 return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25) ^ (m << 20 | m >> 12);
271 // returns a string that can be used to send a cookie to an Origin Server
272 // i.e., only used for clients
273 // see para 4.2.2 of RFC 2109 and para 3.3.4 of RFC 2965
274 // see also bug #316017
275 public override string ToString ()
277 return ToString (null);
280 internal string ToString (Uri uri)
282 if (name.Length == 0)
285 StringBuilder result = new StringBuilder (64);
288 result.Append ("$Version=").Append (version).Append ("; ");
290 result.Append (name).Append ("=").Append (val);
293 return result.ToString ();
295 if (!String.IsNullOrEmpty (path))
296 result.Append ("; $Path=").Append (path);
298 bool append_domain = (uri == null) || (uri.Host != domain);
299 if (append_domain && !String.IsNullOrEmpty (domain))
300 result.Append ("; $Domain=").Append (domain);
302 if (port != null && port.Length != 0)
303 result.Append ("; $Port=").Append (port);
305 return result.ToString ();
308 internal string ToClientString ()
310 if (name.Length == 0)
313 StringBuilder result = new StringBuilder (64);
316 result.Append ("Version=").Append (version).Append (";");
318 result.Append (name).Append ("=").Append (val);
320 if (path != null && path.Length != 0)
321 result.Append (";Path=").Append (QuotedString (path));
323 if (domain != null && domain.Length != 0)
324 result.Append (";Domain=").Append (QuotedString (domain));
326 if (port != null && port.Length != 0)
327 result.Append (";Port=").Append (port);
329 return result.ToString ();
332 // See par 3.6 of RFC 2616
333 string QuotedString (string value)
335 if (version == 0 || IsToken (value))
338 return "\"" + value.Replace("\"", "\\\"") + "\"";
341 bool IsToken (string value)
343 int len = value.Length;
344 for (int i = 0; i < len; i++) {
346 if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)