using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace System.Net
{
/// Provides a container for a collection of objects.
// Token: 0x02000208 RID: 520
[Serializable]
public sealed class CookieContainer
{
/// Initializes a new instance of the class.
// Token: 0x060011AC RID: 4524 RVA: 0x00033FE4 File Offset: 0x000321E4
public CookieContainer()
{
}
/// Initializes a new instance of the class with a specified value for the number of instances that the container can hold.
/// The number of instances that the can hold.
///
/// is less than or equal to zero.
// Token: 0x060011AD RID: 4525 RVA: 0x00034018 File Offset: 0x00032218
public CookieContainer(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentException("Must be greater than zero", "Capacity");
}
this.capacity = capacity;
}
/// Initializes a new instance of the class with specific properties.
/// The number of instances that the can hold.
/// The number of instances per domain.
/// The maximum size in bytes for any single in a .
///
/// is not equal to . and is less than or equal to zero or is greater than .
///
/// is less than or equal to zero.
// Token: 0x060011AE RID: 4526 RVA: 0x00034068 File Offset: 0x00032268
public CookieContainer(int capacity, int perDomainCapacity, int maxCookieSize)
: this(capacity)
{
if (perDomainCapacity != 2147483647 && (perDomainCapacity <= 0 || perDomainCapacity > capacity))
{
throw new ArgumentOutOfRangeException("perDomainCapacity", string.Format("PerDomainCapacity must be greater than {0} and less than {1}.", 0, capacity));
}
if (maxCookieSize <= 0)
{
throw new ArgumentException("Must be greater than zero", "MaxCookieSize");
}
this.perDomainCapacity = perDomainCapacity;
this.maxCookieSize = maxCookieSize;
}
/// Gets the number of instances that a currently holds.
/// The number of instances that a currently holds. This is the total of instances in all domains.
// Token: 0x170005CF RID: 1487
// (get) Token: 0x060011AF RID: 4527 RVA: 0x000340DC File Offset: 0x000322DC
public int Count
{
get
{
return (this.cookies != null) ? this.cookies.Count : 0;
}
}
/// Gets and sets the number of instances that a can hold.
/// The number of instances that a can hold. This is a hard limit and cannot be exceeded by adding a .
///
/// is less than or equal to zero or (value is less than and is not equal to ).
// Token: 0x170005D0 RID: 1488
// (get) Token: 0x060011B0 RID: 4528 RVA: 0x000340FC File Offset: 0x000322FC
// (set) Token: 0x060011B1 RID: 4529 RVA: 0x00034104 File Offset: 0x00032304
public int Capacity
{
get
{
return this.capacity;
}
set
{
if (value < 0 || (value < this.perDomainCapacity && this.perDomainCapacity != 2147483647))
{
throw new ArgumentOutOfRangeException("value", string.Format("Capacity must be greater than {0} and less than {1}.", 0, this.perDomainCapacity));
}
this.capacity = value;
}
}
/// Represents the maximum allowed length of a .
/// The maximum allowed length, in bytes, of a .
///
/// is less than or equal to zero.
// Token: 0x170005D1 RID: 1489
// (get) Token: 0x060011B2 RID: 4530 RVA: 0x00034164 File Offset: 0x00032364
// (set) Token: 0x060011B3 RID: 4531 RVA: 0x0003416C File Offset: 0x0003236C
public int MaxCookieSize
{
get
{
return this.maxCookieSize;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException("value");
}
this.maxCookieSize = value;
}
}
/// Gets and sets the number of instances that a can hold per domain.
/// The number of instances that are allowed per domain.
///
/// is less than or equal to zero. -or- is greater than the maximum allowable number of cookies instances, 300, and is not equal to ).
// Token: 0x170005D2 RID: 1490
// (get) Token: 0x060011B4 RID: 4532 RVA: 0x00034188 File Offset: 0x00032388
// (set) Token: 0x060011B5 RID: 4533 RVA: 0x00034190 File Offset: 0x00032390
public int PerDomainCapacity
{
get
{
return this.perDomainCapacity;
}
set
{
if (value != 2147483647 && (value <= 0 || value > this.capacity))
{
throw new ArgumentOutOfRangeException("value");
}
this.perDomainCapacity = value;
}
}
/// Adds a to a . This method uses the domain from the to determine which domain collection to associate the with.
/// The to be added to the .
///
/// is null.
/// The domain for is null or the empty string ("").
///
/// is larger than . -or- the domain for is not a valid URI.
///
///
///
// Token: 0x060011B6 RID: 4534 RVA: 0x000341D0 File Offset: 0x000323D0
public void Add(Cookie cookie)
{
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
if (cookie.Domain.Length == 0)
{
throw new ArgumentException("Cookie domain not set.", "cookie.Domain");
}
if (cookie.Value.Length > this.maxCookieSize)
{
throw new CookieException("value is larger than MaxCookieSize.");
}
this.AddCookie(new Cookie(cookie.Name, cookie.Value)
{
Path = ((cookie.Path.Length != 0) ? cookie.Path : "/"),
Domain = cookie.Domain,
ExactDomain = cookie.ExactDomain,
Version = cookie.Version
});
}
// Token: 0x060011B7 RID: 4535 RVA: 0x00034294 File Offset: 0x00032494
private void AddCookie(Cookie cookie)
{
if (this.cookies == null)
{
this.cookies = new CookieCollection();
}
if (this.cookies.Count >= this.capacity)
{
this.RemoveOldest(null);
}
if (this.cookies.Count >= this.perDomainCapacity && this.CountDomain(cookie.Domain) >= this.perDomainCapacity)
{
this.RemoveOldest(cookie.Domain);
}
Cookie cookie2 = new Cookie(cookie.Name, cookie.Value);
cookie2.Path = ((cookie.Path.Length != 0) ? cookie.Path : "/");
cookie2.Domain = cookie.Domain;
cookie2.ExactDomain = cookie.ExactDomain;
cookie2.Version = cookie.Version;
cookie2.Expires = cookie.Expires;
cookie2.CommentUri = cookie.CommentUri;
cookie2.Comment = cookie.Comment;
cookie2.Discard = cookie.Discard;
cookie2.HttpOnly = cookie.HttpOnly;
cookie2.Secure = cookie.Secure;
this.cookies.Add(cookie2);
this.CheckExpiration();
}
// Token: 0x060011B8 RID: 4536 RVA: 0x000343C4 File Offset: 0x000325C4
private int CountDomain(string domain)
{
int num = 0;
foreach (object obj in this.cookies)
{
Cookie cookie = (Cookie)obj;
if (CookieContainer.CheckDomain(domain, cookie.Domain, true))
{
num++;
}
}
return num;
}
// Token: 0x060011B9 RID: 4537 RVA: 0x00034448 File Offset: 0x00032648
private void RemoveOldest(string domain)
{
int num = 0;
DateTime dateTime = DateTime.MaxValue;
for (int i = 0; i < this.cookies.Count; i++)
{
Cookie cookie = this.cookies[i];
if (cookie.TimeStamp < dateTime && (domain == null || domain == cookie.Domain))
{
dateTime = cookie.TimeStamp;
num = i;
}
}
this.cookies.List.RemoveAt(num);
}
// Token: 0x060011BA RID: 4538 RVA: 0x000344C8 File Offset: 0x000326C8
private void CheckExpiration()
{
if (this.cookies == null)
{
return;
}
for (int i = this.cookies.Count - 1; i >= 0; i--)
{
Cookie cookie = this.cookies[i];
if (cookie.Expired)
{
this.cookies.List.RemoveAt(i);
}
}
}
/// Adds the contents of a to the .
/// The to be added to the .
///
/// is null.
///
///
///
// Token: 0x060011BB RID: 4539 RVA: 0x00034528 File Offset: 0x00032728
public void Add(CookieCollection cookies)
{
if (cookies == null)
{
throw new ArgumentNullException("cookies");
}
foreach (object obj in cookies)
{
Cookie cookie = (Cookie)obj;
this.Add(cookie);
}
}
// Token: 0x060011BC RID: 4540 RVA: 0x000345A4 File Offset: 0x000327A4
private void Cook(global::System.Uri uri, Cookie cookie)
{
if (CookieContainer.IsNullOrEmpty(cookie.Name))
{
throw new CookieException("Invalid cookie: name");
}
if (cookie.Value == null)
{
throw new CookieException("Invalid cookie: value");
}
if (uri != null && cookie.Domain.Length == 0)
{
cookie.Domain = uri.Host;
}
if (cookie.Version == 0 && CookieContainer.IsNullOrEmpty(cookie.Path))
{
if (uri != null)
{
cookie.Path = uri.AbsolutePath;
}
else
{
cookie.Path = "/";
}
}
if (cookie.Port.Length == 0 && uri != null && !uri.IsDefaultPort)
{
cookie.Port = "\"" + uri.Port.ToString() + "\"";
}
}
/// Adds a to the for a particular URI.
/// The URI of the to be added to the .
/// The to be added to the .
///
/// is null or is null.
///
/// is larger than . -or- The domain for is not a valid URI.
///
///
///
// Token: 0x060011BD RID: 4541 RVA: 0x00034698 File Offset: 0x00032898
public void Add(global::System.Uri uri, Cookie cookie)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
if (cookie == null)
{
throw new ArgumentNullException("cookie");
}
if (!cookie.Expired)
{
this.Cook(uri, cookie);
this.AddCookie(cookie);
}
}
/// Adds the contents of a to the for a particular URI.
/// The URI of the to be added to the .
/// The to be added to the .
///
/// is null.
/// The domain for one of the cookies in is null.
/// One of the cookies in contains an invalid domain.
///
///
///
// Token: 0x060011BE RID: 4542 RVA: 0x000346E8 File Offset: 0x000328E8
public void Add(global::System.Uri uri, CookieCollection cookies)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
if (cookies == null)
{
throw new ArgumentNullException("cookies");
}
foreach (object obj in cookies)
{
Cookie cookie = (Cookie)obj;
if (!cookie.Expired)
{
this.Cook(uri, cookie);
this.AddCookie(cookie);
}
}
}
/// Gets the HTTP cookie header that contains the HTTP cookies that represent the instances that are associated with a specific URI.
/// An HTTP cookie header, with strings representing instances delimited by semicolons.
/// The URI of the instances desired.
///
/// is null.
///
///
///
// Token: 0x060011BF RID: 4543 RVA: 0x00034790 File Offset: 0x00032990
public string GetCookieHeader(global::System.Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
CookieCollection cookieCollection = this.GetCookies(uri);
if (cookieCollection.Count == 0)
{
return string.Empty;
}
StringBuilder stringBuilder = new StringBuilder();
foreach (object obj in cookieCollection)
{
Cookie cookie = (Cookie)obj;
stringBuilder.Append(cookie.ToString(uri));
stringBuilder.Append("; ");
}
if (stringBuilder.Length > 0)
{
stringBuilder.Length -= 2;
}
return stringBuilder.ToString();
}
// Token: 0x060011C0 RID: 4544 RVA: 0x00034868 File Offset: 0x00032A68
private static bool CheckDomain(string domain, string host, bool exact)
{
if (domain.Length == 0)
{
return false;
}
if (exact)
{
return string.Compare(host, domain, true, CultureInfo.InvariantCulture) == 0;
}
if (!CultureInfo.InvariantCulture.CompareInfo.IsSuffix(host, domain, CompareOptions.IgnoreCase))
{
return false;
}
if (domain[0] == '.')
{
return true;
}
int num = host.Length - domain.Length - 1;
return num >= 0 && host[num] == '.';
}
/// Gets a that contains the instances that are associated with a specific URI.
/// A that contains the instances that are associated with a specific URI.
/// The URI of the instances desired.
///
/// is null.
///
///
///
// Token: 0x060011C1 RID: 4545 RVA: 0x000348E8 File Offset: 0x00032AE8
public CookieCollection GetCookies(global::System.Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
this.CheckExpiration();
CookieCollection cookieCollection = new CookieCollection();
if (this.cookies == null)
{
return cookieCollection;
}
foreach (object obj in this.cookies)
{
Cookie cookie = (Cookie)obj;
string domain = cookie.Domain;
if (CookieContainer.CheckDomain(domain, uri.Host, cookie.ExactDomain))
{
if (cookie.Port.Length <= 0 || cookie.Ports == null || uri.Port == -1 || Array.IndexOf(cookie.Ports, uri.Port) != -1)
{
string path = cookie.Path;
string absolutePath = uri.AbsolutePath;
if (path != string.Empty && path != "/" && absolutePath != path)
{
if (!absolutePath.StartsWith(path))
{
continue;
}
if (path[path.Length - 1] != '/' && absolutePath.Length > path.Length && absolutePath[path.Length] != '/')
{
continue;
}
}
if (!cookie.Secure || !(uri.Scheme != "https"))
{
cookieCollection.Add(cookie);
}
}
}
}
cookieCollection.Sort();
return cookieCollection;
}
/// Adds instances for one or more cookies from an HTTP cookie header to the for a specific URI.
/// The URI of the .
/// The contents of an HTTP set-cookie header as returned by a HTTP server, with instances delimited by commas.
///
/// is null.
///
/// is null.
/// One of the cookies is invalid. -or- An error occurred while adding one of the cookies to the container.
// Token: 0x060011C2 RID: 4546 RVA: 0x00034AB4 File Offset: 0x00032CB4
public void SetCookies(global::System.Uri uri, string cookieHeader)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
if (cookieHeader == null)
{
throw new ArgumentNullException("cookieHeader");
}
if (cookieHeader.Length == 0)
{
return;
}
string[] array = cookieHeader.Split(new char[] { ',' });
for (int i = 0; i < array.Length; i++)
{
string text = array[i];
if (array.Length > i + 1 && global::System.Text.RegularExpressions.Regex.IsMatch(array[i], ".*expires\\s*=\\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)", global::System.Text.RegularExpressions.RegexOptions.IgnoreCase) && global::System.Text.RegularExpressions.Regex.IsMatch(array[i + 1], "\\s\\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{4} \\d{2}:\\d{2}:\\d{2} GMT", global::System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
text = new StringBuilder(text).Append(",").Append(array[++i]).ToString();
}
try
{
Cookie cookie = CookieContainer.Parse(text);
if (cookie.Path.Length == 0)
{
cookie.Path = uri.AbsolutePath;
}
else if (!uri.AbsolutePath.StartsWith(cookie.Path))
{
string text2 = string.Format("'Path'='{0}' is invalid with URI", cookie.Path);
throw new CookieException(text2);
}
if (cookie.Domain.Length == 0)
{
cookie.Domain = uri.Host;
cookie.ExactDomain = true;
}
this.AddCookie(cookie);
}
catch (Exception ex)
{
string text3 = string.Format("Could not parse cookies for '{0}'.", uri);
throw new CookieException(text3, ex);
}
}
}
// Token: 0x060011C3 RID: 4547 RVA: 0x00034C38 File Offset: 0x00032E38
private static Cookie Parse(string s)
{
string[] array = s.Split(new char[] { ';' });
Cookie cookie = new Cookie();
int i = 0;
while (i < array.Length)
{
int num = array[i].IndexOf('=');
string text;
string text2;
if (num == -1)
{
text = array[i].Trim();
text2 = string.Empty;
}
else
{
text = array[i].Substring(0, num).Trim();
text2 = array[i].Substring(num + 1).Trim();
}
string text3 = text.ToLower(CultureInfo.InvariantCulture);
if (text3 == null)
{
goto IL_1C4;
}
if (CookieContainer.<>f__switch$map4 == null)
{
CookieContainer.<>f__switch$map4 = new Dictionary(8)
{
{ "path", 0 },
{ "$path", 0 },
{ "domain", 1 },
{ "$domain", 1 },
{ "expires", 2 },
{ "$expires", 2 },
{ "httponly", 3 },
{ "secure", 4 }
};
}
int num2;
if (!CookieContainer.<>f__switch$map4.TryGetValue(text3, out num2))
{
goto IL_1C4;
}
switch (num2)
{
case 0:
if (cookie.Path.Length == 0)
{
cookie.Path = text2;
}
break;
case 1:
if (cookie.Domain.Length == 0)
{
cookie.Domain = text2;
cookie.ExactDomain = false;
}
break;
case 2:
if (cookie.Expires == DateTime.MinValue)
{
cookie.Expires = DateTime.SpecifyKind(DateTime.ParseExact(text2, "ddd, dd-MMM-yyyy HH:mm:ss G\\MT", CultureInfo.InvariantCulture), DateTimeKind.Utc);
}
break;
case 3:
cookie.HttpOnly = true;
break;
case 4:
cookie.Secure = true;
break;
default:
goto IL_1C4;
}
IL_1E8:
i++;
continue;
IL_1C4:
if (cookie.Name.Length == 0)
{
cookie.Name = text;
cookie.Value = text2;
}
goto IL_1E8;
}
return cookie;
}
// Token: 0x060011C4 RID: 4548 RVA: 0x00034E3C File Offset: 0x0003303C
private static bool IsNullOrEmpty(string s)
{
return s == null || s.Length == 0;
}
/// Represents the default maximum size, in bytes, of the instances that the can hold. This field is constant.
// Token: 0x04000F8C RID: 3980
public const int DefaultCookieLengthLimit = 4096;
/// Represents the default maximum number of instances that the can hold. This field is constant.
// Token: 0x04000F8D RID: 3981
public const int DefaultCookieLimit = 300;
/// Represents the default maximum number of instances that the can reference per domain. This field is constant.
// Token: 0x04000F8E RID: 3982
public const int DefaultPerDomainCookieLimit = 20;
// Token: 0x04000F8F RID: 3983
private int capacity = 300;
// Token: 0x04000F90 RID: 3984
private int perDomainCapacity = 20;
// Token: 0x04000F91 RID: 3985
private int maxCookieSize = 4096;
// Token: 0x04000F92 RID: 3986
private CookieCollection cookies;
}
}