internal static IEnumerable<string> GetCookiesFromHeader(string setCookieHeader) 
        {
            List<string> cookieStrings = new List<string>();
            
            try
            {
                CookieParser parser = new CookieParser(setCookieHeader);
                string cookieString;

                while ((cookieString = parser.GetString()) != null)
                {
                    cookieStrings.Add(cookieString);
                }
            }
            catch (InternalCookieException)
            {
                // TODO: We should log this.  But there isn't much we can do about it other
                // than to drop the rest of the cookies.
            }
            
            return cookieStrings;
        }
Exemple #2
0
            int IComparer <string> .Compare(string x, string y)
            {
                string pathLeft  = CookieParser.CheckQuoted(x);
                string pathRight = CookieParser.CheckQuoted(y);
                int    ll        = pathLeft.Length;
                int    lr        = pathRight.Length;
                int    length    = Math.Min(ll, lr);

                for (int i = 0; i < length; ++i)
                {
                    if (pathLeft[i] != pathRight[i])
                    {
                        return(pathLeft[i] - pathRight[i]);
                    }
                }
                return(lr - ll);
            }
Exemple #3
0
            int IComparer.Compare(object ol, object or)
            {
                string pathLeft  = CookieParser.CheckQuoted((string)ol);
                string pathRight = CookieParser.CheckQuoted((string)or);
                int    ll        = pathLeft.Length;
                int    lr        = pathRight.Length;
                int    length    = Math.Min(ll, lr);

                for (int i = 0; i < length; ++i)
                {
                    if (pathLeft[i] != pathRight[i])
                    {
                        return(pathLeft[i] - pathRight[i]);
                    }
                }
                return(lr - ll);
            }
        private CookieCollection ParseCookies(Uri uri, string setCookieHeader)
        {
            Cookie           cookie;
            CookieCollection cookies = new CookieCollection();
            CookieParser     parser  = new CookieParser(setCookieHeader);

Label_000D:
            cookie = parser.GetServer();
            if (cookie != null)
            {
                if (cookie.Name.Length != 0)
                {
                    cookies.InternalAdd(cookie, true);
                }
                goto Label_000D;
            }
            return(cookies);
        }
Exemple #5
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 #6
0
            int IComparer.Compare(object ol, object or)
            {
                //Console.WriteLine("PathListComparer.Compare(" + ol + ", " + or + ")");

                string pathLeft  = CookieParser.CheckQuoted((string)ol);
                string pathRight = CookieParser.CheckQuoted((string)or);
                int    ll        = pathLeft.Length;
                int    lr        = pathRight.Length;
                int    length    = Math.Min(ll, lr);

                for (int i = 0; i < length; ++i)
                {
                    if (pathLeft[i] != pathRight[i])
                    {
                        //Console.WriteLine(" returning " + (pathLeft[i] - pathRight[i]));
                        return(pathLeft[i] - pathRight[i]);
                    }
                }
                //Console.WriteLine(" returning " + (lr - ll));
                return(lr - ll);
            }
Exemple #7
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]);
                    }
                }
            }
        }
        internal static IEnumerable <string> GetCookiesFromHeader(string setCookieHeader)
        {
            List <string> cookieStrings = new List <string>();

            try
            {
                CookieParser parser = new CookieParser(setCookieHeader);
                string       cookieString;

                while ((cookieString = parser.GetString()) != null)
                {
                    cookieStrings.Add(cookieString);
                }
            }
            catch (InternalCookieException)
            {
                // TODO: We should log this.  But there isn't much we can do about it other
                // than to drop the rest of the cookies.
            }

            return(cookieStrings);
        }
