2009-11-02 10:21:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Red Hat licenses this file to you under the Apache License, version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package org.jboss.netty.handler.codec.http;
|
|
|
|
|
|
|
|
import org.jboss.netty.buffer.ChannelBuffer;
|
|
|
|
import org.jboss.netty.buffer.ChannelBuffers;
|
|
|
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
|
|
|
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
|
|
|
import org.jboss.netty.channel.Channels;
|
|
|
|
import org.jboss.netty.channel.MessageEvent;
|
|
|
|
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
|
|
|
import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes the content of the received {@link HttpMessage} and {@link HttpChunk}.
|
|
|
|
* The original content ({@link HttpMessage#getContent()} or {@link HttpChunk#getContent()})
|
|
|
|
* is replaced with the new content decoded by the {@link DecoderEmbedder},
|
2009-11-03 07:47:30 +01:00
|
|
|
* which is created by {@link #newContentDecoder(String)}. Once decoding is finished,
|
2009-11-02 10:21:13 +01:00
|
|
|
* the value of the <tt>'Content-Encoding'</tt> header is set to <tt>'identity'</tt>
|
|
|
|
* and the <tt>'Content-Length'</tt> header is updated to the length of the
|
|
|
|
* decoded content. If the content encoding of the original is not supported
|
2009-11-03 07:47:30 +01:00
|
|
|
* by the decoder, {@link #newContentDecoder(String)} returns {@code null} and no
|
2009-11-02 10:21:13 +01:00
|
|
|
* decoding occurs (i.e. pass-through).
|
|
|
|
* <p>
|
|
|
|
* Please note that this is an abstract class. You have to extend this class
|
2009-11-03 07:47:30 +01:00
|
|
|
* and implement {@link #newContentDecoder(String)} properly to make this class
|
2009-11-02 10:21:13 +01:00
|
|
|
* functional. For example, refer to the source code of {@link HttpContentDecompressor}.
|
|
|
|
*
|
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
2009-11-03 07:49:47 +01:00
|
|
|
* @author Trustin Lee (trustin@gmail.com)
|
2009-11-02 10:21:13 +01:00
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*/
|
|
|
|
@ChannelPipelineCoverage("one")
|
|
|
|
public abstract class HttpContentDecoder extends SimpleChannelUpstreamHandler {
|
|
|
|
|
|
|
|
private volatile DecoderEmbedder<ChannelBuffer> decoder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*/
|
|
|
|
protected HttpContentDecoder() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
|
|
|
Object msg = e.getMessage();
|
|
|
|
if (msg instanceof HttpMessage) {
|
|
|
|
HttpMessage m = (HttpMessage) msg;
|
|
|
|
|
|
|
|
decoder = null;
|
|
|
|
|
|
|
|
// Determine the content encoding.
|
|
|
|
String contentEncoding = m.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
|
|
|
|
if (contentEncoding != null) {
|
|
|
|
contentEncoding = contentEncoding.trim();
|
2009-11-03 03:59:18 +01:00
|
|
|
} else {
|
|
|
|
contentEncoding = HttpHeaders.Values.IDENTITY;
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
|
2009-11-03 07:47:30 +01:00
|
|
|
if ((decoder = newContentDecoder(contentEncoding)) != null) {
|
2009-11-02 10:21:13 +01:00
|
|
|
// Decode the content and remove or replace the existing headers
|
|
|
|
// so that the message looks like a decoded message.
|
2009-11-02 10:58:30 +01:00
|
|
|
m.setHeader(
|
|
|
|
HttpHeaders.Names.CONTENT_ENCODING,
|
2009-11-03 07:47:30 +01:00
|
|
|
getTargetContentEncoding(contentEncoding));
|
2009-11-02 10:21:13 +01:00
|
|
|
|
|
|
|
if (!m.isChunked()) {
|
|
|
|
ChannelBuffer content = m.getContent();
|
2009-11-03 07:47:30 +01:00
|
|
|
// Decode the content
|
|
|
|
content = ChannelBuffers.wrappedBuffer(
|
|
|
|
decode(content), finishDecode());
|
|
|
|
|
|
|
|
// Replace the content.
|
|
|
|
m.setContent(content);
|
|
|
|
if (m.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
|
|
|
|
m.setHeader(
|
|
|
|
HttpHeaders.Names.CONTENT_LENGTH,
|
|
|
|
Integer.toString(content.readableBytes()));
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because HttpMessage is a mutable object, we can simply forward the received event.
|
|
|
|
ctx.sendUpstream(e);
|
|
|
|
} else if (msg instanceof HttpChunk) {
|
|
|
|
HttpChunk c = (HttpChunk) msg;
|
|
|
|
ChannelBuffer content = c.getContent();
|
|
|
|
|
|
|
|
// Decode the chunk if necessary.
|
|
|
|
if (decoder != null) {
|
|
|
|
if (!c.isLast()) {
|
|
|
|
content = decode(content);
|
2009-11-03 03:59:18 +01:00
|
|
|
if (content.readable()) {
|
2009-11-02 10:21:13 +01:00
|
|
|
// Note that HttpChunk is immutable unlike HttpMessage.
|
|
|
|
// XXX API inconsistency? I can live with it though.
|
2009-11-03 03:59:18 +01:00
|
|
|
Channels.fireMessageReceived(
|
|
|
|
ctx, new DefaultHttpChunk(content), e.getRemoteAddress());
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ChannelBuffer lastProduct = finishDecode();
|
|
|
|
|
|
|
|
// Generate an additional chunk if the decoder produced
|
|
|
|
// the last product on closure,
|
2009-11-03 03:59:18 +01:00
|
|
|
if (lastProduct.readable()) {
|
2009-11-02 10:21:13 +01:00
|
|
|
Channels.fireMessageReceived(
|
2009-11-03 03:59:18 +01:00
|
|
|
ctx, new DefaultHttpChunk(lastProduct), e.getRemoteAddress());
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the last chunk.
|
|
|
|
ctx.sendUpstream(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.sendUpstream(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.sendUpstream(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new {@link DecoderEmbedder} that decodes the HTTP message
|
|
|
|
* content encoded in the specified <tt>contentEncoding</tt>.
|
|
|
|
*
|
|
|
|
* @param contentEncoding the value of the {@code "Content-Encoding"} header
|
|
|
|
* @return a new {@link DecoderEmbedder} if the specified encoding is supported.
|
|
|
|
* {@code null} otherwise (alternatively, you can throw an exception
|
|
|
|
* to block unknown encoding).
|
|
|
|
*/
|
2009-11-03 07:47:30 +01:00
|
|
|
protected abstract DecoderEmbedder<ChannelBuffer> newContentDecoder(String contentEncoding) throws Exception;
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2009-11-02 10:58:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the expected content encoding of the decoded content.
|
|
|
|
* This method returns {@code "identity"} by default, which is the case for
|
|
|
|
* most decoders.
|
|
|
|
*
|
|
|
|
* @param contentEncoding the content encoding of the original content
|
|
|
|
* @return the expected content encoding of the new content
|
|
|
|
*/
|
2009-11-03 07:47:30 +01:00
|
|
|
protected String getTargetContentEncoding(String contentEncoding) throws Exception {
|
2009-11-02 10:58:30 +01:00
|
|
|
return HttpHeaders.Values.IDENTITY;
|
|
|
|
}
|
|
|
|
|
2009-11-02 10:21:13 +01:00
|
|
|
private ChannelBuffer decode(ChannelBuffer buf) {
|
|
|
|
decoder.offer(buf);
|
2009-11-03 03:59:18 +01:00
|
|
|
return ChannelBuffers.wrappedBuffer(decoder.pollAll(new ChannelBuffer[decoder.size()]));
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private ChannelBuffer finishDecode() {
|
2009-11-03 07:47:30 +01:00
|
|
|
ChannelBuffer result;
|
2009-11-02 10:21:13 +01:00
|
|
|
if (decoder.finish()) {
|
2009-11-03 07:47:30 +01:00
|
|
|
result = ChannelBuffers.wrappedBuffer(decoder.pollAll(new ChannelBuffer[decoder.size()]));
|
2009-11-02 10:21:13 +01:00
|
|
|
} else {
|
2009-11-03 07:47:30 +01:00
|
|
|
result = ChannelBuffers.EMPTY_BUFFER;
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
decoder = null;
|
|
|
|
return result;
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
}
|