1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ChannelInboundHandlerAdapter;
22 import io.netty.channel.StacklessClosedChannelException;
23 import io.netty.channel.socket.ChannelInputShutdownEvent;
24 import io.netty.handler.codec.quic.QuicChannel;
25 import io.netty.handler.codec.quic.QuicStreamChannel;
26 import io.netty.handler.codec.quic.QuicStreamType;
27 import io.netty.util.ReferenceCountUtil;
28 import io.netty.util.concurrent.Future;
29 import io.netty.util.concurrent.GenericFutureListener;
30 import org.jetbrains.annotations.Nullable;
31
32 import java.nio.channels.ClosedChannelException;
33
34 import static io.netty.handler.codec.http3.Http3CodecUtils.closeOnFailure;
35 import static io.netty.handler.codec.http3.Http3CodecUtils.connectionError;
36 import static io.netty.handler.codec.http3.Http3CodecUtils.criticalStreamClosed;
37 import static io.netty.handler.codec.http3.Http3ErrorCode.H3_FRAME_UNEXPECTED;
38 import static io.netty.handler.codec.http3.Http3ErrorCode.H3_ID_ERROR;
39 import static io.netty.handler.codec.http3.Http3ErrorCode.H3_MISSING_SETTINGS;
40 import static io.netty.handler.codec.http3.Http3ErrorCode.QPACK_ENCODER_STREAM_ERROR;
41 import static io.netty.handler.codec.http3.QpackUtil.toIntOrThrow;
42
43 final class Http3ControlStreamInboundHandler extends Http3FrameTypeInboundValidationHandler<Http3ControlStreamFrame> {
44
45
46 static final ClosedChannelException ENCODER_STREAM_INACTIVE =
47 StacklessClosedChannelException.newInstance(QPackEncoderStreamInitializer.class, "streamClosed()");
48 static final ClosedChannelException DECODER_STREAM_INACTIVE =
49 StacklessClosedChannelException.newInstance(QPackDecoderStreamInitializer.class, "streamClosed()");
50
51 final boolean server;
52 private final ChannelHandler controlFrameHandler;
53 private final QpackEncoder qpackEncoder;
54 private final Http3ControlStreamOutboundHandler remoteControlStreamHandler;
55 private boolean firstFrameRead;
56 private Long receivedGoawayId;
57 private Long receivedMaxPushId;
58
59 Http3ControlStreamInboundHandler(boolean server, @Nullable ChannelHandler controlFrameHandler,
60 QpackEncoder qpackEncoder,
61 Http3ControlStreamOutboundHandler remoteControlStreamHandler) {
62 super(Http3ControlStreamFrame.class);
63 this.server = server;
64 this.controlFrameHandler = controlFrameHandler;
65 this.qpackEncoder = qpackEncoder;
66 this.remoteControlStreamHandler = remoteControlStreamHandler;
67 }
68
69 boolean isServer() {
70 return server;
71 }
72
73 boolean isGoAwayReceived() {
74 return receivedGoawayId != null;
75 }
76
77 long maxPushIdReceived() {
78 return receivedMaxPushId == null ? -1 : receivedMaxPushId;
79 }
80
81 private boolean forwardControlFrames() {
82 return controlFrameHandler != null;
83 }
84
85 @Override
86 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
87 super.handlerAdded(ctx);
88
89 if (controlFrameHandler != null) {
90 ctx.pipeline().addLast(controlFrameHandler);
91 }
92 }
93
94 @Override
95 void readFrameDiscarded(ChannelHandlerContext ctx, Object discardedFrame) {
96 if (!firstFrameRead && !(discardedFrame instanceof Http3SettingsFrame)) {
97 connectionError(ctx, Http3ErrorCode.H3_MISSING_SETTINGS, "Missing settings frame.", forwardControlFrames());
98 }
99 }
100
101 @Override
102 void channelRead(ChannelHandlerContext ctx, Http3ControlStreamFrame frame) throws QpackException {
103 boolean isSettingsFrame = frame instanceof Http3SettingsFrame;
104 if (!firstFrameRead && !isSettingsFrame) {
105 connectionError(ctx, H3_MISSING_SETTINGS, "Missing settings frame.", forwardControlFrames());
106 ReferenceCountUtil.release(frame);
107 return;
108 }
109 if (firstFrameRead && isSettingsFrame) {
110 connectionError(ctx, H3_FRAME_UNEXPECTED, "Second settings frame received.", forwardControlFrames());
111 ReferenceCountUtil.release(frame);
112 return;
113 }
114 firstFrameRead = true;
115
116 final boolean valid;
117 if (isSettingsFrame) {
118 valid = handleHttp3SettingsFrame(ctx, (Http3SettingsFrame) frame);
119 } else if (frame instanceof Http3GoAwayFrame) {
120 valid = handleHttp3GoAwayFrame(ctx, (Http3GoAwayFrame) frame);
121 } else if (frame instanceof Http3MaxPushIdFrame) {
122 valid = handleHttp3MaxPushIdFrame(ctx, (Http3MaxPushIdFrame) frame);
123 } else if (frame instanceof Http3CancelPushFrame) {
124 valid = handleHttp3CancelPushFrame(ctx, (Http3CancelPushFrame) frame);
125 } else {
126
127
128 assert frame instanceof Http3UnknownFrame;
129 valid = true;
130 }
131
132 if (!valid || controlFrameHandler == null) {
133 ReferenceCountUtil.release(frame);
134 return;
135 }
136
137
138
139 ctx.fireChannelRead(frame);
140 }
141
142 private boolean handleHttp3SettingsFrame(ChannelHandlerContext ctx, Http3SettingsFrame settingsFrame)
143 throws QpackException {
144 final QuicChannel quicChannel = (QuicChannel) ctx.channel().parent();
145 final QpackAttributes qpackAttributes = Http3.getQpackAttributes(quicChannel);
146 assert qpackAttributes != null;
147 final GenericFutureListener<Future<? super QuicStreamChannel>> closeOnFailure = future -> {
148 if (!future.isSuccess()) {
149 criticalStreamClosed(ctx);
150 }
151 };
152 if (qpackAttributes.dynamicTableDisabled()) {
153 qpackEncoder.configureDynamicTable(qpackAttributes, 0, 0);
154 return true;
155 }
156 quicChannel.createStream(QuicStreamType.UNIDIRECTIONAL,
157 new QPackEncoderStreamInitializer(qpackEncoder, qpackAttributes,
158 settingsFrame
159 .settings()
160 .getOrDefault(
161 Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(),
162 0L
163 ),
164 settingsFrame
165 .settings()
166 .getOrDefault(
167 Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(),
168 0L
169 )
170 )
171 )
172 .addListener(closeOnFailure);
173 quicChannel.createStream(QuicStreamType.UNIDIRECTIONAL, new QPackDecoderStreamInitializer(qpackAttributes))
174 .addListener(closeOnFailure);
175 return true;
176 }
177
178 private boolean handleHttp3GoAwayFrame(ChannelHandlerContext ctx, Http3GoAwayFrame goAwayFrame) {
179 long id = goAwayFrame.id();
180 if (!server && id % 4 != 0) {
181 connectionError(ctx, H3_FRAME_UNEXPECTED, "GOAWAY received with ID of non-request stream.",
182 forwardControlFrames());
183 return false;
184 }
185 if (receivedGoawayId != null && id > receivedGoawayId) {
186 connectionError(ctx, H3_ID_ERROR,
187 "GOAWAY received with ID larger than previously received.", forwardControlFrames());
188 return false;
189 }
190 receivedGoawayId = id;
191 return true;
192 }
193
194 private boolean handleHttp3MaxPushIdFrame(ChannelHandlerContext ctx, Http3MaxPushIdFrame frame) {
195 long id = frame.id();
196 if (!server) {
197 connectionError(ctx, H3_FRAME_UNEXPECTED, "MAX_PUSH_ID received by client.",
198 forwardControlFrames());
199 return false;
200 }
201 if (receivedMaxPushId != null && id < receivedMaxPushId) {
202 connectionError(ctx, H3_ID_ERROR, "MAX_PUSH_ID reduced limit.", forwardControlFrames());
203 return false;
204 }
205 receivedMaxPushId = id;
206 return true;
207 }
208
209 private boolean handleHttp3CancelPushFrame(ChannelHandlerContext ctx, Http3CancelPushFrame cancelPushFrame) {
210 final Long maxPushId = server ? receivedMaxPushId : remoteControlStreamHandler.sentMaxPushId();
211 if (maxPushId == null || maxPushId < cancelPushFrame.id()) {
212 connectionError(ctx, H3_ID_ERROR, "CANCEL_PUSH received with an ID greater than MAX_PUSH_ID.",
213 forwardControlFrames());
214 return false;
215 }
216 return true;
217 }
218
219 @Override
220 public void channelReadComplete(ChannelHandlerContext ctx) {
221 ctx.fireChannelReadComplete();
222
223
224
225 Http3CodecUtils.readIfNoAutoRead(ctx);
226 }
227
228 @Override
229 public boolean isSharable() {
230
231 return false;
232 }
233
234 @Override
235 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
236 if (evt instanceof ChannelInputShutdownEvent) {
237
238 criticalStreamClosed(ctx);
239 }
240 ctx.fireUserEventTriggered(evt);
241 }
242
243 private abstract static class AbstractQPackStreamInitializer extends ChannelInboundHandlerAdapter {
244 private final int streamType;
245 protected final QpackAttributes attributes;
246
247 AbstractQPackStreamInitializer(int streamType, QpackAttributes attributes) {
248 this.streamType = streamType;
249 this.attributes = attributes;
250 }
251
252 @Override
253 public final void channelActive(ChannelHandlerContext ctx) {
254
255
256
257 ByteBuf buffer = ctx.alloc().buffer(8);
258 Http3CodecUtils.writeVariableLengthInteger(buffer, streamType);
259 closeOnFailure(ctx.writeAndFlush(buffer));
260 streamAvailable(ctx);
261 ctx.fireChannelActive();
262 }
263
264 @Override
265 public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
266 streamClosed(ctx);
267 if (evt instanceof ChannelInputShutdownEvent) {
268
269 criticalStreamClosed(ctx);
270 }
271 ctx.fireUserEventTriggered(evt);
272 }
273
274 @Override
275 public void channelInactive(ChannelHandlerContext ctx) {
276 streamClosed(ctx);
277
278 criticalStreamClosed(ctx);
279 ctx.fireChannelInactive();
280 }
281
282 protected abstract void streamAvailable(ChannelHandlerContext ctx);
283
284 protected abstract void streamClosed(ChannelHandlerContext ctx);
285 }
286
287 private static final class QPackEncoderStreamInitializer extends AbstractQPackStreamInitializer {
288 private final QpackEncoder encoder;
289 private final long maxTableCapacity;
290 private final long blockedStreams;
291
292 QPackEncoderStreamInitializer(QpackEncoder encoder, QpackAttributes attributes, long maxTableCapacity,
293 long blockedStreams) {
294 super(Http3CodecUtils.HTTP3_QPACK_ENCODER_STREAM_TYPE, attributes);
295 this.encoder = encoder;
296 this.maxTableCapacity = maxTableCapacity;
297 this.blockedStreams = blockedStreams;
298 }
299
300 @Override
301 protected void streamAvailable(ChannelHandlerContext ctx) {
302 final QuicStreamChannel stream = (QuicStreamChannel) ctx.channel();
303 attributes.encoderStream(stream);
304
305 try {
306 encoder.configureDynamicTable(attributes, maxTableCapacity, toIntOrThrow(blockedStreams));
307 } catch (QpackException e) {
308 connectionError(ctx, new Http3Exception(QPACK_ENCODER_STREAM_ERROR,
309 "Dynamic table configuration failed.", e), true);
310 }
311 }
312
313 @Override
314 protected void streamClosed(ChannelHandlerContext ctx) {
315 attributes.encoderStreamInactive(ENCODER_STREAM_INACTIVE);
316 }
317 }
318
319 private static final class QPackDecoderStreamInitializer extends AbstractQPackStreamInitializer {
320 private QPackDecoderStreamInitializer(QpackAttributes attributes) {
321 super(Http3CodecUtils.HTTP3_QPACK_DECODER_STREAM_TYPE, attributes);
322 }
323
324 @Override
325 protected void streamAvailable(ChannelHandlerContext ctx) {
326 attributes.decoderStream((QuicStreamChannel) ctx.channel());
327 }
328
329 @Override
330 protected void streamClosed(ChannelHandlerContext ctx) {
331 attributes.decoderStreamInactive(DECODER_STREAM_INACTIVE);
332 }
333 }
334 }