Exemple #9
0
        void SetCookie(string header)
        {
            if (cookieCollection == null)
            {
                cookieCollection = new CookieCollection();
            }

            var parser = new CookieParser(header);

            foreach (var cookie in parser.Parse())
            {
                if (cookie.Domain == "")
                {
                    cookie.Domain = uri.Host;
                }

                cookieCollection.Add(cookie);
                if (cookie_container != null)
                {
                    cookie_container.Add(uri, cookie);
                }
            }
        }
 private CookieCollection ParseCookies(Uri uri, string setCookieHeader)
 {
     Cookie cookie;
     CookieCollection cookies = new CookieCollection();
     CookieParser parser = new CookieParser(setCookieHeader);
 Label_000D:
     cookie = parser.GetServer();
     if (cookie != null)
     {
         if (cookie.Name.Length != 0)
         {
             cookies.InternalAdd(cookie, true);
         }
         goto Label_000D;
     }
     return cookies;
 }
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            GlobalLog.Print("CookieContainer#" + Logging.HashString(this) + "::CookieCutter() uri:" + uri + " headerName:" + headerName + " setCookieHeader:" + setCookieHeader + " isThrow:" + isThrow);
            CookieCollection cookies = new CookieCollection();
            CookieVariant variant = CookieVariant.Unknown;
            if (headerName == null)
            {
                variant = CookieVariant.Default;
            }
            else
            {
                for (int i = 0; i < s_headerInfo.Length; ++i)
                {
                    if ((String.Compare(headerName, s_headerInfo[i].Name, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        variant = s_headerInfo[i].Variant;
                    }
                }
            }

            bool isLocalDomain = IsLocalDomain(uri.Host);
            try
            {
                CookieParser parser = new CookieParser(setCookieHeader);
                do
                {
                    Cookie cookie = parser.Get();
                    GlobalLog.Print("CookieContainer#" + Logging.HashString(this) + "::CookieCutter() CookieParser returned cookie:" + Logging.ObjectToString(cookie));
                    if (cookie == null)
                    {
                        break;
                    }

                    // Parser marks invalid cookies this way
                    if (String.IsNullOrEmpty(cookie.Name))
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.net_cookie_format);
                        }
                        // Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // This will set the default values from the response URI
                    // AND will check for cookie validity
                    if (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, _fqdnMyDomain, true, isThrow))
                    {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);
                } while (true);
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (isThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies)
            {
                Add(c, isThrow);
            }

            return cookies;
        }
 private CookieCollection ParseCookies(Uri uri, string setCookieHeader) {
     GlobalLog.Print("HttpListenerRequest#" + ValidationHelper.HashString(this) + "::ParseCookies() uri:" + uri + " setCookieHeader:" + setCookieHeader);
     CookieCollection cookies = new CookieCollection();
     CookieParser parser = new CookieParser(setCookieHeader);
     for (;;) {
         Cookie cookie = parser.GetServer();
         GlobalLog.Print("HttpListenerRequest#" + ValidationHelper.HashString(this) + "::ParseCookies() CookieParser returned cookie:" + ValidationHelper.ToString(cookie));
         if (cookie==null) {
             // EOF, done.
             break;
         }
         if (cookie.Name.Length==0) {
             continue;
         }
         cookies.InternalAdd(cookie, true);
     }
     return cookies;
 }
Exemple #13
0
		void SetCookie (string header)
		{
			if (cookieCollection == null)
				cookieCollection = new CookieCollection ();

			var parser = new CookieParser (header);
			foreach (var cookie in parser.Parse ()) {
				if (cookie.Domain == "") {
					cookie.Domain = uri.Host;
					cookie.HasDomain = false;
				}

				if (cookie.HasDomain &&
				    !CookieContainer.CheckSameOrigin (uri, cookie.Domain))
					continue;

				cookieCollection.Add (cookie);
				if (cookie_container != null)
					cookie_container.Add (uri, cookie);
			}
		}
Exemple #14
0
        void ProcessArg(Cookie cookie, string name, string val)
        {
            if ((name == null) || (name == string.Empty))
            {
                throw new InvalidOperationException();
            }

            name = name.ToUpper();
            switch (name)
            {
            case "COMMENT":
                if (cookie.Comment == null)
                {
                    cookie.Comment = val;
                }
                break;

            case "COMMENTURL":
                if (cookie.CommentUri == null)
                {
                    cookie.CommentUri = new Uri(val);
                }
                break;

            case "DISCARD":
                cookie.Discard = true;
                break;

            case "DOMAIN":
                if (cookie.Domain == "")
                {
                    cookie.Domain = val;
                }
                break;

            case "HTTPONLY":
                cookie.HttpOnly = true;
                break;

            case "MAX-AGE":             // RFC Style Set-Cookie2
                if (cookie.Expires == DateTime.MinValue)
                {
                    try {
                        cookie.Expires = cookie.TimeStamp.AddSeconds(UInt32.Parse(val));
                    } catch {}
                }
                break;

            case "EXPIRES":             // Netscape Style Set-Cookie
                if (cookie.Expires != DateTime.MinValue)
                {
                    break;
                }

                if ((pos < length) && (header [pos] == ',') && IsWeekDay(val))
                {
                    pos++;
                    val = val + ", " + GetCookieValue();
                }

                cookie.Expires = CookieParser.TryParseCookieExpires(val);
                break;

            case "PATH":
                cookie.Path = val;
                break;

            case "PORT":
                if (cookie.Port == null)
                {
                    cookie.Port = val;
                }
                break;

            case "SECURE":
                cookie.Secure = true;
                break;

            case "VERSION":
                try {
                    cookie.Version = (int)UInt32.Parse(val);
                } catch {}
                break;
            }
        }
