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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations