internal void Reset()
 {
     this.m_eofCookie   = false;
     this.m_name        = string.Empty;
     this.m_quoted      = false;
     this.m_start       = this.m_index;
     this.m_token       = CookieToken.Nothing;
     this.m_tokenLength = 0;
     this.m_value       = string.Empty;
 }
Example #2
0
 // Reset
 //
 // Sets this tokenizer up for finding the next name/value pair,
 // attribute, or end-of-{token,cookie,line}.
 internal void Reset()
 {
     _eofCookie   = false;
     _name        = string.Empty;
     _quoted      = false;
     _start       = _index;
     _token       = CookieToken.Nothing;
     _tokenLength = 0;
     _value       = string.Empty;
 }
Example #3
0
        // Next
        //
        // Get the next cookie name/value or attribute
        //
        // Cookies come in the following formats:
        //
        //     1. Version0
        //         Set-Cookie: [<name>][=][<value>]
        //                     [; expires=<date>]
        //                     [; path=<path>]
        //                     [; domain=<domain>]
        //                     [; secure]
        //         Cookie: <name>=<value>
        //
        //         Notes: <name> and/or <value> may be blank
        //                <date> is the RFC 822/1123 date format that
        //                incorporates commas, e.g.
        //                "Wednesday, 09-Nov-99 23:12:40 GMT"
        //
        //     2. RFC 2109
        //         Set-Cookie: 1#{
        //                         <name>=<value>
        //                         [; comment=<comment>]
        //                         [; domain=<domain>]
        //                         [; max-age=<seconds>]
        //                         [; path=<path>]
        //                         [; secure]
        //                         ; Version=<version>
        //                     }
        //         Cookie: $Version=<version>
        //                 1#{
        //                     ; <name>=<value>
        //                     [; path=<path>]
        //                     [; domain=<domain>]
        //                 }
        //
        //     3. RFC 2965
        //         Set-Cookie2: 1#{
        //                         <name>=<value>
        //                         [; comment=<comment>]
        //                         [; commentURL=<comment>]
        //                         [; discard]
        //                         [; domain=<domain>]
        //                         [; max-age=<seconds>]
        //                         [; path=<path>]
        //                         [; ports=<portlist>]
        //                         [; secure]
        //                         ; Version=<version>
        //                      }
        //         Cookie: $Version=<version>
        //                 1#{
        //                     ; <name>=<value>
        //                     [; path=<path>]
        //                     [; domain=<domain>]
        //                     [; port="<port>"]
        //                 }
        //         [Cookie2: $Version=<version>]
        //
        // Inputs:
        // <argument>  first
        //     true if this is the first name/attribute that we have looked for
        //     in the cookie stream
        //
        // Outputs:
        //
        // Assumes:
        // Nothing
        //
        // Returns:
        // type of CookieToken found:
        //
        //     - Attribute
        //         - token was single-value. May be empty. Caller should check
        //           Eof or EndCookie to determine if any more action needs to
        //           be taken
        //
        //     - NameValuePair
        //         - Name and Value are meaningful. Either may be empty
        //
        // Throws:
        // Nothing
        internal CookieToken Next(bool first, bool parseResponseCookies)
        {
            Reset();

            if (first)
            {
                _cookieStartIndex = _index;
                _cookieLength     = 0;
            }

            CookieToken terminator = FindNext(false, false);

            if (terminator == CookieToken.EndCookie)
            {
                EndOfCookie = true;
            }

            if ((terminator == CookieToken.End) || (terminator == CookieToken.EndCookie))
            {
                if ((Name = Extract()).Length != 0)
                {
                    Token = TokenFromName(parseResponseCookies);
                    return(CookieToken.Attribute);
                }
                return(terminator);
            }
            Name = Extract();
            if (first)
            {
                Token = CookieToken.CookieName;
            }
            else
            {
                Token = TokenFromName(parseResponseCookies);
            }
            if (terminator == CookieToken.Equals)
            {
                terminator = FindNext(!first && (Token == CookieToken.Expires), true);
                if (terminator == CookieToken.EndCookie)
                {
                    EndOfCookie = true;
                }
                Value = Extract();
                return(CookieToken.NameValuePair);
            }
            else
            {
                return(CookieToken.Attribute);
            }
        }
