示例#1
0
        public void TestDecodeOneInvalid()
        {
            // invalid end
            var str    = "2\r\nxabc";
            var stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));

            Assert.Throws(typeof(InvalidChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });
        }
示例#2
0
        public void TestDecodeOneLast()
        {
            var chunkHeader = "0;aa=\"bb\"\r\n";
            var str         = chunkHeader + "header: value\r\nheader2: value2\r\n\r\n";
            var stream      = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            var info        = ChunkedDecoder.DecodeOneChunk(stream);

            Assert.Equal(chunkHeader.Length, info.ChunkLength);
            Assert.Equal(0, info.DataLength);
        }
示例#3
0
        public void TestDecodeOneSimple()
        {
            var str = "A\r\n1234567890\r\n";

            str = str + str;
            var stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            var info   = ChunkedDecoder.DecodeOneChunk(stream);

            Assert.Equal(str.Length / 2, info.ChunkLength);
            Assert.Equal(10, info.DataLength);
            Assert.Equal('5', Convert.ToChar(stream.ReadByte(info.DataStart + 4)));
        }
示例#4
0
        private void parseChunkedBody()
        {
            if (contentData.Length == 0)
            {
                // wait for some data
                return;
            }

            var seg = new StreamSegment(contentData);

            while (seg.Length > 0)
            {
                ChunkedDecoder.DecodeOneInfo chunkInfo;
                try
                {
                    chunkInfo = ChunkedDecoder.DecodeOneChunk(seg);
                }
                catch (PartialChunkException)
                {
                    // wait for more data
                    contentData = new StreamList();
                    if (seg.Length > 0)
                    {
                        contentData.Add(new ByteArray(seg));
                    }
                    return;
                }
                catch
                {
                    throw new HttpInvalidMessageException();
                }
                // finished
                if (chunkInfo.DataLength == 0)
                {
                    seg         = new StreamSegment(seg, chunkInfo.ChunkLength);
                    contentData = new StreamList();
                    contentData.Add(new ByteArray(seg));
                    state = ParsingState.CheckingChunedTrailer;
                    return;
                }
                else
                {
                    dechunkedLength += chunkInfo.DataLength;
                    seg              = new StreamSegment(contentData, seg.Offset + chunkInfo.ChunkLength, seg.Length - chunkInfo.ChunkLength);
                }
            }
            contentData = new StreamList();
            return;
        }
示例#5
0
        public void TestDecodeOnePartial()
        {
            var str    = "A";
            var stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));

            Assert.Throws(typeof(PartialChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });
            str    = "A\r";
            stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            Assert.Throws(typeof(PartialChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });
            str    = "A\r\n";
            stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            Assert.Throws(typeof(PartialChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });
            str    = "A\r\n1234567890\r";
            stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            Assert.Throws(typeof(PartialChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });

            // last chunk without end
            str    = "0\r";
            stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(str));
            Assert.Throws(typeof(PartialChunkException), delegate() { ChunkedDecoder.DecodeOneChunk(stream); });
        }