View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.util.internal.ObjectUtil;
20  import io.netty.util.internal.UnstableApi;
21  
22  import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_HEADER_LIST_SIZE;
23  import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_INITIAL_HUFFMAN_DECODE_CAPACITY;
24  import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
25  import static io.netty.handler.codec.http2.Http2Exception.connectionError;
26  
27  @UnstableApi
28  public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2HeadersDecoder.Configuration {
29      private static final float HEADERS_COUNT_WEIGHT_NEW = 1 / 5f;
30      private static final float HEADERS_COUNT_WEIGHT_HISTORICAL = 1 - HEADERS_COUNT_WEIGHT_NEW;
31  
32      private final HpackDecoder hpackDecoder;
33      private final boolean validateHeaders;
34  
35      /**
36       * Used to calculate an exponential moving average of header sizes to get an estimate of how large the data
37       * structure for storing headers should be.
38       */
39      private float headerArraySizeAccumulator = 8;
40  
41      public DefaultHttp2HeadersDecoder() {
42          this(true);
43      }
44  
45      public DefaultHttp2HeadersDecoder(boolean validateHeaders) {
46          this(validateHeaders, DEFAULT_HEADER_LIST_SIZE);
47      }
48  
49      /**
50       * Create a new instance.
51       * @param validateHeaders {@code true} to validate headers are valid according to the RFC.
52       * @param maxHeaderListSize This is the only setting that can be configured before notifying the peer.
53       *  This is because <a href="https://tools.ietf.org/html/rfc7540#section-6.5.1">SETTINGS_MAX_HEADER_LIST_SIZE</a>
54       *  allows a lower than advertised limit from being enforced, and the default limit is unlimited
55       *  (which is dangerous).
56       */
57      public DefaultHttp2HeadersDecoder(boolean validateHeaders, long maxHeaderListSize) {
58          this(validateHeaders, maxHeaderListSize, DEFAULT_INITIAL_HUFFMAN_DECODE_CAPACITY);
59      }
60  
61      /**
62       * Create a new instance.
63       * @param validateHeaders {@code true} to validate headers are valid according to the RFC.
64       * @param maxHeaderListSize This is the only setting that can be configured before notifying the peer.
65       *  This is because <a href="https://tools.ietf.org/html/rfc7540#section-6.5.1">SETTINGS_MAX_HEADER_LIST_SIZE</a>
66       *  allows a lower than advertised limit from being enforced, and the default limit is unlimited
67       *  (which is dangerous).
68       * @param initialHuffmanDecodeCapacity Size of an intermediate buffer used during huffman decode.
69       */
70      public DefaultHttp2HeadersDecoder(boolean validateHeaders, long maxHeaderListSize,
71                                        int initialHuffmanDecodeCapacity) {
72          this(validateHeaders, new HpackDecoder(maxHeaderListSize, initialHuffmanDecodeCapacity));
73      }
74  
75      /**
76       * Exposed Used for testing only! Default values used in the initial settings frame are overridden intentionally
77       * for testing but violate the RFC if used outside the scope of testing.
78       */
79      DefaultHttp2HeadersDecoder(boolean validateHeaders, HpackDecoder hpackDecoder) {
80          this.hpackDecoder = ObjectUtil.checkNotNull(hpackDecoder, "hpackDecoder");
81          this.validateHeaders = validateHeaders;
82      }
83  
84      @Override
85      public void maxHeaderTableSize(long max) throws Http2Exception {
86          hpackDecoder.setMaxHeaderTableSize(max);
87      }
88  
89      @Override
90      public long maxHeaderTableSize() {
91          return hpackDecoder.getMaxHeaderTableSize();
92      }
93  
94      @Override
95      public void maxHeaderListSize(long max, long goAwayMax) throws Http2Exception {
96          hpackDecoder.setMaxHeaderListSize(max, goAwayMax);
97      }
98  
99      @Override
100     public long maxHeaderListSize() {
101         return hpackDecoder.getMaxHeaderListSize();
102     }
103 
104     @Override
105     public long maxHeaderListSizeGoAway() {
106         return hpackDecoder.getMaxHeaderListSizeGoAway();
107     }
108 
109     @Override
110     public Configuration configuration() {
111         return this;
112     }
113 
114     @Override
115     public Http2Headers decodeHeaders(int streamId, ByteBuf headerBlock) throws Http2Exception {
116         try {
117             final Http2Headers headers = newHeaders();
118             hpackDecoder.decode(streamId, headerBlock, headers, validateHeaders);
119             headerArraySizeAccumulator = HEADERS_COUNT_WEIGHT_NEW * headers.size() +
120                                          HEADERS_COUNT_WEIGHT_HISTORICAL * headerArraySizeAccumulator;
121             return headers;
122         } catch (Http2Exception e) {
123             throw e;
124         } catch (Throwable e) {
125             // Default handler for any other types of errors that may have occurred. For example,
126             // the Header builder throws IllegalArgumentException if the key or value was invalid
127             // for any reason (e.g. the key was an invalid pseudo-header).
128             throw connectionError(COMPRESSION_ERROR, e, e.getMessage());
129         }
130     }
131 
132     /**
133      * A weighted moving average estimating how many headers are expected during the decode process.
134      * @return an estimate of how many headers are expected during the decode process.
135      */
136     protected final int numberOfHeadersGuess() {
137         return (int) headerArraySizeAccumulator;
138     }
139 
140     /**
141      * Determines if the headers should be validated as a result of the decode operation.
142      * @return {@code true} if the headers should be validated as a result of the decode operation.
143      */
144     protected final boolean validateHeaders() {
145         return validateHeaders;
146     }
147 
148     /**
149      * Create a new {@link Http2Headers} object which will store the results of the decode operation.
150      * @return a new {@link Http2Headers} object which will store the results of the decode operation.
151      */
152     protected Http2Headers newHeaders() {
153         return new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator);
154     }
155 }