Remove unnecessary code from HttpObjectDecoder and re-enable all HTTP tests
- Since Netty 4, HTTP decoder does not generate a full message at all. Therefore, there's no need to keep separate states for the content smaller than maxChunkSize. - maxChunkSize must be greater than 0. Setting it to 0 should not disable chunked encoding. We have a dedicated flag for that. - Uncommented the tests that were commented out for an unknown reason, with some fixes. - Added more tests for HTTP decoder. - Removed the Ignore annotation on some tests.
This commit is contained in:
parent
3e8a1ed611
commit
fa336db891
@ -26,7 +26,7 @@ import io.netty.util.internal.AppendableCharSequence;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static io.netty.buffer.ByteBufUtil.readBytes;
|
import static io.netty.buffer.ByteBufUtil.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes {@link ByteBuf}s into {@link HttpMessage}s and
|
* Decodes {@link ByteBuf}s into {@link HttpMessage}s and
|
||||||
@ -107,11 +107,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
private final boolean chunkedSupported;
|
private final boolean chunkedSupported;
|
||||||
protected final boolean validateHeaders;
|
protected final boolean validateHeaders;
|
||||||
|
|
||||||
private ByteBuf content;
|
|
||||||
private HttpMessage message;
|
private HttpMessage message;
|
||||||
private long chunkSize;
|
private long chunkSize;
|
||||||
private int headerSize;
|
private int headerSize;
|
||||||
private int contentRead;
|
|
||||||
private final AppendableCharSequence sb = new AppendableCharSequence(128);
|
private final AppendableCharSequence sb = new AppendableCharSequence(128);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,12 +121,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
READ_INITIAL,
|
READ_INITIAL,
|
||||||
READ_HEADER,
|
READ_HEADER,
|
||||||
READ_VARIABLE_LENGTH_CONTENT,
|
READ_VARIABLE_LENGTH_CONTENT,
|
||||||
READ_VARIABLE_LENGTH_CONTENT_AS_CHUNKS,
|
|
||||||
READ_FIXED_LENGTH_CONTENT,
|
READ_FIXED_LENGTH_CONTENT,
|
||||||
READ_FIXED_LENGTH_CONTENT_AS_CHUNKS,
|
|
||||||
READ_CHUNK_SIZE,
|
READ_CHUNK_SIZE,
|
||||||
READ_CHUNKED_CONTENT,
|
READ_CHUNKED_CONTENT,
|
||||||
READ_CHUNKED_CONTENT_AS_CHUNKS,
|
|
||||||
READ_CHUNK_DELIMITER,
|
READ_CHUNK_DELIMITER,
|
||||||
READ_CHUNK_FOOTER,
|
READ_CHUNK_FOOTER,
|
||||||
BAD_MESSAGE
|
BAD_MESSAGE
|
||||||
@ -170,7 +165,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
"maxHeaderSize must be a positive integer: " +
|
"maxHeaderSize must be a positive integer: " +
|
||||||
maxHeaderSize);
|
maxHeaderSize);
|
||||||
}
|
}
|
||||||
if (maxChunkSize < 0) {
|
if (maxChunkSize <= 0) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"maxChunkSize must be a positive integer: " +
|
"maxChunkSize must be a positive integer: " +
|
||||||
maxChunkSize);
|
maxChunkSize);
|
||||||
@ -221,39 +216,28 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
}
|
}
|
||||||
if (nextState == State.SKIP_CONTROL_CHARS) {
|
if (nextState == State.SKIP_CONTROL_CHARS) {
|
||||||
// No content is expected.
|
// No content is expected.
|
||||||
reset(out);
|
out.add(message);
|
||||||
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||||
|
reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
long contentLength = HttpHeaders.getContentLength(message, -1);
|
long contentLength = HttpHeaders.getContentLength(message, -1);
|
||||||
if (contentLength == 0 || contentLength == -1 && isDecodingRequest()) {
|
if (contentLength == 0 || contentLength == -1 && isDecodingRequest()) {
|
||||||
content = Unpooled.EMPTY_BUFFER;
|
out.add(message);
|
||||||
reset(out);
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||||
|
reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (nextState) {
|
assert nextState == State.READ_FIXED_LENGTH_CONTENT || nextState == State.READ_VARIABLE_LENGTH_CONTENT;
|
||||||
case READ_FIXED_LENGTH_CONTENT:
|
|
||||||
if (contentLength > maxChunkSize || HttpHeaders.is100ContinueExpected(message)) {
|
out.add(message);
|
||||||
// Generate FullHttpMessage first. HttpChunks will follow.
|
|
||||||
checkpoint(State.READ_FIXED_LENGTH_CONTENT_AS_CHUNKS);
|
if (nextState == State.READ_FIXED_LENGTH_CONTENT) {
|
||||||
// chunkSize will be decreased as the READ_FIXED_LENGTH_CONTENT_AS_CHUNKS
|
// chunkSize will be decreased as the READ_FIXED_LENGTH_CONTENT state reads data chunk by chunk.
|
||||||
// state reads data chunk by chunk.
|
chunkSize = HttpHeaders.getContentLength(message, -1);
|
||||||
chunkSize = HttpHeaders.getContentLength(message, -1);
|
|
||||||
out.add(message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case READ_VARIABLE_LENGTH_CONTENT:
|
|
||||||
if (buffer.readableBytes() > maxChunkSize || HttpHeaders.is100ContinueExpected(message)) {
|
|
||||||
// Generate FullHttpMessage first. HttpChunks will follow.
|
|
||||||
checkpoint(State.READ_VARIABLE_LENGTH_CONTENT_AS_CHUNKS);
|
|
||||||
out.add(message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalStateException("Unexpected state: " + nextState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We return here, this forces decode to be called again where we will decode the content
|
// We return here, this forces decode to be called again where we will decode the content
|
||||||
return;
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -261,35 +245,25 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case READ_VARIABLE_LENGTH_CONTENT: {
|
case READ_VARIABLE_LENGTH_CONTENT: {
|
||||||
int toRead = actualReadableBytes();
|
|
||||||
if (toRead > maxChunkSize) {
|
|
||||||
toRead = maxChunkSize;
|
|
||||||
}
|
|
||||||
out.add(message);
|
|
||||||
out.add(new DefaultHttpContent(readBytes(ctx.alloc(), buffer, toRead)));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case READ_VARIABLE_LENGTH_CONTENT_AS_CHUNKS: {
|
|
||||||
// Keep reading data as a chunk until the end of connection is reached.
|
// Keep reading data as a chunk until the end of connection is reached.
|
||||||
int toRead = actualReadableBytes();
|
int toRead = Math.min(actualReadableBytes(), maxChunkSize);
|
||||||
if (toRead > maxChunkSize) {
|
if (toRead > 0) {
|
||||||
toRead = maxChunkSize;
|
ByteBuf content = readBytes(ctx.alloc(), buffer, toRead);
|
||||||
}
|
if (buffer.isReadable()) {
|
||||||
ByteBuf content = readBytes(ctx.alloc(), buffer, toRead);
|
out.add(new DefaultHttpContent(content));
|
||||||
if (!buffer.isReadable()) {
|
} else {
|
||||||
|
// End of connection.
|
||||||
|
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
} else if (!buffer.isReadable()) {
|
||||||
|
// End of connection.
|
||||||
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||||
reset();
|
reset();
|
||||||
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
out.add(new DefaultHttpContent(content));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case READ_FIXED_LENGTH_CONTENT: {
|
case READ_FIXED_LENGTH_CONTENT: {
|
||||||
readFixedLengthContent(ctx, buffer, out);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case READ_FIXED_LENGTH_CONTENT_AS_CHUNKS: {
|
|
||||||
long chunkSize = this.chunkSize;
|
|
||||||
int readLimit = actualReadableBytes();
|
int readLimit = actualReadableBytes();
|
||||||
|
|
||||||
// Check if the buffer is readable first as we use the readable byte count
|
// Check if the buffer is readable first as we use the readable byte count
|
||||||
@ -302,28 +276,20 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int toRead = readLimit;
|
int toRead = Math.min(readLimit, maxChunkSize);
|
||||||
if (toRead > maxChunkSize) {
|
|
||||||
toRead = maxChunkSize;
|
|
||||||
}
|
|
||||||
if (toRead > chunkSize) {
|
if (toRead > chunkSize) {
|
||||||
toRead = (int) chunkSize;
|
toRead = (int) chunkSize;
|
||||||
}
|
}
|
||||||
ByteBuf content = readBytes(ctx.alloc(), buffer, toRead);
|
ByteBuf content = readBytes(ctx.alloc(), buffer, toRead);
|
||||||
if (chunkSize > toRead) {
|
chunkSize -= toRead;
|
||||||
chunkSize -= toRead;
|
|
||||||
} else {
|
|
||||||
chunkSize = 0;
|
|
||||||
}
|
|
||||||
this.chunkSize = chunkSize;
|
|
||||||
|
|
||||||
if (chunkSize == 0) {
|
if (chunkSize == 0) {
|
||||||
// Read all content.
|
// Read all content.
|
||||||
reset();
|
|
||||||
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
||||||
return;
|
reset();
|
||||||
|
} else {
|
||||||
|
out.add(new DefaultHttpContent(content));
|
||||||
}
|
}
|
||||||
out.add(new DefaultHttpContent(content));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -337,9 +303,6 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
if (chunkSize == 0) {
|
if (chunkSize == 0) {
|
||||||
checkpoint(State.READ_CHUNK_FOOTER);
|
checkpoint(State.READ_CHUNK_FOOTER);
|
||||||
return;
|
return;
|
||||||
} else if (chunkSize > maxChunkSize) {
|
|
||||||
// A chunk is too large. Split them into multiple chunks again.
|
|
||||||
checkpoint(State.READ_CHUNKED_CONTENT_AS_CHUNKS);
|
|
||||||
} else {
|
} else {
|
||||||
checkpoint(State.READ_CHUNKED_CONTENT);
|
checkpoint(State.READ_CHUNKED_CONTENT);
|
||||||
}
|
}
|
||||||
@ -349,48 +312,19 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
}
|
}
|
||||||
case READ_CHUNKED_CONTENT: {
|
case READ_CHUNKED_CONTENT: {
|
||||||
assert chunkSize <= Integer.MAX_VALUE;
|
assert chunkSize <= Integer.MAX_VALUE;
|
||||||
HttpContent chunk = new DefaultHttpContent(readBytes(ctx.alloc(), buffer, (int) chunkSize));
|
int toRead = Math.min((int) chunkSize, maxChunkSize);
|
||||||
checkpoint(State.READ_CHUNK_DELIMITER);
|
|
||||||
out.add(chunk);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case READ_CHUNKED_CONTENT_AS_CHUNKS: {
|
|
||||||
assert chunkSize <= Integer.MAX_VALUE;
|
|
||||||
int chunkSize = (int) this.chunkSize;
|
|
||||||
int readLimit = actualReadableBytes();
|
|
||||||
|
|
||||||
// Check if the buffer is readable first as we use the readable byte count
|
|
||||||
// to create the HttpChunk. This is needed as otherwise we may end up with
|
|
||||||
// create a HttpChunk instance that contains an empty buffer and so is
|
|
||||||
// handled like it is the last HttpChunk.
|
|
||||||
//
|
|
||||||
// See https://github.com/netty/netty/issues/433
|
|
||||||
if (readLimit == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int toRead = chunkSize;
|
|
||||||
if (toRead > maxChunkSize) {
|
|
||||||
toRead = maxChunkSize;
|
|
||||||
}
|
|
||||||
if (toRead > readLimit) {
|
|
||||||
toRead = readLimit;
|
|
||||||
}
|
|
||||||
HttpContent chunk = new DefaultHttpContent(readBytes(ctx.alloc(), buffer, toRead));
|
HttpContent chunk = new DefaultHttpContent(readBytes(ctx.alloc(), buffer, toRead));
|
||||||
if (chunkSize > toRead) {
|
chunkSize -= toRead;
|
||||||
chunkSize -= toRead;
|
|
||||||
} else {
|
out.add(chunk);
|
||||||
chunkSize = 0;
|
|
||||||
}
|
|
||||||
this.chunkSize = chunkSize;
|
|
||||||
|
|
||||||
if (chunkSize == 0) {
|
if (chunkSize == 0) {
|
||||||
// Read all content.
|
// Read all content.
|
||||||
checkpoint(State.READ_CHUNK_DELIMITER);
|
checkpoint(State.READ_CHUNK_DELIMITER);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.add(chunk);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
case READ_CHUNK_DELIMITER: {
|
case READ_CHUNK_DELIMITER: {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@ -410,16 +344,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
}
|
}
|
||||||
case READ_CHUNK_FOOTER: try {
|
case READ_CHUNK_FOOTER: try {
|
||||||
LastHttpContent trailer = readTrailingHeaders(buffer);
|
LastHttpContent trailer = readTrailingHeaders(buffer);
|
||||||
if (maxChunkSize == 0) {
|
out.add(trailer);
|
||||||
// Chunked encoding disabled.
|
reset();
|
||||||
reset(out);
|
return;
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
reset();
|
|
||||||
// The last chunk, which is empty
|
|
||||||
out.add(trailer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
out.add(invalidChunk(e));
|
out.add(invalidChunk(e));
|
||||||
return;
|
return;
|
||||||
@ -427,10 +354,6 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
case BAD_MESSAGE: {
|
case BAD_MESSAGE: {
|
||||||
// Keep discarding until disconnection.
|
// Keep discarding until disconnection.
|
||||||
buffer.skipBytes(actualReadableBytes());
|
buffer.skipBytes(actualReadableBytes());
|
||||||
return;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
throw new Error("Shouldn't reach here.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -441,14 +364,8 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
|
|
||||||
// Handle the last unfinished message.
|
// Handle the last unfinished message.
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
// Get the length of the content received so far for the last message.
|
|
||||||
HttpMessage message = this.message;
|
HttpMessage message = this.message;
|
||||||
int actualContentLength;
|
this.message = null;
|
||||||
if (content != null) {
|
|
||||||
actualContentLength = content.readableBytes();
|
|
||||||
} else {
|
|
||||||
actualContentLength = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the closure of the connection signifies the end of the content.
|
// Check if the closure of the connection signifies the end of the content.
|
||||||
boolean prematureClosure;
|
boolean prematureClosure;
|
||||||
@ -460,15 +377,11 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
// If the 'Content-Length' header is absent, the length of the content is determined by the end of the
|
// If the 'Content-Length' header is absent, the length of the content is determined by the end of the
|
||||||
// connection, so it is perfectly fine.
|
// connection, so it is perfectly fine.
|
||||||
long expectedContentLength = HttpHeaders.getContentLength(message, -1);
|
long expectedContentLength = HttpHeaders.getContentLength(message, -1);
|
||||||
prematureClosure = expectedContentLength >= 0 && actualContentLength != expectedContentLength;
|
prematureClosure = expectedContentLength > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!prematureClosure) {
|
if (!prematureClosure) {
|
||||||
if (actualContentLength == 0) {
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||||
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
|
||||||
} else {
|
|
||||||
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -484,11 +397,8 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
// - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html Section 4.4
|
// - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html Section 4.4
|
||||||
// - https://github.com/netty/netty/issues/222
|
// - https://github.com/netty/netty/issues/222
|
||||||
if (code >= 100 && code < 200) {
|
if (code >= 100 && code < 200) {
|
||||||
if (code == 101 && !res.headers().contains(HttpHeaders.Names.SEC_WEBSOCKET_ACCEPT)) {
|
// One exception: Hixie 76 websocket handshake response
|
||||||
// It's Hixie 76 websocket handshake response
|
return !(code == 101 && !res.headers().contains(HttpHeaders.Names.SEC_WEBSOCKET_ACCEPT));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (code) {
|
switch (code) {
|
||||||
@ -500,28 +410,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void reset() {
|
private void reset() {
|
||||||
reset(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reset(List<Object> out) {
|
|
||||||
if (out != null) {
|
|
||||||
HttpMessage message = this.message;
|
|
||||||
ByteBuf content = this.content;
|
|
||||||
LastHttpContent httpContent;
|
|
||||||
|
|
||||||
if (content == null || !content.isReadable()) {
|
|
||||||
httpContent = LastHttpContent.EMPTY_LAST_CONTENT;
|
|
||||||
} else {
|
|
||||||
httpContent = new DefaultLastHttpContent(content, validateHeaders);
|
|
||||||
}
|
|
||||||
|
|
||||||
out.add(message);
|
|
||||||
out.add(httpContent);
|
|
||||||
}
|
|
||||||
|
|
||||||
content = null;
|
|
||||||
message = null;
|
message = null;
|
||||||
|
|
||||||
checkpoint(State.SKIP_CONTROL_CHARS);
|
checkpoint(State.SKIP_CONTROL_CHARS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -554,28 +443,6 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readFixedLengthContent(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
|
|
||||||
//we have a content-length so we just read the correct number of bytes
|
|
||||||
long length = HttpHeaders.getContentLength(message, -1);
|
|
||||||
assert length <= Integer.MAX_VALUE;
|
|
||||||
int toRead = (int) length - contentRead;
|
|
||||||
if (toRead > actualReadableBytes()) {
|
|
||||||
toRead = actualReadableBytes();
|
|
||||||
}
|
|
||||||
contentRead += toRead;
|
|
||||||
if (length < contentRead) {
|
|
||||||
out.add(message);
|
|
||||||
out.add(new DefaultHttpContent(readBytes(ctx.alloc(), buffer, toRead)));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (content == null) {
|
|
||||||
content = readBytes(ctx.alloc(), buffer, (int) length);
|
|
||||||
} else {
|
|
||||||
content.writeBytes(buffer, (int) length);
|
|
||||||
}
|
|
||||||
reset(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
private State readHeaders(ByteBuf buffer) {
|
private State readHeaders(ByteBuf buffer) {
|
||||||
headerSize = 0;
|
headerSize = 0;
|
||||||
final HttpMessage message = this.message;
|
final HttpMessage message = this.message;
|
||||||
|
@ -20,7 +20,6 @@ import io.netty.buffer.Unpooled;
|
|||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
import io.netty.handler.codec.DecoderResult;
|
import io.netty.handler.codec.DecoderResult;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -42,7 +41,6 @@ public class HttpInvalidMessageTest {
|
|||||||
ensureInboundTrafficDiscarded(ch);
|
ensureInboundTrafficDiscarded(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("expected ATM")
|
|
||||||
@Test
|
@Test
|
||||||
public void testRequestWithBadHeader() throws Exception {
|
public void testRequestWithBadHeader() throws Exception {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder());
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder());
|
||||||
@ -70,7 +68,6 @@ public class HttpInvalidMessageTest {
|
|||||||
ensureInboundTrafficDiscarded(ch);
|
ensureInboundTrafficDiscarded(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("expected ATM")
|
|
||||||
@Test
|
@Test
|
||||||
public void testResponseWithBadHeader() throws Exception {
|
public void testResponseWithBadHeader() throws Exception {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
|
@ -19,15 +19,17 @@ package io.netty.handler.codec.http;
|
|||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class HttpRequestDecoderTest {
|
public class HttpRequestDecoderTest {
|
||||||
private static final byte[] CONTENT_CRLF_DELIMITERS = createContent("\r\n");
|
private static final byte[] CONTENT_CRLF_DELIMITERS = createContent("\r\n");
|
||||||
private static final byte[] CONTENT_LF_DELIMITERS = createContent("\n");
|
private static final byte[] CONTENT_LF_DELIMITERS = createContent("\n");
|
||||||
private static final byte[] CONTENT_MIXED_DELIMITERS = createContent("\r\n", "\n");
|
private static final byte[] CONTENT_MIXED_DELIMITERS = createContent("\r\n", "\n");
|
||||||
|
private static final int CONTENT_LENGTH = 8;
|
||||||
|
|
||||||
private static byte[] createContent(String... lineDelimiters) {
|
private static byte[] createContent(String... lineDelimiters) {
|
||||||
String lineDelimiter;
|
String lineDelimiter;
|
||||||
@ -46,7 +48,7 @@ public class HttpRequestDecoderTest {
|
|||||||
"Origin: http://localhost:8080" + lineDelimiter +
|
"Origin: http://localhost:8080" + lineDelimiter +
|
||||||
"Sec-WebSocket-Key1: 10 28 8V7 8 48 0" + lineDelimiter2 +
|
"Sec-WebSocket-Key1: 10 28 8V7 8 48 0" + lineDelimiter2 +
|
||||||
"Sec-WebSocket-Key2: 8 Xt754O3Q3QW 0 _60" + lineDelimiter +
|
"Sec-WebSocket-Key2: 8 Xt754O3Q3QW 0 _60" + lineDelimiter +
|
||||||
"Content-Length: 8" + lineDelimiter2 +
|
"Content-Length: " + CONTENT_LENGTH + lineDelimiter2 +
|
||||||
"\r\n" +
|
"\r\n" +
|
||||||
"12345678").getBytes(CharsetUtil.US_ASCII);
|
"12345678").getBytes(CharsetUtil.US_ASCII);
|
||||||
}
|
}
|
||||||
@ -68,34 +70,36 @@ public class HttpRequestDecoderTest {
|
|||||||
|
|
||||||
private static void testDecodeWholeRequestAtOnce(byte[] content) {
|
private static void testDecodeWholeRequestAtOnce(byte[] content) {
|
||||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
||||||
Assert.assertTrue(channel.writeInbound(Unpooled.wrappedBuffer(content)));
|
assertTrue(channel.writeInbound(Unpooled.wrappedBuffer(content)));
|
||||||
HttpRequest req = channel.readInbound();
|
HttpRequest req = channel.readInbound();
|
||||||
Assert.assertNotNull(req);
|
assertNotNull(req);
|
||||||
checkHeaders(req.headers());
|
checkHeaders(req.headers());
|
||||||
LastHttpContent c = channel.readInbound();
|
LastHttpContent c = channel.readInbound();
|
||||||
Assert.assertEquals(8, c.content().readableBytes());
|
assertEquals(CONTENT_LENGTH, c.content().readableBytes());
|
||||||
Assert.assertEquals(Unpooled.wrappedBuffer(content, content.length - 8, 8), c.content().readBytes(8));
|
assertEquals(
|
||||||
|
Unpooled.wrappedBuffer(content, content.length - CONTENT_LENGTH, CONTENT_LENGTH),
|
||||||
|
c.content().readBytes(CONTENT_LENGTH));
|
||||||
c.release();
|
c.release();
|
||||||
|
|
||||||
Assert.assertFalse(channel.finish());
|
assertFalse(channel.finish());
|
||||||
Assert.assertNull(channel.readInbound());
|
assertNull(channel.readInbound());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkHeaders(HttpHeaders headers) {
|
private static void checkHeaders(HttpHeaders headers) {
|
||||||
Assert.assertEquals(7, headers.names().size());
|
assertEquals(7, headers.names().size());
|
||||||
checkHeader(headers, "Upgrade", "WebSocket");
|
checkHeader(headers, "Upgrade", "WebSocket");
|
||||||
checkHeader(headers, "Connection", "Upgrade");
|
checkHeader(headers, "Connection", "Upgrade");
|
||||||
checkHeader(headers, "Host", "localhost");
|
checkHeader(headers, "Host", "localhost");
|
||||||
checkHeader(headers, "Origin", "http://localhost:8080");
|
checkHeader(headers, "Origin", "http://localhost:8080");
|
||||||
checkHeader(headers, "Sec-WebSocket-Key1", "10 28 8V7 8 48 0");
|
checkHeader(headers, "Sec-WebSocket-Key1", "10 28 8V7 8 48 0");
|
||||||
checkHeader(headers, "Sec-WebSocket-Key2", "8 Xt754O3Q3QW 0 _60");
|
checkHeader(headers, "Sec-WebSocket-Key2", "8 Xt754O3Q3QW 0 _60");
|
||||||
checkHeader(headers, "Content-Length", "8");
|
checkHeader(headers, "Content-Length", String.valueOf(CONTENT_LENGTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkHeader(HttpHeaders headers, String name, String value) {
|
private static void checkHeader(HttpHeaders headers, String name, String value) {
|
||||||
List<String> header1 = headers.getAll(name);
|
List<String> header1 = headers.getAll(name);
|
||||||
Assert.assertEquals(1, header1.size());
|
assertEquals(1, header1.size());
|
||||||
Assert.assertEquals(value, header1.get(0));
|
assertEquals(value, header1.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -121,7 +125,7 @@ public class HttpRequestDecoderTest {
|
|||||||
|
|
||||||
private static void testDecodeWholeRequestInMultipleSteps(byte[] content, int fragmentSize) {
|
private static void testDecodeWholeRequestInMultipleSteps(byte[] content, int fragmentSize) {
|
||||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
|
||||||
int headerLength = content.length - 8;
|
int headerLength = content.length - CONTENT_LENGTH;
|
||||||
|
|
||||||
// split up the header
|
// split up the header
|
||||||
for (int a = 0; a < headerLength;) {
|
for (int a = 0; a < headerLength;) {
|
||||||
@ -136,24 +140,29 @@ public class HttpRequestDecoderTest {
|
|||||||
a += amount;
|
a += amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 8; i > 0; i--) {
|
for (int i = CONTENT_LENGTH; i > 0; i --) {
|
||||||
// Should produce HttpContent
|
// Should produce HttpContent
|
||||||
channel.writeInbound(Unpooled.wrappedBuffer(content, content.length - i, 1));
|
channel.writeInbound(Unpooled.wrappedBuffer(content, content.length - i, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRequest req = channel.readInbound();
|
HttpRequest req = channel.readInbound();
|
||||||
Assert.assertNotNull(req);
|
assertNotNull(req);
|
||||||
checkHeaders(req.headers());
|
checkHeaders(req.headers());
|
||||||
|
|
||||||
LastHttpContent c = channel.readInbound();
|
for (int i = CONTENT_LENGTH; i > 1; i --) {
|
||||||
Assert.assertEquals(8, c.content().readableBytes());
|
HttpContent c = channel.readInbound();
|
||||||
for (int i = 8; i > 1; i--) {
|
assertEquals(1, c.content().readableBytes());
|
||||||
Assert.assertEquals(content[content.length - i], c.content().readByte());
|
assertEquals(content[content.length - i], c.content().readByte());
|
||||||
|
c.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LastHttpContent c = channel.readInbound();
|
||||||
|
assertEquals(1, c.content().readableBytes());
|
||||||
|
assertEquals(content[content.length - 1], c.content().readByte());
|
||||||
c.release();
|
c.release();
|
||||||
|
|
||||||
Assert.assertFalse(channel.finish());
|
assertFalse(channel.finish());
|
||||||
Assert.assertNull(channel.readInbound());
|
assertNull(channel.readInbound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -165,6 +174,6 @@ public class HttpRequestDecoderTest {
|
|||||||
"EmptyHeader:" + crlf + crlf;
|
"EmptyHeader:" + crlf + crlf;
|
||||||
channel.writeInbound(Unpooled.wrappedBuffer(request.getBytes(CharsetUtil.US_ASCII)));
|
channel.writeInbound(Unpooled.wrappedBuffer(request.getBytes(CharsetUtil.US_ASCII)));
|
||||||
HttpRequest req = channel.readInbound();
|
HttpRequest req = channel.readInbound();
|
||||||
Assert.assertEquals("", req.headers().get("EmptyHeader"));
|
assertEquals("", req.headers().get("EmptyHeader"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ package io.netty.handler.codec.http;
|
|||||||
|
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.embedded.EmbeddedChannel;
|
import io.netty.channel.embedded.EmbeddedChannel;
|
||||||
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -27,14 +28,13 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
public class HttpResponseDecoderTest {
|
public class HttpResponseDecoderTest {
|
||||||
|
|
||||||
/*
|
|
||||||
@Test
|
@Test
|
||||||
public void testResponseChunked() {
|
public void testResponseChunked() {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n",
|
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n",
|
||||||
CharsetUtil.US_ASCII));
|
CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
HttpResponse res = (HttpResponse) ch.readInbound();
|
HttpResponse res = ch.readInbound();
|
||||||
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ public class HttpResponseDecoderTest {
|
|||||||
assertFalse(ch.writeInbound(Unpooled.copiedBuffer(Integer.toHexString(data.length) + "\r\n",
|
assertFalse(ch.writeInbound(Unpooled.copiedBuffer(Integer.toHexString(data.length) + "\r\n",
|
||||||
CharsetUtil.US_ASCII)));
|
CharsetUtil.US_ASCII)));
|
||||||
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(data)));
|
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(data)));
|
||||||
HttpContent content = (HttpContent) ch.readInbound();
|
HttpContent content = ch.readInbound();
|
||||||
assertEquals(data.length, content.content().readableBytes());
|
assertEquals(data.length, content.content().readableBytes());
|
||||||
|
|
||||||
byte[] decodedData = new byte[data.length];
|
byte[] decodedData = new byte[data.length];
|
||||||
@ -57,22 +57,26 @@ public class HttpResponseDecoderTest {
|
|||||||
|
|
||||||
assertFalse(ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)));
|
assertFalse(ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)));
|
||||||
}
|
}
|
||||||
assertTrue(ch.finish());
|
|
||||||
|
|
||||||
LastHttpContent content = (LastHttpContent) ch.readInbound();
|
// Write the last chunk.
|
||||||
|
ch.writeInbound(Unpooled.copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Ensure the last chunk was decoded.
|
||||||
|
LastHttpContent content = ch.readInbound();
|
||||||
assertFalse(content.content().isReadable());
|
assertFalse(content.content().isReadable());
|
||||||
content.release();
|
content.release();
|
||||||
|
|
||||||
|
ch.finish();
|
||||||
assertNull(ch.readInbound());
|
assertNull(ch.readInbound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testResponseChunkedExceedMaxChunkSize() {
|
public void testResponseChunkedExceedMaxChunkSize() {
|
||||||
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(4096, 8192, 32));
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(4096, 8192, 32));
|
||||||
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n",
|
ch.writeInbound(
|
||||||
CharsetUtil.US_ASCII));
|
Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
HttpResponse res = (HttpResponse) ch.readInbound();
|
HttpResponse res = ch.readInbound();
|
||||||
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
|
||||||
@ -87,11 +91,11 @@ public class HttpResponseDecoderTest {
|
|||||||
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(data)));
|
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(data)));
|
||||||
|
|
||||||
byte[] decodedData = new byte[data.length];
|
byte[] decodedData = new byte[data.length];
|
||||||
HttpContent content = (HttpContent) ch.readInbound();
|
HttpContent content = ch.readInbound();
|
||||||
assertEquals(32, content.content().readableBytes());
|
assertEquals(32, content.content().readableBytes());
|
||||||
content.content().readBytes(decodedData, 0, 32);
|
content.content().readBytes(decodedData, 0, 32);
|
||||||
|
|
||||||
content = (HttpContent) ch.readInbound();
|
content = ch.readInbound();
|
||||||
assertEquals(32, content.content().readableBytes());
|
assertEquals(32, content.content().readableBytes());
|
||||||
|
|
||||||
content.content().readBytes(decodedData, 32, 32);
|
content.content().readBytes(decodedData, 32, 32);
|
||||||
@ -101,15 +105,122 @@ public class HttpResponseDecoderTest {
|
|||||||
|
|
||||||
assertFalse(ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)));
|
assertFalse(ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)));
|
||||||
}
|
}
|
||||||
assertTrue(ch.finish());
|
|
||||||
|
|
||||||
LastHttpContent content = (LastHttpContent) ch.readInbound();
|
// Write the last chunk.
|
||||||
|
ch.writeInbound(Unpooled.copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Ensure the last chunk was decoded.
|
||||||
|
LastHttpContent content = ch.readInbound();
|
||||||
assertFalse(content.content().isReadable());
|
assertFalse(content.content().isReadable());
|
||||||
content.release();
|
content.release();
|
||||||
|
|
||||||
|
ch.finish();
|
||||||
assertNull(ch.readInbound());
|
assertNull(ch.readInbound());
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@Test
|
||||||
|
public void testClosureWithoutContentLength1() throws Exception {
|
||||||
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
|
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n\r\n", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Read the response headers.
|
||||||
|
HttpResponse res = ch.readInbound();
|
||||||
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
|
||||||
|
// Close the connection without sending anything.
|
||||||
|
assertTrue(ch.finish());
|
||||||
|
|
||||||
|
// The decoder should still produce the last content.
|
||||||
|
LastHttpContent content = ch.readInbound();
|
||||||
|
assertThat(content.content().isReadable(), is(false));
|
||||||
|
content.release();
|
||||||
|
|
||||||
|
// But nothing more.
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClosureWithoutContentLength2() throws Exception {
|
||||||
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
|
|
||||||
|
// Write the partial response.
|
||||||
|
ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n\r\n12345678", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Read the response headers.
|
||||||
|
HttpResponse res = ch.readInbound();
|
||||||
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
|
||||||
|
// Read the partial content.
|
||||||
|
HttpContent content = ch.readInbound();
|
||||||
|
assertThat(content.content().toString(CharsetUtil.US_ASCII), is("12345678"));
|
||||||
|
assertThat(content, is(not(instanceOf(LastHttpContent.class))));
|
||||||
|
content.release();
|
||||||
|
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
|
||||||
|
// Close the connection.
|
||||||
|
assertTrue(ch.finish());
|
||||||
|
|
||||||
|
// The decoder should still produce the last content.
|
||||||
|
LastHttpContent lastContent = ch.readInbound();
|
||||||
|
assertThat(lastContent.content().isReadable(), is(false));
|
||||||
|
lastContent.release();
|
||||||
|
|
||||||
|
// But nothing more.
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrematureClosureWithChunkedEncoding1() throws Exception {
|
||||||
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
|
ch.writeInbound(
|
||||||
|
Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Read the response headers.
|
||||||
|
HttpResponse res = ch.readInbound();
|
||||||
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked"));
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
|
||||||
|
// Close the connection without sending anything.
|
||||||
|
ch.finish();
|
||||||
|
|
||||||
|
// The decoder should not generate the last chunk because it's closed prematurely.
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrematureClosureWithChunkedEncoding2() throws Exception {
|
||||||
|
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
||||||
|
|
||||||
|
// Write the partial response.
|
||||||
|
ch.writeInbound(Unpooled.copiedBuffer(
|
||||||
|
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n8\r\n12345678", CharsetUtil.US_ASCII));
|
||||||
|
|
||||||
|
// Read the response headers.
|
||||||
|
HttpResponse res = ch.readInbound();
|
||||||
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked"));
|
||||||
|
|
||||||
|
// Read the partial content.
|
||||||
|
HttpContent content = ch.readInbound();
|
||||||
|
assertThat(content.content().toString(CharsetUtil.US_ASCII), is("12345678"));
|
||||||
|
assertThat(content, is(not(instanceOf(LastHttpContent.class))));
|
||||||
|
content.release();
|
||||||
|
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
|
||||||
|
// Close the connection.
|
||||||
|
ch.finish();
|
||||||
|
|
||||||
|
// The decoder should not generate the last chunk because it's closed prematurely.
|
||||||
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLastResponseWithEmptyHeaderAndEmptyContent() {
|
public void testLastResponseWithEmptyHeaderAndEmptyContent() {
|
||||||
@ -281,10 +392,15 @@ public class HttpResponseDecoderTest {
|
|||||||
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
|
||||||
LastHttpContent content = ch.readInbound();
|
HttpContent firstContent = ch.readInbound();
|
||||||
assertEquals(10, content.content().readableBytes());
|
assertThat(firstContent.content().readableBytes(), is(5));
|
||||||
assertEquals(Unpooled.wrappedBuffer(data), content.content());
|
assertEquals(Unpooled.wrappedBuffer(data, 0, 5), firstContent.content());
|
||||||
content.release();
|
firstContent.release();
|
||||||
|
|
||||||
|
LastHttpContent lastContent = ch.readInbound();
|
||||||
|
assertEquals(5, lastContent.content().readableBytes());
|
||||||
|
assertEquals(Unpooled.wrappedBuffer(data, 5, 5), lastContent.content());
|
||||||
|
lastContent.release();
|
||||||
|
|
||||||
assertThat(ch.finish(), is(false));
|
assertThat(ch.finish(), is(false));
|
||||||
assertThat(ch.readInbound(), is(nullValue()));
|
assertThat(ch.readInbound(), is(nullValue()));
|
||||||
@ -324,9 +440,14 @@ public class HttpResponseDecoderTest {
|
|||||||
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
|
||||||
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
|
||||||
|
|
||||||
|
HttpContent firstContent = ch.readInbound();
|
||||||
|
assertThat(firstContent.content().readableBytes(), is(5));
|
||||||
|
assertEquals(Unpooled.wrappedBuffer(data, 0, 5), firstContent.content());
|
||||||
|
firstContent.release();
|
||||||
|
|
||||||
LastHttpContent lastContent = ch.readInbound();
|
LastHttpContent lastContent = ch.readInbound();
|
||||||
assertEquals(10, lastContent.content().readableBytes());
|
assertEquals(5, lastContent.content().readableBytes());
|
||||||
assertEquals(Unpooled.wrappedBuffer(data), lastContent.content());
|
assertEquals(Unpooled.wrappedBuffer(data, 5, 5), lastContent.content());
|
||||||
lastContent.release();
|
lastContent.release();
|
||||||
|
|
||||||
assertThat(ch.finish(), is(false));
|
assertThat(ch.finish(), is(false));
|
||||||
|
Loading…
Reference in New Issue
Block a user