Fix a bug where HttpObjectAggregator doesn't always produce FullHttpMessage

- Fixes #2182
- Always convert an unfull invalid message to a full message
This commit is contained in:
Trustin Lee 2014-02-19 15:31:53 -08:00
parent 396a81c811
commit ddb6e3672c
2 changed files with 41 additions and 2 deletions

View File

@ -25,7 +25,6 @@ import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.ReferenceCountUtil;
import java.util.List;
@ -136,7 +135,7 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
if (!m.getDecoderResult().isSuccess()) {
removeTransferEncodingChunked(m);
out.add(ReferenceCountUtil.retain(m));
out.add(toFullMessage(m));
this.currentMessage = null;
return;
}
@ -245,4 +244,25 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
currentMessage = null;
}
}
private static FullHttpMessage toFullMessage(HttpMessage msg) {
if (msg instanceof FullHttpMessage) {
return ((FullHttpMessage) msg).retain();
}
FullHttpMessage fullMsg;
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
fullMsg = new DefaultFullHttpRequest(
req.getProtocolVersion(), req.getMethod(), req.getUri(), Unpooled.EMPTY_BUFFER, false);
} else if (msg instanceof HttpResponse) {
HttpResponse res = (HttpResponse) msg;
fullMsg = new DefaultFullHttpResponse(
res.getProtocolVersion(), res.getStatus(), Unpooled.EMPTY_BUFFER, false);
} else {
throw new IllegalStateException();
}
return fullMsg;
}
}

View File

@ -28,6 +28,7 @@ import org.junit.Test;
import java.util.List;
import static io.netty.util.ReferenceCountUtil.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
public class HttpObjectAggregatorTest {
@ -186,4 +187,22 @@ public class HttpObjectAggregatorTest {
checkContentBuffer(aggratedMessage);
assertNull(embedder.readInbound());
}
@Test
public void testBadRequest() {
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder(), new HttpObjectAggregator(1024 * 1024));
ch.writeInbound(Unpooled.copiedBuffer("GET / HTTP/1.0 with extra\r\n", CharsetUtil.UTF_8));
assertThat(ch.readInbound(), is(instanceOf(FullHttpRequest.class)));
assertNull(ch.readInbound());
ch.finish();
}
@Test
public void testBadResponse() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(), new HttpObjectAggregator(1024 * 1024));
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.0 BAD_CODE Bad Server\r\n", CharsetUtil.UTF_8));
assertThat(ch.readInbound(), is(instanceOf(FullHttpResponse.class)));
assertNull(ch.readInbound());
ch.finish();
}
}