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