Ejemplo n.º 1
0
        protected void ValidateCookie(string name, string value)
        {
            if (!this.Strict)
            {
                return;
            }

            int pos;

            if ((pos = FirstInvalidCookieNameOctet(name)) >= 0)
            {
                throw new ArgumentException($"Cookie name contains an invalid char: {name[pos]}");
            }

            var           sequnce        = new StringCharSequence(value);
            ICharSequence unwrappedValue = UnwrapValue(sequnce);

            if (unwrappedValue == null)
            {
                throw new ArgumentException($"Cookie value wrapping quotes are not balanced: {value}");
            }

            if ((pos = FirstInvalidCookieValueOctet(unwrappedValue)) >= 0)
            {
                throw new ArgumentException($"Cookie value contains an invalid char: {value[pos]}");
            }
        }
Ejemplo n.º 2
0
        private static ICharSequence EscapeChar(ICharSequence str, CultureInfo locale)
        {
            if (str == null || str.Length == 0)
            {
                return(str);
            }

            ICharSequence buffer = str;

            // regular escapable Char for terms
            for (int i = 0; i < escapableTermChars.Length; i++)
            {
                buffer = ReplaceIgnoreCase(buffer, locale.TextInfo.ToLower(escapableTermChars[i]),
                                           "\\", locale);
            }

            // First Character of a term as more escaping chars
            for (int i = 0; i < escapableTermExtraFirstChars.Length; i++)
            {
                if (buffer[0] == escapableTermExtraFirstChars[i][0])
                {
                    buffer = new StringCharSequence("\\" + buffer[0]
                                                    + buffer.Subsequence(1, buffer.Length - 1).ToString()); // LUCENENET: Corrected 2nd Subsequence parameter
                    break;
                }
            }

            return(buffer);
        }
Ejemplo n.º 3
0
        protected void ValidateCookie(string name, string value)
        {
            if (!this.Strict)
            {
                return;
            }

            int pos;

            if ((pos = FirstInvalidCookieNameOctet(name)) >= 0)
            {
                ThrowHelper.ThrowArgumentException_CookieName(name, pos);
            }

            var           sequnce        = new StringCharSequence(value);
            ICharSequence unwrappedValue = UnwrapValue(sequnce);

            if (unwrappedValue is null)
            {
                ThrowHelper.ThrowArgumentException_CookieValue(value);
            }

            if ((pos = FirstInvalidCookieValueOctet(unwrappedValue)) >= 0)
            {
                ThrowHelper.ThrowArgumentException_CookieValue(unwrappedValue, pos);
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void GlobalSetup()
        {
            var bytes = new byte[this.Size];

            RandomGenerator.NextBytes(bytes);

            this.asciiString = new AsciiString(bytes, false);
            string value = Encoding.ASCII.GetString(bytes);

            this.stringValue = new StringCharSequence(value);
        }
Ejemplo n.º 6
0
        public virtual void TestFromCharSequence()
        {
            for (int i = 0; i < 100; i++)
            {
                ICharSequence s  = new StringCharSequence(TestUtil.RandomUnicodeString(Random));
                string        s2 = (new BytesRef(s)).Utf8ToString();
                Assert.AreEqual(s, s2);
            }

            // only for 4.x
            Assert.AreEqual("\uFFFF", (new BytesRef("\uFFFF")).Utf8ToString());
        }
Ejemplo n.º 7
0
        private void ParseRelationStrings(CollationStrength strength, int i)
        {
            // Parse
            //     prefix | str / extension
            // where prefix and extension are optional.
            StringCharSequence prefix    = new StringCharSequence("");
            string             extension = "";

            i = ParseTailoringString(i, rawBuilder.Value);
            char next = (i < rules.Length) ? rules[i] : (char)0;

            if (next == 0x7c)
            {  // '|' separates the context prefix from the string.
                prefix = new StringCharSequence(rawBuilder.ToString());
                i      = ParseTailoringString(i + 1, rawBuilder.Value);
                next   = (i < rules.Length) ? rules[i] : (char)0;
            }
            // str = rawBuilder (do not modify rawBuilder any more in this function)
            if (next == 0x2f)
            {  // '/' separates the string from the extension.
                StringBuilder extBuilder = new StringBuilder();
                i         = ParseTailoringString(i + 1, extBuilder);
                extension = extBuilder.ToString();
            }
            if (prefix.Length != 0)
            {
                int prefix0 = prefix.Value.CodePointAt(0);
                int c       = rawBuilder.Value.CodePointAt(0);
                if (!nfc.HasBoundaryBefore(prefix0) || !nfc.HasBoundaryBefore(c))
                {
                    SetParseError("in 'prefix|str', prefix and str must each start with an NFC boundary");
                    return;
                }
            }
            try
            {
                sink.AddRelation(strength, prefix, rawBuilder, extension);
            }
            catch (Exception e)
            {
                SetParseError("adding relation failed", e);
                return;
            }
            ruleIndex = i;
        }
Ejemplo n.º 8
0
        private void ParseStarredCharacters(CollationStrength strength, int i)
        {
            StringCharSequence empty = new StringCharSequence("");

            i = ParseString(SkipWhiteSpace(i), rawBuilder.Value);
            if (rawBuilder.Length == 0)
            {
                SetParseError("missing starred-relation string");
                return;
            }
            int prev = -1;
            int j    = 0;

            for (; ;)
            {
                while (j < rawBuilder.Length)
                {
                    int cp = rawBuilder.Value.CodePointAt(j);
                    if (!nfd.IsInert(cp))
                    {
                        SetParseError("starred-relation string is not all NFD-inert");
                        return;
                    }
                    try
                    {
                        sink.AddRelation(strength, empty, UTF16.ValueOf(cp).AsCharSequence(), empty.Value);
                    }
                    catch (Exception e)
                    {
                        SetParseError("adding relation failed", e);
                        return;
                    }
                    j   += Character.CharCount(cp);
                    prev = cp;
                }
                if (i >= rules.Length || rules[i] != 0x2d)
                {  // '-'
                    break;
                }
                if (prev < 0)
                {
                    SetParseError("range without start in starred-relation string");
                    return;
                }
                i = ParseString(i + 1, rawBuilder.Value);
                if (rawBuilder.Length == 0)
                {
                    SetParseError("range without end in starred-relation string");
                    return;
                }
                int c = rawBuilder.Value.CodePointAt(0);
                if (c < prev)
                {
                    SetParseError("range start greater than end in starred-relation string");
                    return;
                }
                // range prev-c
                while (++prev <= c)
                {
                    if (!nfd.IsInert(prev))
                    {
                        SetParseError("starred-relation string range is not all NFD-inert");
                        return;
                    }
                    if (IsSurrogate(prev))
                    {
                        SetParseError("starred-relation string range contains a surrogate");
                        return;
                    }
                    if (0xfffd <= prev && prev <= 0xffff)
                    {
                        SetParseError("starred-relation string range contains U+FFFD, U+FFFE or U+FFFF");
                        return;
                    }
                    try
                    {
                        sink.AddRelation(strength, empty, UTF16.ValueOf(prev).AsCharSequence(), empty.Value);
                    }
                    catch (Exception e)
                    {
                        SetParseError("adding relation failed", e);
                        return;
                    }
                }
                prev = -1;
                j    = Character.CharCount(c);
            }
            ruleIndex = SkipWhiteSpace(i);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 public static void SkippingCookieBecauseStartingQuotesAreNotProperlyBalancedIn(this IInternalLogger logger, StringCharSequence sequence)
 {
     logger.Debug("Skipping cookie because starting quotes are not properly balanced in '{}'", sequence);
 }