View Javadoc
1   /*
2    * Copyright 2021 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.http3;
17  
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.channel.ChannelInboundHandlerAdapter;
20  import io.netty.handler.codec.http3.Http3RequestStreamEncodeStateValidator.State;
21  
22  import static io.netty.handler.codec.http3.Http3FrameValidationUtils.frameTypeUnexpected;
23  import static io.netty.handler.codec.http3.Http3RequestStreamEncodeStateValidator.evaluateFrame;
24  import static io.netty.handler.codec.http3.Http3RequestStreamEncodeStateValidator.isFinalHeadersReceived;
25  import static io.netty.handler.codec.http3.Http3RequestStreamEncodeStateValidator.isStreamStarted;
26  import static io.netty.handler.codec.http3.Http3RequestStreamEncodeStateValidator.isTrailersReceived;
27  
28  final class Http3RequestStreamDecodeStateValidator extends ChannelInboundHandlerAdapter
29          implements Http3RequestStreamCodecState {
30      private State state = State.None;
31  
32      @Override
33      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
34          if (!(msg instanceof Http3RequestStreamFrame)) {
35              super.channelRead(ctx, msg);
36              return;
37          }
38          final Http3RequestStreamFrame frame = (Http3RequestStreamFrame) msg;
39          final State nextState = evaluateFrame(state, frame);
40          if (nextState == null) {
41              frameTypeUnexpected(ctx, msg);
42              return;
43          }
44          state = nextState;
45          super.channelRead(ctx, msg);
46      }
47  
48      @Override
49      public boolean started() {
50          return isStreamStarted(state);
51      }
52  
53      @Override
54      public boolean receivedFinalHeaders() {
55          return isFinalHeadersReceived(state);
56      }
57  
58      @Override
59      public boolean terminated() {
60          return isTrailersReceived(state);
61      }
62  }