Cookie.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // System.Net.Cookie.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Text;
  9. namespace System.Net {
  10. // Supported cookie formats are:
  11. // Netscape: http://home.netscape.com/newsref/std/cookie_spec.html
  12. // RFC 2109: http://www.ietf.org/rfc/rfc2109.txt
  13. // RFC 2965: http://www.ietf.org/rfc/rfc2965.txt
  14. [Serializable]
  15. public sealed class Cookie
  16. {
  17. private string comment;
  18. private Uri commentUri;
  19. private bool discard;
  20. private string domain;
  21. private bool expired;
  22. private DateTime expires;
  23. private string name;
  24. private string path;
  25. private string port;
  26. private int [] ports;
  27. private bool secure;
  28. private DateTime timestamp;
  29. private string val;
  30. private int version;
  31. private static char [] reservedCharsName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
  32. private static char [] reservedCharsValue = new char [] {';', ','};
  33. private static char [] portSeparators = new char [] {'"', ','};
  34. private static string tspecials = "()<>@,;:\\\"/[]?={} \t"; // from RFC 2965, 2068
  35. public Cookie ()
  36. : this (String.Empty, String.Empty) {}
  37. public Cookie (string name, string value)
  38. {
  39. Name = name;
  40. Value = value;
  41. discard = false;
  42. expired = false;
  43. secure = false;
  44. expires = DateTime.MinValue;
  45. timestamp = DateTime.Now;
  46. version = 0;
  47. domain = "";
  48. }
  49. public Cookie (string name, string value, string path)
  50. : this (name, value)
  51. {
  52. Path = path;
  53. }
  54. public Cookie (string name, string value, string path, string domain)
  55. : this (name, value, path)
  56. {
  57. Domain = domain;
  58. }
  59. public string Comment {
  60. get { return comment; }
  61. set { comment = value == null ? String.Empty : value; }
  62. }
  63. public Uri CommentUri {
  64. get { return commentUri; }
  65. set { commentUri = value; }
  66. }
  67. public bool Discard {
  68. get { return discard; }
  69. set { discard = value; }
  70. }
  71. public string Domain {
  72. get { return domain; }
  73. set { domain = value == null ? String.Empty : value; }
  74. }
  75. public bool Expired {
  76. get {
  77. return expires <= DateTime.Now &&
  78. expires != DateTime.MinValue;
  79. }
  80. set {
  81. expired = value;
  82. if (expired) {
  83. expires = DateTime.Now;
  84. }
  85. }
  86. }
  87. public DateTime Expires {
  88. get { return expires; }
  89. set { expires = value; }
  90. }
  91. public string Name {
  92. get { return name; }
  93. set {
  94. if (value == null || value.Length == 0) {
  95. throw new CookieException ("Name cannot be empty");
  96. }
  97. if (value [0] == '$' || value.IndexOfAny (reservedCharsName) != -1) {
  98. // see CookieTest, according to MS implementation
  99. // the name value changes even though it's incorrect
  100. name = String.Empty;
  101. throw new CookieException ("Name contains invalid characters");
  102. }
  103. name = value;
  104. }
  105. }
  106. public string Path {
  107. get { return (path == null) ? "/" : path; }
  108. set { path = (value == null) ? String.Empty : value; }
  109. }
  110. public string Port {
  111. get { return port; }
  112. set {
  113. if (value == null || value.Length == 0) {
  114. port = String.Empty;
  115. return;
  116. }
  117. if (value [0] != '"' || value [value.Length - 1] != '"') {
  118. throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Port must be enclosed by double quotes.");
  119. }
  120. port = value;
  121. string [] values = port.Split (portSeparators);
  122. ports = new int[values.Length];
  123. for (int i = 0; i < ports.Length; i++) {
  124. ports [i] = Int32.MinValue;
  125. if (values [i].Length == 0)
  126. continue;
  127. try {
  128. ports [i] = Int32.Parse (values [i]);
  129. } catch (Exception e) {
  130. throw new CookieException("The 'Port'='" + value + "' part of the cookie is invalid. Invalid value: " + values [i], e);
  131. }
  132. }
  133. }
  134. }
  135. int[] Ports {
  136. get { return ports; }
  137. }
  138. public bool Secure {
  139. get { return secure; }
  140. set { secure = value; }
  141. }
  142. public DateTime TimeStamp {
  143. get { return timestamp; }
  144. }
  145. public string Value {
  146. get { return val; }
  147. set {
  148. if (value == null) {
  149. val = String.Empty;
  150. return;
  151. }
  152. // LAMESPEC: According to .Net specs the Value property should not accept
  153. // the semicolon and comma characters, yet it does. For now we'll follow
  154. // the behaviour of MS.Net instead of the specs.
  155. /*
  156. if (value.IndexOfAny(reservedCharsValue) != -1)
  157. throw new CookieException("Invalid value. Value cannot contain semicolon or comma characters.");
  158. */
  159. val = value;
  160. }
  161. }
  162. public int Version {
  163. get { return version; }
  164. set {
  165. if ((value < 0) || (value > 10))
  166. version = 0;
  167. else
  168. version = value;
  169. }
  170. }
  171. public override bool Equals (Object obj)
  172. {
  173. System.Net.Cookie c = obj as System.Net.Cookie;
  174. return c != null &&
  175. String.Compare (this.name, c.name, true) == 0 &&
  176. String.Compare (this.val, c.val, false) == 0 &&
  177. String.Compare (this.path, c.path, false) == 0 &&
  178. String.Compare (this.domain, c.domain, true) == 0 &&
  179. this.version == c.version;
  180. }
  181. public override int GetHashCode ()
  182. {
  183. return hash(name.ToLower ().GetHashCode (),
  184. val.GetHashCode (),
  185. path.GetHashCode (),
  186. domain.ToLower ().GetHashCode (),
  187. version);
  188. }
  189. private static int hash (int i, int j, int k, int l, int m)
  190. {
  191. return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25) ^ (m << 20 | m >> 12);
  192. }
  193. // returns a string that can be used to send a cookie to an Origin Server
  194. // i.e., only used for clients
  195. // see also para 3.3.4 of RFC 1965
  196. public override string ToString ()
  197. {
  198. if (name.Length == 0)
  199. return String.Empty;
  200. StringBuilder result = new StringBuilder (64);
  201. if (version > 0) {
  202. result.Append ("$Version=").Append (version).Append (";");
  203. }
  204. result.Append (name).Append ("=").Append (val);
  205. // in the MS.Net implementation path and domain don't show up in
  206. // the result, I guess that's a bug in their implementation...
  207. if (path != null && path.Length != 0)
  208. result.Append (";$Path=").Append (QuotedString (path));
  209. if (domain != null && domain.Length != 0)
  210. result.Append (";$Domain=").Append (QuotedString (domain));
  211. if (port != null && port.Length != 0)
  212. result.Append (";$Port=").Append (port);
  213. return result.ToString ();
  214. }
  215. // See par 3.6 of RFC 2616
  216. private string QuotedString (string value)
  217. {
  218. if (version == 0 || IsToken (value))
  219. return value;
  220. else
  221. return "\"" + value.Replace("\"", "\\\"") + "\"";
  222. }
  223. private bool IsToken (string value)
  224. {
  225. int len = value.Length;
  226. for (int i = 0; i < len; i++) {
  227. char c = value [i];
  228. if (c < 0x20 || c >= 0x7f || tspecials.IndexOf (c) != -1)
  229. return false;
  230. }
  231. return true;
  232. }
  233. }
  234. }