Example #4
0
        public void CookieToken_SimpleCookie_LastValueReplaced()
        {
            Request request = new Request(new Uri("http://test.com"), HttpMethod.Get);

            request.Content = "";
            request.Headers.Add("Cookie", new List <string> {
                "test1=test2;test3=test4"
            });

            CookieToken token = new CookieToken("test3", "", Types.String);

            token.ReplaceValue(request, "newValue");

            Assert.Equal("test1=test2;test3=newValue", request.Headers["Cookie"][0]);
        }
        internal CookieToken Next(bool first, bool parseResponseCookies)
        {
            this.Reset();
            CookieToken token = this.FindNext(false, false);

            switch (token)
            {
            case CookieToken.EndCookie:
                this.EndOfCookie = true;
                break;

            case CookieToken.End:
            case CookieToken.EndCookie:
                string str;
                this.Name = str = this.Extract();
                if (str.Length != 0)
                {
                    this.Token = this.TokenFromName(parseResponseCookies);
                    return(CookieToken.Attribute);
                }
                return(token);
            }
            this.Name = this.Extract();
            if (first)
            {
                this.Token = CookieToken.CookieName;
            }
            else
            {
                this.Token = this.TokenFromName(parseResponseCookies);
            }
            if (token != CookieToken.Equals)
            {
                return(CookieToken.Attribute);
            }
            if (this.FindNext(!first && (this.Token == CookieToken.Expires), true) == CookieToken.EndCookie)
            {
                this.EndOfCookie = true;
            }
            this.Value = this.Extract();
            return(CookieToken.NameValuePair);
        }
Example #6
0
        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 || string.IsNullOrEmpty(cookie.Name);
                CookieToken token = _tokenizer.Next(first, false);

                if (first && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
                {
                    if (cookie == null)
                    {
                        cookie = new Cookie();
                    }
                    InternalSetNameMethod(cookie, _tokenizer.Name);
                    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);
                                IsQuotedDomainField.SetValue(cookie, _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
                                    InternalSetNameMethod(cookie, string.Empty);
                                }
                            }
                            break;

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

                        case CookieToken.Unknown:
                            // this is a new cookie, the token is for the next cookie.
                            _savedCookie = new Cookie();
                            InternalSetNameMethod(_savedCookie, _tokenizer.Name);
                            _savedCookie.Value = _tokenizer.Value;
                            return(cookie);
                        }
                        break;

                    case CookieToken.Attribute:
                        if (_tokenizer.Token == CookieToken.Port && !portSet)
                        {
                            portSet     = true;
                            cookie.Port = string.Empty;
                        }
                        break;
                    }
                }
            } while (!_tokenizer.Eof && !_tokenizer.EndOfCookie);
            return(cookie);
        }
Example #7
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);
        }
Example #8
0
 internal RecognizedAttribute(string name, CookieToken token)
 {
     _name  = name;
     _token = token;
 }
