Cookie.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // System.Net.Cookie.cs
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) Copyright 2004 Novell, Inc. (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Text;
  32. namespace System.Net {
  33. // Supported cookie formats are:
  34. // Netscape: http://home.netscape.com/newsref/std/cookie_spec.html
  35. // RFC 2109: http://www.ietf.org/rfc/rfc2109.txt
  36. // RFC 2965: http://www.ietf.org/rfc/rfc2965.txt
  37. [Serializable]
  38. public sealed class Cookie
  39. {
  40. string comment;
  41. Uri commentUri;
  42. bool discard;
  43. string domain;
  44. bool expired;
  45. DateTime expires;
  46. string name;
  47. string path;
  48. string port;
  49. int [] ports;
  50. bool secure;
  51. DateTime timestamp;
  52. string val;
  53. int version;
  54. static char [] reservedCharsName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
  55. static char [] reservedCharsValue = new char [] {';', ','};
  56. static char [] portSeparators = new char [] {'"', ','};
  57. static string tspecials = "()<>@,;:\\\"/[]?={} \t"; // from RFC 2965, 2068
  58. public Cookie ()
  59. {
  60. expires = DateTime.MinValue;
  61. timestamp = DateTime.Now;
  62. domain = "";
  63. name = "";
  64. val = "";
  65. comment = "";
  66. domain = "";
  67. port = "";
  68. }
  69. public Cookie (string name, string value)
  70. : this ()
  71. {
  72. Name = name;
  73. Value = value;
  74. }
  75. public Cookie (string name, string value, string path)
  76. : this (name, value)
  77. {
  78. Path = path;
  79. }
  80. public Cookie (string name, string value, string path, string domain)
  81. : this (name, value, path)
  82. {
  83. Domain = domain;
  84. }
  85. public string Comment {
  86. get { return comment; }
  87. set { comment = value == null ? String.Empty : value; }
  88. }
  89. public Uri CommentUri {
  90. get { return commentUri; }
  91. set { commentUri = value; }
  92. }
  93. public bool Discard {
  94. get { return discard; }
  95. set { discard = value; }
  96. }
  97. public string Domain {
  98. get { return domain; }
  99. set { domain = value == null ? String.Empty : value; }
  100. }
  101. public bool Expired {
  102. get {
  103. return expires <= DateTime.Now &&
  104. expires != DateTime.MinValue;
  105. }
  106. set {
  107. expired = value;
  108. if (expired) {
  109. expires = DateTime.Now;
  110. }
  111. }
  112. }
  113. public DateTime Expires {
  114. get { return expires; }
  115. set { expires = value; }
  116. }
  117. public string Name {
  118. get { return name; }
  119. set {
  120. if (value == null || value.Length == 0) {
  121. throw new CookieException ("Name cannot be empty");
  122. }
  123. if (value [0] == '$' || value.IndexOfAny (reservedCharsName) != -1) {
  124. // see CookieTest, according to MS implementation
  125. // the name value changes even though it's incorrect
  126. name = String.Empty;
  127. throw new CookieException ("Name contains invalid characters");
  128. }
  129. name = value;
  130. }
  131. }
  132. public string Path {
  133. get { return (path == null || path == "") ? "/" : path; }
  134. set { path = (value == null) ? String.Empty : value; }
  135. }
  136. public string Port {
  137. get { return port; }
  138. set {
  139. if (value == null || value.Length == 0) {
  140. port = String.Empty;
  141. return;
  142. }
  143. if (value [0] != '"' || value [value.Length - 1] != '"') {
  144. throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Port must be enclosed by double quotes.");
  145. }
  146. port = value;
  147. string [] values = port.Split (portSeparators);
  148. ports = new int[values.Length];
  149. for (int i = 0; i < ports.Length; i++) {
  150. ports [i] = Int32.MinValue;
  151. if (values [i].Length == 0)
  152. continue;
  153. try {
  154. ports [i] = Int32.Parse (values [i]);
  155. } catch (Exception e) {
  156. throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Invalid value: " + values [i], e);
  157. }
  158. }
  159. }
  160. }
  161. internal int [] Ports {
  162. get { return ports; }
  163. }
  164. public bool Secure {
  165. get { return secure; }
  166. set { secure = value; }
  167. }
  168. public DateTime TimeStamp {
  169. get { return timestamp; }
  170. }
  171. public string Value {
  172. get { return val; }
  173. set {
  174. if (value == null) {
  175. val = String.Empty;
  176. return;
  177. }
  178. // LAMESPEC: According to .Net specs the Value property should not accept
  179. // the semicolon and comma characters, yet it does. For now we'll follow
  180. // the behaviour of MS.Net instead of the specs.
  181. /*
  182. if (value.IndexOfAny(reservedCharsValue) != -1)
  183. throw new CookieException("Invalid value. Value cannot contain semicolon or comma characters.");
  184. */
  185. val = value;
  186. }
  187. }
  188. public int Version {
  189. get { return version; }
  190. set {
  191. if ((value < 0) || (value > 10))
  192. version = 0;
  193. else
  194. version = value;
  195. }
  196. }
  197. public override bool Equals (Object obj)
  198. {
  199. System.Net.Cookie c = obj as System.Net.Cookie;
  200. return c != null &&
  201. String.Compare (this.name, c.name, true) == 0 &&
  202. String.Compare (this.val, c.val, false) == 0 &&
  203. String.Compare (this.path, c.path, false) == 0 &&
  204. String.Compare (this.domain, c.domain, true) == 0 &&
  205. this.version == c.version;
  206. }
  207. public override int GetHashCode ()
  208. {
  209. return hash(name.ToLower ().GetHashCode (),
  210. val.GetHashCode (),
  211. path.GetHashCode (),
  212. domain.ToLower ().GetHashCode (),
  213. version);
  214. }
  215. private static int hash (int i, int j, int k, int l, int m)
  216. {
  217. return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25) ^ (m << 20 | m >> 12);
  218. }
  219. // returns a string that can be used to send a cookie to an Origin Server
  220. // i.e., only used for clients
  221. // see also para 3.3.4 of RFC 1965
  222. public override string ToString ()
  223. {
  224. if (name.Length == 0)
  225. return String.Empty;
  226. StringBuilder result = new StringBuilder (64);
  227. if (version > 0) {
  228. result.Append ("$Version=").Append (version).Append (";");
  229. }
  230. result.Append (name).Append ("=").Append (val);
  231. // in the MS.Net implementation path and domain don't show up in
  232. // the result, I guess that's a bug in their implementation...
  233. if (path != null && path.Length != 0)
  234. result.Append (";$Path=").Append (QuotedString (path));
  235. if (domain != null && domain.Length != 0)
  236. result.Append (";$Domain=").Append (QuotedString (domain));
  237. if (port != null && port.Length != 0)
  238. result.Append (";$Port=").Append (port);
  239. return result.ToString ();
  240. }
  241. // See par 3.6 of RFC 2616
  242. string QuotedString (string value)
  243. {
  244. if (version == 0 || IsToken (value))
  245. return value;
  246. else
  247. return "\"" + value.Replace("\"", "\\\"") + "\"";
  248. }
  249. bool IsToken (string value)
  250. {
  251. int len = value.Length;
  252. for (int i = 0; i < len; i++) {
  253. char c = value [i];
  254. if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)
  255. return false;
  256. }
  257. return true;
  258. }
  259. }
  260. }