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
This commit is contained in:
Julien Viet 2019-06-24 10:40:17 +02:00 committed by Norman Maurer
parent 712077cdef
commit 1ad47282c3
3 changed files with 13 additions and 2 deletions

View File

@ -638,7 +638,7 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
replacement.append("; ")
.append(HttpHeaderValues.FILENAME)
.append("=\"")
.append(fileUpload.getFilename())
.append(currentFileUpload.getFilename())
.append('"');
}

View File

@ -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";

View File

@ -0,0 +1 @@
File 03