1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.testsuite.http2;
18
19 import io.netty5.channel.ChannelHandler;
20 import io.netty5.channel.ChannelHandlerContext;
21 import io.netty5.channel.ChannelInitializer;
22 import io.netty5.channel.ChannelPipeline;
23 import io.netty5.channel.SimpleChannelInboundHandler;
24 import io.netty5.channel.socket.SocketChannel;
25 import io.netty5.handler.codec.http.HttpMessage;
26 import io.netty5.handler.codec.http.HttpObjectAggregator;
27 import io.netty5.handler.codec.http.HttpServerCodec;
28 import io.netty5.handler.codec.http.HttpServerUpgradeHandler;
29 import io.netty5.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory;
30 import io.netty5.handler.codec.http2.CleartextHttp2ServerUpgradeHandler;
31 import io.netty5.handler.codec.http2.Http2CodecUtil;
32 import io.netty5.handler.codec.http2.Http2ServerUpgradeCodec;
33 import io.netty5.util.AsciiString;
34 import io.netty5.util.ReferenceCountUtil;
35
36 import static io.netty5.util.internal.ObjectUtil.checkPositiveOrZero;
37
38
39
40
41
42 public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
43
44 private static final UpgradeCodecFactory upgradeCodecFactory = protocol -> {
45 if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
46 return new Http2ServerUpgradeCodec(new HelloWorldHttp2HandlerBuilder().build());
47 } else {
48 return null;
49 }
50 };
51
52 private final int maxHttpContentLength;
53
54 Http2ServerInitializer() {
55 this(16 * 1024);
56 }
57
58 Http2ServerInitializer(int maxHttpContentLength) {
59 checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
60 this.maxHttpContentLength = maxHttpContentLength;
61 }
62
63 @Override
64 public void initChannel(SocketChannel ch) {
65 configureClearText(ch);
66 }
67
68
69
70
71 private void configureClearText(SocketChannel ch) {
72 final ChannelPipeline p = ch.pipeline();
73 final HttpServerCodec sourceCodec = new HttpServerCodec();
74 final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
75 final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler =
76 new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler,
77 new HelloWorldHttp2HandlerBuilder().build());
78
79 p.addLast(cleartextHttp2ServerUpgradeHandler);
80 p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
81 @Override
82 protected void messageReceived(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
83
84 System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
85 ChannelPipeline pipeline = ctx.pipeline();
86 ChannelHandlerContext thisCtx = pipeline.context(this);
87 pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
88 pipeline.addAfter(thisCtx.name(), null, new HttpObjectAggregator(maxHttpContentLength));
89 ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
90 pipeline.remove(this);
91 }
92 });
93
94 p.addLast(new UserEventLogger());
95 }
96
97
98
99
100 private static class UserEventLogger implements ChannelHandler {
101 @Override
102 public void channelInboundEvent(ChannelHandlerContext ctx, Object evt) {
103 System.out.println("User Event Triggered: " + evt);
104 ctx.fireChannelInboundEvent(evt);
105 }
106 }
107 }