Fix test failures introduced by 78d8f05c21

This commit is contained in:
Trustin Lee 2013-06-13 11:51:03 +09:00
parent a403da3042
commit 96380e756c
4 changed files with 51 additions and 9 deletions

View File

@ -21,7 +21,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.MessageList; import io.netty.channel.MessageList;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
@ -436,11 +435,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
actualContentLength = 0; actualContentLength = 0;
} }
// Add the last message (and its content) to the output. // Check if the closure of the connection signifies the end of the content.
reset(out);
// Check if this situation where the connection has been closed before decoding the last message completely
// is expected or not. If unexpected, set decoder result as failure.
boolean prematureClosure; boolean prematureClosure;
if (isDecodingRequest()) { if (isDecodingRequest()) {
// The last request did not wait for a response. // The last request did not wait for a response.
@ -453,8 +448,12 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
prematureClosure = expectedContentLength >= 0 && actualContentLength != expectedContentLength; prematureClosure = expectedContentLength >= 0 && actualContentLength != expectedContentLength;
} }
if (prematureClosure) { if (!prematureClosure) {
message.setDecoderResult(DecoderResult.failure(new PrematureChannelClosureException())); if (actualContentLength == 0) {
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
} else {
out.add(new DefaultLastHttpContent(content));
}
} }
} }
} }

View File

@ -25,7 +25,7 @@ import static org.junit.Assert.*;
public class HttpResponseDecoderTest { public class HttpResponseDecoderTest {
@Test @Test
public void testEmptyHeaderAndEmptyContent() { public void testLastResponseWithEmptyHeaderAndEmptyContent() {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder()); EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n\r\n", CharsetUtil.US_ASCII)); ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n\r\n", CharsetUtil.US_ASCII));
@ -41,4 +41,26 @@ public class HttpResponseDecoderTest {
assertThat(ch.readInbound(), is(nullValue())); assertThat(ch.readInbound(), is(nullValue()));
} }
@Test
public void testLastResponseWithoutContentLengthHeader() {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n\r\n", CharsetUtil.US_ASCII));
HttpResponse res = (HttpResponse) ch.readInbound();
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
assertThat(ch.readInbound(), is(nullValue()));
ch.writeInbound(Unpooled.wrappedBuffer(new byte[1024]));
HttpContent content = (HttpContent) ch.readInbound();
assertThat(content.content().readableBytes(), is(1024));
assertThat(ch.finish(), is(true));
LastHttpContent lastContent = (LastHttpContent) ch.readInbound();
assertThat(lastContent.content().isReadable(), is(false));
assertThat(ch.readInbound(), is(nullValue()));
}
} }

View File

@ -184,6 +184,18 @@ public final class MessageList<T> implements Iterable<T> {
return elements[index]; return elements[index];
} }
/**
* Sets the message on the given index.
*/
public MessageList<T> set(int index, T value) {
checkExclusive(index);
if (value == null) {
throw new NullPointerException("value");
}
elements[index] = value;
return this;
}
/** /**
* Add the message to this {@link MessageList} and return itself. * Add the message to this {@link MessageList} and return itself.
*/ */

View File

@ -280,6 +280,15 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
long expectedWrittenBytes = 0; long expectedWrittenBytes = 0;
for (int i = index; i < size; i++) { for (int i = index; i < size; i++) {
ByteBuf buf = bufs.get(i); ByteBuf buf = bufs.get(i);
if (!buf.isDirect()) {
int readableBytes = buf.readableBytes();
ByteBuf directBuf = alloc().directBuffer(readableBytes);
directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
buf.release();
bufs.set(i, directBuf);
buf = directBuf;
}
int count = buf.nioBufferCount(); int count = buf.nioBufferCount();
if (count == 1) { if (count == 1) {
if (nioBufferCnt == nioBuffers.length) { if (nioBufferCnt == nioBuffers.length) {