[#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-05 23:20:41 +02:00 committed by Norman Maurer
parent f402350d76
commit 8074b5c6ee
2 changed files with 42 additions and 3 deletions

View File

@ -172,6 +172,13 @@ public class HttpPostRequestDecoder implements InterfaceHttpPostRequestDecoder {
if (boundary.length != 2) { if (boundary.length != 2) {
throw new ErrorDataDecoderException("Needs a boundary value"); 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);
}
}
if (headerContentType[crank].toLowerCase().startsWith( if (headerContentType[crank].toLowerCase().startsWith(
HttpHeaders.Values.CHARSET)) { HttpHeaders.Values.CHARSET)) {
String[] charset = StringUtil.split(headerContentType[crank], '='); String[] charset = StringUtil.split(headerContentType[crank], '=');

View File

@ -138,10 +138,10 @@ public class HttpPostRequestDecoderTest {
final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false); final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
// Build test case // Build test case
String extradata = "aaaa"; String extradata = "aaaa";
String [] datas = new String[5]; String[] datas = new String[5];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
datas[i] = extradata; datas[i] = extradata;
for (int j = 0; j < i ; j++) { for (int j = 0; j < i; j++) {
datas[i] += '\r'; datas[i] += '\r';
} }
} }
@ -168,7 +168,7 @@ public class HttpPostRequestDecoderTest {
InterfaceHttpData httpdata = decoder.getBodyHttpData("file" + i); InterfaceHttpData httpdata = decoder.getBodyHttpData("file" + i);
assertNotNull(httpdata); assertNotNull(httpdata);
Attribute attribute = (Attribute) httpdata; Attribute attribute = (Attribute) httpdata;
byte []datar = attribute.get(); byte[] datar = attribute.get();
assertNotNull(datar); assertNotNull(datar);
assertEquals(datas[i].getBytes(CharsetUtil.UTF_8).length, datar.length); assertEquals(datas[i].getBytes(CharsetUtil.UTF_8).length, datar.length);
@ -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 // See https://github.com/netty/netty/issues/1848
@Test @Test
public void testNoZeroOut() throws Exception { public void testNoZeroOut() throws Exception {