示例#1
0
        public void SingleFileUploadNoName()
        {
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");
            var encoder = new HttpPostRequestEncoder(request, true);

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

            this.files.Add(fileStream);
            encoder.AddBodyAttribute("foo", "bar");
            encoder.AddBodyFileUpload("quux", "", fileStream, "text/plain", false);

            string multipartDataBoundary = encoder.MultipartDataBoundary;
            string content = GetRequestBody(encoder);

            string expected = "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": 3" + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" +
                              "\r\n" +
                              "bar" +
                              "\r\n" +
                              "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 01" + /*StringUtil.*/ Newline +
                              "\r\n" +
                              "--" + multipartDataBoundary + "--" + "\r\n";

            Assert.Equal(expected, content);
        }
示例#2
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");
        }
示例#3
0
        static string GetRequestBody(HttpPostRequestEncoder encoder)
        {
            encoder.FinalizeRequest();

            List <IInterfaceHttpData> chunks = encoder.MultipartHttpDatas;
            var buffers = new IByteBuffer[chunks.Count];

            for (int i = 0; i < buffers.Length; i++)
            {
                IInterfaceHttpData data = chunks[i];
                if (data is InternalAttribute attribute)
                {
                    buffers[i] = attribute.ToByteBuffer();
                }
                else if (data is IHttpData httpData)
                {
                    buffers[i] = httpData.GetByteBuffer();
                }
            }

            IByteBuffer content    = Unpooled.WrappedBuffer(buffers);
            string      contentStr = content.ToString(Encoding.UTF8);

            content.Release();
            return(contentStr);
        }
示例#4
0
        static void ShouldThrowExceptionIfNotAllowed(HttpMethod method, FileStream fileStream)
        {
            fileStream.Position = 0; // Reset to the begining
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, method, "http://localhost");

            var encoder = new HttpPostRequestEncoder(request, true);

            encoder.AddBodyAttribute("foo", "bar");
            encoder.AddBodyFileUpload("quux", fileStream, "text/plain", false);

            string multipartDataBoundary = encoder.MultipartDataBoundary;
            string content = GetRequestBody(encoder);

            string expected = "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": 3" + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" +
                              "\r\n" +
                              "bar" +
                              "\r\n" +
                              "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-01.txt\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 01" + /*StringUtil.*/ Newline +
                              "\r\n" +
                              "--" + multipartDataBoundary + "--" + "\r\n";

            Assert.Equal(expected, content);
        }
示例#5
0
        /**
         * Standard post without multipart but already support on Factory (memory management)
         *
         * @return the list of HttpData object (attribute and file) to be reused on next post
         */
        private static async Task <List <IInterfaceHttpData> > FormpostAsync(Bootstrap bootstrap,
                                                                             Uri uriSimple, FileStream file, IHttpDataFactory factory,
                                                                             IList <HeaderEntry <AsciiString, ICharSequence> > headers)
        {
            // XXX /formpost
            // Start the connection attempt.
            IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));

            // Prepare the HTTP request.
            IHttpRequest request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, uriSimple.ToString());

            // Use the PostBody encoder
            HttpPostRequestEncoder bodyRequestEncoder =
                new HttpPostRequestEncoder(factory, request, false);      // false => not multipart

            // it is legal to add directly header or cookie into the request until finalize
            foreach (var entry in headers)
            {
                request.Headers.Set(entry.Key, entry.Value);
            }

            // add Form attribute
            bodyRequestEncoder.AddBodyAttribute("getform", "POST");
            bodyRequestEncoder.AddBodyAttribute("info", "first value");
            bodyRequestEncoder.AddBodyAttribute("secondinfo", "secondvalue ���&");
            bodyRequestEncoder.AddBodyAttribute("thirdinfo", TextArea);
            bodyRequestEncoder.AddBodyAttribute("fourthinfo", TextAreaLong);
            bodyRequestEncoder.AddBodyFileUpload("myfile", file, "application/x-zip-compressed", false);

            // finalize request
            request = bodyRequestEncoder.FinalizeRequest();

            // Create the bodylist to be reused on the last version with Multipart support
            var bodylist = bodyRequestEncoder.GetBodyListAttributes();

            var list = new List <object>();

            // send request
            list.Add(request);

            // test if request was chunked and if so, finish the write
            if (bodyRequestEncoder.IsChunked)
            { // could do either request.isChunked()
              // either do it through ChunkedWriteHandler
                list.Add(bodyRequestEncoder);
            }
            await channel.WriteAndFlushManyAsync(list);

            // Do not clear here since we will reuse the InterfaceHttpData on the next request
            // for the example (limit action on client side). Take this as a broadcast of the same
            // request on both Post actions.
            //
            // On standard program, it is clearly recommended to clean all files after each request
            // bodyRequestEncoder.cleanFiles();

            // Wait for the server to close the connection.
            await channel.CloseCompletion;

            return(bodylist);
        }
