diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersEncoder.java index 5e0a9f6cf1..7b48b96e31 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersEncoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersEncoder.java @@ -43,7 +43,13 @@ public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2Hea public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize, int dynamicTableArraySizeHint) { - this(sensitivityDetector, new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint)); + this(sensitivityDetector, ignoreMaxHeaderListSize, dynamicTableArraySizeHint, HpackEncoder.HUFF_CODE_THRESHOLD); + } + + public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize, + int dynamicTableArraySizeHint, int huffCodeThreshold) { + this(sensitivityDetector, + new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint, huffCodeThreshold)); } /** diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java index 301a2c51cf..a1b25179c1 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java @@ -54,6 +54,7 @@ import static java.lang.Math.max; import static java.lang.Math.min; final class HpackEncoder { + static final int HUFF_CODE_THRESHOLD = 512; // a linked hash map of header fields private final HeaderEntry[] headerFields; private final HeaderEntry head = new HeaderEntry(-1, AsciiString.EMPTY_STRING, @@ -61,6 +62,7 @@ final class HpackEncoder { private final HpackHuffmanEncoder hpackHuffmanEncoder = new HpackHuffmanEncoder(); private final byte hashMask; private final boolean ignoreMaxHeaderListSize; + private final int huffCodeThreshold; private long size; private long maxHeaderTableSize; private long maxHeaderListSize; @@ -76,13 +78,13 @@ final class HpackEncoder { * Creates a new encoder. */ HpackEncoder(boolean ignoreMaxHeaderListSize) { - this(ignoreMaxHeaderListSize, 16); + this(ignoreMaxHeaderListSize, 16, HUFF_CODE_THRESHOLD); } /** * Creates a new encoder. */ - HpackEncoder(boolean ignoreMaxHeaderListSize, int arraySizeHint) { + HpackEncoder(boolean ignoreMaxHeaderListSize, int arraySizeHint, int huffCodeThreshold) { this.ignoreMaxHeaderListSize = ignoreMaxHeaderListSize; maxHeaderTableSize = DEFAULT_HEADER_TABLE_SIZE; maxHeaderListSize = MAX_HEADER_LIST_SIZE; @@ -91,6 +93,7 @@ final class HpackEncoder { headerFields = new HeaderEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))]; hashMask = (byte) (headerFields.length - 1); head.before = head.after = head; + this.huffCodeThreshold = huffCodeThreshold; } /** @@ -250,8 +253,9 @@ final class HpackEncoder { * Encode string literal according to Section 5.2. */ private void encodeStringLiteral(ByteBuf out, CharSequence string) { - int huffmanLength = hpackHuffmanEncoder.getEncodedLength(string); - if (huffmanLength < string.length()) { + int huffmanLength; + if (string.length() >= huffCodeThreshold + && (huffmanLength = hpackHuffmanEncoder.getEncodedLength(string)) < string.length()) { encodeInteger(out, 0x80, 7, huffmanLength); hpackHuffmanEncoder.encode(out, string); } else { diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java index 438097f3e3..b06d12c225 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriterTest.java @@ -15,6 +15,7 @@ package io.netty.handler.codec.http2; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.Channel; @@ -65,8 +66,11 @@ public class DefaultHttp2FrameWriterTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + http2HeadersEncoder = new DefaultHttp2HeadersEncoder( + Http2HeadersEncoder.NEVER_SENSITIVE, new HpackEncoder(false, 16, 0)); - frameWriter = new DefaultHttp2FrameWriter(); + frameWriter = new DefaultHttp2FrameWriter(new DefaultHttp2HeadersEncoder( + Http2HeadersEncoder.NEVER_SENSITIVE, new HpackEncoder(false, 16, 0))); outbound = Unpooled.buffer(); @@ -74,8 +78,6 @@ public class DefaultHttp2FrameWriterTest { promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE); - http2HeadersEncoder = new DefaultHttp2HeadersEncoder(); - Answer answer = new Answer() { @Override public Object answer(InvocationOnMock var1) throws Throwable { diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java index f660381237..fa288bf241 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/Http2TestUtil.java @@ -119,7 +119,7 @@ public final class Http2TestUtil { public static HpackEncoder newTestEncoder(boolean ignoreMaxHeaderListSize, long maxHeaderListSize, long maxHeaderTableSize) throws Http2Exception { - HpackEncoder hpackEncoder = new HpackEncoder(); + HpackEncoder hpackEncoder = new HpackEncoder(false, 16, 0); ByteBuf buf = Unpooled.buffer(); try { hpackEncoder.setMaxHeaderTableSize(buf, maxHeaderTableSize);