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.buffer.Unpooled;
20  import io.netty.util.internal.UnstableApi;
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  @UnstableApi
27  public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2HeadersEncoder.Configuration {
28      private final HpackEncoder hpackEncoder;
29      private final SensitivityDetector sensitivityDetector;
30      private final ByteBuf tableSizeChangeOutput = Unpooled.buffer();
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, new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint));
47      }
48  
49      /**
50       * Exposed Used for testing only! Default values used in the initial settings frame are overridden intentionally
51       * for testing but violate the RFC if used outside the scope of testing.
52       */
53      DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, HpackEncoder hpackEncoder) {
54          this.sensitivityDetector = checkNotNull(sensitivityDetector, "sensitiveDetector");
55          this.hpackEncoder = checkNotNull(hpackEncoder, "hpackEncoder");
56      }
57  
58      @Override
59      public void encodeHeaders(int streamId, Http2Headers headers, ByteBuf buffer) throws Http2Exception {
60          try {
61              // If there was a change in the table size, serialize the output from the hpackEncoder
62              // resulting from that change.
63              if (tableSizeChangeOutput.isReadable()) {
64                  buffer.writeBytes(tableSizeChangeOutput);
65                  tableSizeChangeOutput.clear();
66              }
67  
68              hpackEncoder.encodeHeaders(streamId, buffer, headers, sensitivityDetector);
69          } catch (Http2Exception e) {
70              throw e;
71          } catch (Throwable t) {
72              throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
73          }
74      }
75  
76      @Override
77      public void maxHeaderTableSize(long max) throws Http2Exception {
78          hpackEncoder.setMaxHeaderTableSize(tableSizeChangeOutput, max);
79      }
80  
81      @Override
82      public long maxHeaderTableSize() {
83          return hpackEncoder.getMaxHeaderTableSize();
84      }
85  
86      @Override
87      public void maxHeaderListSize(long max) throws Http2Exception {
88          hpackEncoder.setMaxHeaderListSize(max);
89      }
90  
91      @Override
92      public long maxHeaderListSize() {
93          return hpackEncoder.getMaxHeaderListSize();
94      }
95  
96      @Override
97      public Configuration configuration() {
98          return this;
99      }
100 }