View Javadoc
1   /*
2    * Copyright 2019 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  package io.netty5.handler.codec.http2;
16  
17  import io.netty5.util.internal.ObjectUtil;
18  
19  /**
20   * Enforce a limit on the maximum number of consecutive empty DATA frames (without end_of_stream flag) that are allowed
21   * before the connection will be closed.
22   */
23  final class Http2EmptyDataFrameConnectionDecoder extends DecoratingHttp2ConnectionDecoder {
24  
25      private final int maxConsecutiveEmptyFrames;
26  
27      Http2EmptyDataFrameConnectionDecoder(Http2ConnectionDecoder delegate, int maxConsecutiveEmptyFrames) {
28          super(delegate);
29          this.maxConsecutiveEmptyFrames = ObjectUtil.checkPositive(
30                  maxConsecutiveEmptyFrames, "maxConsecutiveEmptyFrames");
31      }
32  
33      @Override
34      public void frameListener(Http2FrameListener listener) {
35          if (listener != null) {
36              super.frameListener(new Http2EmptyDataFrameListener(listener, maxConsecutiveEmptyFrames));
37          } else {
38              super.frameListener(null);
39          }
40      }
41  
42      @Override
43      public Http2FrameListener frameListener() {
44          Http2FrameListener frameListener = frameListener0();
45          // Unwrap the original Http2FrameListener as we add this decoder under the hood.
46          if (frameListener instanceof Http2EmptyDataFrameListener) {
47              return ((Http2EmptyDataFrameListener) frameListener).listener;
48          }
49          return frameListener;
50      }
51  
52      // Package-private for testing
53      Http2FrameListener frameListener0() {
54          return super.frameListener();
55      }
56  }