Return null in HttpPostRequestEncoder (#9352)

Motivation:

If the encoded value of a form element happens to exactly hit
the chunk limit (8096 bytes), the post request encoder will
throw a NullPointerException.

Modifications:

Catch the null case and return.

Result:

No NPE.
This commit is contained in:
Emily Littleworth 2019-07-16 07:29:33 -04:00 committed by Norman Maurer
parent 7e818352e2
commit 0f8685ae9b
2 changed files with 30 additions and 1 deletions

View File

@ -975,7 +975,11 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
if (buffer.capacity() == 0) { if (buffer.capacity() == 0) {
currentData = null; currentData = null;
if (currentBuffer == null) { if (currentBuffer == null) {
if (delimiter == null) {
return null;
} else {
currentBuffer = delimiter; currentBuffer = delimiter;
}
} else { } else {
if (delimiter != null) { if (delimiter != null) {
currentBuffer = wrappedBuffer(currentBuffer, delimiter); currentBuffer = wrappedBuffer(currentBuffer, delimiter);

View File

@ -18,10 +18,12 @@ package io.netty.handler.codec.http.multipart;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpConstants; import io.netty.handler.codec.http.HttpConstants;
import io.netty.handler.codec.http.HttpContent; import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent; import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode; import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode;
@ -443,4 +445,27 @@ public class HttpPostRequestEncoderTest {
+ readable, expectedSize); + readable, expectedSize);
httpContent.release(); httpContent.release();
} }
@Test
public void testEncodeChunkedContent() throws Exception {
HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, false);
int length = 8077 + 8096;
char[] array = new char[length];
Arrays.fill(array, 'a');
String longText = new String(array);
encoder.addBodyAttribute("data", longText);
encoder.addBodyAttribute("moreData", "abcd");
assertNotNull(encoder.finalizeRequest());
while (!encoder.isEndOfInput()) {
encoder.readChunk((ByteBufAllocator) null).release();
}
assertTrue(encoder.isEndOfInput());
encoder.cleanFiles();
}
} }