Cookie.cs 7.1 KB

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