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.buffer.ByteBuf;
19  import io.netty.channel.ChannelInitializer;
20  import io.netty.channel.ChannelPipeline;
21  import io.netty.handler.codec.quic.QuicStreamChannel;
22  
23  import static io.netty.handler.codec.http3.Http3CodecUtils.isServerInitiatedQuicStream;
24  import static io.netty.handler.codec.http3.Http3CodecUtils.writeVariableLengthInteger;
25  import static io.netty.handler.codec.http3.Http3RequestStreamCodecState.NO_STATE;
26  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
27  
28  /**
29   * Abstract base class that users can extend to init HTTP/3 push-streams for servers. This initializer
30   * will automatically add HTTP/3 codecs etc to the {@link ChannelPipeline} as well.
31   */
32  public abstract class Http3PushStreamServerInitializer extends ChannelInitializer<QuicStreamChannel> {
33  
34      private final long pushId;
35  
36      protected Http3PushStreamServerInitializer(long pushId) {
37          this.pushId = checkPositiveOrZero(pushId, "pushId");
38      }
39  
40      @Override
41      protected final void initChannel(QuicStreamChannel ch) {
42          if (!isServerInitiatedQuicStream(ch)) {
43              throw new IllegalArgumentException("Using server push stream initializer for client stream: " +
44                      ch.streamId());
45          }
46          Http3CodecUtils.verifyIsUnidirectional(ch);
47  
48          // We need to write stream type into the stream before doing anything else.
49          // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.2.1
50          // Just allocate 16 bytes which would be the max needed to write 2 variable length ints.
51          ByteBuf buffer = ch.alloc().buffer(16);
52          writeVariableLengthInteger(buffer, Http3CodecUtils.HTTP3_PUSH_STREAM_TYPE);
53          writeVariableLengthInteger(buffer, pushId);
54          ch.write(buffer);
55  
56          Http3ConnectionHandler connectionHandler = Http3CodecUtils.getConnectionHandlerOrClose(ch.parent());
57          if (connectionHandler == null) {
58              // connection should have been closed
59              return;
60          }
61  
62          ChannelPipeline pipeline = ch.pipeline();
63          Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
64          // Add the encoder and decoder in the pipeline so we can handle Http3Frames
65          pipeline.addLast(connectionHandler.newCodec(encodeStateValidator, NO_STATE));
66          pipeline.addLast(encodeStateValidator);
67          // Add the handler that will validate what we write and receive on this stream.
68          pipeline.addLast(connectionHandler.newPushStreamValidationHandler(ch, NO_STATE));
69          initPushStream(ch);
70      }
71  
72      /**
73       * Initialize the {@link QuicStreamChannel} to handle {@link Http3PushStreamFrame}s. At the point of calling this
74       * method it is already valid to write {@link Http3PushStreamFrame}s as the codec is already in the pipeline.
75       *
76       * @param ch the {QuicStreamChannel} for the push stream.
77       */
78      protected abstract void initPushStream(QuicStreamChannel ch);
79  }