Exemple #15
0
        internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDomain, string localDomain, bool set_default, bool isThrow)
        {
            string host         = uri.Host;
            int    port         = uri.Port;
            string absolutePath = uri.AbsolutePath;
            bool   flag         = true;

            if (set_default)
            {
                if (this.Version == 0)
                {
                    variant = CookieVariant.Plain;
                }
                else if ((this.Version == 1) && (variant == CookieVariant.Unknown))
                {
                    variant = CookieVariant.Rfc2109;
                }
                this.m_cookieVariant = variant;
            }
            if (((this.m_name == null) || (this.m_name.Length == 0)) || ((this.m_name[0] == '$') || (this.m_name.IndexOfAny(Reserved2Name) != -1)))
            {
                if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Name", (this.m_name == null) ? "<null>" : this.m_name }));
                }
                return(false);
            }
            if ((this.m_value == null) || ((((this.m_value.Length <= 2) || (this.m_value[0] != '"')) || (this.m_value[this.m_value.Length - 1] != '"')) && (this.m_value.IndexOfAny(Reserved2Value) != -1)))
            {
                if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Value", (this.m_value == null) ? "<null>" : this.m_value }));
                }
                return(false);
            }
            if (((this.Comment != null) && (((this.Comment.Length <= 2) || (this.Comment[0] != '"')) || (this.Comment[this.Comment.Length - 1] != '"'))) && (this.Comment.IndexOfAny(Reserved2Value) != -1))
            {
                if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Comment", this.Comment }));
                }
                return(false);
            }
            if (((this.Path != null) && (((this.Path.Length <= 2) || (this.Path[0] != '"')) || (this.Path[this.Path.Length - 1] != '"'))) && (this.Path.IndexOfAny(Reserved2Value) != -1))
            {
                if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Path", this.Path }));
                }
                return(false);
            }
            if (set_default && this.m_domain_implicit)
            {
                this.m_domain = host;
            }
            else
            {
                if (!this.m_domain_implicit)
                {
                    string domain = this.m_domain;
                    if (!DomainCharsTest(domain))
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Domain", (domain == null) ? "<null>" : domain }));
                        }
                        return(false);
                    }
                    if (domain[0] != '.')
                    {
                        if ((variant != CookieVariant.Rfc2965) && (variant != CookieVariant.Plain))
                        {
                            if (isThrow)
                            {
                                throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Domain", this.m_domain }));
                            }
                            return(false);
                        }
                        domain = '.' + domain;
                    }
                    int index = host.IndexOf('.');
                    if (isLocalDomain && (string.Compare(localDomain, domain, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        flag = true;
                    }
                    else if (domain.IndexOf('.', 1, domain.Length - 2) == -1)
                    {
                        if (!IsDomainEqualToHost(domain, host))
                        {
                            flag = false;
                        }
                    }
                    else if (variant == CookieVariant.Plain)
                    {
                        if (!IsDomainEqualToHost(domain, host) && ((host.Length <= domain.Length) || (string.Compare(host, host.Length - domain.Length, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0)))
                        {
                            flag = false;
                        }
                    }
                    else if ((((index == -1) || (domain.Length != (host.Length - index))) || (string.Compare(host, index, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0)) && !IsDomainEqualToHost(domain, host))
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        this.m_domainKey = domain.ToLower(CultureInfo.InvariantCulture);
                    }
                }
                else if (string.Compare(host, this.m_domain, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    flag = false;
                }
                if (!flag)
                {
                    if (isThrow)
                    {
                        throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Domain", this.m_domain }));
                    }
                    return(false);
                }
            }
            if (!set_default || !this.m_path_implicit)
            {
                if (!absolutePath.StartsWith(CookieParser.CheckQuoted(this.m_path)))
                {
                    if (isThrow)
                    {
                        throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Path", this.m_path }));
                    }
                    return(false);
                }
            }
            else
            {
                switch (this.m_cookieVariant)
                {
                case CookieVariant.Plain:
                    this.m_path = absolutePath;
                    goto Label_04F0;

                case CookieVariant.Rfc2109:
                    this.m_path = absolutePath.Substring(0, absolutePath.LastIndexOf('/'));
                    goto Label_04F0;
                }
                this.m_path = absolutePath.Substring(0, absolutePath.LastIndexOf('/') + 1);
            }
Label_04F0:
            if ((set_default && !this.m_port_implicit) && (this.m_port.Length == 0))
            {
                this.m_port_list = new int[] { port };
            }
            if (!this.m_port_implicit)
            {
                flag = false;
                foreach (int num3 in this.m_port_list)
                {
                    if (num3 == port)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    if (isThrow)
                    {
                        throw new CookieException(SR.GetString("net_cookie_attribute", new object[] { "Port", this.m_port }));
                    }
                    return(false);
                }
            }
            return(true);
        }
Exemple #16
0
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            if (NetEventSource.IsEnabled)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this, $"uri:{uri} headerName:{headerName} setCookieHeader:{setCookieHeader} isThrow:{isThrow}");
                }
            }

            CookieCollection cookies = new CookieCollection();
            CookieVariant    variant = CookieVariant.Unknown;

            if (headerName == null)
            {
                variant = CookieVariant.Default;
            }
            else
            {
                for (int i = 0; i < s_headerInfo.Length; ++i)
                {
                    if ((String.Compare(headerName, s_headerInfo[i].Name, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        variant = s_headerInfo[i].Variant;
                    }
                }
            }

            bool isLocalDomain = IsLocalDomain(uri.Host);

            try
            {
                CookieParser parser = new CookieParser(setCookieHeader);
                do
                {
                    Cookie cookie = parser.Get();
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Info(this, $"CookieParser returned cookie:{cookie}");
                    }

                    if (cookie == null)
                    {
                        break;
                    }

                    // Parser marks invalid cookies this way
                    if (String.IsNullOrEmpty(cookie.Name))
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.net_cookie_format);
                        }
                        // Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // This will set the default values from the response URI
                    // AND will check for cookie validity
                    if (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow))
                    {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);
                } while (true);
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (isThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies)
            {
                Add(c, isThrow);
            }

            return(cookies);
        }
