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.channel.ChannelHandler;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.handler.codec.quic.QuicStreamChannel;
22 import io.netty.util.internal.ObjectUtil;
23 import org.jetbrains.annotations.Nullable;
24
25 import java.util.function.LongFunction;
26
27
28 /**
29 * Handler that handles <a href="https://tools.ietf.org/html/draft-ietf-quic-http-32">HTTP3</a> for the server-side.
30 */
31 public final class Http3ServerConnectionHandler extends Http3ConnectionHandler {
32 private final ChannelHandler requestStreamHandler;
33
34 /**
35 * Create a new instance.
36 *
37 * @param requestStreamHandler the {@link ChannelHandler} that is used for each new request stream.
38 * This handler will receive {@link Http3HeadersFrame} and {@link Http3DataFrame}s.
39 */
40 public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler) {
41 this(requestStreamHandler, null, null, null, true);
42 }
43
44 /**
45 * Create a new instance.
46 * @param requestStreamHandler the {@link ChannelHandler} that is used for each new request stream.
47 * This handler will receive {@link Http3HeadersFrame} and
48 * {@link Http3DataFrame}s.
49 * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about
50 * {@link Http3RequestStreamFrame}s or {@code null} if the user is not
51 * interested in these.
52 * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom
53 * {@link ChannelHandler} for unknown inbound stream types or
54 * {@code null} if no special handling should be done.
55 * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the
56 * remote peer or {@code null} if the default settings should be used.
57 * @param disableQpackDynamicTable If QPACK dynamic table should be disabled.
58 */
59 public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler,
60 @Nullable ChannelHandler inboundControlStreamHandler,
61 @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
62 @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable) {
63 this(requestStreamHandler, inboundControlStreamHandler, unknownInboundStreamHandlerFactory,
64 localSettings, disableQpackDynamicTable, null);
65 }
66
67 /**
68 * Create a new instance.
69 * @param requestStreamHandler the {@link ChannelHandler} that is used for each new request stream.
70 * This handler will receive {@link Http3HeadersFrame} and
71 * {@link Http3DataFrame}s.
72 * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about
73 * {@link Http3RequestStreamFrame}s or {@code null} if the user is not
74 * interested in these.
75 * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom
76 * {@link ChannelHandler} for unknown inbound stream types or
77 * {@code null} if no special handling should be done.
78 * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the
79 * remote peer or {@code null} if the default settings should be used.
80 * @param disableQpackDynamicTable If QPACK dynamic table should be disabled.
81 * @param nonStandardSettingsValidator the {@link Http3Settings.NonStandardHttp3SettingsValidator} to
82 * use when validating settings that are non-standard.
83 */
84 public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler,
85 @Nullable ChannelHandler inboundControlStreamHandler,
86 @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
87 @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
88 @Nullable Http3Settings.NonStandardHttp3SettingsValidator
89 nonStandardSettingsValidator) {
90 super(true, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
91 disableQpackDynamicTable, nonStandardSettingsValidator);
92 this.requestStreamHandler = ObjectUtil.checkNotNull(requestStreamHandler, "requestStreamHandler");
93 }
94
95 @Override
96 void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
97 ChannelPipeline pipeline = streamChannel.pipeline();
98 Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
99 Http3RequestStreamDecodeStateValidator decodeStateValidator = new Http3RequestStreamDecodeStateValidator();
100 // Add the encoder and decoder in the pipeline so we can handle Http3Frames
101 pipeline.addLast(newCodec(encodeStateValidator, decodeStateValidator));
102 pipeline.addLast(encodeStateValidator);
103 pipeline.addLast(decodeStateValidator);
104 pipeline.addLast(newRequestStreamValidationHandler(streamChannel, encodeStateValidator, decodeStateValidator));
105 pipeline.addLast(requestStreamHandler);
106 }
107
108 @Override
109 void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
110 final long maxTableCapacity = maxTableCapacity();
111 streamChannel.pipeline().addLast(
112 new Http3UnidirectionalStreamInboundServerHandler(codecFactory, nonStandardSettingsValidator,
113 localControlStreamHandler, remoteControlStreamHandler,
114 unknownInboundStreamHandlerFactory,
115 () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
116 () -> new QpackDecoderHandler(qpackEncoder)));
117 }
118 }