// store response, as this channel handler is created per pipeline
Object msg = messageEvent.getMessage();
// it may be a chunked message
if (msg instanceof HttpChunk) {
HttpChunk chunk = (HttpChunk) msg;
if (LOG.isTraceEnabled()) {
LOG.trace("HttpChunk received: {} isLast: {}", chunk, chunk.isLast());
}
if (msg instanceof HttpChunkTrailer) {
// chunk trailer only has headers
HttpChunkTrailer trailer = (HttpChunkTrailer) msg;
for (Map.Entry<String, String> entry : trailer.trailingHeaders()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Adding trailing header {}={}", entry.getKey(), entry.getValue());
}
response.headers().add(entry.getKey(), entry.getValue());
}
} else {
// append chunked content
buffer.writeBytes(chunk.getContent());
if (LOG.isTraceEnabled()) {
LOG.trace("Wrote {} bytes to chunk buffer", buffer.writerIndex());
}
}
if (chunk.isLast()) {
// the content is a copy of the buffer with the actual data we wrote to it
int end = buffer.writerIndex();
ChannelBuffer copy = buffer.copy(0, end);
// the copy must not be readable when the content was chunked, so set the index to the end
copy.setIndex(end, end);