Fix possible NPE in HttpCunkedInput if wrapped ChunkedInput.readChunk(...) return null.

Motivation:

Its completly fine for ChunkedInput.readChunk(...) to return null to indicate there is currently not any data to read. We need to handle this in HttpChunkedInput to not produce a NPE when constructing the HttpContent.

Modifications:

If readChunk(...) return null just return null as well.

Result:

No more NPE.
This commit is contained in:
Norman Maurer 2016-06-13 09:04:41 +02:00
parent 35576de9a2
commit 96d676f07c
2 changed files with 29 additions and 1 deletions

View File

@ -93,6 +93,9 @@ public class HttpChunkedInput implements ChunkedInput<HttpContent> {
}
} else {
ByteBuf buf = input.readChunk(ctx);
if (buf == null) {
return null;
}
return new DefaultHttpContent(buf);
}
}

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.stream.ChunkedFile;
import io.netty.handler.stream.ChunkedInput;
@ -24,6 +25,7 @@ import io.netty.handler.stream.ChunkedNioStream;
import io.netty.handler.stream.ChunkedStream;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -82,6 +84,28 @@ public class HttpChunkedInputTest {
check(new HttpChunkedInput(new ChunkedNioFile(TMP)));
}
@Test
public void testWrappedReturnNull() throws Exception {
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
HttpChunkedInput input = new HttpChunkedInput(new ChunkedInput<ByteBuf>() {
@Override
public boolean isEndOfInput() throws Exception {
return false;
}
@Override
public void close() throws Exception {
// NOOP
}
@Override
public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
return null;
}
});
assertNull(input.readChunk(ctx));
}
private static void check(ChunkedInput<?>... inputs) {
EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
@ -118,6 +142,7 @@ public class HttpChunkedInputTest {
}
assertEquals(BYTES.length * inputs.length, read);
assertSame("Last chunk must be DefaultLastHttpContent", LastHttpContent.EMPTY_LAST_CONTENT, lastHttpContent);
assertSame("Last chunk must be LastHttpContent.EMPTY_LAST_CONTENT",
LastHttpContent.EMPTY_LAST_CONTENT, lastHttpContent);
}
}