Exemple #17
0
		void SetCookie (string header)
		{
			if (cookieCollection == null)
				cookieCollection = new CookieCollection ();

			var parser = new CookieParser (header);
			foreach (var cookie in parser.Parse ()) {
				if (cookie.Domain == "")
					cookie.Domain = uri.Host;

				cookieCollection.Add (cookie);
				if (cookie_container != null)
					cookie_container.Add (uri, cookie);
			}
		}
 internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
 {
     CookieCollection cookieCollection = new CookieCollection();
       CookieVariant variant = CookieVariant.Unknown;
       if (headerName == null)
       {
     variant = CookieVariant.Rfc2109;
       }
       else
       {
     for (int index = 0; index < CookieContainer.HeaderInfo.Length; ++index)
     {
       if (string.Compare(headerName, CookieContainer.HeaderInfo[index].Name, StringComparison.OrdinalIgnoreCase) == 0)
     variant = CookieContainer.HeaderInfo[index].Variant;
     }
       }
       bool isLocalDomain = this.IsLocalDomain(uri.Host);
       try
       {
     CookieParser cookieParser = new CookieParser(setCookieHeader);
     while (true)
     {
       Cookie cookie;
       do
       {
     cookie = cookieParser.Get();
     if (cookie != null)
     {
       if (ValidationHelper.IsBlankString(cookie.Name))
       {
         if (isThrow)
           throw new CookieException(SR.GetString("net_cookie_format"));
       }
     }
     else
       goto label_19;
       }
       while (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, this.m_fqdnMyDomain, true, isThrow));
       cookieCollection.InternalAdd(cookie, true);
     }
       }
       catch (Exception ex)
       {
     if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
       throw;
     else if (isThrow)
       throw new CookieException(SR.GetString("net_cookie_parse_header", new object[1]
       {
     (object) uri.AbsoluteUri
       }), ex);
       }
     label_19:
       foreach (Cookie cookie in cookieCollection)
     this.Add(cookie, isThrow);
       return cookieCollection;
 }
Exemple #19
0
        private void SetCookie(string header)
        {
            Cookie       cookie       = null;
            CookieParser cookieParser = new CookieParser(header);
            string       text;
            string       text2;

            while (cookieParser.GetNextNameValue(out text, out text2))
            {
                if ((text != null && !(text == string.Empty)) || cookie != null)
                {
                    if (cookie == null)
                    {
                        cookie = new Cookie(text, text2);
                    }
                    else
                    {
                        text = text.ToUpper();
                        string text3 = text;
                        switch (text3)
                        {
                        case "COMMENT":
                            if (cookie.Comment == null)
                            {
                                cookie.Comment = text2;
                            }
                            break;

                        case "COMMENTURL":
                            if (cookie.CommentUri == null)
                            {
                                cookie.CommentUri = new System.Uri(text2);
                            }
                            break;

                        case "DISCARD":
                            cookie.Discard = true;
                            break;

                        case "DOMAIN":
                            if (cookie.Domain == string.Empty)
                            {
                                cookie.Domain = text2;
                            }
                            break;

                        case "HTTPONLY":
                            cookie.HttpOnly = true;
                            break;

                        case "MAX-AGE":
                            if (cookie.Expires == DateTime.MinValue)
                            {
                                try
                                {
                                    cookie.Expires = cookie.TimeStamp.AddSeconds(uint.Parse(text2));
                                }
                                catch
                                {
                                }
                            }
                            break;

                        case "EXPIRES":
                            if (!(cookie.Expires != DateTime.MinValue))
                            {
                                cookie.Expires = this.TryParseCookieExpires(text2);
                            }
                            break;

                        case "PATH":
                            cookie.Path = text2;
                            break;

                        case "PORT":
                            if (cookie.Port == null)
                            {
                                cookie.Port = text2;
                            }
                            break;

                        case "SECURE":
                            cookie.Secure = true;
                            break;

                        case "VERSION":
                            try
                            {
                                cookie.Version = (int)uint.Parse(text2);
                            }
                            catch
                            {
                            }
                            break;
                        }
                    }
                }
            }
            if (cookie == null)
            {
                return;
            }
            if (this.cookieCollection == null)
            {
                this.cookieCollection = new CookieCollection();
            }
            if (cookie.Domain == string.Empty)
            {
                cookie.Domain = this.uri.Host;
            }
            this.cookieCollection.Add(cookie);
            if (this.cookie_container != null)
            {
                this.cookie_container.Add(this.uri, cookie);
            }
        }
