Make it configurable if the HttpClientCodec should throw an exception on close when the response and request count does not match. Default is false. See #266
This commit is contained in:
parent
d808cd0475
commit
54559a9595
@ -57,14 +57,16 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
|
||||
private final HttpRequestEncoder encoder = new Encoder();
|
||||
private final HttpResponseDecoder decoder;
|
||||
private final AtomicLong requestResponseCounter = new AtomicLong(0);
|
||||
private final boolean failOnMissingResponse;
|
||||
|
||||
/**
|
||||
* Creates a new instance with the default decoder options
|
||||
* ({@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
|
||||
* {@code maxChunkSize (8192)}).
|
||||
*
|
||||
*/
|
||||
public HttpClientCodec() {
|
||||
this(4096, 8192, 8192);
|
||||
this(4096, 8192, 8192, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,7 +74,16 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
|
||||
*/
|
||||
public HttpClientCodec(
|
||||
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
|
||||
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified decoder options.
|
||||
*/
|
||||
public HttpClientCodec(
|
||||
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse) {
|
||||
decoder = new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize);
|
||||
this.failOnMissingResponse = failOnMissingResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,12 +112,14 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
|
||||
|
||||
Object obj = super.encode(ctx, channel, msg);
|
||||
|
||||
// check if the request is chunked if so do not increment
|
||||
if (msg instanceof HttpRequest && !((HttpRequest) msg).isChunked()) {
|
||||
requestResponseCounter.incrementAndGet();
|
||||
} else if (msg instanceof HttpChunk && ((HttpChunk) msg).isLast()) {
|
||||
// increment as its the last chunk
|
||||
requestResponseCounter.incrementAndGet();
|
||||
if (failOnMissingResponse) {
|
||||
// check if the request is chunked if so do not increment
|
||||
if (msg instanceof HttpRequest && !((HttpRequest) msg).isChunked()) {
|
||||
requestResponseCounter.incrementAndGet();
|
||||
} else if (msg instanceof HttpChunk && ((HttpChunk) msg).isLast()) {
|
||||
// increment as its the last chunk
|
||||
requestResponseCounter.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
@ -127,7 +140,9 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
|
||||
return buffer.readBytes(actualReadableBytes());
|
||||
} else {
|
||||
Object msg = super.decode(ctx, channel, buffer, state);
|
||||
decrement(msg);
|
||||
if (failOnMissingResponse) {
|
||||
decrement(msg);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@ -204,9 +219,11 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
|
||||
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
||||
super.channelClosed(ctx, e);
|
||||
|
||||
long missingResponses = requestResponseCounter.get();
|
||||
if (missingResponses > 0) {
|
||||
throw new PrematureChannelClosureException("Channel closed but still missing " + missingResponses + " response(s)");
|
||||
if (failOnMissingResponse) {
|
||||
long missingResponses = requestResponseCounter.get();
|
||||
if (missingResponses > 0) {
|
||||
throw new PrematureChannelClosureException("Channel closed but still missing " + missingResponses + " response(s)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ public class HttpClientCodecTest {
|
||||
|
||||
@Test
|
||||
public void testFailsNotOnRequestResponse() {
|
||||
HttpClientCodec codec = new HttpClientCodec();
|
||||
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
|
||||
DecoderEmbedder<ChannelBuffer> decoder = new DecoderEmbedder<ChannelBuffer>(codec);
|
||||
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
|
||||
|
||||
@ -52,7 +52,7 @@ public class HttpClientCodecTest {
|
||||
|
||||
@Test
|
||||
public void testFailsNotOnRequestResponseChunked() {
|
||||
HttpClientCodec codec = new HttpClientCodec();
|
||||
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
|
||||
DecoderEmbedder<ChannelBuffer> decoder = new DecoderEmbedder<ChannelBuffer>(codec);
|
||||
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
|
||||
|
||||
@ -66,7 +66,7 @@ public class HttpClientCodecTest {
|
||||
|
||||
@Test
|
||||
public void testFailsOnMissingResponse() {
|
||||
HttpClientCodec codec = new HttpClientCodec();
|
||||
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
|
||||
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
|
||||
|
||||
encoder.offer(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
|
||||
@ -82,7 +82,7 @@ public class HttpClientCodecTest {
|
||||
|
||||
@Test
|
||||
public void testFailsOnIncompleteChunkedResponse() {
|
||||
HttpClientCodec codec = new HttpClientCodec();
|
||||
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
|
||||
DecoderEmbedder<ChannelBuffer> decoder = new DecoderEmbedder<ChannelBuffer>(codec);
|
||||
|
||||
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
|
||||
|
Loading…
x
Reference in New Issue
Block a user