HttpCookie.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // System.Web.HttpCookie.cs
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Text;
  30. using System.Collections.Specialized;
  31. using System.Security.Permissions;
  32. namespace System.Web
  33. {
  34. [Flags]
  35. internal enum CookieFlags : byte {
  36. Secure = 1,
  37. HttpOnly = 2
  38. }
  39. // CAS - no InheritanceDemand here as the class is sealed
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. #if TARGET_J2EE
  42. // Cookies must be serializable to be saved in the session for J2EE portal
  43. [Serializable]
  44. #endif
  45. public sealed class HttpCookie {
  46. string path = "/";
  47. string domain;
  48. DateTime expires = DateTime.MinValue;
  49. string name;
  50. CookieFlags flags = 0;
  51. NameValueCollection values;
  52. [Obsolete]
  53. internal HttpCookie (string name, string value, string path, DateTime expires)
  54. {
  55. this.name = name;
  56. this.values = new CookieNVC();
  57. this.Value = value;
  58. this.path = path;
  59. this.expires = expires;
  60. }
  61. public HttpCookie (string name)
  62. {
  63. this.name = name;
  64. values = new CookieNVC();
  65. Value = "";
  66. }
  67. public HttpCookie (string name, string value)
  68. : this (name)
  69. {
  70. Value = value;
  71. }
  72. internal string GetCookieHeaderValue ()
  73. {
  74. StringBuilder builder = new StringBuilder ();
  75. builder.Append (name);
  76. builder.Append ("=");
  77. builder.Append (Value);
  78. if (domain != null) {
  79. builder.Append ("; domain=");
  80. builder.Append (domain);
  81. }
  82. if (path != null) {
  83. builder.Append ("; path=");
  84. builder.Append (path);
  85. }
  86. if (expires != DateTime.MinValue) {
  87. builder.Append ("; expires=");
  88. builder.Append (expires.ToUniversalTime().ToString("r"));
  89. }
  90. if ((flags & CookieFlags.Secure) != 0) {
  91. builder.Append ("; secure");
  92. }
  93. if ((flags & CookieFlags.HttpOnly) != 0){
  94. builder.Append ("; HttpOnly");
  95. }
  96. return builder.ToString ();
  97. }
  98. public string Domain {
  99. get {
  100. return domain;
  101. }
  102. set {
  103. domain = value;
  104. }
  105. }
  106. public DateTime Expires {
  107. get {
  108. return expires;
  109. }
  110. set {
  111. expires = value;
  112. }
  113. }
  114. public bool HasKeys {
  115. get {
  116. return values.HasKeys();
  117. }
  118. }
  119. public string this [ string key ] {
  120. get {
  121. return values [ key ];
  122. }
  123. set {
  124. values [ key ] = value;
  125. }
  126. }
  127. public string Name {
  128. get {
  129. return name;
  130. }
  131. set {
  132. name = value;
  133. }
  134. }
  135. public string Path {
  136. get {
  137. return path;
  138. }
  139. set {
  140. path = value;
  141. }
  142. }
  143. public bool Secure {
  144. get {
  145. return (flags & CookieFlags.Secure) == CookieFlags.Secure;
  146. }
  147. set {
  148. if (value)
  149. flags |= CookieFlags.Secure;
  150. else
  151. flags &= ~CookieFlags.Secure;
  152. }
  153. }
  154. public string Value {
  155. get {
  156. return HttpUtility.UrlDecode(values.ToString ());
  157. }
  158. set {
  159. values.Clear ();
  160. if (value != null && value != "") {
  161. string [] components = value.Split ('&');
  162. foreach (string kv in components){
  163. int pos = kv.IndexOf ('=');
  164. if (pos == -1){
  165. values.Add (null, kv);
  166. } else {
  167. string key = kv.Substring (0, pos);
  168. string val = kv.Substring (pos+1);
  169. values.Add (key, val);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. public NameValueCollection Values {
  176. get {
  177. return values;
  178. }
  179. }
  180. public bool HttpOnly {
  181. get {
  182. return (flags & CookieFlags.HttpOnly) == CookieFlags.HttpOnly;
  183. }
  184. set {
  185. if (value)
  186. flags |= CookieFlags.HttpOnly;
  187. else
  188. flags &= ~CookieFlags.HttpOnly;
  189. }
  190. }
  191. /*
  192. * simple utility class that just overrides ToString
  193. * to get the desired behavior for
  194. * HttpCookie.Values
  195. */
  196. sealed class CookieNVC : NameValueCollection
  197. {
  198. public CookieNVC ()
  199. : base (StringComparer.OrdinalIgnoreCase)
  200. {
  201. }
  202. public override string ToString ()
  203. {
  204. StringBuilder builder = new StringBuilder ("");
  205. bool first_key = true;
  206. foreach (string key in Keys) {
  207. if (!first_key)
  208. builder.Append ("&");
  209. string[] vals = GetValues (key);
  210. if(vals == null)
  211. vals = new string[1] {String.Empty};
  212. bool first_val = true;
  213. foreach (string v in vals) {
  214. if (!first_val)
  215. builder.Append ("&");
  216. if (key != null && key.Length > 0) {
  217. builder.Append (HttpUtility.UrlEncode(key));
  218. builder.Append ("=");
  219. }
  220. if(v != null && v.Length > 0)
  221. builder.Append (HttpUtility.UrlEncode(v));
  222. first_val = false;
  223. }
  224. first_key = false;
  225. }
  226. return builder.ToString();
  227. }
  228. /* MS's implementation has the interesting quirk that if you do:
  229. * cookie.Values[null] = "foo"
  230. * it clears out the rest of the values.
  231. */
  232. public override void Set (string name, string value)
  233. {
  234. if (this.IsReadOnly)
  235. throw new NotSupportedException ("Collection is read-only");
  236. if (name == null) {
  237. Clear();
  238. name = string.Empty;
  239. }
  240. // if (value == null)
  241. // value = string.Empty;
  242. base.Set (name, value);
  243. }
  244. }
  245. }
  246. }