Set default CONTENT_TYPE when it is absent in multipart request body

Motivation:

I am use netty as a http server, it fail to decode some POST request when the request absent Content-Type in the multipart/form-data body.

Modifications:

Set content_type with default application/octet-stream to parse the uploaded file data when the Content-Type is absent in multipart request body

Result:

Can decode the http request as normal.
This commit is contained in:
houdejun214 2016-01-21 17:02:19 +08:00 committed by Norman Maurer
parent 7bf3792e6f
commit aa43c10403
2 changed files with 37 additions and 4 deletions

View File

@ -834,9 +834,6 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest
Attribute filenameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.FILENAME);
Attribute nameAttribute = currentFieldAttributes.get(HttpPostBodyUtil.NAME);
Attribute contentTypeAttribute = currentFieldAttributes.get(HttpHeaders.Names.CONTENT_TYPE);
if (contentTypeAttribute == null) {
throw new ErrorDataDecoderException("Content-Type is absent but required");
}
Attribute lengthAttribute = currentFieldAttributes.get(HttpHeaders.Names.CONTENT_LENGTH);
long size;
try {
@ -847,9 +844,15 @@ public class HttpPostMultipartRequestDecoder implements InterfaceHttpPostRequest
size = 0;
}
try {
String contentType;
if (contentTypeAttribute != null) {
contentType = contentTypeAttribute.getValue();
} else {
contentType = HttpPostBodyUtil.DEFAULT_BINARY_CONTENT_TYPE;
}
currentFileUpload = factory.createFileUpload(request,
cleanString(nameAttribute.getValue()), cleanString(filenameAttribute.getValue()),
contentTypeAttribute.getValue(), mechanism.value(), localCharset,
contentType, mechanism.value(), localCharset,
size);
} catch (NullPointerException e) {
throw new ErrorDataDecoderException(e);

View File

@ -376,4 +376,34 @@ public class HttpPostRequestDecoderTest {
assertEquals("tmp 0.txt", fileUpload.getFilename());
decoder.destroy();
}
@Test
public void testMultipartRequestWithoutContentTypeBody() {
final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
final DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
"http://localhost");
req.setDecoderResult(DecoderResult.SUCCESS);
req.headers().add(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
// Force to use memory-based data.
final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
final String body =
"--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" +
"\r\n" +
data + "\r\n" +
"--" + boundary + "--\r\n";
req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8));
}
// Create decoder instance to test without any exception.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
assertFalse(decoder.getBodyHttpDatas().isEmpty());
decoder.destroy();
}
}