Repository: netty/netty
Issue: Netty force encodes already encoded responses
Labels: defect

### Expected behavior

Brotli content passes through with Content-Encoding being untouched

### Actual behavior

Content-Encoding changed to gzip and already encoded content (in brotli) encoded again with gzip

### Steps to reproduce

1) Encode response with Brotli
2) Set Content-Encoding to "br"

### Netty version

4.1.11


Issue comments:
Can you provide a reproducer / testcase?

> Am 26.05.2017 um 18:18 schrieb NSProgrammer <notifications@github.com>:
> 
> Expected behavior
> 
> Brotli content passes through with Content-Encoding being untouched
> 
> Actual behavior
> 
> Content-Encoding changed to gzip and already encoded content (in brotli) encoded again with gzip
> 
> Steps to reproduce
> 
> Encode response with Brotli
> Set Content-Encoding to "br"
> Netty version
> 
> 4.1.11
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub, or mute the thread.
> 

Here's a unit test

https://github.com/netty/netty/pull/6785/commits/efebcc327ccb61876fb7e70c1ee7cf3ce65a6525

Pull request review comments:

Patch that resolved the issue:
diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
index 11c61293338..fcf22e011ad 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java
@@ -101,7 +101,9 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
     @Override
     protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception {
         String contentEncoding = headers.headers().get(HttpHeaderNames.CONTENT_ENCODING);
-        if (HttpHeaderValues.IDENTITY.contentEqualsIgnoreCase(contentEncoding)) {
+        if (contentEncoding != null) {
+            // Content-Encoding was set, either as something specific or as the IDENTITY encoding
+            // Therefore, we should NOT encode here
             return null;
         }
 


Test patch:
diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java
index 4946328eba2..94b1aa6cd68 100644
--- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java
+++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java
@@ -395,6 +395,28 @@ public void testIdentity() throws Exception {
         assertTrue(ch.finishAndReleaseAll());
     }
 
+    @Test
+    public void testCustomEncoding() throws Exception {
+        EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
+        assertTrue(ch.writeInbound(newRequest()));
+
+        FullHttpResponse res = new DefaultFullHttpResponse(
+                HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
+                Unpooled.copiedBuffer("Hello, World", CharsetUtil.US_ASCII));
+        int len = res.content().readableBytes();
+        res.headers().set(HttpHeaderNames.CONTENT_LENGTH, len);
+        res.headers().set(HttpHeaderNames.CONTENT_ENCODING, "ascii");
+        assertTrue(ch.writeOutbound(res));
+
+        FullHttpResponse response = (FullHttpResponse) ch.readOutbound();
+        assertEquals(String.valueOf(len), response.headers().get(HttpHeaderNames.CONTENT_LENGTH));
+        assertEquals("ascii", response.headers().get(HttpHeaderNames.CONTENT_ENCODING));
+        assertEquals("Hello, World", response.content().toString(CharsetUtil.US_ASCII));
+        response.release();
+
+        assertTrue(ch.finishAndReleaseAll());
+    }
+
     private static FullHttpRequest newRequest() {
         FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
         req.headers().set(HttpHeaderNames.ACCEPT_ENCODING, "gzip");