1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.example.http2.helloworld.frame.server;
16
17 import io.netty5.channel.ChannelHandlerContext;
18 import io.netty5.example.http2.helloworld.server.HelloWorldHttp1Handler;
19 import io.netty5.handler.codec.http.HttpObjectAggregator;
20 import io.netty5.handler.codec.http.HttpServerCodec;
21 import io.netty5.handler.codec.http2.Http2FrameCodecBuilder;
22 import io.netty5.handler.ssl.ApplicationProtocolNames;
23 import io.netty5.handler.ssl.ApplicationProtocolNegotiationHandler;
24
25
26
27
28
29 public class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler {
30
31 private static final int MAX_CONTENT_LENGTH = 1024 * 100;
32
33 protected Http2OrHttpHandler() {
34 super(ApplicationProtocolNames.HTTP_1_1);
35 }
36
37 @Override
38 protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
39 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
40 ctx.pipeline().addLast(Http2FrameCodecBuilder.forServer().build(), new HelloWorldHttp2Handler());
41 return;
42 }
43
44 if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
45 ctx.pipeline().addLast(new HttpServerCodec(),
46 new HttpObjectAggregator(MAX_CONTENT_LENGTH),
47 new HelloWorldHttp1Handler("ALPN Negotiation"));
48 return;
49 }
50
51 throw new IllegalStateException("unknown protocol: " + protocol);
52 }
53 }