Search in sources :

Example 1 with HttpMessage

use of org.jboss.netty.handler.codec.http.HttpMessage 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 2 with HttpMessage

use of org.jboss.netty.handler.codec.http.HttpMessage in project bagheera by mozilla-metrics.

the class RootResponse method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg instanceof HttpMessage) {
        HttpRequest request = (HttpRequest) msg;
        if (ROOT_PATH.equals(request.getUri()) || request.getUri().isEmpty()) {
            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
            ChannelFuture future = e.getChannel().write(response);
            future.addListener(ChannelFutureListener.CLOSE);
        } else {
            Channels.fireMessageReceived(ctx, request, e.getRemoteAddress());
        }
    } else {
        ctx.sendUpstream(e);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpMessage(org.jboss.netty.handler.codec.http.HttpMessage)

Example 3 with HttpMessage

use of org.jboss.netty.handler.codec.http.HttpMessage in project bagheera by mozilla-metrics.

the class ContentEncodingCorrector method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg instanceof HttpMessage) {
        HttpMessage m = (HttpMessage) msg;
        String contentEncoding = m.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
        if (contentEncoding == null) {
            String contentType = m.getHeader(HttpHeaders.Names.CONTENT_TYPE);
            if (contentType != null && contentType.startsWith(MIME_TYPE_JSON_ZLIB)) {
                m.setHeader(HttpHeaders.Names.CONTENT_ENCODING, "deflate");
            }
        }
        Channels.fireMessageReceived(ctx, m, e.getRemoteAddress());
    } else {
        ctx.sendUpstream(e);
    }
}
Also used : HttpMessage(org.jboss.netty.handler.codec.http.HttpMessage)

Example 4 with HttpMessage

use of org.jboss.netty.handler.codec.http.HttpMessage 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)

Aggregations

HttpMessage (org.jboss.netty.handler.codec.http.HttpMessage)4 TooLongFrameException (org.jboss.netty.handler.codec.frame.TooLongFrameException)2 Entry (java.util.Map.Entry)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)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 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1