View Javadoc
1   /*
2    * Copyright 2020 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.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandler;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelPromise;
22  import io.netty.channel.socket.ChannelInputShutdownEvent;
23  import io.netty.util.ReferenceCountUtil;
24  import io.netty.util.internal.ObjectUtil;
25  import org.jetbrains.annotations.Nullable;
26  
27  import static io.netty.handler.codec.http3.Http3CodecUtils.closeOnFailure;
28  
29  final class Http3ControlStreamOutboundHandler
30          extends Http3FrameTypeDuplexValidationHandler<Http3ControlStreamFrame> {
31      private final boolean server;
32      private final ChannelHandler codec;
33      private Long sentMaxPushId;
34      private Long sendGoAwayId;
35      private Http3SettingsFrame localSettings;
36  
37      Http3ControlStreamOutboundHandler(boolean server, Http3SettingsFrame localSettings, ChannelHandler codec) {
38          super(Http3ControlStreamFrame.class);
39          this.server = server;
40          this.localSettings = ObjectUtil.checkNotNull(localSettings, "localSettings");
41          this.codec = ObjectUtil.checkNotNull(codec, "codec");
42      }
43  
44      /**
45       * Returns the last id that was sent in a MAX_PUSH_ID frame or {@code null} if none was sent yet.
46       *
47       * @return the id.
48       */
49      @Nullable
50      Long sentMaxPushId() {
51          return sentMaxPushId;
52      }
53  
54      @Override
55      public void channelActive(ChannelHandlerContext ctx) {
56          // We need to write 0x00 into the stream before doing anything else.
57          // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.2.1
58          // Just allocate 8 bytes which would be the max needed.
59          ByteBuf buffer = ctx.alloc().buffer(8);
60          Http3CodecUtils.writeVariableLengthInteger(buffer, Http3CodecUtils.HTTP3_CONTROL_STREAM_TYPE);
61          ctx.write(buffer);
62          // Add the encoder and decoder in the pipeline so we can handle Http3Frames. This needs to happen after
63          // we did write the type via a ByteBuf.
64          ctx.pipeline().addFirst(codec);
65  
66          assert localSettings != null;
67          // If writing of the local settings fails let's just teardown the connection.
68          closeOnFailure(ctx.writeAndFlush(localSettings));
69  
70          // Let the GC collect localSettings.
71          localSettings = null;
72  
73          ctx.fireChannelActive();
74      }
75  
76      @Override
77      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
78          if (evt instanceof ChannelInputShutdownEvent) {
79              // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.2.1
80              Http3CodecUtils.criticalStreamClosed(ctx);
81          }
82          ctx.fireUserEventTriggered(evt);
83      }
84  
85      @Override
86      public void channelInactive(ChannelHandlerContext ctx) {
87          // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.2.1
88          Http3CodecUtils.criticalStreamClosed(ctx);
89          ctx.fireChannelInactive();
90      }
91  
92      @Override
93      void write(ChannelHandlerContext ctx, Http3ControlStreamFrame msg, ChannelPromise promise) {
94          if (msg instanceof Http3MaxPushIdFrame && !handleHttp3MaxPushIdFrame(promise, (Http3MaxPushIdFrame) msg)) {
95              ReferenceCountUtil.release(msg);
96              return;
97          } else if (msg instanceof Http3GoAwayFrame && !handleHttp3GoAwayFrame(promise, (Http3GoAwayFrame) msg)) {
98              ReferenceCountUtil.release(msg);
99              return;
100         }
101 
102         ctx.write(msg, promise);
103     }
104 
105     private boolean handleHttp3MaxPushIdFrame(ChannelPromise promise, Http3MaxPushIdFrame maxPushIdFrame) {
106         long id = maxPushIdFrame.id();
107 
108         // See https://datatracker.ietf.org/doc/html/draft-ietf-quic-http-32#section-7.2.7
109         if (sentMaxPushId != null && id < sentMaxPushId) {
110             promise.setFailure(new Http3Exception(Http3ErrorCode.H3_ID_ERROR, "MAX_PUSH_ID reduced limit."));
111             return false;
112         }
113 
114         sentMaxPushId = maxPushIdFrame.id();
115         return true;
116     }
117 
118     private boolean handleHttp3GoAwayFrame(ChannelPromise promise, Http3GoAwayFrame goAwayFrame) {
119         long id = goAwayFrame.id();
120 
121         // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-5.2
122         if (server && id % 4 != 0) {
123             promise.setFailure(new Http3Exception(Http3ErrorCode.H3_ID_ERROR,
124                     "GOAWAY id not valid : " + id));
125             return false;
126         }
127 
128         if (sendGoAwayId != null && id > sendGoAwayId) {
129             promise.setFailure(new Http3Exception(Http3ErrorCode.H3_ID_ERROR,
130                     "GOAWAY id is bigger then the last sent: " + id + " > " + sendGoAwayId));
131             return false;
132         }
133 
134         sendGoAwayId = id;
135         return true;
136     }
137 
138     @Override
139     public boolean isSharable() {
140         // This handle keeps state so we cant reuse it.
141         return false;
142     }
143 }