use of org.jboss.netty.handler.codec.frame.TooLongFrameException in project opentsdb by OpenTSDB.
the class LineBasedFrameDecoder method decode.
@Override
protected Object decode(final ChannelHandlerContext ctx, final Channel channel, final ChannelBuffer buffer) throws Exception {
final int eol = findEndOfLine(buffer);
if (eol != -1) {
final ChannelBuffer frame;
final int length = eol - buffer.readerIndex();
assert length >= 0 : "WTF? length=" + length;
if (discarding) {
frame = null;
buffer.skipBytes(length);
} else {
frame = buffer.readBytes(length);
}
final byte delim = buffer.readByte();
if (delim == '\r') {
// Skip the \n.
buffer.skipBytes(1);
}
return frame;
}
final int buffered = buffer.readableBytes();
if (!discarding && buffered > max_length) {
discarding = true;
Channels.fireExceptionCaught(ctx.getChannel(), new TooLongFrameException("Frame length exceeds " + max_length + " (" + buffered + " bytes buffered already)"));
}
if (discarding) {
buffer.skipBytes(buffer.readableBytes());
}
return null;
}
use of org.jboss.netty.handler.codec.frame.TooLongFrameException in project databus by linkedin.
the class FooterAwareHttpChunkAggregator method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object msg = e.getMessage();
HttpMessage currentMessage = this.currentMessage;
if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
if (m.isChunked()) {
// A chunked message - remove 'Transfer-Encoding' header,
// initialize the cumulative buffer, and wait for incoming chunks.
List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
encodings.remove(HttpHeaders.Values.CHUNKED);
if (encodings.isEmpty()) {
m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
}
m.setContent(ChannelBuffers.dynamicBuffer(e.getChannel().getConfig().getBufferFactory()));
this.currentMessage = m;
} else {
// Not a chunked message - pass through.
this.currentMessage = null;
ctx.sendUpstream(e);
}
} else if (msg instanceof HttpChunk) {
// Sanity check
if (currentMessage == null) {
throw new IllegalStateException("received " + HttpChunk.class.getSimpleName() + " without " + HttpMessage.class.getSimpleName());
}
// Merge the received chunk into the content of the current message.
HttpChunk chunk = (HttpChunk) msg;
ChannelBuffer content = currentMessage.getContent();
if (content.readableBytes() > maxContentLength - chunk.getContent().readableBytes()) {
throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes.");
}
content.writeBytes(chunk.getContent());
if (chunk.isLast()) {
this.currentMessage = null;
currentMessage.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(content.readableBytes()));
if (chunk instanceof HttpChunkTrailer) {
HttpChunkTrailer chunkTrailer = (HttpChunkTrailer) chunk;
for (Entry<String, String> footer : chunkTrailer.getHeaders()) {
currentMessage.setHeader(footer.getKey(), footer.getValue());
}
}
Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
}
} else {
// Neither HttpMessage or HttpChunk
ctx.sendUpstream(e);
}
}
use of org.jboss.netty.handler.codec.frame.TooLongFrameException in project bagheera by mozilla-metrics.
the class ContentLengthFilter method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object msg = e.getMessage();
if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
if (m.getContent().readableBytes() > maxContentLength) {
throw new TooLongFrameException("HTTP content length exceeded: " + maxContentLength + " bytes.");
}
Channels.fireMessageReceived(ctx, m, e.getRemoteAddress());
} else {
ctx.sendUpstream(e);
}
}
use of org.jboss.netty.handler.codec.frame.TooLongFrameException in project bagheera by mozilla-metrics.
the class SubmissionHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
Throwable cause = e.getCause();
HttpResponse response = null;
if (cause instanceof ClosedChannelException) {
// NOOP
} else if (cause instanceof TooLongFrameException) {
response = new DefaultHttpResponse(HTTP_1_1, REQUEST_ENTITY_TOO_LARGE);
} else if (cause instanceof InvalidPathException) {
response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
} else if (cause instanceof HttpSecurityException) {
LOG.error(cause.getMessage());
response = new DefaultHttpResponse(HTTP_1_1, FORBIDDEN);
} else {
LOG.error(cause.getMessage());
response = new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR);
}
if (response != null) {
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
updateResponseMetrics(null, response.getStatus().getCode());
}
}
use of org.jboss.netty.handler.codec.frame.TooLongFrameException in project UniversalMediaServer by UniversalMediaServer.
the class RequestHandlerV2 method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
Channel ch = e.getChannel();
Throwable cause = e.getCause();
if (cause instanceof TooLongFrameException) {
sendError(ctx, HttpResponseStatus.BAD_REQUEST);
return;
}
if (cause != null) {
if (cause.getClass().equals(IOException.class)) {
LOGGER.debug("Connection error: " + cause);
StartStopListenerDelegate startStopListenerDelegate = (StartStopListenerDelegate) ctx.getAttachment();
if (startStopListenerDelegate != null) {
LOGGER.debug("Premature end, stopping...");
startStopListenerDelegate.stop();
}
} else if (!cause.getClass().equals(ClosedChannelException.class)) {
LOGGER.debug("Caught exception: {}", cause.getMessage());
LOGGER.trace("", cause);
}
}
if (ch.isConnected()) {
sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
ch.close();
}
Aggregations