Esempio n. 1
0
        /// <inheritdoc />
        protected internal override void EncodeInitialLine(IByteBuffer buf, IHttpRequest request)
        {
            ByteBufferUtil.Copy(request.Method.AsciiName, buf);

            string uri = request.Uri;

            if (string.IsNullOrEmpty(uri))
            {
                // Add / as absolute path if no is present.
                // See http://tools.ietf.org/html/rfc2616#section-5.1.2
                _ = buf.WriteMedium(SpaceSlashAndSpaceMedium);
            }
            else
            {
                var uriCharSequence = new StringBuilderCharSequence();
                uriCharSequence.Append(uri);

                bool needSlash = false;
                int  start     = uri.IndexOf("://", StringComparison.Ordinal);
                if (start != -1 && uri[0] != Slash)
                {
                    start += 3;
                    // Correctly handle query params.
                    // See https://github.com/netty/netty/issues/2732
                    int index = uri.IndexOf(QuestionMark, start);
                    if (index == -1)
                    {
                        if (uri.LastIndexOf(Slash) < start)
                        {
                            needSlash = true;
                        }
                    }
                    else
                    {
                        if (uri.LastIndexOf(Slash, index) < start)
                        {
                            uriCharSequence.Insert(index, Slash);
                        }
                    }
                }

                _ = buf.WriteByte(HorizontalSpace).WriteCharSequence(uriCharSequence, Encoding.UTF8);
                if (needSlash)
                {
                    // write "/ " after uri
                    _ = buf.WriteShort(SlashAndSpaceShort);
                }
                else
                {
                    _ = buf.WriteByte(HorizontalSpace);
                }
            }

            request.ProtocolVersion.Encode(buf);
            _ = buf.WriteShort(CrlfShort);
        }
Esempio n. 2
0
            static ICharSequence CommaSeparateEscapedValues(ICharSequence currentValue, ICharSequence value)
            {
                var builder = new StringBuilderCharSequence(currentValue.Count + 1 + value.Count);

                builder.Append(currentValue);
                builder.Append(Comma);
                builder.Append(value);

                return(builder);
            }
Esempio n. 3
0
        static void DoCaseSensitivity(int len)
        {
            // Build an upper case and lower case string
            const int UpperA       = 'A';
            const int UpperZ       = 'Z';
            const int UpperToLower = (int)'a' - UpperA;

            var lowerCaseBytes   = new byte[len];
            var upperCaseBuilder = new StringBuilderCharSequence(len);

            for (int i = 0; i < len; ++i)
            {
                char upper = (char)(Rand.Next((UpperZ - UpperA) + 1) + UpperA);
                upperCaseBuilder.Append(upper);
                lowerCaseBytes[i] = (byte)(upper + UpperToLower);
            }
            var upperCaseString = (StringCharSequence)upperCaseBuilder.ToString();
            var lowerCaseString = (StringCharSequence) new string(lowerCaseBytes.Select(x => (char)x).ToArray());
            var lowerCaseAscii  = new AsciiString(lowerCaseBytes, false);
            var upperCaseAscii  = new AsciiString(upperCaseString);

            // Test upper case hash codes are equal
            int upperCaseExpected = upperCaseAscii.GetHashCode();

            Assert.Equal(upperCaseExpected, AsciiString.GetHashCode(upperCaseBuilder));
            Assert.Equal(upperCaseExpected, AsciiString.GetHashCode(upperCaseString));
            Assert.Equal(upperCaseExpected, upperCaseAscii.GetHashCode());

            // Test lower case hash codes are equal
            int lowerCaseExpected = lowerCaseAscii.GetHashCode();

            Assert.Equal(lowerCaseExpected, AsciiString.GetHashCode(lowerCaseAscii));
            Assert.Equal(lowerCaseExpected, AsciiString.GetHashCode(lowerCaseString));
            Assert.Equal(lowerCaseExpected, lowerCaseAscii.GetHashCode());

            // Test case insensitive hash codes are equal
            int expectedCaseInsensitive = lowerCaseAscii.GetHashCode();

            Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseBuilder));
            Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseString));
            Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(lowerCaseString));
            Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(lowerCaseAscii));
            Assert.Equal(expectedCaseInsensitive, AsciiString.GetHashCode(upperCaseAscii));
            Assert.Equal(expectedCaseInsensitive, lowerCaseAscii.GetHashCode());
            Assert.Equal(expectedCaseInsensitive, upperCaseAscii.GetHashCode());

            // Test that opposite cases are equal
            Assert.Equal(lowerCaseAscii.GetHashCode(), AsciiString.GetHashCode(upperCaseString));
            Assert.Equal(upperCaseAscii.GetHashCode(), AsciiString.GetHashCode(lowerCaseString));
        }
Esempio n. 4
0
            static ICharSequence CommaSeparate(IEnumerable <ICharSequence> values)
            {
                StringBuilderCharSequence sb = values is ICollection collection
                    ? new StringBuilderCharSequence(collection.Count * ValueLengthEstimate)
                    : new StringBuilderCharSequence();

                foreach (ICharSequence value in values)
                {
                    if (sb.Count > 0)
                    {
                        sb.Append(Comma);
                    }

                    sb.Append(EscapeCsv(value));
                }

                return(sb);
            }
