Merge pull request #172 from jpinner/http_accept_encoding_3_2
Fix #164: HttpContentCompressor accepts encodings whose qvalue is 0
This commit is contained in:
commit
a7d419a481
@ -120,12 +120,42 @@ public class HttpContentCompressor extends HttpContentEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ZlibWrapper determineWrapper(String acceptEncoding) {
|
private ZlibWrapper determineWrapper(String acceptEncoding) {
|
||||||
// FIXME: Use the Q value.
|
float starQ = -1.0f;
|
||||||
if (acceptEncoding.indexOf("gzip") >= 0) {
|
float gzipQ = -1.0f;
|
||||||
return ZlibWrapper.GZIP;
|
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) {
|
if (gzipQ > 0.0f || deflateQ > 0.0f) {
|
||||||
return ZlibWrapper.ZLIB;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.jboss.netty.handler.codec.http;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HttpContentCompressorTest {
|
||||||
|
|
||||||
|
private HttpContentCompressor httpContentCompressor =
|
||||||
|
new HttpContentCompressor();
|
||||||
|
|
||||||
|
@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];
|
||||||
|
String targetEncoding = compressor.getTargetContentEncoding(acceptEncoding);
|
||||||
|
Assert.assertEquals(contentEncoding, targetEncoding);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user