Skip to content

Commit

Permalink
Preserve the original filename when encoding a multipart/form in mixe…
Browse files Browse the repository at this point in the history
…d mode. (netty#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
  • Loading branch information
vietj authored and normanmaurer committed Jun 24, 2019
1 parent 712077c commit 1ad4728
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public void addBodyHttpData(InterfaceHttpData data) throws ErrorDataEncoderExcep
replacement.append("; ")
.append(HttpHeaderValues.FILENAME)
.append("=\"")
.append(fileUpload.getFilename())
.append(currentFileUpload.getFilename())
.append('"');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ public void testMultiFileUploadInMixedMode() throws Exception {
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.
Expand All @@ -160,7 +162,7 @@ public void testMultiFileUploadInMixedMode() throws Exception {
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" +
Expand All @@ -175,6 +177,14 @@ public void testMultiFileUploadInMixedMode() throws Exception {
"\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";

Expand Down
1 change: 1 addition & 0 deletions codec-http/src/test/resources/file-03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File 03

0 comments on commit 1ad4728

Please sign in to comment.