Exemplo n.º 1
0
        public void DataIsMultipleOfChunkSize1()
        {
            var factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MinSize);
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");
            var encoder = new HttpPostRequestEncoder(factory, request, true,
                                                     HttpPostRequestEncoder.EncoderMode.RFC1738);

            var first = new MemoryFileUpload("resources", "", "application/json", null, Encoding.UTF8, -1);

            first.MaxSize = -1;
            first.SetContent(new MemoryStream(new byte[7955]));
            encoder.AddBodyHttpData(first);

            var second = new MemoryFileUpload("resources2", "", "application/json", null, Encoding.UTF8, -1);

            second.MaxSize = -1;
            second.SetContent(new MemoryStream(new byte[7928]));
            encoder.AddBodyHttpData(second);

            Assert.NotNull(encoder.FinalizeRequest());

            CheckNextChunkSize(encoder, 8080);
            CheckNextChunkSize(encoder, 8055);

            IHttpContent httpContent = encoder.ReadChunk(default(IByteBufferAllocator));

            Assert.True(httpContent is ILastHttpContent, "Expected LastHttpContent is not received");
            httpContent.Release();

            Assert.True(encoder.IsEndOfInput, "Expected end of input is not receive");
        }
Exemplo n.º 2
0
        static void CheckNextChunkSize(HttpPostRequestEncoder encoder, int sizeWithoutDelimiter)
        {
            // 16 bytes as HttpPostRequestEncoder uses Long.toHexString(...) to generate a hex-string which will be between
            // 2 and 16 bytes.
            // See https://github.com/netty/netty/blob/4.1/codec-http/src/main/java/io/netty/handler/
            // codec/http/multipart/HttpPostRequestEncoder.java#L291
            int expectedSizeMin = sizeWithoutDelimiter + (2 + 2);   // Two multipar boundary strings
            int expectedSizeMax = sizeWithoutDelimiter + (16 + 16); // Two multipar boundary strings

            IHttpContent httpContent = encoder.ReadChunk(default(IByteBufferAllocator));

            int  readable     = httpContent.Content.ReadableBytes;
            bool expectedSize = readable >= expectedSizeMin && readable <= expectedSizeMax;

            Assert.True(expectedSize, $"Chunk size is not in expected range ({expectedSizeMin} - {expectedSizeMax}), was: {readable}");
            httpContent.Release();
        }
Exemplo n.º 3
0
        public void HttpPostRequestEncoderSlicedBuffer()
        {
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");

            var encoder = new HttpPostRequestEncoder(request, true);

            // add Form attribute
            encoder.AddBodyAttribute("getform", "POST");
            encoder.AddBodyAttribute("info", "first value");
            encoder.AddBodyAttribute("secondinfo", "secondvalue a&");
            encoder.AddBodyAttribute("thirdinfo", "short text");

            const int Length = 100000;
            var       array  = new char[Length];

            array.Fill('a');
            string longText = new string(array);

            encoder.AddBodyAttribute("fourthinfo", longText.Substring(0, 7470));

            FileStream fileStream1 = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read);

            this.files.Add(fileStream1);
            encoder.AddBodyFileUpload("myfile", fileStream1, "application/x-zip-compressed", false);
            encoder.FinalizeRequest();

            while (!encoder.IsEndOfInput)
            {
                IHttpContent httpContent = encoder.ReadChunk(null);
                IByteBuffer  content     = httpContent.Content;
                int          refCnt      = content.ReferenceCount;
                Assert.True(
                    (ReferenceEquals(content.Unwrap(), content) || content.Unwrap() == null) && refCnt == 1 ||
                    !ReferenceEquals(content.Unwrap(), content) && refCnt == 2,
                    "content: " + content + " content.unwrap(): " + content.Unwrap() + " refCnt: " + refCnt);
                httpContent.Release();
            }

            encoder.CleanFiles();
            encoder.Close();
        }
Exemplo n.º 4
0
        public void EncodeChunkedContent()
        {
            IHttpRequest           req     = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/");
            HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, false);

            int    length   = 8077 + 8096;
            string longText = new string('a', length);

            encoder.AddBodyAttribute("data", longText);
            encoder.AddBodyAttribute("moreData", "abcd");

            Assert.NotNull(encoder.FinalizeRequest());

            while (!encoder.IsEndOfInput)
            {
                encoder.ReadChunk((IByteBufferAllocator)null).Release();
            }

            Assert.True(encoder.IsEndOfInput);
            encoder.CleanFiles();
        }
Exemplo n.º 5
0
        public void DataIsMultipleOfChunkSize2()
        {
            var       request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");
            var       encoder = new HttpPostRequestEncoder(request, true);
            const int Length  = 7943;
            var       array   = new char[Length];

            array.Fill('a');
            string longText = new string(array);

            encoder.AddBodyAttribute("foo", longText);

            Assert.NotNull(encoder.FinalizeRequest());

            // In Netty this is 8080 due to random long hex size difference
            CheckNextChunkSize(encoder, 109 + Length + 8);

            IHttpContent httpContent = encoder.ReadChunk(default(IByteBufferAllocator));

            Assert.True(httpContent is ILastHttpContent, "Expected LastHttpContent is not received");
            httpContent.Release();

            Assert.True(encoder.IsEndOfInput, "Expected end of input is not receive");
        }