From 1ad47282c335323a3a244be91f619dcb97b69d20 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Mon, 24 Jun 2019 10:40:17 +0200 Subject: [PATCH] Preserve the original filename when encoding a multipart/form in mixed mode. (#9270) Motivation: The HttpPostRequestEncoder overwrites the original filename of file uploads sharing the same name encoded in mixed mode when it rewrites the multipart body header of the previous file. The original filename should be preserved instead. Modifications: Change the HttpPostRequestEncoder to reuse the correct filename when the encoder switches to mixed mode. The original test is incorrect and has been modified too, in addition it tests with an extra file upload since the current test was not testing the continuation of a mixed mode. Result: The HttpPostRequestEncoder will preserve the original filename of the first fileupload when switching to mixed mode --- .../codec/http/multipart/HttpPostRequestEncoder.java | 2 +- .../http/multipart/HttpPostRequestEncoderTest.java | 12 +++++++++++- codec-http/src/test/resources/file-03.txt | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 codec-http/src/test/resources/file-03.txt diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java index 21faffe4a3..236b6acd51 100755 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java @@ -638,7 +638,7 @@ public class HttpPostRequestEncoder implements ChunkedInput { replacement.append("; ") .append(HttpHeaderValues.FILENAME) .append("=\"") - .append(fileUpload.getFilename()) + .append(currentFileUpload.getFilename()) .append('"'); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java index 7669f971b1..ad03da0b66 100755 --- a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java @@ -139,9 +139,11 @@ public class HttpPostRequestEncoderTest { HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true); File file1 = new File(getClass().getResource("/file-01.txt").toURI()); File file2 = new File(getClass().getResource("/file-02.txt").toURI()); + File file3 = new File(getClass().getResource("/file-03.txt").toURI()); encoder.addBodyAttribute("foo", "bar"); encoder.addBodyFileUpload("quux", file1, "text/plain", false); encoder.addBodyFileUpload("quux", file2, "text/plain", false); + encoder.addBodyFileUpload("quux", file3, "text/plain", false); // We have to query the value of these two fields before finalizing // the request, which unsets one of them. @@ -160,7 +162,7 @@ public class HttpPostRequestEncoderTest { CONTENT_TYPE + ": multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + - CONTENT_DISPOSITION + ": attachment; filename=\"file-02.txt\"" + "\r\n" + + CONTENT_DISPOSITION + ": attachment; filename=\"file-01.txt\"" + "\r\n" + CONTENT_LENGTH + ": " + file1.length() + "\r\n" + CONTENT_TYPE + ": text/plain" + "\r\n" + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + @@ -175,6 +177,14 @@ public class HttpPostRequestEncoderTest { "\r\n" + "File 02" + StringUtil.NEWLINE + "\r\n" + + "--" + multipartMixedBoundary + "\r\n" + + CONTENT_DISPOSITION + ": attachment; filename=\"file-03.txt\"" + "\r\n" + + CONTENT_LENGTH + ": " + file3.length() + "\r\n" + + CONTENT_TYPE + ": text/plain" + "\r\n" + + CONTENT_TRANSFER_ENCODING + ": binary" + "\r\n" + + "\r\n" + + "File 03" + StringUtil.NEWLINE + + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n"; diff --git a/codec-http/src/test/resources/file-03.txt b/codec-http/src/test/resources/file-03.txt new file mode 100644 index 0000000000..b545f1b452 --- /dev/null +++ b/codec-http/src/test/resources/file-03.txt @@ -0,0 +1 @@ +File 03