[#2542] HTTP post request decoder does not support quoted boundaries

Motivation:
According to RFC2616 section 19, boundary string could be quoted, but
currently the PostRequestDecoder does not support it while it should.

Modifications:
Once the boundary is found, one check is made to verify if the boundary
is "quoted", and if so, it is "unqoted".

Note: in following usage of this boundary (as delimiter), quote seems no
more allowed according to the same RFC, so the reason that only the
boundary definition is corrected.

Result:
Now the boundary could be whatever quoted or not. A Junit test case
checks it.
This commit is contained in:
Frederic Bregier 2014-06-08 16:40:35 +02:00 committed by Norman Maurer
parent a0a8f1032b
commit 6b69ccb585
2 changed files with 39 additions and 0 deletions

View File

@ -272,6 +272,13 @@ public class HttpPostRequestDecoder {
if (boundary.length != 2) {
throw new ErrorDataDecoderException("Needs a boundary value");
}
if (boundary[1].charAt(0) == '"') {
String bound = boundary[1].trim();
int index = bound.length() - 1;
if (bound.charAt(index) == '"') {
boundary[1] = bound.substring(1, index);
}
}
multipartDataBoundary = "--" + boundary[1];
isMultipart = true;
currentStatus = MultiPartStatus.HEADERDELIMITER;

View File

@ -176,6 +176,38 @@ public class HttpPostRequestDecoderTest {
}
}
// See https://github.com/netty/netty/issues/2542
@Test
public void testQuotedBoundary() throws Exception {
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" +
"Content-Type: image/gif\r\n" +
"\r\n" +
data + "\r\n" +
"--" + boundary + "--\r\n";
req.content().writeBytes(body.getBytes(CharsetUtil.UTF_8));
}
// Create decoder instance to test.
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
assertFalse(decoder.getBodyHttpDatas().isEmpty());
decoder.destroy();
}
// See https://github.com/netty/netty/issues/1848
@Test
public void testNoZeroOut() throws Exception {