12 [global::__DynamicallyInvokable]
16 [global::__DynamicallyInvokable]
20 [global::__DynamicallyInvokable]
24 [global::__DynamicallyInvokable]
27 private static readonly HeaderVariantInfo[] HeaderInfo =
new HeaderVariantInfo[2]
29 new HeaderVariantInfo(
"Set-Cookie", CookieVariant.Rfc2109),
30 new HeaderVariantInfo(
"Set-Cookie2", CookieVariant.Rfc2965)
35 private int m_maxCookieSize = 4096;
37 private int m_maxCookies = 300;
39 private int m_maxCookiesPerDomain = 20;
43 private string m_fqdnMyDomain =
string.Empty;
49 [global::__DynamicallyInvokable]
52 [global::__DynamicallyInvokable]
57 [global::__DynamicallyInvokable]
60 if (value <= 0 || (value < m_maxCookiesPerDomain && m_maxCookiesPerDomain !=
int.MaxValue))
64 if (value < m_maxCookies)
75 [global::__DynamicallyInvokable]
78 [global::__DynamicallyInvokable]
89 [global::__DynamicallyInvokable]
92 [global::__DynamicallyInvokable]
95 return m_maxCookieSize;
97 [global::__DynamicallyInvokable]
104 m_maxCookieSize = value;
113 [global::__DynamicallyInvokable]
116 [global::__DynamicallyInvokable]
119 return m_maxCookiesPerDomain;
121 [global::__DynamicallyInvokable]
124 if (value <= 0 || (value > m_maxCookies && value !=
int.MaxValue))
128 if (value < m_maxCookiesPerDomain)
130 m_maxCookiesPerDomain = value;
133 m_maxCookiesPerDomain = value;
138 [global::__DynamicallyInvokable]
142 if (domainName !=
null && domainName.Length > 1)
144 m_fqdnMyDomain =
"." + domainName;
159 m_maxCookies = capacity;
174 if (perDomainCapacity !=
int.MaxValue && (perDomainCapacity <= 0 || perDomainCapacity > capacity))
176 throw new ArgumentOutOfRangeException(
"perDomainCapacity", SR.GetString(
"net_cookie_capacity_range",
"PerDomainCapacity", 0, capacity));
178 m_maxCookiesPerDomain = perDomainCapacity;
179 if (maxCookieSize <= 0)
183 m_maxCookieSize = maxCookieSize;
199 if (cookie.
Domain.Length == 0)
201 throw new ArgumentException(SR.GetString(
"net_emptystringcall"),
"cookie.Domain");
205 if (!cookie.DomainImplicit && cookie.
Domain[0] ==
'.')
207 stringBuilder.
Append(
"0");
210 if (cookie.PortList !=
null)
219 Cookie cookie2 = cookie.Clone();
220 cookie2.VerifySetDefaults(cookie2.Variant, result, IsLocalDomain(result.Host), m_fqdnMyDomain, set_default:
true, isThrow:
true);
221 Add(cookie2, throwOnError:
true);
224 private void AddRemoveDomain(
string key, PathList value)
230 m_domainTable.
Remove(key);
234 m_domainTable[key] = value;
239 internal void Add(
Cookie cookie,
bool throwOnError)
241 if (cookie.Value.Length > m_maxCookieSize)
245 throw new CookieException(SR.GetString(
"net_cookie_size", cookie.ToString(), m_maxCookieSize));
255 pathList = (PathList)m_domainTable[cookie.DomainKey];
256 if (pathList ==
null)
258 pathList =
new PathList();
259 AddRemoveDomain(cookie.DomainKey, pathList);
262 int cookiesCount = pathList.GetCookiesCount();
263 CookieCollection cookieCollection;
264 lock (pathList.SyncRoot)
266 cookieCollection = (CookieCollection)pathList[cookie.Path];
267 if (cookieCollection ==
null)
269 cookieCollection =
new CookieCollection();
270 pathList[cookie.Path] = cookieCollection;
275 lock (cookieCollection)
277 int num = cookieCollection.IndexOf(cookie);
280 cookieCollection.RemoveAt(num);
285 else if ((cookiesCount < m_maxCookiesPerDomain || AgeCookies(cookie.DomainKey)) && (m_count < m_maxCookies || AgeCookies(
null)))
287 lock (cookieCollection)
289 m_count += cookieCollection.InternalAdd(cookie, isStrict:
true);
301 throw new CookieException(SR.GetString(
"net_container_add_cookie"), ex);
307 private bool AgeCookies(
string domain)
309 if (m_maxCookies == 0 || m_maxCookiesPerDomain == 0)
317 CookieCollection cookieCollection =
null;
323 if (m_count > m_maxCookies)
325 num4 = (float)m_maxCookies / (
float)m_count;
334 text2 = (string)item.Key;
335 pathList = (PathList)item.Value;
340 pathList = (PathList)m_domainTable[domain];
343 lock (pathList.SyncRoot)
345 foreach (CookieCollection value
in pathList.Values)
347 num3 = ExpireCollection(value);
352 if (value.Count > 0 && (dateTime2 = value.TimeStamp(CookieCollection.Stamp.Check)) < dateTime)
355 cookieCollection = value;
356 dateTime = dateTime2;
360 int num5 = Math.Min((
int)((
float)num2 * num4), Math.Min(m_maxCookiesPerDomain, m_maxCookies) - 1);
365 lock (pathList.SyncRoot)
367 array = Array.CreateInstance(typeof(CookieCollection), pathList.Count);
368 array2 = Array.CreateInstance(typeof(DateTime), pathList.Count);
369 foreach (CookieCollection value2
in pathList.Values)
371 array2.SetValue(value2.TimeStamp(CookieCollection.Stamp.Check), num3);
372 array.SetValue(value2, num3);
376 Array.Sort(array2, array);
378 for (
int i = 0; i < array.Length; i++)
380 CookieCollection cookieCollection4 = (CookieCollection)array.GetValue(i);
381 lock (cookieCollection4)
383 while (num2 > num5 && cookieCollection4.Count > 0)
385 cookieCollection4.RemoveAt(0);
396 if (num2 > num5 && domain !=
null)
415 lock (cookieCollection)
417 while (m_count >= m_maxCookies && cookieCollection.Count > 0)
419 cookieCollection.RemoveAt(0);
426 private int ExpireCollection(CookieCollection cc)
430 int count = cc.Count;
431 for (
int num = count - 1; num >= 0; num--)
439 return count - cc.Count;
453 foreach (
Cookie cooky
in cookies)
459 internal bool IsLocalDomain(
string host)
461 int num = host.IndexOf(
'.');
466 if (host ==
"127.0.0.1" || host ==
"::1" || host ==
"0:0:0:0:0:0:0:1")
470 if (
string.Compare(m_fqdnMyDomain, 0, host, num, m_fqdnMyDomain.Length,
StringComparison.OrdinalIgnoreCase) == 0)
474 string[] array = host.Split(
'.');
475 if (array !=
null && array.Length == 4 && array[0] ==
"127")
478 for (i = 1; i < 4; i++)
480 switch (array[i].Length)
483 if (array[i][2] <
'0' || array[i][2] >
'9')
489 if (array[i][1] <
'0' || array[i][1] >
'9')
495 if (array[i][0] >=
'0' && array[i][0] <=
'9')
518 [global::__DynamicallyInvokable]
529 Cookie cookie2 = cookie.Clone();
530 cookie2.VerifySetDefaults(cookie2.Variant, uri, IsLocalDomain(uri.
Host), m_fqdnMyDomain, set_default:
true, isThrow:
true);
531 Add(cookie2, throwOnError:
true);
541 [global::__DynamicallyInvokable]
552 bool isLocalDomain = IsLocalDomain(uri.
Host);
553 foreach (
Cookie cooky
in cookies)
555 Cookie cookie2 = cooky.Clone();
556 cookie2.VerifySetDefaults(cookie2.Variant, uri, isLocalDomain, m_fqdnMyDomain, set_default:
true, isThrow:
true);
557 Add(cookie2, throwOnError:
true);
561 internal CookieCollection CookieCutter(
Uri uri,
string headerName,
string setCookieHeader,
bool isThrow)
564 CookieVariant variant = CookieVariant.Unknown;
565 if (headerName ==
null)
567 variant = CookieVariant.Rfc2109;
571 for (
int i = 0; i < HeaderInfo.Length; i++)
573 if (
string.Compare(headerName, HeaderInfo[i].Name,
StringComparison.OrdinalIgnoreCase) == 0)
575 variant = HeaderInfo[i].Variant;
579 bool isLocalDomain = IsLocalDomain(uri.
Host);
582 CookieParser cookieParser =
new CookieParser(setCookieHeader);
585 Cookie cookie = cookieParser.Get();
590 if (ValidationHelper.IsBlankString(cookie.Name))
594 throw new CookieException(SR.GetString(
"net_cookie_format"));
597 else if (cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, set_default:
true, isThrow))
599 cookieCollection.InternalAdd(cookie, isStrict:
true);
611 throw new CookieException(SR.GetString(
"net_cookie_parse_header", uri.
AbsoluteUri), ex);
614 foreach (
Cookie item
in cookieCollection)
618 return cookieCollection;
626 [global::__DynamicallyInvokable]
633 return InternalGetCookies(uri);
643 string host = uri.
Host;
645 list.Add(
"." + host);
646 int num = host.IndexOf(
'.');
649 if (m_fqdnMyDomain !=
null && m_fqdnMyDomain.Length != 0)
651 list.Add(host + m_fqdnMyDomain);
652 list.Add(m_fqdnMyDomain);
657 list.Add(host.Substring(num));
660 int num2 = host.LastIndexOf(
'.', host.Length - 2);
663 num2 = host.LastIndexOf(
'.', num2 - 1);
667 while (num < num2 && (num = host.IndexOf(
'.', num + 1)) != -1)
669 list2.
Add(host.Substring(num));
674 BuildCookieCollectionFromDomainMatches(uri, isSecure, port, cookieCollection, list, matchOnlyPlainCookie:
false);
675 BuildCookieCollectionFromDomainMatches(uri, isSecure, port, cookieCollection, list2, matchOnlyPlainCookie:
true);
676 return cookieCollection;
679 private void BuildCookieCollectionFromDomainMatches(Uri uri,
bool isSecure,
int port, CookieCollection cookies,
List<string> domainAttribute,
bool matchOnlyPlainCookie)
681 for (
int i = 0; i < domainAttribute.
Count; i++)
686 lock (m_domainTable.SyncRoot)
688 pathList = (PathList)m_domainTable[domainAttribute[i]];
690 if (pathList ==
null)
694 lock (pathList.SyncRoot)
698 string text = (string)item.Key;
699 if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(text)))
702 CookieCollection cookieCollection = (CookieCollection)item.Value;
703 cookieCollection.TimeStamp(CookieCollection.Stamp.Set);
704 MergeUpdateCollections(cookies, cookieCollection, port, isSecure, matchOnlyPlainCookie);
718 CookieCollection cookieCollection2 = (CookieCollection)pathList[
"/"];
719 if (cookieCollection2 !=
null)
721 cookieCollection2.TimeStamp(CookieCollection.Stamp.Set);
722 MergeUpdateCollections(cookies, cookieCollection2, port, isSecure, matchOnlyPlainCookie);
725 if (pathList.Count == 0)
727 AddRemoveDomain(domainAttribute[i],
null);
732 private void MergeUpdateCollections(CookieCollection destination, CookieCollection source,
int port,
bool isSecure,
bool isPlainOnly)
736 for (
int i = 0; i < source.Count; i++)
739 Cookie cookie = source[i];
748 if (!isPlainOnly || cookie.Variant == CookieVariant.Plain)
750 if (cookie.PortList !=
null)
752 int[] portList = cookie.PortList;
753 foreach (
int num
in portList)
767 if (cookie.Secure && !isSecure)
773 destination.InternalAdd(cookie, isStrict:
false);
785 [global::__DynamicallyInvokable]
799 string text =
string.Empty;
800 string str =
string.Empty;
801 foreach (
Cookie item
in cookieCollection)
803 text = text + str + item.ToString();
818 [global::__DynamicallyInvokable]
825 if (cookieHeader ==
null)
829 CookieCutter(uri,
null, cookieHeader, isThrow:
true);
UriKind
Defines the kinds of T:System.Uris for the M:System.Uri.IsWellFormedUriString(System....
int Port
Gets the port number of this URI.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
The Cookie header, which specifies cookie data presented to the server.
static readonly string UriSchemeHttp
Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP)....
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
string AbsoluteUri
Gets the absolute URI.
Provides a collection container for instances of the T:System.Net.Cookie class.
string Scheme
Gets the scheme name for this URI.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
void Add(Uri uri, CookieCollection cookies)
Adds the contents of a T:System.Net.CookieCollection to the T:System.Net.CookieContainer for a partic...
CookieContainer(int capacity, int perDomainCapacity, int maxCookieSize)
Initializes a new instance of the T:System.Net.CookieContainer class with specific properties.
Provides a container for a collection of T:System.Net.CookieCollection objects.
string? Path
Gets or sets the URIs to which the T:System.Net.Cookie applies.
const int DefaultCookieLimit
Represents the default maximum number of T:System.Net.Cookie instances that the T:System....
string? Domain
Gets or sets the URI for which the T:System.Net.Cookie is valid.
static bool TryCreate(string uriString, UriKind uriKind, out Uri result)
Creates a new T:System.Uri using the specified T:System.String instance and a T:System....
A type representing a date and time value.
const int DefaultPerDomainCookieLimit
Represents the default maximum number of T:System.Net.Cookie instances that the T:System....
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
CookieCollection GetCookies(Uri uri)
Gets a T:System.Net.CookieCollection that contains the T:System.Net.Cookie instances that are associa...
Represents a collection of key/value pairs that are organized based on the hash code of the key....
int Capacity
Gets and sets the number of T:System.Net.Cookie instances that a T:System.Net.CookieContainer can hol...
const int DefaultCookieLengthLimit
Represents the default maximum size, in bytes, of the T:System.Net.Cookie instances that the T:System...
int MaxCookieSize
Represents the maximum allowed length of a T:System.Net.Cookie.
CookieContainer()
Initializes a new instance of the T:System.Net.CookieContainer class.
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
int PerDomainCapacity
Gets and sets the number of T:System.Net.Cookie instances that a T:System.Net.CookieContainer can hol...
CookieContainer(int capacity)
Initializes a new instance of the T:System.Net.CookieContainer class with a specified value for the n...
The exception that is thrown when one of the arguments provided to a method is not valid.
void Add(Uri uri, Cookie cookie)
Adds a T:System.Net.Cookie to the T:System.Net.CookieContainer for a particular URI.
string GetCookieHeader(Uri uri)
Gets the HTTP cookie header that contains the HTTP cookies that represent the T:System....
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
bool Secure
Gets or sets the security level of a T:System.Net.Cookie.
Specifies that the class can be serialized.
void SetCookies(Uri uri, string cookieHeader)
Adds T:System.Net.Cookie instances for one or more cookies from an HTTP cookie header to the T:System...
static readonly string SchemeDelimiter
Specifies the characters that separate the communication protocol scheme from the address portion of ...
Provides a set of properties and methods that are used to manage cookies. This class cannot be inheri...
int Count
Gets the number of T:System.Net.Cookie instances that a T:System.Net.CookieContainer currently holds.
void Add(CookieCollection cookies)
Adds the contents of a T:System.Net.CookieCollection to the T:System.Net.CookieContainer.
The exception that is thrown when an error is made adding a T:System.Net.Cookie to a T:System....
The exception that is thrown when a call is made to the M:System.Threading.Thread....
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
static readonly string UriSchemeHttps
Specifies that the URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS)....
Defines a dictionary key/value pair that can be set or retrieved.
string Host
Gets the host component of this instance.
void Add(Cookie cookie)
Adds a T:System.Net.Cookie to a T:System.Net.CookieContainer. This method uses the domain from the T:...