Example #9
0
        // properties

        // methods

        //
        // Get
        //
        //  Gets the next cookie
        //
        // Inputs:
        //  Nothing
        //
        // Outputs:
        //  Nothing
        //
        // Assumes:
        //  Nothing
        //
        // Returns:
        //  new cookie object, or null if there's no more
        //
        // Throws:
        //  Nothing
        //

        internal CookieInternal Get()
        {
            CookieInternal cookie = null;

            // only first ocurence 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 as it may have no value in header
            bool versionSet    = false;
            bool secureSet     = false;
            bool discardSet    = false;
            bool rejected      = false;

            do
            {
                CookieToken token = m_tokenizer.Next(cookie == null, true);
                if (cookie == null && (token == CookieToken.NameValuePair || token == CookieToken.Attribute))
                {
                    cookie = new CookieInternal();
                    if (cookie.InternalSetName(m_tokenizer.Name) == false)
                    {
                        //will be rejected
                        cookie.InternalSetName(string.Empty);
                        rejected = true;
                    }
                    cookie.Value = m_tokenizer.Value;
                }
                else
                {
                    switch (token)
                    {
                    case CookieToken.NameValuePair:
                        switch (m_tokenizer.Token)
                        {
                        case CookieToken.Comment:
                        {
                            if (!commentSet)
                            {
                                commentSet     = true;
                                cookie.Comment = m_tokenizer.Value;
                            }
                            break;
                        }

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

                            break;
                        }

                        case CookieToken.Domain:
                        {
                            if (!domainSet)
                            {
                                domainSet             = true;
                                cookie.Domain         = CheckQuoted(m_tokenizer.Value);
                                cookie.IsQuotedDomain = m_tokenizer.Quoted;
                            }

                            break;
                        }

                        case CookieToken.Expires:
                        {
                            if (!expiresSet)
                            {
                                expiresSet = true;
                                const DateTimeStyles style = DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal;
                                var unQuotedValue          = CheckQuoted(m_tokenizer.Value);

                                if (DateTime.TryParse(unQuotedValue,
                                                      CultureInfo.InvariantCulture, style, out var expires))
                                {
                                    cookie.Expires = expires;
                                }
                                else if (DateTime.TryParseExact(unQuotedValue, _dateTimeFormats,
                                                                DateTimeCultureInfo.TwoDigitYear, style, out expires))
                                {
                                    cookie.Expires = expires;
                                }
                                else
                                {
                                    //this cookie will be rejected
                                    cookie.InternalSetName(string.Empty);
                                    rejected = true;
                                }
                            }
                            break;
                        }

                        case CookieToken.MaxAge:
                        {
                            if (!expiresSet)
                            {
                                expiresSet = true;
                                if (int.TryParse(CheckQuoted(m_tokenizer.Value), out var parsed))
                                {
                                    cookie.Expires = DateTime.Now.AddSeconds((double)parsed);
                                }
                                else
                                {
                                    //this cookie will be rejected
                                    cookie.InternalSetName(string.Empty);
                                    rejected = true;
                                }
                            }

                            break;
                        }

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

                            break;
                        }

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

                            break;
                        }

                        case CookieToken.Version:
                        {
                            if (!versionSet)
                            {
                                versionSet = true;
                                if (int.TryParse(CheckQuoted(m_tokenizer.Value), out var parsed))
                                {
                                    cookie.Version         = parsed;
                                    cookie.IsQuotedVersion = m_tokenizer.Quoted;
                                }
                                else
                                {
                                    //this cookie will be rejected
                                    cookie.InternalSetName(string.Empty);
                                    rejected = true;
                                }
                            }
                            break;
                        }
                        }
                        break;

                    case CookieToken.Attribute:
                        switch (m_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 (!m_tokenizer.Eof && !m_tokenizer.EndOfCookie && !rejected);
            return(cookie);
        }
        internal CookieToken FindNext(bool ignoreComma, bool ignoreEquals)
        {
            this.m_tokenLength = 0;
            this.m_start       = this.m_index;
            while ((this.m_index < this.m_length) && char.IsWhiteSpace(this.m_tokenStream[this.m_index]))
            {
                this.m_index++;
                this.m_start++;
            }
            CookieToken end = CookieToken.End;
            int         num = 1;

            if (!this.Eof)
            {
                if (this.m_tokenStream[this.m_index] == '"')
                {
                    this.Quoted = true;
                    this.m_index++;
                    bool flag = false;
                    while (this.m_index < this.m_length)
                    {
                        char ch = this.m_tokenStream[this.m_index];
                        if (!flag && (ch == '"'))
                        {
                            break;
                        }
                        if (flag)
                        {
                            flag = false;
                        }
                        else if (ch == '\\')
                        {
                            flag = true;
                        }
                        this.m_index++;
                    }
                    if (this.m_index < this.m_length)
                    {
                        this.m_index++;
                    }
                    this.m_tokenLength = this.m_index - this.m_start;
                    num         = 0;
                    ignoreComma = false;
                }
                while ((((this.m_index < this.m_length) && (this.m_tokenStream[this.m_index] != ';')) && (ignoreEquals || (this.m_tokenStream[this.m_index] != '='))) && (ignoreComma || (this.m_tokenStream[this.m_index] != ',')))
                {
                    if (this.m_tokenStream[this.m_index] == ',')
                    {
                        this.m_start       = this.m_index + 1;
                        this.m_tokenLength = -1;
                        ignoreComma        = false;
                    }
                    this.m_index++;
                    this.m_tokenLength += num;
                }
                if (this.Eof)
                {
                    return(end);
                }
                switch (this.m_tokenStream[this.m_index])
                {
                case ';':
                    end = CookieToken.EndToken;
                    break;

                case '=':
                    end = CookieToken.Equals;
                    break;

                default:
                    end = CookieToken.EndCookie;
                    break;
                }
                this.m_index++;
            }
            return(end);
        }
Example #11
0
 // Reset
 //
 // Sets this tokenizer up for finding the next name/value pair,
 // attribute, or end-of-{token,cookie,line}.
 internal void Reset()
 {
     _eofCookie = false;
     _name = string.Empty;
     _quoted = false;
     _start = _index;
     _token = CookieToken.Nothing;
     _tokenLength = 0;
     _value = string.Empty;
 }
        internal Cookie Get()
        {
            Cookie cookie = null;
            bool   flag   = false;
            bool   flag2  = false;
            bool   flag3  = false;
            bool   flag4  = false;
            bool   flag5  = false;
            bool   flag6  = false;
            bool   flag7  = false;
            bool   flag8  = false;
            bool   flag9  = false;

            do
            {
                CookieToken token = this.m_tokenizer.Next(cookie == null, true);
                if ((cookie == null) && ((token == CookieToken.NameValuePair) || (token == CookieToken.Attribute)))
                {
                    cookie = new Cookie();
                    if (!cookie.InternalSetName(this.m_tokenizer.Name))
                    {
                        cookie.InternalSetName(string.Empty);
                    }
                    cookie.Value = this.m_tokenizer.Value;
                }
                else
                {
                    switch (token)
                    {
                    case CookieToken.NameValuePair:
                        switch (this.m_tokenizer.Token)
                        {
                        case CookieToken.Comment:
                            if (!flag)
                            {
                                flag           = true;
                                cookie.Comment = this.m_tokenizer.Value;
                            }
                            break;

                        case CookieToken.CommentUrl:
                            if (!flag2)
                            {
                                Uri uri;
                                flag2 = true;
                                if (Uri.TryCreate(CheckQuoted(this.m_tokenizer.Value), UriKind.Absolute, out uri))
                                {
                                    cookie.CommentUri = uri;
                                }
                            }
                            break;

                        case CookieToken.Domain:
                            if (!flag3)
                            {
                                flag3                 = true;
                                cookie.Domain         = CheckQuoted(this.m_tokenizer.Value);
                                cookie.IsQuotedDomain = this.m_tokenizer.Quoted;
                            }
                            break;

                        case CookieToken.Expires:
                            if (!flag4)
                            {
                                DateTime time;
                                flag4 = true;
                                if (DateTime.TryParse(CheckQuoted(this.m_tokenizer.Value), out time))
                                {
                                    cookie.Expires = time;
                                }
                                else
                                {
                                    cookie.InternalSetName(string.Empty);
                                }
                            }
                            break;

                        case CookieToken.MaxAge:
                            if (!flag4)
                            {
                                int num;
                                flag4 = true;
                                if (int.TryParse(CheckQuoted(this.m_tokenizer.Value), out num))
                                {
                                    cookie.Expires = DateTime.Now.AddSeconds((double)num);
                                }
                                else
                                {
                                    cookie.InternalSetName(string.Empty);
                                }
                            }
                            break;

                        case CookieToken.Path:
                            if (!flag5)
                            {
                                flag5       = true;
                                cookie.Path = this.m_tokenizer.Value;
                            }
                            break;

                        case CookieToken.Port:
                            if (!flag6)
                            {
                                flag6 = true;
                                try
                                {
                                    cookie.Port = this.m_tokenizer.Value;
                                }
                                catch
                                {
                                    cookie.InternalSetName(string.Empty);
                                }
                            }
                            break;

                        case CookieToken.Version:
                            if (!flag7)
                            {
                                int num2;
                                flag7 = true;
                                if (int.TryParse(CheckQuoted(this.m_tokenizer.Value), out num2))
                                {
                                    cookie.Version         = num2;
                                    cookie.IsQuotedVersion = this.m_tokenizer.Quoted;
                                }
                                else
                                {
                                    cookie.InternalSetName(string.Empty);
                                }
                            }
                            break;
                        }
                        break;

                    case CookieToken.Attribute:
                        switch (this.m_tokenizer.Token)
                        {
                        case CookieToken.Port:
                            if (!flag6)
                            {
                                flag6       = true;
                                cookie.Port = string.Empty;
                            }
                            break;

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

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

                        case CookieToken.Discard:
                            if (!flag9)
                            {
                                flag9          = true;
                                cookie.Discard = true;
                            }
                            break;
                        }
                        break;
                    }
                }
            }while (!this.m_tokenizer.Eof && !this.m_tokenizer.EndOfCookie);
            return(cookie);
        }
        internal Cookie GetServer()
        {
            Cookie savedCookie = this.m_savedCookie;

            this.m_savedCookie = null;
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = false;

            do
            {
                bool        first = ((savedCookie == null) || (savedCookie.Name == null)) || (savedCookie.Name.Length == 0);
                CookieToken token = this.m_tokenizer.Next(first, false);
                if (first && ((token == CookieToken.NameValuePair) || (token == CookieToken.Attribute)))
                {
                    if (savedCookie == null)
                    {
                        savedCookie = new Cookie();
                    }
                    if (!savedCookie.InternalSetName(this.m_tokenizer.Name))
                    {
                        savedCookie.InternalSetName(string.Empty);
                    }
                    savedCookie.Value = this.m_tokenizer.Value;
                }
                else
                {
                    switch (token)
                    {
                    case CookieToken.NameValuePair:
                        switch (this.m_tokenizer.Token)
                        {
                        case CookieToken.Domain:
                            if (!flag)
                            {
                                flag = true;
                                savedCookie.Domain         = CheckQuoted(this.m_tokenizer.Value);
                                savedCookie.IsQuotedDomain = this.m_tokenizer.Quoted;
                            }
                            break;

                        case CookieToken.Path:
                            if (!flag2)
                            {
                                flag2            = true;
                                savedCookie.Path = this.m_tokenizer.Value;
                            }
                            break;

                        case CookieToken.Port:
                            if (!flag3)
                            {
                                flag3 = true;
                                try
                                {
                                    savedCookie.Port = this.m_tokenizer.Value;
                                }
                                catch (CookieException)
                                {
                                    savedCookie.InternalSetName(string.Empty);
                                }
                            }
                            break;

                        case CookieToken.Unknown:
                            this.m_savedCookie = new Cookie();
                            if (!this.m_savedCookie.InternalSetName(this.m_tokenizer.Name))
                            {
                                this.m_savedCookie.InternalSetName(string.Empty);
                            }
                            this.m_savedCookie.Value = this.m_tokenizer.Value;
                            return(savedCookie);

                        case CookieToken.Version:
                            int num;
                            this.m_savedCookie = new Cookie();
                            if (int.TryParse(this.m_tokenizer.Value, out num))
                            {
                                this.m_savedCookie.Version = num;
                            }
                            return(savedCookie);
                        }
                        break;

                    case CookieToken.Attribute:
                        if ((this.m_tokenizer.Token == CookieToken.Port) && !flag3)
                        {
                            flag3            = true;
                            savedCookie.Port = string.Empty;
                        }
                        break;
                    }
                }
            }while (!this.m_tokenizer.Eof && !this.m_tokenizer.EndOfCookie);
            return(savedCookie);
        }
Example #14
0
        //
        // Reset
        //
        //  set up this tokenizer for finding the next name/value pair or
        //  attribute, or end-of-[token, cookie, or line]
        //

        internal void Reset() {
            m_eofCookie = false;
            m_name = string.Empty;
            m_quoted = false;
            m_start = m_index;
            m_token = CookieToken.Nothing;
            m_tokenLength = 0;
            m_value = string.Empty;
        }
Example #15
0
        // FindNext
        //
        // Find the start and length of the next token. The token is terminated
        // by one of:
        //     - end-of-line
        //     - end-of-cookie: unquoted comma separates multiple cookies
        //     - end-of-token: unquoted semi-colon
        //     - end-of-name: unquoted equals
        //
        // Inputs:
        // <argument>  ignoreComma
        //     true if parsing doesn't stop at a comma. This is only true when
        //     we know we're parsing an original cookie that has an expires=
        //     attribute, because the format of the time/date used in expires
        //     is:
        //         Wdy, dd-mmm-yyyy HH:MM:SS GMT
        //
        // <argument>  ignoreEquals
        //     true if parsing doesn't stop at an equals sign. The LHS of the
        //     first equals sign is an attribute name. The next token may
        //     include one or more equals signs. For example:
        //          SESSIONID=ID=MSNx45&q=33
        //
        // Outputs:
        // <member>    _index
        //     incremented to the last position in _tokenStream contained by
        //     the current token
        //
        // <member>    _start
        //     incremented to the start of the current token
        //
        // <member>    _tokenLength
        //     set to the length of the current token
        //
        // Assumes: Nothing
        //
        // Returns:
        // type of CookieToken found:
        //
        //     End         - end of the cookie string
        //     EndCookie   - end of current cookie in (potentially) a
        //                   multi-cookie string
        //     EndToken    - end of name=value pair, or end of an attribute
        //     Equals      - end of name=
        //
        // Throws: Nothing
        internal CookieToken FindNext(bool ignoreComma, bool ignoreEquals)
        {
            _tokenLength = 0;
            _start       = _index;
            while ((_index < _length) && char.IsWhiteSpace(_tokenStream[_index]))
            {
                ++_index;
                ++_start;
            }

            CookieToken token     = CookieToken.End;
            int         increment = 1;

            if (!Eof)
            {
                if (_tokenStream[_index] == '"')
                {
                    Quoted = true;
                    ++_index;
                    bool quoteOn = false;
                    while (_index < _length)
                    {
                        char currChar = _tokenStream[_index];
                        if (!quoteOn && currChar == '"')
                        {
                            break;
                        }

                        if (quoteOn)
                        {
                            quoteOn = false;
                        }
                        else if (currChar == '\\')
                        {
                            quoteOn = true;
                        }
                        ++_index;
                    }
                    if (_index < _length)
                    {
                        ++_index;
                    }
                    _tokenLength = _index - _start;
                    increment    = 0;
                    // If we are here, reset ignoreComma.
                    // In effect, we ignore everything after quoted string until the next delimiter.
                    ignoreComma = false;
                }
                while ((_index < _length) &&
                       (_tokenStream[_index] != ';') &&
                       (ignoreEquals || (_tokenStream[_index] != '=')) &&
                       (ignoreComma || (_tokenStream[_index] != ',')))
                {
                    // Fixing 2 things:
                    // 1) ignore day of week in cookie string
                    // 2) revert ignoreComma once meet it, so won't miss the next cookie)
                    if (_tokenStream[_index] == ',')
                    {
                        _start       = _index + 1;
                        _tokenLength = -1;
                        ignoreComma  = false;
                    }
                    ++_index;
                    _tokenLength += increment;
                }
                if (!Eof)
                {
                    switch (_tokenStream[_index])
                    {
                    case ';':
                        token = CookieToken.EndToken;
                        break;

                    case '=':
                        token = CookieToken.Equals;
                        break;

                    default:
                        _cookieLength = _index - _cookieStartIndex;
                        token         = CookieToken.EndCookie;
                        break;
                    }
                    ++_index;
                }

                if (Eof)
                {
                    _cookieLength = _index - _cookieStartIndex;
                }
            }
            return(token);
        }
Example #16
0
 internal RecognizedAttribute(string name, CookieToken token) {
     m_name = name;
     m_token = token;
 }
Example #17
0
        // twin parsing method, different enough that it's better to split it into
        // a different method
        internal CookieInternal GetServer()
        {
            CookieInternal cookie = m_savedCookie;

            m_savedCookie = null;

            // only first ocurence of an attribute value must be counted
            bool domainSet = false;
            bool pathSet   = false;
            bool portSet   = false; //special case as it may have no value in header

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

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

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

                        case CookieToken.Port:
                            if (!portSet)
                            {
                                portSet = true;
                                try
                                {
                                    cookie.Port = m_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.
                            m_savedCookie = new CookieInternal();
                            int parsed;
                            if (int.TryParse(m_tokenizer.Value, out parsed))
                            {
                                m_savedCookie.Version = parsed;
                            }
                            return(cookie);

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

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