CookieContainer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // System.Net.CookieContainer
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Runtime.Serialization;
  33. using System.Text;
  34. namespace System.Net
  35. {
  36. [Serializable]
  37. public class CookieContainer
  38. {
  39. public const int DefaultCookieLengthLimit = 4096;
  40. public const int DefaultCookieLimit = 300;
  41. public const int DefaultPerDomainCookieLimit = 20;
  42. int count;
  43. int capacity = DefaultCookieLimit;
  44. int perDomainCapacity = DefaultPerDomainCookieLimit;
  45. int maxCookieSize = DefaultCookieLengthLimit;
  46. CookieCollection cookies;
  47. // ctors
  48. public CookieContainer ()
  49. {
  50. }
  51. public CookieContainer (int capacity)
  52. {
  53. if (capacity <= 0)
  54. throw new ArgumentException ("Must be greater than zero", "capacity");
  55. this.capacity = capacity;
  56. }
  57. public CookieContainer (int capacity, int perDomainCapacity, int maxCookieSize)
  58. : this (capacity)
  59. {
  60. if (perDomainCapacity <= 0 || perDomainCapacity < capacity)
  61. throw new ArgumentException ("Invalid value", "perDomaniCapacity");
  62. if (maxCookieSize <= 0)
  63. throw new ArgumentException ("Must be greater than zero", "maxCookieSize");
  64. this.perDomainCapacity = perDomainCapacity;
  65. this.maxCookieSize = maxCookieSize;
  66. }
  67. // properties
  68. public int Count {
  69. get { return count; }
  70. }
  71. public int Capacity {
  72. get { return capacity; }
  73. set {
  74. if ((value <= 0) ||
  75. (value < perDomainCapacity && perDomainCapacity != Int32.MaxValue))
  76. throw new ArgumentOutOfRangeException ("value");
  77. if (value < maxCookieSize)
  78. maxCookieSize = value;
  79. capacity = value;
  80. }
  81. }
  82. public int MaxCookieSize {
  83. get { return maxCookieSize; }
  84. set {
  85. if (value <= 0)
  86. throw new ArgumentOutOfRangeException ("value");
  87. maxCookieSize = value;
  88. }
  89. }
  90. public int PerDomainCapacity {
  91. get { return perDomainCapacity; }
  92. set {
  93. if ((value <= 0) ||
  94. (value > DefaultCookieLimit && value != Int32.MaxValue))
  95. throw new ArgumentOutOfRangeException ("value");
  96. if (value < perDomainCapacity)
  97. perDomainCapacity = value;
  98. perDomainCapacity = value;
  99. }
  100. }
  101. public void Add (Cookie cookie)
  102. {
  103. if (cookie == null)
  104. throw new ArgumentNullException ("cookie");
  105. if (cookie.Domain == null && cookie.Domain == "")
  106. throw new ArgumentException ("Cookie domain not set.", "cookie");
  107. if (cookie.Value.ToString ().Length > maxCookieSize)
  108. throw new CookieException ("Cookie size too big");
  109. AddCookie (cookie);
  110. }
  111. void AddCookie (Cookie cookie)
  112. {
  113. lock (this) {
  114. if (cookies == null)
  115. cookies = new CookieCollection ();
  116. if (count + 1 > capacity)
  117. throw new CookieException ("Capacity exceeded");
  118. cookies.Add (cookie);
  119. count++;
  120. }
  121. }
  122. public void Add (CookieCollection cookies)
  123. {
  124. if (cookies == null)
  125. throw new ArgumentNullException ("cookies");
  126. foreach (Cookie cookie in cookies)
  127. Add (cookie);
  128. }
  129. [MonoTODO ("Uri")]
  130. public void Add (Uri uri, Cookie cookie)
  131. {
  132. if (uri == null)
  133. throw new ArgumentNullException ("uri");
  134. Add (cookie);
  135. }
  136. [MonoTODO("Uri")]
  137. public void Add (Uri uri, CookieCollection cookies)
  138. {
  139. if (uri == null)
  140. throw new ArgumentNullException ("uri");
  141. Add (cookies);
  142. }
  143. [MonoTODO("Uri")]
  144. public string GetCookieHeader (Uri uri)
  145. {
  146. if (uri == null)
  147. throw new ArgumentNullException ("uri");
  148. if (cookies == null)
  149. return "";
  150. StringBuilder result = new StringBuilder ();
  151. bool notfirst = false;
  152. foreach (Cookie cookie in cookies) {
  153. if (notfirst)
  154. result.Append (';');
  155. result.Append (cookie.ToString ());
  156. notfirst = true;
  157. }
  158. return result.ToString ();
  159. }
  160. [MonoTODO("Uri")]
  161. public CookieCollection GetCookies (Uri uri)
  162. {
  163. if (uri == null)
  164. throw new ArgumentNullException ("uri");
  165. CookieCollection coll = new CookieCollection ();
  166. if (cookies == null)
  167. return coll;
  168. foreach (Cookie cookie in cookies)
  169. coll.Add (cookie);
  170. return coll;
  171. }
  172. public void SetCookies (Uri uri, string cookieHeader)
  173. {
  174. if (uri == null)
  175. throw new ArgumentNullException ("uri");
  176. if (cookieHeader == null)
  177. throw new ArgumentNullException ("cookieHeader");
  178. ParseAndAddCookies (cookieHeader);
  179. }
  180. // GetCookieValue, GetCookieName and ParseAndAddCookies copied from HttpRequest.cs
  181. static string GetCookieValue (string str, int length, ref int i)
  182. {
  183. if (i >= length)
  184. return null;
  185. int k = i;
  186. while (k < length && Char.IsWhiteSpace (str [k]))
  187. k++;
  188. int begin = k;
  189. while (k < length && str [k] != ';')
  190. k++;
  191. i = k;
  192. return str.Substring (begin, i - begin).Trim ();
  193. }
  194. static string GetCookieName (string str, int length, ref int i)
  195. {
  196. if (i >= length)
  197. return null;
  198. int k = i;
  199. while (k < length && Char.IsWhiteSpace (str [k]))
  200. k++;
  201. int begin = k;
  202. while (k < length && str [k] != ';' && str [k] != '=')
  203. k++;
  204. i = k + 1;
  205. return str.Substring (begin, k - begin).Trim ();
  206. }
  207. void ParseAndAddCookies (string header)
  208. {
  209. if (header.Length == 0)
  210. return;
  211. /* RFC 2109
  212. * cookie = "Cookie:" cookie-version
  213. * 1*((";" | ",") cookie-value)
  214. * cookie-value = NAME "=" VALUE [";" path] [";" domain]
  215. * cookie-version = "$Version" "=" value
  216. * NAME = attr
  217. * VALUE = value
  218. * path = "$Path" "=" value
  219. * domain = "$Domain" "=" value
  220. *
  221. * MS ignores $Version!
  222. * ',' as a separator produces errors.
  223. */
  224. string [] name_values = header.Trim ().Split (';');
  225. int length = name_values.Length;
  226. Cookie cookie = null;
  227. int pos;
  228. for (int i = 0; i < length; i++) {
  229. pos = 0;
  230. string name_value = name_values [i].Trim ();
  231. string name = GetCookieName (name_value, name_value.Length, ref pos);
  232. string value = GetCookieValue (name_value, name_value.Length, ref pos);
  233. if (cookie != null) {
  234. if (name == "$Path") {
  235. cookie.Path = value;
  236. continue;
  237. } else if (name == "$Domain") {
  238. cookie.Domain = value;
  239. continue;
  240. } else {
  241. Add (cookie);
  242. cookie = null;
  243. }
  244. }
  245. cookie = new Cookie (name, value);
  246. }
  247. if (cookie != null)
  248. Add (cookie);
  249. }
  250. } // CookieContainer
  251. } // System.Net