From 844976a0a2a940eb28293e3f74bc133cbbde0bac Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 22 May 2016 20:02:38 +0200 Subject: [PATCH] Ensure the same ByteBufAllocator is used in the EmbeddedChannel when compress / decompress. Related to [#5294] Motivation: The user may specify to use a different allocator then the default. In this case we need to ensure it is shared when creating the EmbeddedChannel inside of a ChannelHandler Modifications: Use the config of the "original" Channel in the EmbeddedChannel and so share the same allocator etc. Result: Same type of buffers are used. --- .../codec/http/HttpContentCompressor.java | 10 ++++++- .../codec/http/HttpContentDecoder.java | 7 +++++ .../codec/http/HttpContentDecompressor.java | 7 +++-- .../CompressorHttp2ConnectionEncoder.java | 24 +++++++++------ .../DelegatingDecompressorFrameListener.java | 19 +++++++----- .../channel/embedded/EmbeddedChannel.java | 30 +++++++++++++++++-- 6 files changed, 75 insertions(+), 22 deletions(-) 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 6c90b0cad6..70dbfe903f 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 @@ -15,6 +15,7 @@ */ package io.netty.handler.codec.http; +import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.compression.ZlibCodecFactory; import io.netty.handler.codec.compression.ZlibWrapper; @@ -32,6 +33,7 @@ public class HttpContentCompressor extends HttpContentEncoder { private final int compressionLevel; private final int windowBits; private final int memLevel; + private ChannelHandlerContext ctx; /** * Creates a new handler with the default compression level (6), @@ -92,6 +94,11 @@ public class HttpContentCompressor extends HttpContentEncoder { this.memLevel = memLevel; } + @Override + public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + this.ctx = ctx; + } + @Override protected Result beginEncode(HttpResponse headers, String acceptEncoding) throws Exception { String contentEncoding = headers.headers().get(HttpHeaderNames.CONTENT_ENCODING); @@ -119,7 +126,8 @@ public class HttpContentCompressor extends HttpContentEncoder { return new Result( targetContentEncoding, - new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder( + new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), + ctx.channel().config(), ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel))); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java index 3fbc1e12fc..375bac5462 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java @@ -47,6 +47,7 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder() { @Override