From 225d10e1ade6906ce75257261729412ad5a80e6f Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 31 Mar 2017 17:56:59 -0700 Subject: [PATCH] HTTP/2 Make DefaultHttp2HeadersDecoder's Http2Headers object creation extensible Motivation: It is generally useful to override DefaultHttp2HeadersDecoder's creation of a new Http2Headers object so more optimized versions can be substituted if the use case allows for it. Modifications: - DefaultHttp2HeadersDecoder should support an overridable method to generate the new Http2Headers object for each decode operation Result: DefaultHttp2HeadersDecoder is more extensible. Fixes https://github.com/netty/netty/issues/6591. --- .../http2/DefaultHttp2HeadersDecoder.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersDecoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersDecoder.java index 671d8a524c..f07221b7ab 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersDecoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2HeadersDecoder.java @@ -112,7 +112,7 @@ public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2Hea @Override public Http2Headers decodeHeaders(int streamId, ByteBuf headerBlock) throws Http2Exception { try { - final Http2Headers headers = new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator); + final Http2Headers headers = newHeaders(); hpackDecoder.decode(streamId, headerBlock, headers); headerArraySizeAccumulator = HEADERS_COUNT_WEIGHT_NEW * headers.size() + HEADERS_COUNT_WEIGHT_HISTORICAL * headerArraySizeAccumulator; @@ -126,4 +126,28 @@ public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2Hea throw connectionError(COMPRESSION_ERROR, e, e.getMessage()); } } + + /** + * A weighted moving average estimating how many headers are expected during the decode process. + * @return an estimate of how many headers are expected during the decode process. + */ + protected final int numberOfHeadersGuess() { + return (int) headerArraySizeAccumulator; + } + + /** + * Determines if the headers should be validated as a result of the decode operation. + * @return {@code true} if the headers should be validated as a result of the decode operation. + */ + protected final boolean validateHeaders() { + return validateHeaders; + } + + /** + * Create a new {@link Http2Headers} object which will store the results of the decode operation. + * @return a new {@link Http2Headers} object which will store the results of the decode operation. + */ + protected Http2Headers newHeaders() { + return new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator); + } }