Exemple #20
0
        internal CookieCollection CookieCutter(Uri uri, string HeaderName, string setCookieHeader, bool isThrow) {

            CookieCollection cookies = new CookieCollection();

            CookieVariant variant = CookieVariant.Unknown;
            if (HeaderName == null) {
                variant = CookieVariant.Default;
            }
            else for (int i = 0; i < HeaderInfo.Length; ++i) {
                if ((String.Compare(HeaderName, HeaderInfo[i].Name, true, CultureInfo.InvariantCulture) == 0)) {
                        variant  = HeaderInfo[i].Variant;
                }
            }

            bool isLocalDomain = IsLocal(uri.Host);

            try {
                CookieParser parser = new CookieParser(setCookieHeader);
                do {

                    Cookie cookie = parser.Get();
                    if (cookie == null) {
//Console.WriteLine("CookieCutter: eof cookies");
                        break;
                    }

                    //Parser marks invalid cookies this way
                    if (cookie.Name == string.Empty) {
                        if(isThrow) {
                            throw new CookieException(SR.GetString(SR.net_cookie_format));
                        }
                        //Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // this will set the default values from the response URI
                    // AND will check for cookie validity
                    if(!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow)) {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);

                } while (true);
            }
            catch (Exception e) {
                if(isThrow) {
                    throw new CookieException(SR.GetString(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies) {
                Add(c, isThrow);
            }

            return cookies;
        }
Exemple #21
0
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            GlobalLog.Print("CookieContainer#" + ValidationHelper.HashString(this) + "::CookieCutter() uri:" + uri + " headerName:" + headerName + " setCookieHeader:" + setCookieHeader + " isThrow:" + isThrow);
            CookieCollection cookies = new CookieCollection();
            CookieVariant    variant = CookieVariant.Unknown;

            if (headerName == null)
            {
                variant = CookieVariant.Default;
            }
            else
            {
                for (int i = 0; i < HeaderInfo.Length; ++i)
                {
                    if ((String.Compare(headerName, HeaderInfo[i].Name, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        variant = HeaderInfo[i].Variant;
                    }
                }
            }
            bool isLocalDomain = IsLocalDomain(uri.Host);

            try {
                CookieParser parser = new CookieParser(setCookieHeader);
                do
                {
                    Cookie cookie = parser.Get();
                    GlobalLog.Print("CookieContainer#" + ValidationHelper.HashString(this) + "::CookieCutter() CookieParser returned cookie:" + ValidationHelper.ToString(cookie));
                    if (cookie == null)
                    {
                        break;
                    }

                    //Parser marks invalid cookies this way
                    if (ValidationHelper.IsBlankString(cookie.Name))
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.GetString(SR.net_cookie_format));
                        }
                        //Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // this will set the default values from the response URI
                    // AND will check for cookie validity
                    if (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow))
                    {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);
                } while (true);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }

                if (isThrow)
                {
                    throw new CookieException(SR.GetString(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies)
            {
                Add(c, isThrow);
            }

            return(cookies);
        }
 internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
 {
     CookieCollection cookies = new CookieCollection();
     CookieVariant unknown = CookieVariant.Unknown;
     if (headerName == null)
     {
         unknown = CookieVariant.Rfc2109;
     }
     else
     {
         for (int i = 0; i < HeaderInfo.Length; i++)
         {
             if (string.Compare(headerName, HeaderInfo[i].Name, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 unknown = HeaderInfo[i].Variant;
             }
         }
     }
     bool isLocalDomain = this.IsLocalDomain(uri.Host);
     try
     {
         Cookie cookie;
         CookieParser parser = new CookieParser(setCookieHeader);
     Label_0060:
         cookie = parser.Get();
         if (cookie != null)
         {
             if (ValidationHelper.IsBlankString(cookie.Name))
             {
                 if (isThrow)
                 {
                     throw new CookieException(SR.GetString("net_cookie_format"));
                 }
             }
             else if (cookie.VerifySetDefaults(unknown, uri, isLocalDomain, this.m_fqdnMyDomain, true, isThrow))
             {
                 cookies.InternalAdd(cookie, true);
             }
             goto Label_0060;
         }
     }
     catch (Exception exception)
     {
         if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
         {
             throw;
         }
         if (isThrow)
         {
             throw new CookieException(SR.GetString("net_cookie_parse_header", new object[] { uri.AbsoluteUri }), exception);
         }
     }
     foreach (Cookie cookie2 in cookies)
     {
         this.Add(cookie2, isThrow);
     }
     return cookies;
 }
Exemple #23
0
		void SetCookie (string header)
		{
			string name, val;
			Cookie cookie = null;
			CookieParser parser = new CookieParser (header);

			while (parser.GetNextNameValue (out name, out val)) {
				if ((name == null || name == "") && cookie == null)
					continue;

				if (cookie == null) {
					cookie = new Cookie (name, val);
					continue;
				}

				name = name.ToUpper ();
				switch (name) {
				case "COMMENT":
					if (cookie.Comment == null)
						cookie.Comment = val;
					break;
				case "COMMENTURL":
					if (cookie.CommentUri == null)
						cookie.CommentUri = new Uri (val);
					break;
				case "DISCARD":
					cookie.Discard = true;
					break;
				case "DOMAIN":
					if (cookie.Domain == "")
						cookie.Domain = val;
					break;
				case "HTTPONLY":
					cookie.HttpOnly = true;
					break;
				case "MAX-AGE": // RFC Style Set-Cookie2
					if (cookie.Expires == DateTime.MinValue) {
						try {
						cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
						} catch {}
					}
					break;
				case "EXPIRES": // Netscape Style Set-Cookie
					if (cookie.Expires != DateTime.MinValue)
						break;

					cookie.Expires = CookieParser.TryParseCookieExpires (val);
					break;
				case "PATH":
					cookie.Path = val;
					break;
				case "PORT":
					if (cookie.Port == null)
						cookie.Port = val;
					break;
				case "SECURE":
					cookie.Secure = true;
					break;
				case "VERSION":
					try {
						cookie.Version = (int) UInt32.Parse (val);
					} catch {}
					break;
				}
			}

			if (cookie == null)
				return;

			if (cookieCollection == null)
				cookieCollection = new CookieCollection ();

			if (cookie.Domain == "")
				cookie.Domain = uri.Host;

			cookieCollection.Add (cookie);
			if (cookie_container != null)
				cookie_container.Add (uri, cookie);
		}
Exemple #24
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);
        }
        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 #26
0
        private void SetCookie(string header)
        {
            Cookie       cookie       = null;
            CookieParser cookieParser = new CookieParser(header);
            string       name;
            string       val;

            while (cookieParser.GetNextNameValue(out name, out val))
            {
                if ((name == null || name == string.Empty) && cookie == null)
                {
                    continue;
                }
                if (cookie == null)
                {
                    cookie = new Cookie(name, val);
                    continue;
                }
                name = name.ToUpper();
                switch (name)
                {
                case "COMMENT":
                    if (cookie.Comment == null)
                    {
                        cookie.Comment = val;
                    }
                    break;

                case "COMMENTURL":
                    if (cookie.CommentUri == null)
                    {
                        cookie.CommentUri = new Uri(val);
                    }
                    break;

                case "DISCARD":
                    cookie.Discard = true;
                    break;

                case "DOMAIN":
                    if (cookie.Domain == string.Empty)
                    {
                        cookie.Domain = val;
                    }
                    break;

                case "HTTPONLY":
                    cookie.HttpOnly = true;
                    break;

                case "MAX-AGE":
                    if (cookie.Expires == DateTime.MinValue)
                    {
                        try
                        {
                            cookie.Expires = cookie.TimeStamp.AddSeconds(uint.Parse(val));
                        }
                        catch
                        {
                        }
                    }
                    break;

                case "EXPIRES":
                    if (!(cookie.Expires != DateTime.MinValue))
                    {
                        cookie.Expires = TryParseCookieExpires(val);
                    }
                    break;

                case "PATH":
                    cookie.Path = val;
                    break;

                case "PORT":
                    if (cookie.Port == null)
                    {
                        cookie.Port = val;
                    }
                    break;

                case "SECURE":
                    cookie.Secure = true;
                    break;

                case "VERSION":
                    try
                    {
                        cookie.Version = (int)uint.Parse(val);
                    }
                    catch
                    {
                    }
                    break;
                }
            }
            if (cookie != null)
            {
                if (cookieCollection == null)
                {
                    cookieCollection = new CookieCollection();
                }
                if (cookie.Domain == string.Empty)
                {
                    cookie.Domain = uri.Host;
                }
                cookieCollection.Add(cookie);
                if (cookie_container != null)
                {
                    cookie_container.Add(uri, cookie);
                }
            }
        }
Exemple #27
0
        internal CookieCollection CookieCutter(Uri uri, string HeaderName, string setCookieHeader, bool isThrow)
        {
            CookieCollection cookies = new CookieCollection();

            CookieVariant variant = CookieVariant.Unknown;

            if (HeaderName == null)
            {
                variant = CookieVariant.Default;
            }
            else
            {
                for (int i = 0; i < HeaderInfo.Length; ++i)
                {
                    if ((String.Compare(HeaderName, HeaderInfo[i].Name, true, CultureInfo.InvariantCulture) == 0))
                    {
                        variant = HeaderInfo[i].Variant;
                    }
                }
            }

            bool isLocalDomain = IsLocal(uri.Host);

            try {
                CookieParser parser = new CookieParser(setCookieHeader);
                do
                {
                    Cookie cookie = parser.Get();
                    if (cookie == null)
                    {
//Console.WriteLine("CookieCutter: eof cookies");
                        break;
                    }

                    //Parser marks invalid cookies this way
                    if (cookie.Name == string.Empty)
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.GetString(SR.net_cookie_format));
                        }
                        //Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // this will set the default values from the response URI
                    // AND will check for cookie validity
                    if (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow))
                    {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);
                } while (true);
            }
            catch (Exception e) {
                if (isThrow)
                {
                    throw new CookieException(SR.GetString(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies)
            {
                Add(c, isThrow);
            }

            return(cookies);
        }
Exemple #28
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 #29
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 #30
0
        void SetCookie(string header)
        {
            string       name, val;
            Cookie       cookie = null;
            CookieParser parser = new CookieParser(header);

            while (parser.GetNextNameValue(out name, out val))
            {
                if ((name == null || name == "") && cookie == null)
                {
                    continue;
                }

                if (cookie == null)
                {
                    cookie = new Cookie(name, val);
                    continue;
                }

                name = name.ToUpper();
                switch (name)
                {
                case "COMMENT":
                    if (cookie.Comment == null)
                    {
                        cookie.Comment = val;
                    }
                    break;

                case "COMMENTURL":
                    if (cookie.CommentUri == null)
                    {
                        cookie.CommentUri = new Uri(val);
                    }
                    break;

                case "DISCARD":
                    cookie.Discard = true;
                    break;

                case "DOMAIN":
                    if (cookie.Domain == "")
                    {
                        cookie.Domain = val;
                    }
                    break;

                case "MAX-AGE":                 // RFC Style Set-Cookie2
                    if (cookie.Expires == DateTime.MinValue)
                    {
                        try {
                            cookie.Expires = cookie.TimeStamp.AddSeconds(UInt32.Parse(val));
                        } catch {}
                    }
                    break;

                case "EXPIRES":                 // Netscape Style Set-Cookie
                    if (cookie.Expires != DateTime.MinValue)
                    {
                        break;
                    }

                    cookie.Expires = TryParseCookieExpires(val);
                    break;

                case "PATH":
                    cookie.Path = val;
                    break;

                case "PORT":
                    if (cookie.Port == null)
                    {
                        cookie.Port = val;
                    }
                    break;

                case "SECURE":
                    cookie.Secure = true;
                    break;

                case "VERSION":
                    try {
                        cookie.Version = (int)UInt32.Parse(val);
                    } catch {}
                    break;
                }
            }

            if (cookieCollection == null)
            {
                cookieCollection = new CookieCollection();
            }

            if (cookie.Domain == "")
            {
                cookie.Domain = uri.Host;
            }

            cookieCollection.Add(cookie);
            if (cookie_container != null)
            {
                cookie_container.Add(uri, cookie);
            }
        }
Exemple #31
0
        // According to spec we must assume default values for attributes but still
        // keep in mind that we must not include them into the requests.
        // We also check the validity of all attributes based on the version and variant (read RFC)
        //
        // To work properly this function must be called after cookie construction with
        // default (response) URI AND setDefault == true
        //
        // Afterwards, the function can be called many times with other URIs and
        // setDefault == false to check whether this cookie matches given uri
        internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDomain, string localDomain, bool setDefault, bool shouldThrow)
        {
            string host  = uri.Host;
            int    port  = uri.Port;
            string path  = uri.AbsolutePath;
            bool   valid = true;

            if (setDefault)
            {
                // Set Variant. If version is zero => reset cookie to Version0 style
                if (Version == 0)
                {
                    variant = CookieVariant.Plain;
                }
                else if (Version == 1 && variant == CookieVariant.Unknown)
                {
                    // Since we don't expose Variant to an app, set it to Default
                    variant = CookieVariant.Default;
                }
                m_cookieVariant = variant;
            }

            // Check the name
            if (m_name == null || m_name.Length == 0 || m_name[0] == '$' || m_name.IndexOfAny(ReservedToName) != -1)
            {
                if (shouldThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_attribute, "Name", m_name == null ? "<null>" : m_name));
                }
                return(false);
            }

            // Check the value
            if (m_value == null ||
                (!(m_value.Length > 2 && m_value[0] == '\"' && m_value[m_value.Length - 1] == '\"') && m_value.IndexOfAny(ReservedToValue) != -1))
            {
                if (shouldThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_attribute, "Value", m_value == null ? "<null>" : m_value));
                }
                return(false);
            }

            // Check Comment syntax
            if (Comment != null && !(Comment.Length > 2 && Comment[0] == '\"' && Comment[Comment.Length - 1] == '\"') &&
                (Comment.IndexOfAny(ReservedToValue) != -1))
            {
                if (shouldThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.CommentAttributeName, Comment));
                }
                return(false);
            }

            // Check Path syntax
            if (Path != null && !(Path.Length > 2 && Path[0] == '\"' && Path[Path.Length - 1] == '\"') &&
                (Path.IndexOfAny(ReservedToValue) != -1))
            {
                if (shouldThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PathAttributeName, Path));
                }
                return(false);
            }

            // Check/set domain
            //
            // If domain is implicit => assume a) uri is valid, b) just set domain to uri hostname.
            if (setDefault && m_domainImplicit == true)
            {
                m_domain = host;
            }
            else
            {
                if (!m_domainImplicit)
                {
                    // Forwarding note: If Uri.Host is of IP address form then the only supported case
                    // is for IMPLICIT domain property of a cookie.
                    // The code below (explicit cookie.Domain value) will try to parse Uri.Host IP string
                    // as a fqdn and reject the cookie.

                    // Aliasing since we might need the KeyValue (but not the original one).
                    string domain = m_domain;

                    // Syntax check for Domain charset plus empty string.
                    if (!DomainCharsTest(domain))
                    {
                        if (shouldThrow)
                        {
                            throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, domain == null ? "<null>" : domain));
                        }
                        return(false);
                    }

                    // Domain must start with '.' if set explicitly.
                    if (domain[0] != '.')
                    {
                        if (!(variant == CookieVariant.Rfc2965 || variant == CookieVariant.Plain))
                        {
                            if (shouldThrow)
                            {
                                throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, m_domain));
                            }
                            return(false);
                        }
                        domain = '.' + domain;
                    }

                    int host_dot = host.IndexOf('.');

                    // First quick check is for pushing a cookie into the local domain.
                    if (isLocalDomain && string.Equals(localDomain, domain, StringComparison.OrdinalIgnoreCase))
                    {
                        valid = true;
                    }
                    else if (domain.IndexOf('.', 1, domain.Length - 2) == -1)
                    {
                        // A single label domain is valid only if the domain is exactly the same as the host specified in the URI.
                        if (!IsDomainEqualToHost(domain, host))
                        {
                            valid = false;
                        }
                    }
                    else if (variant == CookieVariant.Plain)
                    {
                        // We distinguish between Version0 cookie and other versions on domain issue.
                        // According to Version0 spec a domain must be just a substring of the hostname.

                        if (!IsDomainEqualToHost(domain, host))
                        {
                            if (host.Length <= domain.Length ||
                                (string.Compare(host, host.Length - domain.Length, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0))
                            {
                                valid = false;
                            }
                        }
                    }
                    else if (host_dot == -1 ||
                             domain.Length != host.Length - host_dot ||
                             (string.Compare(host, host_dot, domain, 0, domain.Length, StringComparison.OrdinalIgnoreCase) != 0))
                    {
                        // Starting from the first dot, the host must match the domain.
                        //
                        // For null hosts, the host must match the domain exactly.
                        if (!IsDomainEqualToHost(domain, host))
                        {
                            valid = false;
                        }
                    }

                    if (valid)
                    {
                        m_domainKey = domain.ToLowerInvariant();
                    }
                }
                else
                {
                    // For implicitly set domain AND at the set_default == false time
                    // we simply need to match uri.Host against m_domain.
                    if (!string.Equals(host, m_domain, StringComparison.OrdinalIgnoreCase))
                    {
                        valid = false;
                    }
                }
                if (!valid)
                {
                    if (shouldThrow)
                    {
                        throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, m_domain));
                    }
                    return(false);
                }
            }

            // Check/Set Path
            if (setDefault && m_pathImplicit == true)
            {
                // This code assumes that the URI path is always valid and contains at least one '/'.
                switch (m_cookieVariant)
                {
                case CookieVariant.Plain:
                    m_path = path;
                    break;

                case CookieVariant.Rfc2109:
                    m_path = path.Substring(0, path.LastIndexOf('/'));     // May be empty
                    break;

                case CookieVariant.Rfc2965:
                default:
                    // NOTE: this code is not resilient against future versions with different 'Path' semantics.
                    m_path = path.Substring(0, path.LastIndexOf('/') + 1);
                    break;
                }
            }
            else
            {
                // Check current path (implicit/explicit) against given URI.
                if (!path.StartsWith(CookieParser.CheckQuoted(m_path)))
                {
                    if (shouldThrow)
                    {
                        throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PathAttributeName, m_path));
                    }
                    return(false);
                }
            }

            // Set the default port if Port attribute was present but had no value.
            if (setDefault && (m_portImplicit == false && m_port.Length == 0))
            {
                m_portList = new int[1] {
                    port
                };
            }

            if (m_portImplicit == false)
            {
                // Port must match against the one from the uri.
                valid = false;
                foreach (int p in m_portList)
                {
                    if (p == port)
                    {
                        valid = true;
                        break;
                    }
                }
                if (!valid)
                {
                    if (shouldThrow)
                    {
                        throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, m_port));
                    }
                    return(false);
                }
            }
            return(true);
        }
