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.netty5.handler.codec.http2;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferAllocator;
20  import io.netty5.util.internal.UnstableApi;
21  
22  import static io.netty5.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
23  import static io.netty5.handler.codec.http2.Http2Exception.connectionError;
24  import static java.util.Objects.requireNonNull;
25  
26  @UnstableApi
27  public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2HeadersEncoder.Configuration {
28      private final HpackEncoder hpackEncoder;
29      private final SensitivityDetector sensitivityDetector;
30      private final Buffer tableSizeChangeOutput = BufferAllocator.onHeapUnpooled().allocate(256);
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 = requireNonNull(sensitivityDetector, "sensitiveDetector");
61          this.hpackEncoder = requireNonNull(hpackEncoder, "hpackEncoder");
62      }
63  
64      @Override
65      public void encodeHeaders(int streamId, Http2Headers headers, Buffer buffer) throws Http2Exception {
66          try {
67              // If there was a change in the table size, serialize the output from the hpackEncoder
68              // resulting from that change.
69              if (tableSizeChangeOutput.readableBytes() > 0) {
70                  buffer.writeBytes(tableSizeChangeOutput);
71                  tableSizeChangeOutput.resetOffsets();
72              }
73  
74              hpackEncoder.encodeHeaders(streamId, buffer, headers, sensitivityDetector);
75          } catch (Http2Exception e) {
76              throw e;
77          } catch (Throwable t) {
78              throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
79          }
80      }
81  
82      @Override
83      public void maxHeaderTableSize(long max) throws Http2Exception {
84          hpackEncoder.setMaxHeaderTableSize(tableSizeChangeOutput, max);
85      }
86  
87      @Override
88      public long maxHeaderTableSize() {
89          return hpackEncoder.getMaxHeaderTableSize();
90      }
91  
92      @Override
93      public void maxHeaderListSize(long max) throws Http2Exception {
94          hpackEncoder.setMaxHeaderListSize(max);
95      }
96  
97      @Override
98      public long maxHeaderListSize() {
99          return hpackEncoder.getMaxHeaderListSize();
100     }
101 
102     @Override
103     public Configuration configuration() {
104         return this;
105     }
106 }