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    * https://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 java.io.Closeable;
19  
20  import io.netty.buffer.ByteBuf;
21  
22  import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
23  import static io.netty.handler.codec.http2.Http2Exception.connectionError;
24  import static io.netty.util.internal.ObjectUtil.checkNotNull;
25  
26  public class DefaultHttp2HeadersEncoder implements
27      Http2HeadersEncoder, Http2HeadersEncoder.Configuration, Closeable {
28  
29      private final HpackEncoder hpackEncoder;
30      private final SensitivityDetector sensitivityDetector;
31  
32      public DefaultHttp2HeadersEncoder() {
33          this(NEVER_SENSITIVE);
34      }
35  
36      public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector) {
37          this(sensitivityDetector, new HpackEncoder());
38      }
39  
40      public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize) {
41          this(sensitivityDetector, new HpackEncoder(ignoreMaxHeaderListSize));
42      }
43  
44      public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize,
45                                        int dynamicTableArraySizeHint) {
46          this(sensitivityDetector, ignoreMaxHeaderListSize, dynamicTableArraySizeHint, HpackEncoder.HUFF_CODE_THRESHOLD);
47      }
48  
49      public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize,
50                                        int dynamicTableArraySizeHint, int huffCodeThreshold) {
51          this(sensitivityDetector,
52                  new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint, huffCodeThreshold));
53      }
54  
55      /**
56       * Exposed Used for testing only! Default values used in the initial settings frame are overridden intentionally
57       * for testing but violate the RFC if used outside the scope of testing.
58       */
59      DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, HpackEncoder hpackEncoder) {
60          this.sensitivityDetector = checkNotNull(sensitivityDetector, "sensitiveDetector");
61          this.hpackEncoder = checkNotNull(hpackEncoder, "hpackEncoder");
62      }
63  
64      @Override
65      public void encodeHeaders(int streamId, Http2Headers headers, ByteBuf buffer) throws Http2Exception {
66          try {
67              hpackEncoder.encodeHeaders(streamId, buffer, headers, sensitivityDetector);
68          } catch (Http2Exception e) {
69              throw e;
70          } catch (Throwable t) {
71              throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
72          }
73      }
74  
75      @Override
76      public void maxHeaderTableSize(long max) throws Http2Exception {
77          hpackEncoder.setMaxHeaderTableSize(max);
78      }
79  
80      @Override
81      public long maxHeaderTableSize() {
82          return hpackEncoder.getMaxHeaderTableSize();
83      }
84  
85      @Override
86      public void maxHeaderListSize(long max) throws Http2Exception {
87          hpackEncoder.setMaxHeaderListSize(max);
88      }
89  
90      @Override
91      public long maxHeaderListSize() {
92          return hpackEncoder.getMaxHeaderListSize();
93      }
94  
95      @Override
96      public Configuration configuration() {
97          return this;
98      }
99  
100     /**
101      * Close the encoder and release all its associated data.
102      */
103     @Override
104     public void close() {
105     }
106 }