Search in sources :

Example 1 with TooLongFrameException

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;
}
Also used : TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with TooLongFrameException

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);
    }
}
Also used : Entry(java.util.Map.Entry) TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException) HttpChunkTrailer(org.jboss.netty.handler.codec.http.HttpChunkTrailer) HttpMessage(org.jboss.netty.handler.codec.http.HttpMessage) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with TooLongFrameException

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);
    }
}
Also used : TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException) HttpMessage(org.jboss.netty.handler.codec.http.HttpMessage)

Example 4 with TooLongFrameException

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());
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ClosedChannelException(java.nio.channels.ClosedChannelException) TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Example 5 with TooLongFrameException

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();
}
Also used : StartStopListenerDelegate(net.pms.external.StartStopListenerDelegate) TooLongFrameException(org.jboss.netty.handler.codec.frame.TooLongFrameException)

Aggregations

TooLongFrameException (org.jboss.netty.handler.codec.frame.TooLongFrameException)6 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 HttpMessage (org.jboss.netty.handler.codec.http.HttpMessage)2 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Entry (java.util.Map.Entry)1 StartStopListenerDelegate (net.pms.external.StartStopListenerDelegate)1 Channel (org.jboss.netty.channel.Channel)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)1 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1