2009-11-03 07:47:30 +01:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 The Netty Project
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project 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:
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-11-03 07:47:30 +01:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.buffer.ChannelBuffer;
|
|
|
|
import io.netty.handler.codec.compression.ZlibEncoder;
|
|
|
|
import io.netty.handler.codec.compression.ZlibWrapper;
|
|
|
|
import io.netty.handler.codec.embedder.EncoderEmbedder;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
|
|
|
/**
|
2009-11-03 08:11:52 +01:00
|
|
|
* Compresses an {@link HttpMessage} and an {@link HttpChunk} in {@code gzip} or
|
|
|
|
* {@code deflate} encoding while respecting the {@code "Accept-Encoding"} header.
|
|
|
|
* If there is no matching encoding, no compression is done. For more
|
|
|
|
* information on how this handler modifies the message, please refer to
|
|
|
|
* {@link HttpContentEncoder}.
|
2009-11-03 07:47:30 +01:00
|
|
|
*/
|
|
|
|
public class HttpContentCompressor extends HttpContentEncoder {
|
|
|
|
|
|
|
|
private final int compressionLevel;
|
|
|
|
|
2009-11-03 08:11:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a new handler with the default compression level (<tt>6</tt>).
|
|
|
|
*/
|
2009-11-03 07:47:30 +01:00
|
|
|
public HttpContentCompressor() {
|
|
|
|
this(6);
|
|
|
|
}
|
|
|
|
|
2009-11-03 08:11:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a new handler with the specified compression level.
|
|
|
|
*
|
|
|
|
* @param compressionLevel
|
|
|
|
* {@code 1} yields the fastest compression and {@code 9} yields the
|
|
|
|
* best compression. {@code 0} means no compression. The default
|
|
|
|
* compression level is {@code 6}.
|
|
|
|
*/
|
2009-11-03 07:47:30 +01:00
|
|
|
public HttpContentCompressor(int compressionLevel) {
|
|
|
|
if (compressionLevel < 0 || compressionLevel > 9) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"compressionLevel: " + compressionLevel +
|
|
|
|
" (expected: 0-9)");
|
|
|
|
}
|
|
|
|
this.compressionLevel = compressionLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-10-23 07:34:03 +02:00
|
|
|
protected Result beginEncode(HttpMessage msg, String acceptEncoding) throws Exception {
|
|
|
|
String contentEncoding = msg.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
|
|
|
|
if (contentEncoding != null &&
|
|
|
|
!HttpHeaders.Values.IDENTITY.equalsIgnoreCase(contentEncoding)) {
|
2009-11-03 07:47:30 +01:00
|
|
|
return null;
|
|
|
|
}
|
2011-10-23 07:34:03 +02:00
|
|
|
|
2009-11-03 07:47:30 +01:00
|
|
|
ZlibWrapper wrapper = determineWrapper(acceptEncoding);
|
|
|
|
if (wrapper == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-10-23 07:34:03 +02:00
|
|
|
String targetContentEncoding;
|
2009-11-03 07:47:30 +01:00
|
|
|
switch (wrapper) {
|
|
|
|
case GZIP:
|
2011-10-23 07:34:03 +02:00
|
|
|
targetContentEncoding = "gzip";
|
|
|
|
break;
|
2009-11-03 07:47:30 +01:00
|
|
|
case ZLIB:
|
2011-10-23 07:34:03 +02:00
|
|
|
targetContentEncoding = "deflate";
|
|
|
|
break;
|
2009-11-03 07:47:30 +01:00
|
|
|
default:
|
|
|
|
throw new Error();
|
|
|
|
}
|
2011-10-23 07:34:03 +02:00
|
|
|
|
|
|
|
return new Result(
|
|
|
|
targetContentEncoding,
|
|
|
|
new EncoderEmbedder<ChannelBuffer>(
|
|
|
|
new ZlibEncoder(wrapper, compressionLevel)));
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private ZlibWrapper determineWrapper(String acceptEncoding) {
|
|
|
|
// FIXME: Use the Q value.
|
|
|
|
if (acceptEncoding.indexOf("gzip") >= 0) {
|
|
|
|
return ZlibWrapper.GZIP;
|
|
|
|
}
|
|
|
|
if (acceptEncoding.indexOf("deflate") >= 0) {
|
|
|
|
return ZlibWrapper.ZLIB;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|