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