View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty5.example.http2.helloworld.client;
16  
17  import io.netty5.bootstrap.Bootstrap;
18  import io.netty5.channel.Channel;
19  import io.netty5.channel.ChannelOption;
20  import io.netty5.channel.EventLoopGroup;
21  import io.netty5.channel.MultithreadEventLoopGroup;
22  import io.netty5.channel.nio.NioHandler;
23  import io.netty5.channel.socket.nio.NioSocketChannel;
24  import io.netty5.handler.codec.http.DefaultFullHttpRequest;
25  import io.netty5.handler.codec.http.FullHttpRequest;
26  import io.netty5.handler.codec.http.HttpHeaderNames;
27  import io.netty5.handler.codec.http.HttpHeaderValues;
28  import io.netty5.handler.codec.http.HttpScheme;
29  import io.netty5.handler.codec.http2.Http2SecurityUtil;
30  import io.netty5.handler.codec.http2.HttpConversionUtil;
31  import io.netty5.handler.ssl.ApplicationProtocolConfig;
32  import io.netty5.handler.ssl.ApplicationProtocolConfig.Protocol;
33  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
34  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
35  import io.netty5.handler.ssl.ApplicationProtocolNames;
36  import io.netty5.handler.ssl.SslContext;
37  import io.netty5.handler.ssl.SslContextBuilder;
38  import io.netty5.handler.ssl.SslProvider;
39  import io.netty5.handler.ssl.SupportedCipherSuiteFilter;
40  import io.netty5.handler.ssl.util.InsecureTrustManagerFactory;
41  import io.netty5.util.AsciiString;
42  import io.netty5.util.CharsetUtil;
43  
44  import java.util.concurrent.TimeUnit;
45  
46  import static io.netty5.handler.codec.http.HttpMethod.GET;
47  import static io.netty5.handler.codec.http.HttpMethod.POST;
48  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
49  import static io.netty5.handler.ssl.SslProvider.JDK;
50  import static io.netty5.handler.ssl.SslProvider.OPENSSL;
51  import static io.netty5.handler.ssl.SslProvider.isAlpnSupported;
52  
53  /**
54   * An HTTP2 client that allows you to send HTTP2 frames to a server using HTTP1-style approaches
55   * (via {@link io.netty5.handler.codec.http2.InboundHttp2ToHttpAdapter}). Inbound and outbound
56   * frames are logged.
57   * When run from the command-line, sends a single HEADERS frame to the server and gets back
58   * a "Hello World" response.
59   * See the ./http2/helloworld/frame/client/ example for a HTTP2 client example which does not use
60   * HTTP1-style objects and patterns.
61   */
62  public final class Http2Client {
63  
64      static final boolean SSL = System.getProperty("ssl") != null;
65      static final String HOST = System.getProperty("host", "127.0.0.1");
66      static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
67      static final String URL = System.getProperty("url", "/whatever");
68      static final String URL2 = System.getProperty("url2");
69      static final String URL2DATA = System.getProperty("url2data", "test data!");
70      static final byte[] URL_2_DATA_BYTES = URL2DATA.getBytes(CharsetUtil.UTF_8);
71  
72      public static void main(String[] args) throws Exception {
73          // Configure SSL.
74          final SslContext sslCtx;
75          if (SSL) {
76              SslProvider provider = isAlpnSupported(OPENSSL) ? OPENSSL : JDK;
77              sslCtx = SslContextBuilder.forClient()
78                  .sslProvider(provider)
79                  /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
80                   * Please refer to the HTTP/2 specification for cipher requirements. */
81                  .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
82                  .trustManager(InsecureTrustManagerFactory.INSTANCE)
83                  .applicationProtocolConfig(new ApplicationProtocolConfig(
84                      Protocol.ALPN,
85                      // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
86                      SelectorFailureBehavior.NO_ADVERTISE,
87                      // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
88                      SelectedListenerFailureBehavior.ACCEPT,
89                      ApplicationProtocolNames.HTTP_2,
90                      ApplicationProtocolNames.HTTP_1_1))
91                  .build();
92          } else {
93              sslCtx = null;
94          }
95  
96          EventLoopGroup workerGroup = new MultithreadEventLoopGroup(NioHandler.newFactory());
97          Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);
98  
99          try {
100             // Configure the client.
101             Bootstrap b = new Bootstrap();
102             b.group(workerGroup);
103             b.channel(NioSocketChannel.class);
104             b.option(ChannelOption.SO_KEEPALIVE, true);
105             b.remoteAddress(HOST, PORT);
106             b.handler(initializer);
107 
108             // Start the client.
109             Channel channel = b.connect().asStage().get();
110             System.out.println("Connected to [" + HOST + ':' + PORT + ']');
111 
112             // Wait for the HTTP/2 upgrade to occur.
113             Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
114             http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
115 
116             HttpResponseHandler responseHandler = initializer.responseHandler();
117             int streamId = 3;
118             HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
119             AsciiString hostName = new AsciiString(HOST + ':' + PORT);
120             System.err.println("Sending request(s)...");
121             if (URL != null) {
122                 // Create a simple GET request.
123                 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL,
124                         channel.bufferAllocator().allocate(0));
125                 request.headers().add(HttpHeaderNames.HOST, hostName);
126                 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
127                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
128                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
129                 responseHandler.put(streamId, channel.write(request), channel.newPromise());
130                 streamId += 2;
131             }
132             if (URL2 != null) {
133                 // Create a simple POST request with a body.
134                 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2,
135                         channel.bufferAllocator().allocate(URL_2_DATA_BYTES.length).writeBytes(URL_2_DATA_BYTES));
136                 request.headers().add(HttpHeaderNames.HOST, hostName);
137                 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
138                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
139                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
140                 responseHandler.put(streamId, channel.write(request), channel.newPromise());
141             }
142             channel.flush();
143             responseHandler.awaitResponses(5, TimeUnit.SECONDS);
144             System.out.println("Finished HTTP/2 request(s)");
145 
146             // Wait until the connection is closed.
147             channel.close().asStage().sync();
148         } finally {
149             workerGroup.shutdownGracefully();
150         }
151     }
152 }