Exemple #32
0
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            CookieCollection cookieCollection = new CookieCollection();
            CookieVariant    variant          = CookieVariant.Unknown;

            if (headerName == null)
            {
                variant = CookieVariant.Rfc2109;
            }
            else
            {
                for (int index = 0; index < CookieContainer.HeaderInfo.Length; ++index)
                {
                    if (string.Compare(headerName, CookieContainer.HeaderInfo[index].Name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        variant = CookieContainer.HeaderInfo[index].Variant;
                    }
                }
            }
            bool isLocalDomain = this.IsLocalDomain(uri.Host);

            try
            {
                CookieParser cookieParser = new CookieParser(setCookieHeader);
                while (true)
                {
                    Cookie cookie;
                    do
                    {
                        cookie = cookieParser.Get();
                        if (cookie != null)
                        {
                            if (ValidationHelper.IsBlankString(cookie.Name))
                            {
                                if (isThrow)
                                {
                                    throw new CookieException(SR.GetString("net_cookie_format"));
                                }
                            }
                        }
                        else
                        {
                            goto label_19;
                        }
                    }while (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, this.m_fqdnMyDomain, true, isThrow));
                    cookieCollection.InternalAdd(cookie, true);
                }
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
                else if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_parse_header", new object[1]
                    {
                        (object)uri.AbsoluteUri
                    }), ex);
                }
            }
label_19:
            foreach (Cookie cookie in cookieCollection)
            {
                this.Add(cookie, isThrow);
            }
            return(cookieCollection);
        }