TimeStamp() private méthode

private TimeStamp ( Stamp how ) : DateTime
how Stamp
Résultat DateTime
Exemple #1
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, CookieCollection cookies, List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                bool     found        = false;
                bool     defaultAdded = false;
                PathList pathList;
                lock (m_domainTable.SyncRoot) {
                    pathList = (PathList)m_domainTable[domainAttribute[i]];
                }

                if (pathList == null)
                {
                    continue;
                }

                lock (pathList.SyncRoot) {
                    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, matchOnlyPlainCookie);

                            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, matchOnlyPlainCookie);
                    }
                }

                // Remove unused domain
                // (This is the only place that does domain removal)
                if (pathList.Count == 0)
                {
                    AddRemoveDomain(domainAttribute[i], null);
                }
            }
        }
Exemple #2
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection cookies, System.Collections.Generic.List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                PathList pathList;
                lock (m_domainTable.SyncRoot)
                {
                    pathList = (PathList)m_domainTable[domainAttribute[i]];
                    if (pathList == null)
                    {
                        continue;
                    }
                }

                lock (pathList.SyncRoot)
                {
                    // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations.
                    IDictionaryEnumerator e = pathList.GetEnumerator();
                    while (e.MoveNext())
                    {
                        string path = (string)e.Key;
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path)))
                        {
                            CookieCollection cc = (CookieCollection)e.Value;
                            cc.TimeStamp(CookieCollection.Stamp.Set);
                            MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);
                        }
                    }
                }

                // Remove unused domain
                // (This is the only place that does domain removal)
                if (pathList.Count == 0)
                {
                    lock (m_domainTable.SyncRoot)
                    {
                        m_domainTable.Remove(domainAttribute[i]);
                    }
                }
            }
        }
Exemple #3
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection?cookies, System.Collections.Generic.List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                PathList pathList;
                lock (m_domainTable.SyncRoot)
                {
                    pathList = (PathList)m_domainTable[domainAttribute[i]] !;
                    if (pathList == null)
                    {
                        continue;
                    }
                }

                lock (pathList.SyncRoot)
                {
                    SortedList list      = pathList.List;
                    int        listCount = list.Count;
                    for (int e = 0; e < listCount; e++)
                    {
                        string path = (string)list.GetKey(e);
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path), StringComparison.Ordinal))
                        {
                            CookieCollection cc = (CookieCollection)list.GetByIndex(e) !;
                            cc.TimeStamp(CookieCollection.Stamp.Set);
                            MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);
                        }
                    }
                }

                // Remove unused domain.
                if (pathList.Count == 0)
                {
                    lock (m_domainTable.SyncRoot)
                    {
                        m_domainTable.Remove(domainAttribute[i]);
                    }
                }
            }
        }
Exemple #4
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection cookies, List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                bool     found        = false;
                bool     defaultAdded = false;
                PathList pathList;
                lock (m_domainTable)
                {
                    if (!m_domainTable.TryGetValue(domainAttribute[i], out pathList))
                    {
                        continue;
                    }
                }

                lock (pathList.SyncRoot)
                {
                    foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                    {
                        string path = pair.Key;
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path)))
                        {
                            found = true;

                            CookieCollection cc = pair.Value;
                            cc.TimeStamp(CookieCollection.Stamp.Set);
                            MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);

                            if (path == "/")
                            {
                                defaultAdded = true;
                            }
                        }
                        else if (found)
                        {
                            break;
                        }
                    }
                }

                if (!defaultAdded)
                {
                    CookieCollection cc = pathList["/"];

                    if (cc != null)
                    {
                        cc.TimeStamp(CookieCollection.Stamp.Set);
                        MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);
                    }
                }

                // Remove unused domain
                // (This is the only place that does domain removal)
                if (pathList.Count == 0)
                {
                    lock (m_domainTable)
                    {
                        m_domainTable.Remove(domainAttribute[i]);
                    }
                }
            }
        }
Exemple #5
0
        // This function, when called, must delete at least one cookie.
        // If there are expired cookies in given scope they are cleaned up.
        // If nothing is found the least used Collection will be found and removed
        // from the container.
        //
        // Also note that expired cookies are also removed during request preparation
        // (this.GetCookies method).
        //
        // Param. 'domain' == null means to age in the whole container.
        private bool AgeCookies(string domain)
        {
            Debug.Assert(m_maxCookies != 0);
            Debug.Assert(m_maxCookiesPerDomain != 0);

            int      removed = 0;
            DateTime oldUsed = DateTime.MaxValue;
            DateTime tempUsed;

            CookieCollection lruCc      = null;
            string           lruDomain  = null;
            string           tempDomain = null;

            PathList pathList;
            int      domain_count      = 0;
            int      itemp             = 0;
            float    remainingFraction = 1.0F;

            // The container was shrunk, might need additional cleanup for each domain
            if (m_count > m_maxCookies)
            {
                // Means the fraction of the container to be left.
                // Each domain will be cut accordingly.
                remainingFraction = (float)m_maxCookies / (float)m_count;
            }
            lock (m_domainTable)
            {
                foreach (KeyValuePair <string, PathList> entry in m_domainTable)
                {
                    if (domain == null)
                    {
                        tempDomain = entry.Key;
                        pathList   = entry.Value; // Aliasing to trick foreach
                    }
                    else
                    {
                        tempDomain = domain;
                        m_domainTable.TryGetValue(domain, out pathList);
                    }

                    domain_count = 0; // Cookies in the domain
                    lock (pathList.SyncRoot)
                    {
                        foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                        {
                            CookieCollection cc = pair.Value;
                            itemp         = ExpireCollection(cc);
                            removed      += itemp;
                            m_count      -= itemp; // Update this container's count
                            domain_count += cc.Count;

                            // We also find the least used cookie collection in ENTIRE container.
                            // We count the collection as LRU only if it holds 1+ elements.
                            if (cc.Count > 0 && (tempUsed = cc.TimeStamp(CookieCollection.Stamp.Check)) < oldUsed)
                            {
                                lruDomain = tempDomain;
                                lruCc     = cc;
                                oldUsed   = tempUsed;
                            }
                        }
                    }

                    // Check if we have reduced to the limit of the domain by expiration only.
                    int min_count = Math.Min((int)(domain_count * remainingFraction), Math.Min(m_maxCookiesPerDomain, m_maxCookies) - 1);
                    if (domain_count > min_count)
                    {
                        // This case requires sorting all domain collections by timestamp.
                        KeyValuePair <DateTime, CookieCollection>[] cookies;
                        lock (pathList.SyncRoot)
                        {
                            cookies = new KeyValuePair <DateTime, CookieCollection> [pathList.Count];
                            foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                            {
                                CookieCollection cc = pair.Value;
                                cookies[itemp] = new KeyValuePair <DateTime, CookieCollection>(cc.TimeStamp(CookieCollection.Stamp.Check), cc);
                                ++itemp;
                            }
                        }
                        Array.Sort(cookies, (a, b) => a.Key.CompareTo(b.Key));

                        itemp = 0;
                        for (int i = 0; i < cookies.Length; ++i)
                        {
                            CookieCollection cc = cookies[i].Value;

                            lock (cc)
                            {
                                while (domain_count > min_count && cc.Count > 0)
                                {
                                    cc.RemoveAt(0);
                                    --domain_count;
                                    --m_count;
                                    ++removed;
                                }
                            }
                            if (domain_count <= min_count)
                            {
                                break;
                            }
                        }

                        if (domain_count > min_count && domain != null)
                        {
                            // Cannot complete aging of explicit domain (no cookie adding allowed).
                            return(false);
                        }
                    }
                }
            }

            // We have completed aging of the specified domain.
            if (domain != null)
            {
                return(true);
            }

            // The rest is for entire container aging.
            // We must get at least one free slot.

            // Don't need to apply LRU if we already cleaned something.
            if (removed != 0)
            {
                return(true);
            }

            if (oldUsed == DateTime.MaxValue)
            {
                // Something strange. Either capacity is 0 or all collections are locked with cc.Used.
                return(false);
            }

            // Remove oldest cookies from the least used collection.
            lock (lruCc)
            {
                while (m_count >= m_maxCookies && lruCc.Count > 0)
                {
                    lruCc.RemoveAt(0);
                    --m_count;
                }
            }
            return(true);
        }
        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);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        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);
        }