예제 #1
0
        public void DisableHeaderValidation()
        {
            IByteBuffer input = Unpooled.Buffer(200);

            try
            {
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)":test", (AsciiString)"1");
                toEncode.Add((AsciiString)":status", (AsciiString)"200");
                toEncode.Add((AsciiString)":method", (AsciiString)"GET");
                hpackEncoder.EncodeHeaders(1, input, toEncode, NeverSensitiveDetector.Instance);

                IHttp2Headers decoded = new DefaultHttp2Headers();

                hpackDecoder.Decode(1, input, decoded, false);

                Assert.Equal("1", decoded.GetAll((AsciiString)":test")[0]);
                Assert.Equal("200", decoded.Status);
                Assert.Equal("GET", decoded.Method);
            }
            finally
            {
                input.Release();
            }
        }
예제 #2
0
        public void TestAccountForHeaderOverhead()
        {
            IByteBuffer input = Unpooled.Buffer(100);

            try
            {
                string headerName  = "12345";
                string headerValue = "56789";
                long   headerSize  = headerName.Length + headerValue.Length;
                hpackDecoder.SetMaxHeaderListSize(headerSize);
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)headerName, (AsciiString)headerValue);
                hpackEncoder.EncodeHeaders(1, input, toEncode, NeverSensitiveDetector.Instance);

                IHttp2Headers decoded = new DefaultHttp2Headers();

                // SETTINGS_MAX_HEADER_LIST_SIZE is big enough for the header to fit...
                Assert.True(hpackDecoder.GetMaxHeaderListSize() >= headerSize);

                // ... but decode should fail because we add some overhead for each header entry
                Assert.Throws <HeaderListSizeException>(() => hpackDecoder.Decode(1, input, decoded, true));
            }
            finally
            {
                input.Release();
            }
        }
예제 #3
0
        public DefaultHttp2FrameReaderTest()
        {
            this.listener = new Mock <IHttp2FrameListener>();
            this.ctx      = new Mock <IChannelHandlerContext>();
            this.ctx.Setup(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);

            this.frameReader  = new DefaultHttp2FrameReader();
            this.hpackEncoder = new HpackEncoder();
        }
        private static IByteBuffer Encode(params byte[][] entries)
        {
            HpackEncoder hpackEncoder = Http2TestUtil.NewTestEncoder();
            var          output       = Unpooled.Buffer();
            var          http2Headers = new DefaultHttp2Headers(false);

            for (int ix = 0; ix < entries.Length;)
            {
                http2Headers.Add(new AsciiString(entries[ix++], false), new AsciiString(entries[ix++], false));
            }
            hpackEncoder.EncodeHeaders(3 /* randomly chosen */, output, http2Headers, NeverSensitiveDetector.Instance);
            return(output);
        }
예제 #5
0
        internal static HpackEncoder NewTestEncoder(bool ignoreMaxHeaderListSize, long maxHeaderListSize, long maxHeaderTableSize)
        {
            HpackEncoder hpackEncoder = new HpackEncoder(false, 16, 0);
            var          buf          = Unpooled.Buffer();

            try
            {
                hpackEncoder.SetMaxHeaderTableSize(buf, maxHeaderTableSize);
                hpackEncoder.SetMaxHeaderListSize(maxHeaderListSize);
            }
            finally
            {
                buf.Release();
            }
            return(hpackEncoder);
        }
예제 #6
0
        public void UnknownPseudoHeader()
        {
            IByteBuffer input = Unpooled.Buffer(200);

            try
            {
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)":test", (AsciiString)"1");
                hpackEncoder.EncodeHeaders(1, input, toEncode, NeverSensitiveDetector.Instance);

                IHttp2Headers decoded = new DefaultHttp2Headers();

                Assert.Throws <StreamException>(() => hpackDecoder.Decode(1, input, decoded, true));
            }
            finally
            {
                input.Release();
            }
        }
예제 #7
0
        internal void TestCompress()
        {
            HpackEncoder hpackEncoder = this.CreateEncoder();

            foreach (HeaderBlock headerBlock in this.headerBlocks)
            {
                byte[] actual =
                    Encode(hpackEncoder, headerBlock.GetHeaders(), headerBlock.GetMaxHeaderTableSize(),
                           this.sensitiveHeaders);

                if (!(actual.Length == headerBlock.encodedBytes.Length && PlatformDependent.ByteArrayEquals(actual, 0, headerBlock.encodedBytes, 0, actual.Length)))
                {
                    throw new Exception(
                              "\nEXPECTED:\n" + headerBlock.GetEncodedStr() +
                              "\nACTUAL:\n" + StringUtil.ToHexString(actual));
                }

                List <HpackHeaderField> actualDynamicTable = new List <HpackHeaderField>();
                for (int index = 0; index < hpackEncoder.Length(); index++)
                {
                    actualDynamicTable.Add(hpackEncoder.GetHeaderField(index));
                }

                List <HpackHeaderField> expectedDynamicTable = headerBlock.GetDynamicTable();

                if (!HeadersEqual(expectedDynamicTable, actualDynamicTable))
                {
                    throw new Exception(
                              "\nEXPECTED DYNAMIC TABLE:\n" + expectedDynamicTable +
                              "\nACTUAL DYNAMIC TABLE:\n" + actualDynamicTable);
                }

                if (headerBlock.GetTableSize() != hpackEncoder.Size())
                {
                    throw new Exception(
                              "\nEXPECTED TABLE SIZE: " + headerBlock.GetTableSize() +
                              "\n ACTUAL TABLE SIZE : " + hpackEncoder.Size());
                }
            }
        }
