Exemplo n.º 1
0
        protected DefaultCookie InitCookie(string header, int nameBegin, int nameEnd, int valueBegin, int valueEnd)
        {
            if (nameBegin == -1 || nameBegin == nameEnd)
            {
                //Logger.Debug("Skipping cookie with null name");
                return(null);
            }

            if (valueBegin == -1)
            {
                //Logger.Debug("Skipping cookie with null value");
                return(null);
            }

            var           sequence       = new StringCharSequence(header, valueBegin, valueEnd - valueBegin);
            ICharSequence unwrappedValue = UnwrapValue(sequence);

            if (unwrappedValue == null)
            {
                //Logger.Debug("Skipping cookie because starting quotes are not properly balanced in '{}'", sequence);
                return(null);
            }

            string name = header.Substring(nameBegin, nameEnd - nameBegin);

            int invalidOctetPos;

            if (this.Strict && (invalidOctetPos = FirstInvalidCookieNameOctet(name)) >= 0)
            {
                //if (//Logger.DebugEnabled)
                //{
                //    //Logger.Debug("Skipping cookie because name '{}' contains invalid char '{}'",
                //        //name, name[invalidOctetPos]);
                //}
                return(null);
            }

            bool wrap = unwrappedValue.Count != valueEnd - valueBegin;

            if (this.Strict && (invalidOctetPos = FirstInvalidCookieValueOctet(unwrappedValue)) >= 0)
            {
                //if (//Logger.DebugEnabled)
                //{
                //    //Logger.Debug("Skipping cookie because value '{}' contains invalid char '{}'",
                //        unwrappedValue, unwrappedValue[invalidOctetPos]);
                //}

                return(null);
            }

            var cookie = new DefaultCookie(name, unwrappedValue.ToString());

            cookie.Wrap = wrap;
            return(cookie);
        }
