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.handler.codec.quic.QuicStreamChannel;
21 import org.jetbrains.annotations.Nullable;
22
23 import java.util.function.LongFunction;
24
25 public class Http3ClientConnectionHandler extends Http3ConnectionHandler {
26
27 private final LongFunction<ChannelHandler> pushStreamHandlerFactory;
28
29 /**
30 * Create a new instance.
31 */
32 public Http3ClientConnectionHandler() {
33 this(null, null, null, null, true);
34 }
35
36 /**
37 * Create a new instance.
38 *
39 * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about
40 * {@link Http3RequestStreamFrame}s or {@code null} if the user is not
41 * interested in these.
42 * @param pushStreamHandlerFactory the {@link LongFunction} that will provide a custom
43 * {@link ChannelHandler} for push streams {@code null} if no special
44 * handling should be done. When present, push ID will be passed as an
45 * argument to the {@link LongFunction}.
46 * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom
47 * {@link ChannelHandler} for unknown inbound stream types or
48 * {@code null} if no special handling should be done.
49 * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the
50 * remote peer or {@code null} if the default settings should be used.
51 * @param disableQpackDynamicTable If QPACK dynamic table should be disabled.
52 */
53 public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler,
54 @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory,
55 @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
56 @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable) {
57 this(inboundControlStreamHandler, pushStreamHandlerFactory, unknownInboundStreamHandlerFactory, localSettings,
58 disableQpackDynamicTable, null);
59 }
60
61 /**
62 * Create a new instance.
63 *
64 * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about
65 * {@link Http3RequestStreamFrame}s or {@code null} if the user is not
66 * interested in these.
67 * @param pushStreamHandlerFactory the {@link LongFunction} that will provide a custom
68 * {@link ChannelHandler} for push streams {@code null} if no special
69 * handling should be done. When present, push ID will be passed as an
70 * argument to the {@link LongFunction}.
71 * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom
72 * {@link ChannelHandler} for unknown inbound stream types or
73 * {@code null} if no special handling should be done.
74 * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the
75 * remote peer or {@code null} if the default settings should be used.
76 * @param disableQpackDynamicTable If QPACK dynamic table should be disabled.
77 * @param nonStandardSettingsValidator the {@link Http3Settings.NonStandardHttp3SettingsValidator} to use
78 * when validating settings that are non-standard.
79 */
80 public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler,
81 @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory,
82 @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
83 @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
84 @Nullable Http3Settings.NonStandardHttp3SettingsValidator
85 nonStandardSettingsValidator) {
86 this(inboundControlStreamHandler, pushStreamHandlerFactory, unknownInboundStreamHandlerFactory,
87 localSettings, disableQpackDynamicTable, nonStandardSettingsValidator, null,
88 Http3CodecUtils.DEFAULT_MAX_UNKNOWN_FRAME_PAYLOAD_LENGTH);
89 }
90
91 /**
92 * Create a new instance.
93 *
94 * @param inboundControlStreamHandler the {@link ChannelHandler} which will be notified about
95 * {@link Http3RequestStreamFrame}s or {@code null} if the user is not
96 * interested in these.
97 * @param pushStreamHandlerFactory the {@link LongFunction} that will provide a custom
98 * {@link ChannelHandler} for push streams {@code null} if no special
99 * handling should be done. When present, push ID will be passed as an
100 * argument to the {@link LongFunction}.
101 * @param unknownInboundStreamHandlerFactory the {@link LongFunction} that will provide a custom
102 * {@link ChannelHandler} for unknown inbound stream types or
103 * {@code null} if no special handling should be done.
104 * @param localSettings the local {@link Http3SettingsFrame} that should be sent to the
105 * remote peer or {@code null} if the default settings should be used.
106 * @param disableQpackDynamicTable If QPACK dynamic table should be disabled.
107 * @param nonStandardSettingsValidator the {@link Http3Settings.NonStandardHttp3SettingsValidator} to use
108 * when validating settings that are non-standard.
109 * @param sensitivityDetector detector that marks sensitive headers for QPACK Never Indexed
110 * encoding, or {@code null} to use the historical default.
111 * @param maxUnknownFramePayloadLength the maximum payload size of an unknown frame.
112 */
113 public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler,
114 @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory,
115 @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
116 @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
117 @Nullable Http3Settings.NonStandardHttp3SettingsValidator
118 nonStandardSettingsValidator,
119 @Nullable QpackSensitivityDetector sensitivityDetector,
120 int maxUnknownFramePayloadLength) {
121 super(false, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
122 disableQpackDynamicTable, nonStandardSettingsValidator, sensitivityDetector,
123 maxUnknownFramePayloadLength);
124 this.pushStreamHandlerFactory = pushStreamHandlerFactory;
125 }
126
127 @Override
128 protected void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel channel) {
129 // See https://datatracker.ietf.org/doc/html/rfc9114#name-bidirectional-streams
130 // https://datatracker.ietf.org/doc/html/rfc9114#section-6.1-3
131 Http3CodecUtils.connectionError(ctx, Http3ErrorCode.H3_STREAM_CREATION_ERROR,
132 "Server initiated bidirectional streams are not allowed", true);
133 }
134
135 @Override
136 protected void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
137 final long maxTableCapacity = maxTableCapacity();
138 streamChannel.pipeline().addLast(
139 new Http3UnidirectionalStreamInboundClientHandler(codecFactory, nonStandardSettingsValidator,
140 localControlStreamHandler, remoteControlStreamHandler,
141 unknownInboundStreamHandlerFactory, pushStreamHandlerFactory,
142 () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
143 () -> new QpackDecoderHandler(qpackEncoder)));
144 }
145 }