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 io.netty.util.concurrent.Ticker;
19  
20  import static io.netty.util.internal.ObjectUtil.checkPositive;
21  
22  
23  /**
24   * Enforce a limit on the maximum number of RST frames that are allowed per a window
25   * before the connection will be closed with a GO_AWAY frame.
26   */
27  final class Http2MaxRstFrameDecoder extends DecoratingHttp2ConnectionDecoder {
28      private final int maxRstFramesPerWindow;
29      private final int secondsPerWindow;
30  
31      Http2MaxRstFrameDecoder(Http2ConnectionDecoder delegate, int maxRstFramesPerWindow, int secondsPerWindow) {
32          super(delegate);
33          this.maxRstFramesPerWindow = checkPositive(maxRstFramesPerWindow, "maxRstFramesPerWindow");
34          this.secondsPerWindow = checkPositive(secondsPerWindow, "secondsPerWindow");
35      }
36  
37      @Override
38      public void frameListener(Http2FrameListener listener) {
39          if (listener != null) {
40              super.frameListener(new Http2MaxRstFrameListener(
41                      listener, maxRstFramesPerWindow, secondsPerWindow, Ticker.systemTicker()));
42          } else {
43              super.frameListener(null);
44          }
45      }
46  
47      @Override
48      public Http2FrameListener frameListener() {
49          Http2FrameListener frameListener = frameListener0();
50          // Unwrap the original Http2FrameListener as we add this decoder under the hood.
51          if (frameListener instanceof Http2MaxRstFrameListener) {
52              return ((Http2MaxRstFrameListener) frameListener).listener;
53          }
54          return frameListener;
55      }
56  
57      // Package-private for testing
58      Http2FrameListener frameListener0() {
59          return super.frameListener();
60      }
61  }