Skip to content

Commit

Permalink
Return null in HttpPostRequestEncoder (netty#9352)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emlittleworth authored and normanmaurer committed Jul 16, 2019
1 parent 3062993 commit d07d7e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,11 @@ private HttpContent encodeNextChunkUrlEncoded(int sizeleft) throws ErrorDataEnco
if (buffer.capacity() == 0) {
currentData = null;
if (currentBuffer == null) {
currentBuffer = delimiter;
if (delimiter == null) {
return null;
} else {
currentBuffer = delimiter;
}
} else {
if (delimiter != null) {
currentBuffer = wrappedBuffer(currentBuffer, delimiter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
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.HttpConstants;
import io.netty.handler.codec.http.HttpContent;
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.LastHttpContent;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode;
Expand Down Expand Up @@ -443,4 +445,27 @@ private static void checkNextChunkSize(HttpPostRequestEncoder encoder, int sizeW
+ readable, expectedSize);
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();
}
}

0 comments on commit d07d7e2

Please sign in to comment.