Cookie.cs 9.0 KB

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