internal void Add(Cookie cookie, bool throwOnError) { if (cookie.Value.Length > this.m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.GetString("net_cookie_size", new object[] { cookie.ToString(), this.m_maxCookieSize })); } } else { try { PathList list = (PathList)this.m_domainTable[cookie.DomainKey]; if (list == null) { list = new PathList(); this.AddRemoveDomain(cookie.DomainKey, list); } int cookiesCount = list.GetCookiesCount(); CookieCollection cookies = (CookieCollection)list[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); list[cookie.Path] = cookies; } if (cookie.Expired) { lock (cookies) { int index = cookies.IndexOf(cookie); if (index != -1) { cookies.RemoveAt(index); this.m_count--; } return; } } if (((cookiesCount < this.m_maxCookiesPerDomain) || this.AgeCookies(cookie.DomainKey)) && ((this.m_count < this.m_maxCookies) || this.AgeCookies(null))) { lock (cookies) { this.m_count += cookies.InternalAdd(cookie, true); } } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } if (throwOnError) { throw new CookieException(SR.GetString("net_container_add_cookie"), exception); } } } }
private void AddRemoveDomain(string key, PathList value) { lock (this) { if (value == null) { this.m_domainTable.Remove(key); } else { this.m_domainTable[key] = value; } } }
private void AddRemoveDomain(string key, PathList value) { // Hashtable support multiple readers and one writer // Synchronize writers lock (m_domainTable.SyncRoot) { if (value == null) { m_domainTable.Remove(key); } else { m_domainTable[key] = value; } } }
private void AddRemoveDomain(string key, PathList value) { // Hashtable support multiple readers ans one writer // Synchronize writers (make them to be just one) lock (this) { if (value == null) { m_domainTable.Remove(key); } else { m_domainTable[key] = value; } } }
/// <summary>Gets a <see cref="CookieCollection"/> that contains all of the <see cref="Cookie"/> instances in the container.</summary> /// <returns>A <see cref="CookieCollection"/> that contains all of the <see cref="Cookie"/> instances in the container.</returns> public CookieCollection GetAllCookies() { var result = new CookieCollection(); lock (m_domainTable.SyncRoot) { IDictionaryEnumerator lists = m_domainTable.GetEnumerator(); while (lists.MoveNext()) { PathList list = (PathList)lists.Value !; lock (list.SyncRoot) { IDictionaryEnumerator collections = list.List.GetEnumerator(); while (collections.MoveNext()) { result.Add((CookieCollection)collections.Value !); } } } } return(result); }
internal void Add(Cookie cookie, bool throwOnError) { PathList?pathList; if (cookie.Value.Length > m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.Format(SR.net_cookie_size, cookie, m_maxCookieSize)); } return; } try { lock (m_domainTable.SyncRoot) { pathList = (PathList?)m_domainTable[cookie.DomainKey]; if (pathList == null) { m_domainTable[cookie.DomainKey] = (pathList = new PathList()); } } int domain_count = pathList.GetCookiesCount(); CookieCollection?cookies; lock (pathList.SyncRoot) { cookies = (CookieCollection?)pathList[cookie.Path] !; if (cookies == null) { cookies = new CookieCollection(); pathList[cookie.Path] = cookies; } } if (cookie.Expired) { // Explicit removal command (Max-Age == 0) lock (cookies) { int idx = cookies.IndexOf(cookie); if (idx != -1) { cookies.RemoveAt(idx); --m_count; } } } else { // This is about real cookie adding, check Capacity first if (domain_count >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) { return; // Cannot age: reject new cookie } else if (m_count >= m_maxCookies && !AgeCookies(null)) { return; // Cannot age: reject new cookie } // About to change the collection. lock (cookies) { m_count += cookies.InternalAdd(cookie, true); } } // We don't want to cleanup m_domaintable/m_list too often. Add check to avoid overhead. if (m_domainTable.Count > m_count || pathList.Count > m_maxCookiesPerDomain) { DomainTableCleanup(); } } catch (OutOfMemoryException) { throw; } catch (Exception e) { if (throwOnError) { throw new CookieException(SR.net_container_add_cookie, e); } } }
// This method is called *only* when cookie verification is done, so unlike with public // Add(Cookie cookie) the cookie is in a reasonable condition. internal void Add(Cookie cookie, bool throwOnError) { PathList pathList; if (cookie.Value.Length > _maxCookieSize) { if (throwOnError) { throw new CookieException(SR.Format(SR.net_cookie_size, cookie.ToString(), _maxCookieSize)); } return; } try { lock (_domainTable) { _domainTable.TryGetValue(cookie.DomainKey, out pathList); if (pathList == null) { pathList = new PathList(); AddRemoveDomain(cookie.DomainKey, pathList); } } int domain_count = pathList.GetCookiesCount(); CookieCollection cookies; lock (pathList.SyncRoot) { cookies = (CookieCollection)pathList[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); pathList[cookie.Path] = cookies; } } if (cookie.Expired) { // Explicit removal command (Max-Age == 0) lock (cookies) { int idx = cookies.IndexOf(cookie); if (idx != -1) { cookies.RemoveAt(idx); --_count; } } } else { // This is about real cookie adding, check Capacity first if (domain_count >= _maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) { return; // Cannot age: reject new cookie } else if (_count >= _maxCookies && !AgeCookies(null)) { return; // Cannot age: reject new cookie } // About to change the collection lock (cookies) { _count += cookies.InternalAdd(cookie, true); } } } catch (OutOfMemoryException) { throw; } catch (Exception e) { if (throwOnError) { throw new CookieException(SR.net_container_add_cookie, e); } } }
internal CookieCollection InternalGetCookies(Uri uri) { bool isSecure = (uri.Scheme == Uri.UriSchemeHttps); int port = uri.Port; CookieCollection cookies = new CookieCollection(); ArrayList nameKeys = new ArrayList(); int firstCompatibleVersion0SpecKey = 0; string fqdnRemote = uri.Host; int dot = fqdnRemote.IndexOf('.'); if (dot == -1) { // DNS.resolve may return short names even for other inet domains ;-( // We _don't_ know what the exact domain is, so try also grab short hostname cookies. nameKeys.Add(fqdnRemote); // grab long name from the local domain nameKeys.Add(fqdnRemote + m_fqdnMyDomain); // grab the local domain itself nameKeys.Add(m_fqdnMyDomain); firstCompatibleVersion0SpecKey = 3; } else { // grab the host itself nameKeys.Add(fqdnRemote); // grab the host domain nameKeys.Add(fqdnRemote.Substring(dot)); firstCompatibleVersion0SpecKey = 2; // The following block is only for compatibility with Version0 spec. // Still, we'll add only Plain-Variant cookies if found under below keys if (fqdnRemote.Length > 2) { // We ignore the '.' at the end on the name int last = fqdnRemote.LastIndexOf('.', fqdnRemote.Length - 2); //AND keys with <2 dots inside. if (last > 0) { last = fqdnRemote.LastIndexOf('.', last - 1); } if (last != -1) { while ((dot < last) && (dot = fqdnRemote.IndexOf('.', dot + 1)) != -1) { nameKeys.Add(fqdnRemote.Substring(dot)); } } } } foreach (string key in nameKeys) { bool found = false; bool defaultAdded = false; PathList pathList = (PathList)m_domainTable[key]; --firstCompatibleVersion0SpecKey; if (pathList == null) { continue; } foreach (DictionaryEntry entry in pathList) { string path = (string)entry.Key; if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path))) { found = true; CookieCollection cc = (CookieCollection)entry.Value; cc.TimeStamp(CookieCollection.Stamp.Set); MergeUpdateCollections(cookies, cc, port, isSecure, (firstCompatibleVersion0SpecKey < 0)); if (path == "/") { defaultAdded = true; } } else if (found) { break; } } if (!defaultAdded) { CookieCollection cc = (CookieCollection)pathList["/"]; if (cc != null) { cc.TimeStamp(CookieCollection.Stamp.Set); MergeUpdateCollections(cookies, cc, port, isSecure, (firstCompatibleVersion0SpecKey < 0)); } } // Remove unused domain // (This is the only place that does domain removal) if (pathList.Count == 0) { AddRemoveDomain(key, null); } } return(cookies); }
internal CookieCollection InternalGetCookies(Uri uri) { bool isSecure = uri.Scheme == Uri.UriSchemeHttps; int port = uri.Port; CookieCollection destination = new CookieCollection(); ArrayList arrayList = new ArrayList(); string host = uri.Host; int startIndex = host.IndexOf('.'); int num1; if (startIndex == -1) { arrayList.Add((object)host); arrayList.Add((object)("." + host)); if (this.m_fqdnMyDomain != null && this.m_fqdnMyDomain.Length != 0) { arrayList.Add((object)(host + this.m_fqdnMyDomain)); arrayList.Add((object)this.m_fqdnMyDomain); num1 = 3; } else { num1 = 1; } } else { arrayList.Add((object)host); arrayList.Add((object)("." + host)); arrayList.Add((object)host.Substring(startIndex)); num1 = 2; if (host.Length > 2) { int num2 = host.LastIndexOf('.', host.Length - 2); if (num2 > 0) { num2 = host.LastIndexOf('.', num2 - 1); } if (num2 != -1) { while (startIndex < num2 && (startIndex = host.IndexOf('.', startIndex + 1)) != -1) { arrayList.Add((object)host.Substring(startIndex)); } } } } foreach (string key in arrayList) { bool flag1 = false; bool flag2 = false; PathList pathList = (PathList)this.m_domainTable[(object)key]; --num1; if (pathList != null) { foreach (DictionaryEntry dictionaryEntry in pathList) { string str = (string)dictionaryEntry.Key; if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(str))) { flag1 = true; CookieCollection source = (CookieCollection)dictionaryEntry.Value; source.TimeStamp(CookieCollection.Stamp.Set); this.MergeUpdateCollections(destination, source, port, isSecure, num1 < 0); if (str == "/") { flag2 = true; } } else if (flag1) { break; } } if (!flag2) { CookieCollection source = (CookieCollection)pathList["/"]; if (source != null) { source.TimeStamp(CookieCollection.Stamp.Set); this.MergeUpdateCollections(destination, source, port, isSecure, num1 < 0); } } if (pathList.Count == 0) { this.AddRemoveDomain(key, (PathList)null); } } } return(destination); }
internal void Add(Cookie cookie, bool throwOnError) { if (cookie.Value.Length > this.m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.GetString("net_cookie_size", new object[] { cookie.ToString(), this.m_maxCookieSize })); } } else { try { PathList list = (PathList) this.m_domainTable[cookie.DomainKey]; if (list == null) { list = new PathList(); this.AddRemoveDomain(cookie.DomainKey, list); } int cookiesCount = list.GetCookiesCount(); CookieCollection cookies = (CookieCollection) list[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); list[cookie.Path] = cookies; } if (cookie.Expired) { lock (cookies) { int index = cookies.IndexOf(cookie); if (index != -1) { cookies.RemoveAt(index); this.m_count--; } return; } } if (((cookiesCount < this.m_maxCookiesPerDomain) || this.AgeCookies(cookie.DomainKey)) && ((this.m_count < this.m_maxCookies) || this.AgeCookies(null))) { lock (cookies) { this.m_count += cookies.InternalAdd(cookie, true); } } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } if (throwOnError) { throw new CookieException(SR.GetString("net_container_add_cookie"), exception); } } } }
private void AddRemoveDomain(string key, PathList value) { lock (this) { if (value == null) this.m_domainTable.Remove((object) key); else this.m_domainTable[(object) key] = (object) value; } }
internal void Add(Cookie cookie, bool throwOnError) { if (cookie.Value.Length > this.m_maxCookieSize) { if (!throwOnError) return; throw new CookieException(SR.GetString("net_cookie_size", (object) cookie.ToString(), (object) this.m_maxCookieSize)); } else { try { PathList pathList = (PathList) this.m_domainTable[(object) cookie.DomainKey]; if (pathList == null) { pathList = new PathList(); this.AddRemoveDomain(cookie.DomainKey, pathList); } int cookiesCount = pathList.GetCookiesCount(); CookieCollection cookieCollection = (CookieCollection) pathList[cookie.Path]; if (cookieCollection == null) { cookieCollection = new CookieCollection(); pathList[cookie.Path] = (object) cookieCollection; } if (cookie.Expired) { lock (cookieCollection) { int local_3 = cookieCollection.IndexOf(cookie); if (local_3 == -1) return; cookieCollection.RemoveAt(local_3); --this.m_count; } } else { if (cookiesCount >= this.m_maxCookiesPerDomain && !this.AgeCookies(cookie.DomainKey) || this.m_count >= this.m_maxCookies && !this.AgeCookies((string) null)) return; lock (cookieCollection) this.m_count += cookieCollection.InternalAdd(cookie, true); } } catch (Exception ex) { if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException) throw; else if (throwOnError) throw new CookieException(SR.GetString("net_container_add_cookie"), ex); } } }
// This method is called *only* when cookie verification is done, // so unlike with public Add(Cookie cookie) the cookie is in sane condition internal void Add(Cookie cookie, bool throwOnError) { PathList pathList; if (cookie.Value.Length > m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.GetString(SR.net_cookie_size, cookie.ToString(), m_maxCookieSize)); } return; } try { pathList = (PathList)m_domainTable[cookie.DomainKey]; if (pathList == null) { pathList = new PathList(); AddRemoveDomain(cookie.DomainKey, pathList); } int domain_count = pathList.GetCookiesCount(); CookieCollection cookies = (CookieCollection)pathList[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); pathList[cookie.Path] = cookies; } if(cookie.Expired) { //Explicit removal command (Max-Age == 0) lock (cookies) { int idx = cookies.IndexOf(cookie); if (idx != -1) { cookies.RemoveAt(idx); --m_count; } } } else { //This is about real cookie adding, check Capacity first if (domain_count >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) { return; //cannot age -> reject new cookie } else if (this.m_count >= m_maxCookies && !AgeCookies(null)) { return; //cannot age -> reject new cookie } //about to change the collection lock (cookies) { m_count += cookies.InternalAdd(cookie, true); } } } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (throwOnError) { throw new CookieException(SR.GetString(SR.net_container_add_cookie), e); } } catch { if (throwOnError) { throw new CookieException(SR.GetString(SR.net_container_add_cookie), new Exception(SR.GetString(SR.net_nonClsCompliantException))); } } }
private void AddRemoveDomain(string key, PathList value) { // Hashtable support multiple readers ans one writer // Synchronize writers (make them to be just one) lock(this) { if (value == null) { m_domainTable.Remove(key); } else { m_domainTable[key] = value; } } }
internal CookieCollection InternalGetCookies(Uri uri) { bool isSecure = uri.Scheme == Uri.UriSchemeHttps; int port = uri.Port; CookieCollection destination = new CookieCollection(); ArrayList list = new ArrayList(); int num2 = 0; string host = uri.Host; int index = host.IndexOf('.'); if (index == -1) { list.Add(host); list.Add("." + host); if ((this.m_fqdnMyDomain != null) && (this.m_fqdnMyDomain.Length != 0)) { list.Add(host + this.m_fqdnMyDomain); list.Add(this.m_fqdnMyDomain); num2 = 3; } else { num2 = 1; } } else { list.Add(host); list.Add("." + host); list.Add(host.Substring(index)); num2 = 2; if (host.Length > 2) { int num4 = host.LastIndexOf('.', host.Length - 2); if (num4 > 0) { num4 = host.LastIndexOf('.', num4 - 1); } if (num4 != -1) { while ((index < num4) && ((index = host.IndexOf('.', index + 1)) != -1)) { list.Add(host.Substring(index)); } } } } foreach (string str2 in list) { bool flag2 = false; bool flag3 = false; PathList list2 = (PathList)this.m_domainTable[str2]; num2--; if (list2 != null) { foreach (DictionaryEntry entry in list2) { string key = (string)entry.Key; if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(key))) { flag2 = true; CookieCollection source = (CookieCollection)entry.Value; source.TimeStamp(CookieCollection.Stamp.Set); this.MergeUpdateCollections(destination, source, port, isSecure, num2 < 0); if (key == "/") { flag3 = true; } } else if (flag2) { break; } } if (!flag3) { CookieCollection cookies3 = (CookieCollection)list2["/"]; if (cookies3 != null) { cookies3.TimeStamp(CookieCollection.Stamp.Set); this.MergeUpdateCollections(destination, cookies3, port, isSecure, num2 < 0); } } if (list2.Count == 0) { this.AddRemoveDomain(str2, null); } } } return(destination); }
// This method is called *only* when cookie verification is done, so unlike with public // Add(Cookie cookie) the cookie is in a reasonable condition. internal void Add(Cookie cookie, bool throwOnError) { PathList pathList; if (cookie.Value.Length > m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.Format(SR.net_cookie_size, cookie.ToString(), m_maxCookieSize)); } return; } try { lock (m_domainTable) { if (!m_domainTable.TryGetValue(cookie.DomainKey, out pathList)) { m_domainTable[cookie.DomainKey] = (pathList = PathList.Create()); } } int domain_count = pathList.GetCookiesCount(); CookieCollection cookies; lock (pathList.SyncRoot) { cookies = pathList[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); pathList[cookie.Path] = cookies; } } if (cookie.Expired) { // Explicit removal command (Max-Age == 0) lock (cookies) { int idx = cookies.IndexOf(cookie); if (idx != -1) { cookies.RemoveAt(idx); --m_count; } } } else { // This is about real cookie adding, check Capacity first if (domain_count >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) { return; // Cannot age: reject new cookie } else if (m_count >= m_maxCookies && !AgeCookies(null)) { return; // Cannot age: reject new cookie } // About to change the collection lock (cookies) { m_count += cookies.InternalAdd(cookie, true); } } } catch (OutOfMemoryException) { throw; } catch (Exception e) { if (throwOnError) { throw new CookieException(SR.net_container_add_cookie, e); } } }
// This method is called *only* when cookie verification is done, // so unlike with public Add(Cookie cookie) the cookie is in sane condition internal void Add(Cookie cookie, bool throwOnError) { PathList pathList; if (cookie.Value.Length > m_maxCookieSize) { if (throwOnError) { throw new CookieException(SR.GetString(SR.net_cookie_size, cookie.ToString(), m_maxCookieSize)); } return; } try { pathList = (PathList)m_domainTable[cookie.DomainKey]; if (pathList == null) { pathList = new PathList(); AddRemoveDomain(cookie.DomainKey, pathList); } int domain_count = pathList.GetCookiesCount(); CookieCollection cookies = (CookieCollection)pathList[cookie.Path]; if (cookies == null) { cookies = new CookieCollection(); pathList[cookie.Path] = cookies; } if (cookie.Expired) { //Explicit removal command (Max-Age == 0) lock (cookies) { int idx = cookies.IndexOf(cookie); if (idx != -1) { cookies.RemoveAt(idx); --m_count; } } } else { //This is about real cookie adding, check Capacity first if (domain_count >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) { return; //cannot age -> reject new cookie } else if (this.m_count >= m_maxCookies && !AgeCookies(null)) { return; //cannot age -> reject new cookie } //about to change the collection lock (cookies) { m_count += cookies.InternalAdd(cookie, true); } } } catch (Exception e) { if (throwOnError) { throw new CookieException(SR.GetString(SR.net_container_add_cookie), e); } } }
internal void Add(Cookie cookie, bool throwOnError) { if (cookie.Value.Length > this.m_maxCookieSize) { if (!throwOnError) { return; } throw new CookieException(SR.GetString("net_cookie_size", (object)cookie.ToString(), (object)this.m_maxCookieSize)); } else { try { PathList pathList = (PathList)this.m_domainTable[(object)cookie.DomainKey]; if (pathList == null) { pathList = new PathList(); this.AddRemoveDomain(cookie.DomainKey, pathList); } int cookiesCount = pathList.GetCookiesCount(); CookieCollection cookieCollection = (CookieCollection)pathList[cookie.Path]; if (cookieCollection == null) { cookieCollection = new CookieCollection(); pathList[cookie.Path] = (object)cookieCollection; } if (cookie.Expired) { lock (cookieCollection) { int local_3 = cookieCollection.IndexOf(cookie); if (local_3 == -1) { return; } cookieCollection.RemoveAt(local_3); --this.m_count; } } else { if (cookiesCount >= this.m_maxCookiesPerDomain && !this.AgeCookies(cookie.DomainKey) || this.m_count >= this.m_maxCookies && !this.AgeCookies((string)null)) { return; } lock (cookieCollection) this.m_count += cookieCollection.InternalAdd(cookie, true); } } catch (Exception ex) { if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException) { throw; } else if (throwOnError) { throw new CookieException(SR.GetString("net_container_add_cookie"), ex); } } } }