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.ChannelInitializer;
19 import io.netty.channel.ChannelPipeline;
20 import io.netty.handler.codec.quic.QuicStreamChannel;
21
22 import static io.netty.handler.codec.http3.Http3CodecUtils.isServerInitiatedQuicStream;
23 import static io.netty.handler.codec.http3.Http3RequestStreamCodecState.NO_STATE;
24
25 /**
26 * Abstract base class that users can extend to init HTTP/3 push-streams for clients. This initializer
27 * will automatically add HTTP/3 codecs etc to the {@link ChannelPipeline} as well.
28 */
29 public abstract class Http3PushStreamClientInitializer extends ChannelInitializer<QuicStreamChannel> {
30
31 @Override
32 protected final void initChannel(QuicStreamChannel ch) {
33 if (isServerInitiatedQuicStream(ch)) {
34 throw new IllegalArgumentException("Using client push stream initializer for server stream: " +
35 ch.streamId());
36 }
37 Http3CodecUtils.verifyIsUnidirectional(ch);
38
39 Http3ConnectionHandler connectionHandler = Http3CodecUtils.getConnectionHandlerOrClose(ch.parent());
40 if (connectionHandler == null) {
41 // connection should have been closed
42 return;
43 }
44 ChannelPipeline pipeline = ch.pipeline();
45 Http3RequestStreamDecodeStateValidator decodeStateValidator = new Http3RequestStreamDecodeStateValidator();
46 // Add the encoder and decoder in the pipeline, so we can handle Http3Frames
47 pipeline.addLast(connectionHandler.newCodec(NO_STATE, decodeStateValidator));
48 pipeline.addLast(decodeStateValidator);
49 // Add the handler that will validate what we write and receive on this stream.
50 pipeline.addLast(connectionHandler.newPushStreamValidationHandler(ch, decodeStateValidator));
51 initPushStream(ch);
52 }
53
54 /**
55 * Initialize the {@link QuicStreamChannel} to handle {@link Http3PushStreamFrame}s. At the point of calling this
56 * method it is already valid to write {@link Http3PushStreamFrame}s as the codec is already in the pipeline.
57 *
58 * @param ch the {QuicStreamChannel} for the push stream.
59 */
60 protected abstract void initPushStream(QuicStreamChannel ch);
61 }