System.Net.CookieParser.GetServer C# (CSharp) Method

GetServer() private method

private GetServer ( ) : Cookie
return Cookie
        internal Cookie GetServer()
        {
            Cookie cookie = _savedCookie;
            _savedCookie = null;

            // Only the first occurrence of an attribute value must be counted.
            bool domainSet = false;
            bool pathSet = false;
            bool portSet = false; // Special case: may have no value in header.

            do
            {
                bool first = cookie == null || cookie.Name == null || cookie.Name.Length == 0;
                CookieToken token = _tokenizer.Next(first, false);

                if (first && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
                {
                    if (cookie == null)
                    {
                        cookie = new Cookie();
                    }
                    if (cookie.InternalSetName(_tokenizer.Name) == false)
                    {
                        // will be rejected
                        cookie.InternalSetName(string.Empty);
                    }
                    cookie.Value = _tokenizer.Value;
                }
                else
                {
                    switch (token)
                    {
                        case CookieToken.NameValuePair:
                            switch (_tokenizer.Token)
                            {
                                case CookieToken.Domain:
                                    if (!domainSet)
                                    {
                                        domainSet = true;
                                        cookie.Domain = CheckQuoted(_tokenizer.Value);
                                        cookie.IsQuotedDomain = _tokenizer.Quoted;
                                    }
                                    break;

                                case CookieToken.Path:
                                    if (!pathSet)
                                    {
                                        pathSet = true;
                                        cookie.Path = _tokenizer.Value;
                                    }
                                    break;

                                case CookieToken.Port:
                                    if (!portSet)
                                    {
                                        portSet = true;
                                        try
                                        {
                                            cookie.Port = _tokenizer.Value;
                                        }
                                        catch (CookieException)
                                        {
                                            // this cookie will be rejected
                                            cookie.InternalSetName(string.Empty);
                                        }
                                    }
                                    break;

                                case CookieToken.Version:
                                    // this is a new cookie, this token is for the next cookie.
                                    _savedCookie = new Cookie();
                                    int parsed;
                                    if (int.TryParse(_tokenizer.Value, out parsed))
                                    {
                                        _savedCookie.Version = parsed;
                                    }
                                    return cookie;

                                case CookieToken.Unknown:
                                    // this is a new cookie, the token is for the next cookie.
                                    _savedCookie = new Cookie();
                                    if (_savedCookie.InternalSetName(_tokenizer.Name) == false)
                                    {
                                        // will be rejected
                                        _savedCookie.InternalSetName(string.Empty);
                                    }
                                    _savedCookie.Value = _tokenizer.Value;
                                    return cookie;
                            }
                            break;

                        case CookieToken.Attribute:
                            switch (_tokenizer.Token)
                            {
                                case CookieToken.Port:
                                    if (!portSet)
                                    {
                                        portSet = true;
                                        cookie.Port = string.Empty;
                                    }
                                    break;
                            }
                            break;
                    }
                }
            } while (!_tokenizer.Eof && !_tokenizer.EndOfCookie);
            return cookie;
        }

Usage Example

示例#1
0
        private CookieCollection ParseCookies(Uri uri, string setCookieHeader)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, "uri:" + uri + " setCookieHeader:" + setCookieHeader);
            }
            CookieCollection cookies = new CookieCollection();
            CookieParser     parser  = new CookieParser(setCookieHeader);

            while (true)
            {
                Cookie cookie = parser.GetServer();
                if (cookie == null)
                {
                    // EOF, done.
                    break;
                }
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this, "CookieParser returned cookie: " + cookie.ToString());
                }
                if (cookie.Name.Length == 0)
                {
                    continue;
                }

                InternalAddMethod(cookies, cookie, true);
            }
            return(cookies);
        }
All Usage Examples Of System.Net.CookieParser::GetServer