Split HttpDecompressionHandler into HttpContentDecoder and HttpContentDecompressor
This commit is contained in:
parent
f0e766dcb3
commit
b105461383
|
@ -19,7 +19,7 @@ import static org.jboss.netty.channel.Channels.*;
|
|||
|
||||
import org.jboss.netty.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.ChannelPipelineFactory;
|
||||
import org.jboss.netty.handler.codec.http.HttpDecompressionHandler;
|
||||
import org.jboss.netty.handler.codec.http.HttpContentDecompressor;
|
||||
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
|
||||
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class HttpClientPipelineFactory implements ChannelPipelineFactory {
|
|||
ChannelPipeline pipeline = pipeline();
|
||||
pipeline.addLast("decoder", new HttpResponseDecoder());
|
||||
// Remove the following line if you don't want automatic content decompression.
|
||||
pipeline.addLast("inflater", new HttpDecompressionHandler());
|
||||
pipeline.addLast("inflater", new HttpContentDecompressor());
|
||||
// Uncomment the following line if you don't want to handle HttpChunks.
|
||||
//pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
|
||||
pipeline.addLast("encoder", new HttpRequestEncoder());
|
||||
|
|
|
@ -0,0 +1,199 @@
|
|||
/*
|
||||
* 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},
|
||||
* which is created by {@link #beginDecode(String)}. Once decoding is finished,
|
||||
* 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
|
||||
* by the decoder, {@link #beginDecode(String)} returns {@code null} and no
|
||||
* decoding occurs (i.e. pass-through).
|
||||
* <p>
|
||||
* Please note that this is an abstract class. You have to extend this class
|
||||
* and implement {@link #beginDecode(String)} properly to make this class
|
||||
* functional. For example, refer to the source code of {@link HttpContentDecompressor}.
|
||||
*
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Trustin Lee (tlee@redhat.com)
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
@ChannelPipelineCoverage("one")
|
||||
public abstract class HttpContentDecoder extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private volatile HttpMessage previous;
|
||||
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;
|
||||
if (m.isChunked()) {
|
||||
previous = m;
|
||||
} else {
|
||||
previous = null;
|
||||
}
|
||||
|
||||
// Determine the content encoding.
|
||||
String contentEncoding = m.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
|
||||
if (contentEncoding != null) {
|
||||
contentEncoding = contentEncoding.trim();
|
||||
}
|
||||
|
||||
if (contentEncoding != null && (decoder = beginDecode(contentEncoding)) != null) {
|
||||
// Decode the content and remove or replace the existing headers
|
||||
// so that the message looks like a decoded message.
|
||||
m.setHeader(HttpHeaders.Names.CONTENT_ENCODING, HttpHeaders.Values.IDENTITY);
|
||||
|
||||
if (!m.isChunked()) {
|
||||
ChannelBuffer content = m.getContent();
|
||||
if (content.readable()) {
|
||||
content = decode(content);
|
||||
|
||||
// Finish decoding.
|
||||
ChannelBuffer lastProduct = finishDecode();
|
||||
|
||||
// Merge the last product into the content.
|
||||
if (content == null) {
|
||||
if (lastProduct != null) {
|
||||
content = lastProduct;
|
||||
}
|
||||
} else {
|
||||
if (lastProduct != null) {
|
||||
content = ChannelBuffers.wrappedBuffer(content, lastProduct);
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the content if necessary.
|
||||
if (content != null) {
|
||||
m.setContent(content);
|
||||
if (m.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
|
||||
m.setHeader(
|
||||
HttpHeaders.Names.CONTENT_LENGTH,
|
||||
Integer.toString(content.readableBytes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Because HttpMessage is a mutable object, we can simply forward the received event.
|
||||
ctx.sendUpstream(e);
|
||||
} else if (msg instanceof HttpChunk) {
|
||||
assert previous != null;
|
||||
|
||||
HttpChunk c = (HttpChunk) msg;
|
||||
ChannelBuffer content = c.getContent();
|
||||
|
||||
// Decode the chunk if necessary.
|
||||
if (decoder != null) {
|
||||
if (!c.isLast()) {
|
||||
content = decode(content);
|
||||
if (content != null) {
|
||||
// Note that HttpChunk is immutable unlike HttpMessage.
|
||||
// XXX API inconsistency? I can live with it though.
|
||||
Channels.fireMessageReceived(ctx, new DefaultHttpChunk(content), e.getRemoteAddress());
|
||||
}
|
||||
} else {
|
||||
ChannelBuffer lastProduct = finishDecode();
|
||||
previous = null;
|
||||
decoder = null;
|
||||
|
||||
// Generate an additional chunk if the decoder produced
|
||||
// the last product on closure,
|
||||
if (lastProduct != null) {
|
||||
Channels.fireMessageReceived(
|
||||
ctx,
|
||||
new DefaultHttpChunk(lastProduct),
|
||||
e.getRemoteAddress());
|
||||
}
|
||||
|
||||
// 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).
|
||||
*/
|
||||
protected abstract DecoderEmbedder<ChannelBuffer> beginDecode(String contentEncoding) throws Exception;
|
||||
|
||||
private ChannelBuffer decode(ChannelBuffer buf) {
|
||||
decoder.offer(buf);
|
||||
return pollDecodeResult();
|
||||
}
|
||||
|
||||
private ChannelBuffer finishDecode() {
|
||||
if (decoder.finish()) {
|
||||
return pollDecodeResult();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ChannelBuffer pollDecodeResult() {
|
||||
ChannelBuffer result = decoder.poll();
|
||||
if (result != null) {
|
||||
for (;;) {
|
||||
ChannelBuffer moreResult = decoder.poll();
|
||||
if (moreResult == null) {
|
||||
break;
|
||||
}
|
||||
result = ChannelBuffers.wrappedBuffer(result, moreResult);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.readable()) {
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
||||
import org.jboss.netty.handler.codec.compression.ZlibDecoder;
|
||||
import org.jboss.netty.handler.codec.compression.ZlibWrapper;
|
||||
import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
|
||||
|
||||
/**
|
||||
* Decompresses an {@link HttpMessage} and an {@link HttpChunk} compressed in
|
||||
* {@code gzip} or {@code deflate} encoding. Insert this handler after
|
||||
* {@link HttpMessageDecoder} in the {@link ChannelPipeline}:
|
||||
* <pre>
|
||||
* ChannelPipeline p = ...;
|
||||
* ...
|
||||
* p.addLast("decoder", new HttpRequestDecoder());
|
||||
* p.addLast("inflater", <b>new HttpContentDecomperssor()</b>);
|
||||
* ...
|
||||
* p.addLast("encoder", new HttpResponseEncoder());
|
||||
* p.addLast("handler", new HttpRequestHandler());
|
||||
* </pre>
|
||||
*
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Trustin Lee (tlee@redhat.com)
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
@ChannelPipelineCoverage("one")
|
||||
public class HttpContentDecompressor extends HttpContentDecoder {
|
||||
@Override
|
||||
protected DecoderEmbedder<ChannelBuffer> beginDecode(String contentEncoding) throws Exception {
|
||||
if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) {
|
||||
return new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.GZIP));
|
||||
} else if ("deflate".equalsIgnoreCase(contentEncoding) || "x-deflate".equalsIgnoreCase(contentEncoding)) {
|
||||
return new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.ZLIB));
|
||||
}
|
||||
|
||||
// 'identity' or unsupported
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,154 +0,0 @@
|
|||
/*
|
||||
* 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.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.compression.ZlibDecoder;
|
||||
import org.jboss.netty.handler.codec.compression.ZlibWrapper;
|
||||
import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
|
||||
|
||||
/**
|
||||
* Decompresses an {@link HttpMessage} and an {@link HttpChunk} compressed in
|
||||
* {@code gzip}, {@code deflate}, and {@code compress} encoding. During the
|
||||
* decompression, the {@code "Content-Encoding"} header is removed and the
|
||||
* value of the {@code "Content-Length"} header is updated with the length
|
||||
* of the decompressed content. If the received message is not compressed,
|
||||
* no modification is made. To use this handler, place it after
|
||||
* {@link HttpMessageDecoder} in the pipeline.
|
||||
*
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Trustin Lee (tlee@redhat.com)
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
@ChannelPipelineCoverage("one")
|
||||
public class HttpDecompressionHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private volatile HttpMessage previous;
|
||||
private volatile int previousEncoding; // 0 - no compression, 1 - gzip, 2 - deflate, 3 - lzw
|
||||
private volatile DecoderEmbedder<ChannelBuffer> inflater;
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||
Object msg = e.getMessage();
|
||||
if (msg instanceof HttpMessage) {
|
||||
HttpMessage m = (HttpMessage) msg;
|
||||
|
||||
// Determine the content encoding.
|
||||
String contentEncoding = m.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
|
||||
if (contentEncoding != null) {
|
||||
contentEncoding = contentEncoding.trim();
|
||||
}
|
||||
|
||||
int encoding;
|
||||
if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) {
|
||||
encoding = 1;
|
||||
} else if ("deflate".equalsIgnoreCase(contentEncoding) || "x-deflate".equalsIgnoreCase(contentEncoding)) {
|
||||
encoding = 2;
|
||||
} else {
|
||||
// FIXME Implement 'compress' encoding (lzw)
|
||||
encoding = 0;
|
||||
}
|
||||
|
||||
if (m.isChunked()) {
|
||||
previous = m;
|
||||
previousEncoding = encoding;
|
||||
} else {
|
||||
previous = null;
|
||||
previousEncoding = 0;
|
||||
}
|
||||
|
||||
// Decompress the content and remove or replace the existing headers
|
||||
// so that the message looks like an uncompressed message.
|
||||
if (encoding != 0) {
|
||||
if (contentEncoding != null) {
|
||||
m.setHeader(HttpHeaders.Names.CONTENT_ENCODING, HttpHeaders.Values.IDENTITY);
|
||||
}
|
||||
ChannelBuffer content = m.getContent();
|
||||
if (content.readable()) {
|
||||
beginDecompression(encoding);
|
||||
content = decompress(content);
|
||||
m.setContent(content);
|
||||
if (m.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
|
||||
m.setHeader(
|
||||
HttpHeaders.Names.CONTENT_LENGTH,
|
||||
Integer.toString(content.readableBytes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Because HttpMessage is a mutable object, we can simply forward the received event.
|
||||
ctx.sendUpstream(e);
|
||||
} else if (msg instanceof HttpChunk) {
|
||||
assert previous != null;
|
||||
|
||||
HttpChunk c = (HttpChunk) msg;
|
||||
ChannelBuffer content = c.getContent();
|
||||
|
||||
// Decompress the chunk if necessary.
|
||||
if (previousEncoding != 0) {
|
||||
if (!c.isLast()) {
|
||||
content = decompress(content);
|
||||
// HttpChunk is immutable unlike HttpMessage.
|
||||
// XXX API inconsistency? I can live with it though.
|
||||
Channels.fireMessageReceived(ctx, new DefaultHttpChunk(content), e.getRemoteAddress());
|
||||
} else {
|
||||
finishDecompression();
|
||||
previous = null;
|
||||
previousEncoding = 0;
|
||||
ctx.sendUpstream(e);
|
||||
}
|
||||
} else {
|
||||
// No need to
|
||||
ctx.sendUpstream(e);
|
||||
}
|
||||
} else {
|
||||
ctx.sendUpstream(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void beginDecompression(int encoding) {
|
||||
switch (encoding) {
|
||||
case 1:
|
||||
inflater = new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.GZIP));
|
||||
break;
|
||||
case 2:
|
||||
inflater = new DecoderEmbedder<ChannelBuffer>(new ZlibDecoder(ZlibWrapper.ZLIB));
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
private ChannelBuffer decompress(ChannelBuffer buf) {
|
||||
inflater.offer(buf);
|
||||
// FIXME Some inflater might produce either an empty buffer or many buffers.
|
||||
// Empty buffer should not generate an event and many buffers should generate many events.
|
||||
return inflater.poll();
|
||||
}
|
||||
|
||||
private void finishDecompression() {
|
||||
if (inflater.finish()) {
|
||||
throw new IllegalStateException("trailing data produced by inflater");
|
||||
}
|
||||
// TODO Make sure ZlibDecoder.isClosed() is true.
|
||||
// The compressed stream ended prematurely if false.
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user