Cookie.cs 8.0 KB

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