View Javadoc
1   /*
2    * Copyright 2023 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import static io.netty.util.internal.ObjectUtil.checkPositive;
19  
20  
21  /**
22   * Enforce a limit on the maximum number of RST frames that are allowed per a window
23   * before the connection will be closed with a GO_AWAY frame.
24   */
25  final class Http2MaxRstFrameDecoder extends DecoratingHttp2ConnectionDecoder {
26      private final int maxRstFramesPerWindow;
27      private final int secondsPerWindow;
28  
29      Http2MaxRstFrameDecoder(Http2ConnectionDecoder delegate, int maxRstFramesPerWindow, int secondsPerWindow) {
30          super(delegate);
31          this.maxRstFramesPerWindow = checkPositive(maxRstFramesPerWindow, "maxRstFramesPerWindow");
32          this.secondsPerWindow = checkPositive(secondsPerWindow, "secondsPerWindow");
33      }
34  
35      @Override
36      public void frameListener(Http2FrameListener listener) {
37          if (listener != null) {
38              super.frameListener(new Http2MaxRstFrameListener(listener, maxRstFramesPerWindow, secondsPerWindow));
39          } else {
40              super.frameListener(null);
41          }
42      }
43  
44      @Override
45      public Http2FrameListener frameListener() {
46          Http2FrameListener frameListener = frameListener0();
47          // Unwrap the original Http2FrameListener as we add this decoder under the hood.
48          if (frameListener instanceof Http2MaxRstFrameListener) {
49              return ((Http2MaxRstFrameListener) frameListener).listener;
50          }
51          return frameListener;
52      }
53  
54      // Package-private for testing
55      Http2FrameListener frameListener0() {
56          return super.frameListener();
57      }
58  }