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. (#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 dd6ba29 commit 2d72a7c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Expand Up @@ -636,7 +636,7 @@ public void addBodyHttpData(InterfaceHttpData data) throws ErrorDataEncoderExcep
replacement.append("; ")
.append(HttpHeaderValues.FILENAME)
.append("=\"")
.append(fileUpload.getFilename())
.append(currentFileUpload.getFilename())
.append('"');
}

Expand Down
Expand Up @@ -138,9 +138,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 @@ -159,7 +161,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 @@ -174,6 +176,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
@@ -0,0 +1 @@
File 03

0 comments on commit 2d72a7c

Please sign in to comment.