Fix issue if encoding is enabled but not used (#11358)
Motivation: Fixes an IllegalReferenceCountException Modification: Retained the buffers so the encoder works correctly. Result: Fixes #11357
This commit is contained in:
parent
10b3cee3fa
commit
9f8bfa348e
|
@ -142,7 +142,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
|
|||
if (isFull) {
|
||||
out.add(ReferenceCountUtil.retain(res));
|
||||
} else {
|
||||
out.add(res);
|
||||
out.add(ReferenceCountUtil.retain(res));
|
||||
// Pass through all following contents.
|
||||
state = State.PASS_THROUGH;
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
|
|||
if (isFull) {
|
||||
out.add(ReferenceCountUtil.retain(res));
|
||||
} else {
|
||||
out.add(res);
|
||||
out.add(ReferenceCountUtil.retain(res));
|
||||
// Pass through all following contents.
|
||||
state = State.PASS_THROUGH;
|
||||
}
|
||||
|
|
|
@ -44,6 +44,8 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpHeadersTestUtils.of;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
@ -217,6 +219,65 @@ public class HttpContentCompressorTest {
|
|||
assertThat(ch.readOutbound(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChunkedContentWithAssembledResponseIdentityEncoding() throws Exception {
|
||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
|
||||
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
|
||||
|
||||
HttpResponse res = new AssembledHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
|
||||
Unpooled.copiedBuffer("Hell", CharsetUtil.US_ASCII));
|
||||
res.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
|
||||
ch.writeOutbound(res);
|
||||
|
||||
ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("o, w", CharsetUtil.US_ASCII)));
|
||||
ch.writeOutbound(new DefaultLastHttpContent(Unpooled.copiedBuffer("orld", CharsetUtil.US_ASCII)));
|
||||
|
||||
HttpContent chunk;
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("Hell"));
|
||||
chunk.release();
|
||||
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("o, w"));
|
||||
chunk.release();
|
||||
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("orld"));
|
||||
assertThat(chunk, is(instanceOf(LastHttpContent.class)));
|
||||
chunk.release();
|
||||
|
||||
assertThat(ch.readOutbound(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentWithAssembledResponseIdentityEncodingHttp10() throws Exception {
|
||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
|
||||
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/"));
|
||||
|
||||
HttpResponse res = new AssembledHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK,
|
||||
Unpooled.copiedBuffer("Hell", CharsetUtil.US_ASCII));
|
||||
ch.writeOutbound(res);
|
||||
|
||||
ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("o, w", CharsetUtil.US_ASCII)));
|
||||
ch.writeOutbound(new DefaultLastHttpContent(Unpooled.copiedBuffer("orld", CharsetUtil.US_ASCII)));
|
||||
|
||||
HttpContent chunk;
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("Hell"));
|
||||
chunk.release();
|
||||
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("o, w"));
|
||||
chunk.release();
|
||||
|
||||
chunk = ch.readOutbound();
|
||||
assertThat(chunk.content().toString(StandardCharsets.UTF_8), is("orld"));
|
||||
assertThat(chunk, is(instanceOf(LastHttpContent.class)));
|
||||
chunk.release();
|
||||
|
||||
assertThat(ch.readOutbound(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChunkedContentWithTrailingHeader() throws Exception {
|
||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
|
||||
|
|
Loading…
Reference in New Issue
Block a user