diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java index b125dfae0e..9e82ad7caf 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java @@ -1132,7 +1132,7 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest newOffset = toRead; return -newOffset; } - newOffset = posFirstChar + offset; + newOffset = posFirstChar + newOffset; if (newOffset + delimeterLength > toRead) { return -newOffset; } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java index 45b8461ce7..acbcf1219d 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java @@ -938,4 +938,42 @@ public class HttpPostRequestDecoderTest { assertTrue(req.release()); } } + + @Test + public void testDecodeMultipartRequest() { + byte[] bodyBytes = ("--be38b42a9ad2713f\n" + + "content-disposition: form-data; name=\"title\"\n" + + "content-length: 10\n" + + "content-type: text/plain; charset=UTF-8\n" + + "\n" + + "bar-stream\n" + + "--be38b42a9ad2713f\n" + + "content-disposition: form-data; name=\"data\"; filename=\"data.json\"\n" + + "content-length: 16\n" + + "content-type: application/json; charset=UTF-8\n" + + "\n" + + "{\"title\":\"Test\"}\n" + + "--be38b42a9ad2713f--").getBytes(); + ByteBuf content = Unpooled.directBuffer(bodyBytes.length); + content.writeBytes(bodyBytes); + FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content); + req.headers().add("Content-Type", "multipart/form-data;boundary=be38b42a9ad2713f"); + + try { + HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req); + InterfaceHttpData data = decoder.getBodyHttpData("title"); + assertTrue(data instanceof MemoryAttribute); + assertEquals("bar-stream", ((MemoryAttribute) data).getString()); + assertTrue(data.release()); + data = decoder.getBodyHttpData("data"); + assertTrue(data instanceof MemoryFileUpload); + assertEquals("{\"title\":\"Test\"}", ((MemoryFileUpload) data).getString()); + assertTrue(data.release()); + decoder.destroy(); + } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) { + fail("Was not expecting an exception"); + } finally { + assertTrue(req.release()); + } + } }