Exemplo n.º 2
0
        private void Decode(string header, ICollection <ICookie> cookies)
        {
            int headerLen = header.Length;
            int i         = 0;

            bool rfc2965Style = false;

            if (CharUtil.RegionMatchesIgnoreCase(header, 0, RFC2965Version, 0, RFC2965Version.Count))
            {
                // RFC 2965 style cookie, move to after version value
                i            = header.IndexOf(';') + 1;
                rfc2965Style = true;
            }

            // loop
            while (true)
            {
                // Skip spaces and separators.
                while (true)
                {
                    if (i == headerLen)
                    {
                        goto loop;
                    }
                    char c = header[i];
                    if (IsSpace(c))
                    {
                        i++;
                        continue;
                    }
                    break;
                }

                int nameBegin = i;
                int nameEnd;
                int valueBegin;
                int valueEnd;

                while (true)
                {
                    char curChar = header[i];
                    switch (curChar)
                    {
                    case HttpConstants.SemicolonChar:
                        // NAME; (no value till ';')
                        nameEnd    = i;
                        valueBegin = valueEnd = -1;
                        goto loop0;

                    case HttpConstants.EqualsSignChar:
                        // NAME=VALUE
                        nameEnd = i;
                        i++;
                        if (i == headerLen)
                        {
                            // NAME= (empty value, i.e. nothing after '=')
                            valueBegin = valueEnd = 0;
                            goto loop0;
                        }

                        valueBegin = i;
                        // NAME=VALUE;
                        int semiPos = header.IndexOf(';', i);
                        valueEnd = i = semiPos > 0 ? semiPos : headerLen;
                        goto loop0;

                    default:
                        i++;
                        break;
                    }

                    if (i == headerLen)
                    {
                        // NAME (no value till the end of string)
                        nameEnd    = headerLen;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                }
loop0:
                if (rfc2965Style && (CharUtil.RegionMatches(header, nameBegin, RFC2965Path, 0, RFC2965Path.Count) ||
                                     CharUtil.RegionMatches(header, nameBegin, RFC2965Domain, 0, RFC2965Domain.Count) ||
                                     CharUtil.RegionMatches(header, nameBegin, RFC2965Port, 0, RFC2965Port.Count)))
                {
                    // skip obsolete RFC2965 fields
                    continue;
                }

                DefaultCookie cookie = InitCookie(header, nameBegin, nameEnd, valueBegin, valueEnd);
                if (cookie is object)
                {
                    cookies.Add(cookie);
                }
            }

loop:
            return;
        }
        public ISet <ICookie> Decode(string header)
        {
            Contract.Requires(header != null);

            int headerLen = header.Length;

            if (headerLen == 0)
            {
                return(Empty);
            }

            var cookies = new SortedSet <ICookie>();

            int i = 0;

            bool rfc2965Style = false;

            if (CharUtil.RegionMatchesIgnoreCase(header, 0, RFC2965Version, 0, RFC2965Version.Count))
            {
                // RFC 2965 style cookie, move to after version value
                i            = header.IndexOf(';') + 1;
                rfc2965Style = true;
            }

            // loop
            for (;;)
            {
                // Skip spaces and separators.
                for (;;)
                {
                    if (i == headerLen)
                    {
                        goto loop;
                    }
                    char c = header[i];
                    if (c == '\t' || c == '\n' || c == 0x0b || c == '\f' ||
                        c == '\r' || c == ' ' || c == ',' || c == ';')
                    {
                        i++;
                        continue;
                    }
                    break;
                }

                int nameBegin = i;
                int nameEnd;
                int valueBegin;
                int valueEnd;

                for (;;)
                {
                    char curChar = header[i];
                    if (curChar == ';')
                    {
                        // NAME; (no value till ';')
                        nameEnd    = i;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                    else if (curChar == '=')
                    {
                        // NAME=VALUE
                        nameEnd = i;
                        i++;
                        if (i == headerLen)
                        {
                            // NAME= (empty value, i.e. nothing after '=')
                            valueBegin = valueEnd = 0;
                            break;
                        }

                        valueBegin = i;
                        // NAME=VALUE;
                        int semiPos = header.IndexOf(';', i);
                        valueEnd = i = semiPos > 0 ? semiPos : headerLen;
                        break;
                    }
                    else
                    {
                        i++;
                    }

                    if (i == headerLen)
                    {
                        // NAME (no value till the end of string)
                        nameEnd    = headerLen;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                }

                if (rfc2965Style && (CharUtil.RegionMatches(header, nameBegin, RFC2965Path, 0, RFC2965Path.Count) ||
                                     CharUtil.RegionMatches(header, nameBegin, RFC2965Domain, 0, RFC2965Domain.Count) ||
                                     CharUtil.RegionMatches(header, nameBegin, RFC2965Port, 0, RFC2965Port.Count)))
                {
                    // skip obsolete RFC2965 fields
                    continue;
                }

                DefaultCookie cookie = this.InitCookie(header, nameBegin, nameEnd, valueBegin, valueEnd);
                if (cookie != null)
                {
                    cookies.Add(cookie);
                }
            }

loop:
            return(cookies);
        }
Exemplo n.º 4
0
        protected DefaultCookie InitCookie(string header, int nameBegin, int nameEnd, int valueBegin, int valueEnd)
        {
            if (nameBegin == -1 || nameBegin == nameEnd)
            {
                if (Logger.DebugEnabled)
                {
                    Logger.SkippingCookieWithNullName();
                }
                return(null);
            }

            if (valueBegin == -1)
            {
                if (Logger.DebugEnabled)
                {
                    Logger.SkippingCookieWithNullValue();
                }
                return(null);
            }

            var           sequence       = new StringCharSequence(header, valueBegin, valueEnd - valueBegin);
            ICharSequence unwrappedValue = UnwrapValue(sequence);

            if (unwrappedValue is null)
            {
                if (Logger.DebugEnabled)
                {
                    Logger.SkippingCookieBecauseStartingQuotesAreNotProperlyBalancedIn(sequence);
                }
                return(null);
            }

            string name = header.Substring(nameBegin, nameEnd - nameBegin);

            int invalidOctetPos;

            if (this.Strict && (invalidOctetPos = FirstInvalidCookieNameOctet(name)) >= 0)
            {
                if (Logger.DebugEnabled)
                {
                    Logger.SkippingCookieBecauseNameContainsInvalidChar(name, invalidOctetPos);
                }
                return(null);
            }

            bool wrap = unwrappedValue.Count != valueEnd - valueBegin;

            if (this.Strict && (invalidOctetPos = FirstInvalidCookieValueOctet(unwrappedValue)) >= 0)
            {
                if (Logger.DebugEnabled)
                {
                    Logger.SkippingCookieBecauseValueContainsInvalidChar(unwrappedValue, invalidOctetPos);
                }

                return(null);
            }

            var cookie = new DefaultCookie(name, unwrappedValue.ToString());

            cookie.Wrap = wrap;
            return(cookie);
        }
Exemplo n.º 5
0
        public ICookie Decode(string header)
        {
            if (header is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.header);
            }

            int headerLen = header.Length;

            if (0u >= (uint)headerLen)
            {
                return(null);
            }

            CookieBuilder cookieBuilder = null;

            //loop:
            for (int i = 0; ;)
            {
                // Skip spaces and separators.
                while (true)
                {
                    if (i == headerLen)
                    {
                        goto loop;
                    }
                    char c = header[i];
                    if (c == HttpConstants.CommaChar)
                    {
                        // Having multiple cookies in a single Set-Cookie header is
                        // deprecated, modern browsers only parse the first one
                        goto loop;
                    }
                    else if (IsSpace(c))
                    {
                        i++;
                        continue;
                    }
                    break;
                }

                int nameBegin  = i;
                int nameEnd    = 0;
                int valueBegin = 0;
                int valueEnd   = 0;

                while (true)
                {
                    char curChar = header[i];
                    switch (curChar)
                    {
                    case HttpConstants.SemicolonChar:
                        // NAME; (no value till ';')
                        nameEnd    = i;
                        valueBegin = valueEnd = -1;
                        goto loop0;

                    case HttpConstants.EqualsSignChar:
                        // NAME=VALUE
                        nameEnd = i;
                        i++;
                        if (i == headerLen)
                        {
                            // NAME= (empty value, i.e. nothing after '=')
                            valueBegin = valueEnd = 0;
                            goto loop0;
                        }

                        valueBegin = i;
                        // NAME=VALUE;
                        int semiPos = header.IndexOf(';', i);
                        valueEnd = i = semiPos > 0 ? semiPos : headerLen;
                        goto loop0;

                    default:
                        i++;
                        break;
                    }

                    if (i == headerLen)
                    {
                        // NAME (no value till the end of string)
                        nameEnd    = headerLen;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                }

loop0:
                if (valueEnd > 0 && header[valueEnd - 1] == HttpConstants.CommaChar)
                {
                    // old multiple cookies separator, skipping it
                    valueEnd--;
                }

                if (cookieBuilder is null)
                {
                    // cookie name-value pair
                    DefaultCookie cookie = InitCookie(header, nameBegin, nameEnd, valueBegin, valueEnd);

                    if (cookie is null)
                    {
                        return(null);
                    }

                    cookieBuilder = new CookieBuilder(cookie, header);
                }
                else
                {
                    // cookie attribute
                    cookieBuilder.AppendAttribute(nameBegin, nameEnd, valueBegin, valueEnd);
                }
            }

loop:
            return(cookieBuilder?.Cookie());
        }
Exemplo n.º 6
0
 internal CookieBuilder(DefaultCookie cookie, string header)
 {
     _cookie = cookie;
     _header = header;
 }
        public ICookie Decode(string header)
        {
            Contract.Requires(header != null);

            int headerLen = header.Length;

            if (headerLen == 0)
            {
                return(null);
            }

            CookieBuilder cookieBuilder = null;

            //loop:
            for (int i = 0;;)
            {
                // Skip spaces and separators.
                for (;;)
                {
                    if (i == headerLen)
                    {
                        goto loop;
                    }
                    char c = header[i];
                    if (c == ',')
                    {
                        // Having multiple cookies in a single Set-Cookie header is
                        // deprecated, modern browsers only parse the first one
                        goto loop;
                    }
                    else if (c == '\t' || c == '\n' || c == 0x0b || c == '\f' ||
                             c == '\r' || c == ' ' || c == ';')
                    {
                        i++;
                        continue;
                    }
                    break;
                }

                int nameBegin = i;
                int nameEnd;
                int valueBegin;
                int valueEnd;

                for (;;)
                {
                    char curChar = header[i];
                    if (curChar == ';')
                    {
                        // NAME; (no value till ';')
                        nameEnd    = i;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                    else if (curChar == '=')
                    {
                        // NAME=VALUE
                        nameEnd = i;
                        i++;
                        if (i == headerLen)
                        {
                            // NAME= (empty value, i.e. nothing after '=')
                            valueBegin = valueEnd = 0;
                            break;
                        }

                        valueBegin = i;
                        // NAME=VALUE;
                        int semiPos = header.IndexOf(';', i);
                        valueEnd = i = semiPos > 0 ? semiPos : headerLen;
                        break;
                    }
                    else
                    {
                        i++;
                    }

                    if (i == headerLen)
                    {
                        // NAME (no value till the end of string)
                        nameEnd    = headerLen;
                        valueBegin = valueEnd = -1;
                        break;
                    }
                }

                if (valueEnd > 0 && header[valueEnd - 1] == ',')
                {
                    // old multiple cookies separator, skipping it
                    valueEnd--;
                }

                if (cookieBuilder == null)
                {
                    // cookie name-value pair
                    DefaultCookie cookie = this.InitCookie(header, nameBegin, nameEnd, valueBegin, valueEnd);

                    if (cookie == null)
                    {
                        return(null);
                    }

                    cookieBuilder = new CookieBuilder(cookie, header);
                }
                else
                {
                    // cookie attribute
                    cookieBuilder.AppendAttribute(nameBegin, nameEnd, valueBegin, valueEnd);
                }
            }

loop:
            Debug.Assert(cookieBuilder != null);
            return(cookieBuilder.Cookie());
        }
 internal CookieBuilder(DefaultCookie cookie, string header)
 {
     this.cookie = cookie;
     this.header = header;
 }