* Discard too long frame in a more robust manner (i.e. should continue decoding after raising TooLongFrameException)

This commit is contained in:
Trustin Lee 2010-06-24 01:53:51 +00:00
parent bae46eca49
commit ab0facdee5
2 changed files with 23 additions and 7 deletions

View File

@ -214,13 +214,13 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
Channels.fireExceptionCaught(
ctx.getChannel(),
new TooLongFrameException(
"The frame length exceeds " + maxFrameLength +
"frame length exceeds " + maxFrameLength +
": " + frameLength + " - discarded"));
} else {
Channels.fireExceptionCaught(
ctx.getChannel(),
new TooLongFrameException(
"The frame length exceeds " + maxFrameLength +
"frame length exceeds " + maxFrameLength +
" - discarding"));
}
}

View File

@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
/**
@ -292,16 +293,15 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
if (bytesToDiscard == 0) {
// Reset to the initial state and tell the handlers that
// the frame was too large.
discardingTooLongFrame = false;
// TODO Let user choose when the exception should be raised - early or late?
// If early, fail() should be called when discardingTooLongFrame is set to true.
long tooLongFrameLength = this.tooLongFrameLength;
this.tooLongFrameLength = 0;
throw new TooLongFrameException(
"Adjusted frame length exceeds " + maxFrameLength +
": " + tooLongFrameLength);
fail(ctx, tooLongFrameLength);
} else {
// Keep discarding.
return null;
}
return null;
}
if (buffer.readableBytes() < lengthFieldEndOffset) {
@ -395,4 +395,20 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
frame.writeBytes(buffer, index, length);
return frame;
}
private void fail(ChannelHandlerContext ctx, long frameLength) {
if (frameLength > 0) {
Channels.fireExceptionCaught(
ctx.getChannel(),
new TooLongFrameException(
"Adjusted frame length exceeds " + maxFrameLength +
": " + frameLength + " - discarded"));
} else {
Channels.fireExceptionCaught(
ctx.getChannel(),
new TooLongFrameException(
"Adjusted frame length exceeds " + maxFrameLength +
" - discarding"));
}
}
}