2 // System.Net.CookieContainer
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
\r
12 using System.Collections;
\r
13 using System.Runtime.Serialization;
\r
16 namespace System.Net
\r
19 public class CookieContainer
\r
21 public const int DefaultCookieLengthLimit = 4096;
\r
22 public const int DefaultCookieLimit = 300;
\r
23 public const int DefaultPerDomainCookieLimit = 20;
\r
26 int capacity = DefaultCookieLimit;
\r
27 int perDomainCapacity = DefaultPerDomainCookieLimit;
\r
28 int maxCookieSize = DefaultCookieLengthLimit;
\r
29 CookieCollection cookies;
\r
32 public CookieContainer ()
\r
36 public CookieContainer (int capacity)
\r
39 throw new ArgumentException ("Must be greater than zero", "capacity");
\r
41 this.capacity = capacity;
\r
44 public CookieContainer (int capacity, int perDomainCapacity, int maxCookieSize)
\r
47 if (perDomainCapacity <= 0 || perDomainCapacity < capacity)
\r
48 throw new ArgumentException ("Invalid value", "perDomaniCapacity");
\r
50 if (maxCookieSize <= 0)
\r
51 throw new ArgumentException ("Must be greater than zero", "maxCookieSize");
\r
53 this.perDomainCapacity = perDomainCapacity;
\r
54 this.maxCookieSize = maxCookieSize;
\r
60 get { return count; }
\r
63 public int Capacity {
\r
64 get { return capacity; }
\r
67 (value < perDomainCapacity && perDomainCapacity != Int32.MaxValue))
\r
68 throw new ArgumentOutOfRangeException ("value");
\r
69 if (value < maxCookieSize)
\r
70 maxCookieSize = value;
\r
75 public int MaxCookieSize {
\r
76 get { return maxCookieSize; }
\r
79 throw new ArgumentOutOfRangeException ("value");
\r
80 maxCookieSize = value;
\r
84 public int PerDomainCapacity {
\r
85 get { return perDomainCapacity; }
\r
88 (value > DefaultCookieLimit && value != Int32.MaxValue))
\r
89 throw new ArgumentOutOfRangeException ("value");
\r
90 if (value < perDomainCapacity)
\r
91 perDomainCapacity = value;
\r
92 perDomainCapacity = value;
\r
96 public void Add (Cookie cookie)
\r
99 throw new ArgumentNullException ("cookie");
\r
101 if (cookie.Domain == null && cookie.Domain == "")
\r
102 throw new ArgumentException ("Cookie domain not set.", "cookie");
\r
104 if (cookie.Value.ToString ().Length > maxCookieSize)
\r
105 throw new CookieException ("Cookie size too big");
\r
107 AddCookie (cookie);
\r
110 void AddCookie (Cookie cookie)
\r
113 if (cookies == null)
\r
114 cookies = new CookieCollection ();
\r
116 if (count + 1 > capacity)
\r
117 throw new CookieException ("Capacity exceeded");
\r
119 cookies.Add (cookie);
\r
124 public void Add (CookieCollection cookies)
\r
126 if (cookies == null)
\r
127 throw new ArgumentNullException ("cookies");
\r
129 foreach (Cookie cookie in cookies)
\r
134 public void Add (Uri uri, Cookie cookie)
\r
137 throw new ArgumentNullException ("uri");
\r
143 public void Add (Uri uri, CookieCollection cookies)
\r
146 throw new ArgumentNullException ("uri");
\r
152 public string GetCookieHeader (Uri uri)
\r
155 throw new ArgumentNullException ("uri");
\r
157 if (cookies == null)
\r
160 StringBuilder result = new StringBuilder ();
\r
161 bool notfirst = false;
\r
162 foreach (Cookie cookie in cookies) {
\r
164 result.Append (';');
\r
166 result.Append (cookie.ToString ());
\r
169 return result.ToString ();
\r
173 public CookieCollection GetCookies (Uri uri)
\r
176 throw new ArgumentNullException ("uri");
\r
178 CookieCollection coll = new CookieCollection ();
\r
179 if (cookies == null)
\r
182 foreach (Cookie cookie in cookies)
\r
188 public void SetCookies (Uri uri, string cookieHeader)
\r
191 throw new ArgumentNullException ("uri");
\r
193 if (cookieHeader == null)
\r
194 throw new ArgumentNullException ("cookieHeader");
\r
196 ParseAndAddCookies (cookieHeader);
\r
199 // GetCookieValue, GetCookieName and ParseAndAddCookies copied from HttpRequest.cs
\r
200 static string GetCookieValue (string str, int length, ref int i)
\r
206 while (k < length && Char.IsWhiteSpace (str [k]))
\r
210 while (k < length && str [k] != ';')
\r
214 return str.Substring (begin, i - begin).Trim ();
\r
217 static string GetCookieName (string str, int length, ref int i)
\r
223 while (k < length && Char.IsWhiteSpace (str [k]))
\r
227 while (k < length && str [k] != ';' && str [k] != '=')
\r
231 return str.Substring (begin, k - begin).Trim ();
\r
235 void ParseAndAddCookies (string header)
\r
237 if (header.Length == 0)
\r
241 * cookie = "Cookie:" cookie-version
\r
242 * 1*((";" | ",") cookie-value)
\r
243 * cookie-value = NAME "=" VALUE [";" path] [";" domain]
\r
244 * cookie-version = "$Version" "=" value
\r
247 * path = "$Path" "=" value
\r
248 * domain = "$Domain" "=" value
\r
250 * MS ignores $Version!
\r
251 * ',' as a separator produces errors.
\r
254 string [] name_values = header.Trim ().Split (';');
\r
255 int length = name_values.Length;
\r
256 Cookie cookie = null;
\r
258 for (int i = 0; i < length; i++) {
\r
260 string name_value = name_values [i].Trim ();
\r
261 string name = GetCookieName (name_value, name_value.Length, ref pos);
\r
262 string value = GetCookieValue (name_value, name_value.Length, ref pos);
\r
263 if (cookie != null) {
\r
264 if (name == "$Path") {
\r
265 cookie.Path = value;
\r
267 } else if (name == "$Domain") {
\r
268 cookie.Domain = value;
\r
275 cookie = new Cookie (name, value);
\r
278 if (cookie != null)
\r
282 } // CookieContainer
\r