* Code clean-up
* More strict sanity check
This commit is contained in:
parent
48696d909f
commit
ba08d11356
@ -75,19 +75,15 @@ public class HttpChunkAggregator extends SimpleChannelUpstreamHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
Object msg = e.getMessage();
|
|
||||||
if (!(msg instanceof HttpMessage) && !(msg instanceof HttpChunk)) {
|
|
||||||
ctx.sendUpstream(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Object msg = e.getMessage();
|
||||||
HttpMessage currentMessage = this.currentMessage;
|
HttpMessage currentMessage = this.currentMessage;
|
||||||
if (currentMessage == null) {
|
|
||||||
|
if (msg instanceof HttpMessage) {
|
||||||
HttpMessage m = (HttpMessage) msg;
|
HttpMessage m = (HttpMessage) msg;
|
||||||
if (m.isChunked()) {
|
if (m.isChunked()) {
|
||||||
// A chunked message - remove 'Transfer-Encoding' header,
|
// A chunked message - remove 'Transfer-Encoding' header,
|
||||||
// initialize the cumulative buffer, and wait for incoming chunks.
|
// initialize the cumulative buffer, and wait for incoming chunks.
|
||||||
// TODO Add HttpMessage/HttpChunkTrailer.removeHeader(name, value)
|
|
||||||
List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
|
List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
|
||||||
encodings.remove(HttpHeaders.Values.CHUNKED);
|
encodings.remove(HttpHeaders.Values.CHUNKED);
|
||||||
if (encodings.isEmpty()) {
|
if (encodings.isEmpty()) {
|
||||||
@ -97,9 +93,17 @@ public class HttpChunkAggregator extends SimpleChannelUpstreamHandler {
|
|||||||
this.currentMessage = m;
|
this.currentMessage = m;
|
||||||
} else {
|
} else {
|
||||||
// Not a chunked message - pass through.
|
// Not a chunked message - pass through.
|
||||||
|
this.currentMessage = null;
|
||||||
ctx.sendUpstream(e);
|
ctx.sendUpstream(e);
|
||||||
}
|
}
|
||||||
} else {
|
} 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.
|
// Merge the received chunk into the content of the current message.
|
||||||
HttpChunk chunk = (HttpChunk) msg;
|
HttpChunk chunk = (HttpChunk) msg;
|
||||||
ChannelBuffer content = currentMessage.getContent();
|
ChannelBuffer content = currentMessage.getContent();
|
||||||
@ -118,6 +122,9 @@ public class HttpChunkAggregator extends SimpleChannelUpstreamHandler {
|
|||||||
String.valueOf(content.readableBytes()));
|
String.valueOf(content.readableBytes()));
|
||||||
Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
|
Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Neither HttpMessage or HttpChunk
|
||||||
|
ctx.sendUpstream(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user