System.Net.CookieContainer.InternalGetCookies C# (CSharp) Method

InternalGetCookies() private method

private InternalGetCookies ( Uri uri ) : CookieCollection
uri Uri
return CookieCollection
        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
                if (m_fqdnMyDomain != null && m_fqdnMyDomain.Length != 0) {
                    nameKeys.Add(fqdnRemote + m_fqdnMyDomain);
                    // grab the local domain itself
                    nameKeys.Add(m_fqdnMyDomain);
                    firstCompatibleVersion0SpecKey = 3;
                }
                else {
                    firstCompatibleVersion0SpecKey = 1;
                }
            }
            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;
        }