1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.example.http2.helloworld.client;
16
17 import io.netty.bootstrap.Bootstrap;
18 import io.netty.buffer.Unpooled;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelOption;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.channel.socket.nio.NioSocketChannel;
24 import io.netty.handler.codec.http.DefaultFullHttpRequest;
25 import io.netty.handler.codec.http.FullHttpRequest;
26 import io.netty.handler.codec.http.HttpHeaderNames;
27 import io.netty.handler.codec.http.HttpHeaderValues;
28 import io.netty.handler.codec.http.HttpScheme;
29 import io.netty.handler.codec.http2.Http2SecurityUtil;
30 import io.netty.handler.codec.http2.HttpConversionUtil;
31 import io.netty.handler.ssl.ApplicationProtocolConfig;
32 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
33 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
34 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
35 import io.netty.handler.ssl.ApplicationProtocolNames;
36 import io.netty.handler.ssl.OpenSsl;
37 import io.netty.handler.ssl.SslContext;
38 import io.netty.handler.ssl.SslContextBuilder;
39 import io.netty.handler.ssl.SslProvider;
40 import io.netty.handler.ssl.SupportedCipherSuiteFilter;
41 import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
42 import io.netty.util.AsciiString;
43 import io.netty.util.CharsetUtil;
44
45 import java.util.concurrent.TimeUnit;
46
47 import static io.netty.buffer.Unpooled.wrappedBuffer;
48 import static io.netty.handler.codec.http.HttpMethod.GET;
49 import static io.netty.handler.codec.http.HttpMethod.POST;
50 import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
51
52
53
54
55
56
57
58
59
60
61 public final class Http2Client {
62
63 static final boolean SSL = System.getProperty("ssl") != null;
64 static final String HOST = System.getProperty("host", "127.0.0.1");
65 static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
66 static final String URL = System.getProperty("url", "/whatever");
67 static final String URL2 = System.getProperty("url2");
68 static final String URL2DATA = System.getProperty("url2data", "test data!");
69
70 public static void main(String[] args) throws Exception {
71
72 final SslContext sslCtx;
73 if (SSL) {
74 SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
75 sslCtx = SslContextBuilder.forClient()
76 .sslProvider(provider)
77
78
79 .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
80 .trustManager(InsecureTrustManagerFactory.INSTANCE)
81 .applicationProtocolConfig(new ApplicationProtocolConfig(
82 Protocol.ALPN,
83
84 SelectorFailureBehavior.NO_ADVERTISE,
85
86 SelectedListenerFailureBehavior.ACCEPT,
87 ApplicationProtocolNames.HTTP_2,
88 ApplicationProtocolNames.HTTP_1_1))
89 .build();
90 } else {
91 sslCtx = null;
92 }
93
94 EventLoopGroup workerGroup = new NioEventLoopGroup();
95 Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);
96
97 try {
98
99 Bootstrap b = new Bootstrap();
100 b.group(workerGroup);
101 b.channel(NioSocketChannel.class);
102 b.option(ChannelOption.SO_KEEPALIVE, true);
103 b.remoteAddress(HOST, PORT);
104 b.handler(initializer);
105
106
107 Channel channel = b.connect().syncUninterruptibly().channel();
108 System.out.println("Connected to [" + HOST + ':' + PORT + ']');
109
110
111 Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
112 http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
113
114 HttpResponseHandler responseHandler = initializer.responseHandler();
115 int streamId = 3;
116 HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
117 AsciiString hostName = new AsciiString(HOST + ':' + PORT);
118 System.err.println("Sending request(s)...");
119 if (URL != null) {
120
121 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL, Unpooled.EMPTY_BUFFER);
122 request.headers().add(HttpHeaderNames.HOST, hostName);
123 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
124 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
125 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
126 responseHandler.put(streamId, channel.write(request), channel.newPromise());
127 streamId += 2;
128 }
129 if (URL2 != null) {
130
131 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2,
132 wrappedBuffer(URL2DATA.getBytes(CharsetUtil.UTF_8)));
133 request.headers().add(HttpHeaderNames.HOST, hostName);
134 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
135 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
136 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
137 responseHandler.put(streamId, channel.write(request), channel.newPromise());
138 }
139 channel.flush();
140 responseHandler.awaitResponses(5, TimeUnit.SECONDS);
141 System.out.println("Finished HTTP/2 request(s)");
142
143
144 channel.close().syncUninterruptibly();
145 } finally {
146 workerGroup.shutdownGracefully();
147 }
148 }
149 }