View Javadoc
1   /*
2    * Copyright 2016 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  
17  package io.netty5.example.http2.helloworld.multiplex.server;
18  
19  import io.netty5.bootstrap.ServerBootstrap;
20  import io.netty5.channel.Channel;
21  import io.netty5.channel.ChannelOption;
22  import io.netty5.channel.EventLoopGroup;
23  import io.netty5.channel.MultithreadEventLoopGroup;
24  import io.netty5.channel.nio.NioHandler;
25  import io.netty5.channel.socket.nio.NioServerSocketChannel;
26  import io.netty5.handler.codec.http2.Http2SecurityUtil;
27  import io.netty5.handler.logging.LogLevel;
28  import io.netty5.handler.logging.LoggingHandler;
29  import io.netty5.handler.ssl.ApplicationProtocolConfig;
30  import io.netty5.handler.ssl.ApplicationProtocolConfig.Protocol;
31  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
32  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
33  import io.netty5.handler.ssl.ApplicationProtocolNames;
34  import io.netty5.handler.ssl.SslContext;
35  import io.netty5.handler.ssl.SslContextBuilder;
36  import io.netty5.handler.ssl.SslProvider;
37  import io.netty5.handler.ssl.SupportedCipherSuiteFilter;
38  import io.netty5.handler.ssl.util.SelfSignedCertificate;
39  
40  import static io.netty5.handler.ssl.SslProvider.JDK;
41  import static io.netty5.handler.ssl.SslProvider.OPENSSL;
42  import static io.netty5.handler.ssl.SslProvider.isAlpnSupported;
43  
44  /**
45   * An HTTP/2 Server that responds to requests with a Hello World. Once started, you can test the
46   * server with the example client.
47   *
48   * <p>This example is making use of the "multiplexing" http2 API, where streams are mapped to child
49   * Channels. This API is very experimental and incomplete.
50   */
51  public final class Http2Server {
52  
53      static final boolean SSL = System.getProperty("ssl") != null;
54  
55      static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
56  
57      public static void main(String[] args) throws Exception {
58          // Configure SSL.
59          final SslContext sslCtx;
60          if (SSL) {
61              SslProvider provider = isAlpnSupported(OPENSSL) ? OPENSSL : JDK;
62              SelfSignedCertificate ssc = new SelfSignedCertificate();
63              sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
64                  .sslProvider(provider)
65                  /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
66                   * Please refer to the HTTP/2 specification for cipher requirements. */
67                  .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
68                  .applicationProtocolConfig(new ApplicationProtocolConfig(
69                      Protocol.ALPN,
70                      // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
71                      SelectorFailureBehavior.NO_ADVERTISE,
72                      // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
73                      SelectedListenerFailureBehavior.ACCEPT,
74                      ApplicationProtocolNames.HTTP_2,
75                      ApplicationProtocolNames.HTTP_1_1))
76                  .build();
77          } else {
78              sslCtx = null;
79          }
80          // Configure the server.
81          EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
82          try {
83              ServerBootstrap b = new ServerBootstrap();
84              b.option(ChannelOption.SO_BACKLOG, 1024);
85              b.group(group)
86               .channel(NioServerSocketChannel.class)
87               .handler(new LoggingHandler(LogLevel.INFO))
88               .childHandler(new Http2ServerInitializer(sslCtx));
89  
90              Channel ch = b.bind(PORT).asStage().get();
91  
92              System.err.println("Open your HTTP/2-enabled web browser and navigate to " +
93                      (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
94  
95              ch.closeFuture().asStage().sync();
96          } finally {
97              group.shutdownGracefully();
98          }
99      }
100 }