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