Esempio n. 5
0
            ICharSequence CommaSeparate(IEnumerable <object> values)
            {
                StringBuilderCharSequence sb = values is ICollection collection
                    ? new StringBuilderCharSequence(collection.Count * ValueLengthEstimate)
                    : new StringBuilderCharSequence();

                foreach (object value in values)
                {
                    if (sb.Count > 0)
                    {
                        sb.Append(Comma);
                    }

                    sb.Append(EscapeCsv(this.ValueConverter.ConvertObject(value)));
                }

                return(sb);
            }
Esempio n. 6
0
        private void HandleCE32(int start, int end, int ce32)
        {
            for (; ;)
            {
                if ((ce32 & 0xff) < Collation.SPECIAL_CE32_LOW_BYTE)
                {
                    // !isSpecialCE32()
                    if (sink != null)
                    {
                        sink.HandleCE(Collation.CeFromSimpleCE32(ce32));
                    }
                    return;
                }
                switch (Collation.TagFromCE32(ce32))
                {
                case Collation.FALLBACK_TAG:
                    return;

                case Collation.RESERVED_TAG_3:
                case Collation.BUILDER_DATA_TAG:
                case Collation.LEAD_SURROGATE_TAG:
                    // Java porting note: U_INTERNAL_PROGRAM_ERROR is set to errorCode in ICU4C.
                    throw new InvalidOperationException(
                              string.Format("Unexpected CE32 tag type {0} for ce32=0x{1:x8}",
                                            Collation.TagFromCE32(ce32), ce32));

                case Collation.LONG_PRIMARY_TAG:
                    if (sink != null)
                    {
                        sink.HandleCE(Collation.CeFromLongPrimaryCE32(ce32));
                    }
                    return;

                case Collation.LONG_SECONDARY_TAG:
                    if (sink != null)
                    {
                        sink.HandleCE(Collation.CeFromLongSecondaryCE32(ce32));
                    }
                    return;

                case Collation.LATIN_EXPANSION_TAG:
                    if (sink != null)
                    {
                        ces[0] = Collation.LatinCE0FromCE32(ce32);
                        ces[1] = Collation.LatinCE1FromCE32(ce32);
                        sink.HandleExpansion(ces, 0, 2);
                    }
                    // Optimization: If we have a prefix,
                    // then the relevant strings have been added already.
                    if (unreversedPrefix.Length == 0)
                    {
                        AddExpansions(start, end);
                    }
                    return;

                case Collation.EXPANSION32_TAG:
                    if (sink != null)
                    {
                        int idx    = Collation.IndexFromCE32(ce32);
                        int length = Collation.LengthFromCE32(ce32);
                        for (int i = 0; i < length; ++i)
                        {
                            ces[i] = Collation.CeFromCE32(data.ce32s[idx + i]);
                        }
                        sink.HandleExpansion(ces, 0, length);
                    }
                    // Optimization: If we have a prefix,
                    // then the relevant strings have been added already.
                    if (unreversedPrefix.Length == 0)
                    {
                        AddExpansions(start, end);
                    }
                    return;

                case Collation.EXPANSION_TAG:
                    if (sink != null)
                    {
                        int idx    = Collation.IndexFromCE32(ce32);
                        int length = Collation.LengthFromCE32(ce32);
                        sink.HandleExpansion(data.ces, idx, length);
                    }
                    // Optimization: If we have a prefix,
                    // then the relevant strings have been added already.
                    if (unreversedPrefix.Length == 0)
                    {
                        AddExpansions(start, end);
                    }
                    return;

                case Collation.PREFIX_TAG:
                    HandlePrefixes(start, end, ce32);
                    return;

                case Collation.CONTRACTION_TAG:
                    HandleContractions(start, end, ce32);
                    return;

                case Collation.DIGIT_TAG:
                    // Fetch the non-numeric-collation CE32 and continue.
                    ce32 = data.ce32s[Collation.IndexFromCE32(ce32)];
                    break;

                case Collation.U0000_TAG:
                    Debug.Assert(start == 0 && end == 0);
                    // Fetch the normal ce32 for U+0000 and continue.
                    ce32 = data.ce32s[0];
                    break;

                case Collation.HANGUL_TAG:
                    if (sink != null)
                    {
                        // TODO: This should be optimized,
                        // especially if [start..end] is the complete Hangul range. (assert that)
                        UTF16CollationIterator    iter   = new UTF16CollationIterator(data);
                        StringBuilderCharSequence hangul = new StringBuilderCharSequence(new StringBuilder(1));
                        for (int c = start; c <= end; ++c)
                        {
                            hangul.StringBuilder.Length = 0;
                            hangul.StringBuilder.AppendCodePoint(c);
                            iter.SetText(false, hangul, 0);
                            int length = iter.FetchCEs();
                            // Ignore the terminating non-CE.
                            Debug.Assert(length >= 2 && iter.GetCE(length - 1) == Collation.NO_CE);
                            sink.HandleExpansion(iter.GetCEs(), 0, length - 1);
                        }
                    }
                    // Optimization: If we have a prefix,
                    // then the relevant strings have been added already.
                    if (unreversedPrefix.Length == 0)
                    {
                        AddExpansions(start, end);
                    }
                    return;

                case Collation.OFFSET_TAG:
                    // Currently no need to send offset CEs to the sink.
                    return;

                case Collation.IMPLICIT_TAG:
                    // Currently no need to send implicit CEs to the sink.
                    return;
                }
            }
        }