parent
cd69bd4e2c
commit
1f6e96ed15
@ -123,13 +123,43 @@ public class HttpContentCompressor extends HttpContentEncoder {
|
||||
new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)));
|
||||
}
|
||||
|
||||
private ZlibWrapper determineWrapper(String acceptEncoding) {
|
||||
// FIXME: Use the Q value.
|
||||
if (acceptEncoding.indexOf("gzip") >= 0) {
|
||||
return ZlibWrapper.GZIP;
|
||||
protected ZlibWrapper determineWrapper(String acceptEncoding) {
|
||||
float starQ = -1.0f;
|
||||
float gzipQ = -1.0f;
|
||||
float deflateQ = -1.0f;
|
||||
for (String encoding : acceptEncoding.split(",")) {
|
||||
float q = 1.0f;
|
||||
int equalsPos = encoding.indexOf('=');
|
||||
if (equalsPos != -1) {
|
||||
try {
|
||||
q = Float.valueOf(encoding.substring(equalsPos + 1));
|
||||
} catch (NumberFormatException e) {
|
||||
// Ignore encoding
|
||||
q = 0.0f;
|
||||
}
|
||||
}
|
||||
if (encoding.indexOf("*") >= 0) {
|
||||
starQ = q;
|
||||
} else if (encoding.indexOf("gzip") >= 0 && q > gzipQ) {
|
||||
gzipQ = q;
|
||||
} else if (encoding.indexOf("deflate") >= 0 && q > deflateQ) {
|
||||
deflateQ = q;
|
||||
}
|
||||
}
|
||||
if (acceptEncoding.indexOf("deflate") >= 0) {
|
||||
return ZlibWrapper.ZLIB;
|
||||
if (gzipQ > 0.0f || deflateQ > 0.0f) {
|
||||
if (gzipQ >= deflateQ) {
|
||||
return ZlibWrapper.GZIP;
|
||||
} else {
|
||||
return ZlibWrapper.ZLIB;
|
||||
}
|
||||
}
|
||||
if (starQ > 0.0f) {
|
||||
if (gzipQ == -1.0f) {
|
||||
return ZlibWrapper.GZIP;
|
||||
}
|
||||
if (deflateQ == -1.0f) {
|
||||
return ZlibWrapper.ZLIB;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
* 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 io.netty.handler.codec.http;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.netty.handler.codec.compression.ZlibWrapper;
|
||||
|
||||
public class HttpContentCompressorTest {
|
||||
@Test
|
||||
public void testGetTargetContentEncoding() throws Exception {
|
||||
HttpContentCompressor compressor = new HttpContentCompressor();
|
||||
|
||||
String[] tests = {
|
||||
// Accept-Encoding -> Content-Encoding
|
||||
"", null,
|
||||
"*", "gzip",
|
||||
"*;q=0.0", null,
|
||||
"gzip", "gzip",
|
||||
"compress, gzip;q=0.5", "gzip",
|
||||
"gzip; q=0.5, identity", "gzip",
|
||||
"gzip ; q=0.1", "gzip",
|
||||
"gzip; q=0, deflate", "deflate",
|
||||
" defalte ; q=0 , *;q=0.5", "gzip",
|
||||
};
|
||||
for (int i = 0; i < tests.length; i += 2) {
|
||||
String acceptEncoding = tests[i];
|
||||
String contentEncoding = tests[i + 1];
|
||||
ZlibWrapper targetWrapper = compressor.determineWrapper(acceptEncoding);
|
||||
String targetEncoding = null;
|
||||
if (targetWrapper != null) {
|
||||
switch (targetWrapper) {
|
||||
case GZIP:
|
||||
targetEncoding = "gzip";
|
||||
break;
|
||||
case ZLIB:
|
||||
targetEncoding = "deflate";
|
||||
break;
|
||||
default:
|
||||
if (targetWrapper != null) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(contentEncoding, targetEncoding);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user