Next() private method

private Next ( bool first, bool parseResponseCookies ) : CookieToken
first bool
parseResponseCookies bool
return CookieToken
Example #1
0
        // GetString
        //
        // Gets the next cookie string
        internal string GetString()
        {
            bool first = true;

            if (_tokenizer.Eof)
            {
                return(null);
            }

            do
            {
                _tokenizer.Next(first, true);
                first = false;
            } while (!_tokenizer.Eof && !_tokenizer.EndOfCookie);

            return(_tokenizer.GetCookieString());
        }
Example #2
0
        // Get
        //
        // Gets the next cookie or null if there are no more cookies.
        internal Cookie Get()
        {
            Cookie cookie = null;

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

            do
            {
                CookieToken token = _tokenizer.Next(cookie == null, true);
                if (cookie == null && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
                {
                    cookie = new Cookie();
                    InternalSetNameMethod(cookie, _tokenizer.Name);
                    cookie.Value = _tokenizer.Value;
                }
                else
                {
                    switch (token)
                    {
                    case CookieToken.NameValuePair:
                        switch (_tokenizer.Token)
                        {
                        case CookieToken.Comment:
                            if (!commentSet)
                            {
                                commentSet     = true;
                                cookie.Comment = _tokenizer.Value;
                            }
                            break;

                        case CookieToken.CommentUrl:
                            if (!commentUriSet)
                            {
                                commentUriSet = true;
                                if (Uri.TryCreate(CheckQuoted(_tokenizer.Value), UriKind.Absolute, out Uri parsed))
                                {
                                    cookie.CommentUri = parsed;
                                }
                            }
                            break;

                        case CookieToken.Domain:
                            if (!domainSet)
                            {
                                domainSet     = true;
                                cookie.Domain = CheckQuoted(_tokenizer.Value);
                                IsQuotedDomainField.SetValue(cookie, _tokenizer.Quoted);
                            }
                            break;

                        case CookieToken.Expires:
                            if (!expiresSet)
                            {
                                expiresSet = true;

                                if (DateTime.TryParse(CheckQuoted(_tokenizer.Value),
                                                      CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out DateTime expires))
                                {
                                    cookie.Expires = expires;
                                }
                                else
                                {
                                    // This cookie will be rejected
                                    InternalSetNameMethod(cookie, string.Empty);
                                }
                            }
                            break;

                        case CookieToken.MaxAge:
                            if (!expiresSet)
                            {
                                expiresSet = true;
                                if (int.TryParse(CheckQuoted(_tokenizer.Value), out int parsed))
                                {
                                    cookie.Expires = DateTime.Now.AddSeconds(parsed);
                                }
                                else
                                {
                                    // This cookie will be rejected
                                    InternalSetNameMethod(cookie, string.Empty);
                                }
                            }
                            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
                                {
                                    // This cookie will be rejected
                                    InternalSetNameMethod(cookie, string.Empty);
                                }
                            }
                            break;

                        case CookieToken.Version:
                            if (!versionSet)
                            {
                                versionSet = true;
                                int parsed;
                                if (int.TryParse(CheckQuoted(_tokenizer.Value), out parsed))
                                {
                                    cookie.Version = parsed;
                                    IsQuotedVersionField.SetValue(cookie, _tokenizer.Quoted);
                                }
                                else
                                {
                                    // This cookie will be rejected
                                    InternalSetNameMethod(cookie, string.Empty);
                                }
                            }
                            break;
                        }
                        break;

                    case CookieToken.Attribute:
                        switch (_tokenizer.Token)
                        {
                        case CookieToken.Discard:
                            if (!discardSet)
                            {
                                discardSet     = true;
                                cookie.Discard = true;
                            }
                            break;

                        case CookieToken.Secure:
                            if (!secureSet)
                            {
                                secureSet     = true;
                                cookie.Secure = true;
                            }
                            break;

                        case CookieToken.HttpOnly:
                            cookie.HttpOnly = true;
                            break;

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

            return(cookie);
        }