-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathHTTP2Examples.java
285 lines (217 loc) · 7.52 KB
/
HTTP2Examples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package examples;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.net.JksOptions;
/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class HTTP2Examples {
public void example0(Vertx vertx) {
HttpServerOptions options = new HttpServerOptions()
.setUseAlpn(true)
.setSsl(true)
.setKeyCertOptions(new JksOptions().setPath("/path/to/my/keystore"));
HttpServer server = vertx.createHttpServer(options);
}
public void example1(HttpServerRequest request) {
request.customFrameHandler(frame -> {
System.out.println("Received a frame type=" + frame.type() +
" payload" + frame.payload().toString());
});
}
public void example2(HttpServerResponse response) {
int frameType = 40;
int frameStatus = 10;
Buffer payload = Buffer.buffer("some data");
// Sending a frame to the client
response.writeCustomFrame(frameType, frameStatus, payload);
}
public void example3(HttpServerRequest request) {
// Reset the stream
request.response().reset();
}
public void example4(HttpServerRequest request) {
// Cancel the stream
request.response().reset(8);
}
public void example5(HttpServerRequest request) {
request.response().exceptionHandler(err -> {
if (err instanceof StreamResetException) {
StreamResetException reset = (StreamResetException) err;
System.out.println("Stream reset " + reset.getCode());
}
});
}
public void example6(HttpServerRequest request) {
HttpServerResponse response = request.response();
// Push main.js to the client
response
.push(HttpMethod.GET, "/main.js")
.onComplete(ar -> {
if (ar.succeeded()) {
// The server is ready to push the response
HttpServerResponse pushedResponse = ar.result();
// Send main.js response
pushedResponse.
putHeader("content-type", "application/json").
end("alert(\"Push response hello\")");
} else {
System.out.println("Could not push client resource " + ar.cause());
}
});
// Send the requested resource
response.sendFile("<html><head><script src=\"/main.js\"></script></head><body></body></html>");
}
public void example7(Vertx vertx) {
HttpClientOptions options = new HttpClientOptions().
setProtocolVersion(HttpVersion.HTTP_2).
setSsl(true).
setUseAlpn(true).
setTrustAll(true);
HttpClient client = vertx.createHttpClient(options);
}
public void example8(Vertx vertx) {
HttpClientOptions options = new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2);
HttpClient client = vertx.createHttpClient(options);
}
public void example9(HttpClientRequest request) {
int frameType = 40;
int frameStatus = 10;
Buffer payload = Buffer.buffer("some data");
// Sending a frame to the server
request.writeCustomFrame(frameType, frameStatus, payload);
}
public void example10(HttpClientRequest request) {
request.reset();
}
public void example11(HttpClientRequest request) {
request.reset(8);
}
public void example12(HttpClientRequest request) {
request.exceptionHandler(err -> {
if (err instanceof StreamResetException) {
StreamResetException reset = (StreamResetException) err;
System.out.println("Stream reset " + reset.getCode());
}
});
}
public void example13(HttpClient client) {
client.request(HttpMethod.GET, "/index.html")
.onSuccess(request -> {
request
.response().onComplete(response -> {
// Process index.html response
});
// Set a push handler to be aware of any resource pushed by the server
request.pushHandler(pushedRequest -> {
// A resource is pushed for this request
System.out.println("Server pushed " + pushedRequest.path());
// Set an handler for the response
pushedRequest.response().onComplete(pushedResponse -> {
System.out.println("The response for the pushed request");
});
});
// End the request
request.end();
});
}
public void example14(HttpClientRequest request) {
request.pushHandler(pushedRequest -> {
if (pushedRequest.path().equals("/main.js")) {
pushedRequest.reset();
} else {
// Handle it
}
});
}
public void example15(HttpClientResponse response) {
response.customFrameHandler(frame -> {
System.out.println("Received a frame type=" + frame.type() +
" payload" + frame.payload().toString());
});
}
public void example16(HttpServerRequest request) {
HttpConnection connection = request.connection();
}
public void example17(Vertx vertx, HttpServerOptions http2Options) {
HttpServer server = vertx.createHttpServer(http2Options);
server.connectionHandler(connection -> {
System.out.println("A client connected");
});
}
public void example18(HttpClientRequest request) {
HttpConnection connection = request.connection();
}
public void example19(Vertx vertx, HttpClientOptions options) {
vertx
.httpClientBuilder()
.with(options)
.withConnectHandler(connection -> {
System.out.println("Connected to the server");
})
.build();
}
public void example20(HttpConnection connection) {
connection.updateHttpSettings(new Http2Settings().setMaxConcurrentStreams(100));
}
public void example21(HttpConnection connection) {
connection
.updateHttpSettings(new Http2Settings().setMaxConcurrentStreams(100))
.onSuccess(v -> System.out.println("The settings update has been acknowledged "));
}
public void example22(HttpConnection connection) {
connection.remoteHttpSettingsHandler(settings -> {
System.out.println("Received new settings");
});
}
public void example23(HttpConnection connection) {
Buffer data = Buffer.buffer();
for (byte i = 0;i < 8;i++) {
data.appendByte(i);
}
connection
.ping(data)
.onSuccess(pong -> System.out.println("Remote side replied"));
}
public void example24(HttpConnection connection) {
connection.pingHandler(ping -> {
System.out.println("Got pinged by remote side");
});
}
public void example25(HttpConnection connection) {
connection.shutdown();
}
public void example26(HttpConnection connection) {
connection.goAway(0);
}
public void example27(HttpConnection connection) {
connection.goAwayHandler(goAway -> {
System.out.println("Received a go away frame");
});
}
public void example28(HttpConnection connection) {
connection.goAway(0);
connection.shutdownHandler(v -> {
// All streams are closed, close the connection
connection.close();
});
}
public void useMaxStreams(Vertx vertx) {
// Uses up to 3 connections and up to 10 streams per connection
HttpClient client = vertx.createHttpClient(
new HttpClientOptions().setHttp2MultiplexingLimit(10),
new PoolOptions().setHttp2MaxSize(3)
);
}
}