예제 #8
0
        public void TestDecodeLargerThanMaxHeaderListSizeUpdatesDynamicTable()
        {
            IByteBuffer input = Unpooled.Buffer(300);

            try
            {
                hpackDecoder.SetMaxHeaderListSize(200);
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                // encode headers that are slightly larger than maxHeaderListSize
                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)"test_1", (AsciiString)"1");
                toEncode.Add((AsciiString)"test_2", (AsciiString)"2");
                toEncode.Add((AsciiString)"long", (AsciiString)"A".PadRight(100, 'A')); //string.Format("{0,0100:d}", 0).Replace('0', 'A')
                toEncode.Add((AsciiString)"test_3", (AsciiString)"3");
                hpackEncoder.EncodeHeaders(1, input, toEncode, NeverSensitiveDetector.Instance);

                // decode the headers, we should get an exception
                IHttp2Headers decoded = new DefaultHttp2Headers();
                Assert.Throws <HeaderListSizeException>(() => hpackDecoder.Decode(1, input, decoded, true));

                // but the dynamic table should have been updated, so that later blocks
                // can refer to earlier headers
                input.Clear();
                // 0x80, "indexed header field representation"
                // index 62, the first (most recent) dynamic table entry
                input.WriteByte(0x80 | 62);
                IHttp2Headers decoded2 = new DefaultHttp2Headers();
                hpackDecoder.Decode(1, input, decoded2, true);

                IHttp2Headers golden = new DefaultHttp2Headers();
                golden.Add((AsciiString)"test_3", (AsciiString)"3");
                Assert.Equal(golden, decoded2);
            }
            finally
            {
                input.Release();
            }
        }
예제 #9
0
        private static byte[] Encode(HpackEncoder hpackEncoder, List <HpackHeaderField> headers, int maxHeaderTableSize, bool sensitive)
        {
            IHttp2Headers        http2Headers        = ToHttp2Headers(headers);
            ISensitivityDetector sensitivityDetector = sensitive ? AlwaysSensitiveDetector.Instance : NeverSensitiveDetector.Instance;
            var buffer = Unpooled.Buffer();

            try
            {
                if (maxHeaderTableSize != -1)
                {
                    hpackEncoder.SetMaxHeaderTableSize(buffer, maxHeaderTableSize);
                }

                hpackEncoder.EncodeHeaders(3 /* randomly chosen */, buffer, http2Headers, sensitivityDetector);
                byte[] bytes = new byte[buffer.ReadableBytes];
                buffer.ReadBytes(bytes);
                return(bytes);
            }
            finally
            {
                buffer.Release();
            }
        }
예제 #10
0
        public void FailedValidationDoesntCorruptHpack()
        {
            IByteBuffer in1 = Unpooled.Buffer(200);
            IByteBuffer in2 = Unpooled.Buffer(200);

            try
            {
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)":method", (AsciiString)"GET");
                toEncode.Add((AsciiString)":status", (AsciiString)"200");
                toEncode.Add((AsciiString)"foo", (AsciiString)"bar");
                hpackEncoder.EncodeHeaders(1, in1, toEncode, NeverSensitiveDetector.Instance);

                IHttp2Headers decoded = new DefaultHttp2Headers();

                var expected = Assert.Throws <StreamException>(() => hpackDecoder.Decode(1, in1, decoded, true));
                Assert.Equal(1, expected.StreamId);

                // Do it again, this time without validation, to make sure the HPACK state is still sane.
                decoded.Clear();
                hpackEncoder.EncodeHeaders(1, in2, toEncode, NeverSensitiveDetector.Instance);
                hpackDecoder.Decode(1, in2, decoded, false);

                Assert.Equal(3, decoded.Size);
                Assert.Equal("GET", decoded.Method);
                Assert.Equal("200", decoded.Status);
                Assert.Equal("bar", decoded.Get((AsciiString)"foo", null));
            }
            finally
            {
                in1.Release();
                in2.Release();
            }
        }
예제 #11
0
        public void TestDecodeCountsNamesOnlyOnce()
        {
            IByteBuffer input = Unpooled.Buffer(200);

            try
            {
                hpackDecoder.SetMaxHeaderListSize(3500);
                HpackEncoder hpackEncoder = new HpackEncoder(true);

                // encode headers that are slightly larger than maxHeaderListSize
                IHttp2Headers toEncode = new DefaultHttp2Headers();
                toEncode.Add((AsciiString)("0".PadRight(3000, '0').Replace('0', 'f')), (AsciiString)"value");
                toEncode.Add((AsciiString)"accept", (AsciiString)"value");
                hpackEncoder.EncodeHeaders(1, input, toEncode, NeverSensitiveDetector.Instance);

                IHttp2Headers decoded = new DefaultHttp2Headers();
                hpackDecoder.Decode(1, input, decoded, true);
                Assert.Equal(2, decoded.Size);
            }
            finally
            {
                input.Release();
            }
        }
예제 #12
0
 public HpackEncoderTest()
 {
     hpackEncoder = new HpackEncoder();
     hpackDecoder = new HpackDecoder(Http2CodecUtil.DefaultHeaderListSize);
     mockHeaders  = new Mock <IHttp2Headers>(MockBehavior.Strict);
 }