1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
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
74 final SslContext sslCtx;
75 if (SSL) {
76 SslProvider provider = isAlpnSupported(OPENSSL) ? OPENSSL : JDK;
77 sslCtx = SslContextBuilder.forClient()
78 .sslProvider(provider)
79
80
81 .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
82 .trustManager(InsecureTrustManagerFactory.INSTANCE)
83 .applicationProtocolConfig(new ApplicationProtocolConfig(
84 Protocol.ALPN,
85
86 SelectorFailureBehavior.NO_ADVERTISE,
87
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
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
109 Channel channel = b.connect().asStage().get();
110 System.out.println("Connected to [" + HOST + ':' + PORT + ']');
111
112
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
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
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
147 channel.close().asStage().sync();
148 } finally {
149 workerGroup.shutdownGracefully();
150 }
151 }
152 }