示例#6
0
        public void MultiFileUploadInMixedModeNoName()
        {
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");
            var encoder = new HttpPostRequestEncoder(request, true);

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

            this.files.Add(fileStream1);
            FileStream fileStream2 = File.Open("./Multipart/file-02.txt", FileMode.Open, FileAccess.Read);

            this.files.Add(fileStream2);

            encoder.AddBodyAttribute("foo", "bar");
            encoder.AddBodyFileUpload("quux", "", fileStream1, "text/plain", false);
            encoder.AddBodyFileUpload("quux", "", fileStream2, "text/plain", false);

            // We have to query the value of these two fields before finalizing
            // the request, which unsets one of them.
            string multipartDataBoundary  = encoder.MultipartDataBoundary;
            string multipartMixedBoundary = encoder.MultipartMixedBoundary;
            string content = GetRequestBody(encoder);

            string expected = "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": 3" + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" +
                              "\r\n" +
                              "bar" + "\r\n" +
                              "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"" + "\r\n" +
                              HttpHeaderNames.ContentType + ": multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" +
                              "\r\n" +
                              "--" + multipartMixedBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": attachment\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream1.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 01" + /*StringUtil.*/ Newline +
                              "\r\n" +
                              "--" + multipartMixedBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": attachment\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream2.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 02" + /*StringUtil.*/ Newline +
                              "\r\n" +
                              "--" + multipartMixedBoundary + "--" + "\r\n" +
                              "--" + multipartDataBoundary + "--" + "\r\n";

            Assert.Equal(expected, content);
        }
        public void SingleFileUploadInHtml5Mode()
        {
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost");
            var factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MinSize);
            var encoder = new HttpPostRequestEncoder(
                factory,
                request,
                true,
                Encoding.UTF8,
                HttpPostRequestEncoder.EncoderMode.HTML5);

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

            this.files.Add(fileStream1);
            FileStream fileStream2 = File.Open("./Multipart/file-02.txt", FileMode.Open, FileAccess.Read);

            this.files.Add(fileStream2);

            encoder.AddBodyAttribute("foo", "bar");
            encoder.AddBodyFileUpload("quux", fileStream1, "text/plain", false);
            encoder.AddBodyFileUpload("quux", fileStream2, "text/plain", false);

            string multipartDataBoundary = encoder.MultipartDataBoundary;
            string content = GetRequestBody(encoder);

            string expected = "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": 3" + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" +
                              "\r\n" +
                              "bar" + "\r\n" +
                              "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-01.txt\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream1.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 01" + StringUtil.Newline + "\r\n" +
                              "--" + multipartDataBoundary + "\r\n" +
                              HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-02.txt\"" + "\r\n" +
                              HttpHeaderNames.ContentLength + ": " + fileStream2.Length + "\r\n" +
                              HttpHeaderNames.ContentType + ": text/plain" + "\r\n" +
                              HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" +
                              "\r\n" +
                              "File 02" + StringUtil.Newline +
                              "\r\n" +
                              "--" + multipartDataBoundary + "--" + "\r\n";

            Assert.Equal(expected, content);
        }
示例#8
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();
        }
示例#9
0
        /**
         * Multipart example
         */
        private static async Task FormpostmultipartAsync(
            Bootstrap bootstrap, Uri uriFile, IHttpDataFactory factory,
            IList <HeaderEntry <AsciiString, ICharSequence> > headers, List <IInterfaceHttpData> bodylist)
        {
            // XXX /formpostmultipart
            // Start the connection attempt.
            IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));

            // Prepare the HTTP request.
            var request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, uriFile.ToString());

            // Use the PostBody encoder
            HttpPostRequestEncoder bodyRequestEncoder =
                new HttpPostRequestEncoder(factory, request, true);     // true => multipart

            // it is legal to add directly header or cookie into the request until finalize
            foreach (var entry in headers)
            {
                request.Headers.Set(entry.Key, entry.Value);
            }

            // add Form attribute from previous request in formpost()
            bodyRequestEncoder.SetBodyHttpDatas(bodylist);

            // finalize request
            bodyRequestEncoder.FinalizeRequest();

            var list = new List <object>();

            // send request
            list.Add(request);

            // test if request was chunked and if so, finish the write
            if (bodyRequestEncoder.IsChunked)
            {
                list.Add(bodyRequestEncoder);
            }
            await channel.WriteAndFlushManyAsync(list);

            // Now no more use of file representation (and list of HttpData)
            bodyRequestEncoder.CleanFiles();

            // Wait for the server to close the connection.
            await channel.CloseCompletion;
        }
示例#10
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();
        }
示例#11
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();
        }
示例#12
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");
        }