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.netty.handler.codec.http2;
16  
17  import io.netty.buffer.ByteBuf;
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.util.internal.ObjectUtil;
20  
21  /**
22   * Enforce a limit on the maximum number of consecutive empty DATA frames (without end_of_stream flag) that are allowed
23   * before the connection will be closed.
24   */
25  final class Http2EmptyDataFrameListener extends Http2FrameListenerDecorator {
26      private final int maxConsecutiveEmptyFrames;
27  
28      private boolean violationDetected;
29      private int emptyDataFrames;
30  
31      Http2EmptyDataFrameListener(Http2FrameListener listener, int maxConsecutiveEmptyFrames) {
32          super(listener);
33          this.maxConsecutiveEmptyFrames = ObjectUtil.checkPositive(
34                  maxConsecutiveEmptyFrames, "maxConsecutiveEmptyFrames");
35      }
36  
37      @Override
38      public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
39              throws Http2Exception {
40          if (endOfStream || data.isReadable()) {
41              emptyDataFrames = 0;
42          } else if (emptyDataFrames++ == maxConsecutiveEmptyFrames && !violationDetected) {
43              violationDetected = true;
44              throw Http2Exception.connectionError(Http2Error.ENHANCE_YOUR_CALM,
45                      "Maximum number %d of empty data frames without end_of_stream flag received",
46                      maxConsecutiveEmptyFrames);
47          }
48  
49          return super.onDataRead(ctx, streamId, data, padding, endOfStream);
50      }
51  
52      @Override
53      public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
54                                int padding, boolean endStream) throws Http2Exception {
55          emptyDataFrames = 0;
56          super.onHeadersRead(ctx, streamId, headers, padding, endStream);
57      }
58  
59      @Override
60      public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency,
61                                short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
62          emptyDataFrames = 0;
63          super.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive, padding, endStream);
64      }
65  }