@Override HttpClient createHttpClient(HttpClientOptions options) { return vertx.createHttpClient(options.setUseAlpn(true)); }
@Override public void start() throws Exception { // Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks. HttpClientOptions options = new HttpClientOptions(). setSsl(true). setUseAlpn(true). setProtocolVersion(HttpVersion.HTTP_2). setTrustAll(true); vertx.createHttpClient(options ).getNow(8443, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); } }
@Override public void start() throws Exception { // Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks. HttpClientOptions options = new HttpClientOptions(). setSsl(true). setUseAlpn(true). setProtocolVersion(HttpVersion.HTTP_2). setTrustAll(true); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); // Set handler for server side push request.pushHandler(pushedReq -> { System.out.println("Receiving pushed content"); pushedReq.handler(pushedResp -> { pushedResp.bodyHandler(body -> System.out.println("Got pushed data " + body.toString("ISO-8859-1"))); }); }); request.end(); } }
setUseAlpn(true). setProtocolVersion(HttpVersion.HTTP_2). setTrustAll(true);
static HttpClientOptions createHttp2ClientOptions() { return new HttpClientOptions(). setUseAlpn(true). setSsl(true). setTrustStoreOptions(Trust.SERVER_JKS.get()). setProtocolVersion(HttpVersion.HTTP_2); }
@Override public void setUp() throws Exception { eventLoopGroups.clear(); super.setUp(); clientOptions = new HttpClientOptions(). setUseAlpn(true). setSsl(true). setTrustStoreOptions(Trust.SERVER_JKS.get()). setProtocolVersion(HttpVersion.HTTP_2); client = vertx.createHttpClient(clientOptions); }
@Test public void testClientMakeRequestHttp2WithSSLWithoutAlpn() throws Exception { client.close(); client = vertx.createHttpClient(createBaseClientOptions().setUseAlpn(false)); try { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI); fail(); } catch (IllegalArgumentException ignore) { // Expected } }
@Test public void testIdleTimeoutClearTextUpgrade() throws Exception { testIdleTimeout(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTPS_HOST), clientOptions.setDefaultPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(true)); }
@Test public void testIdleTimeoutClearTextDirect() throws Exception { testIdleTimeout(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTPS_HOST), clientOptions.setDefaultPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(false)); }
private void testClearText(boolean upgrade) throws Exception { ServerBootstrap bootstrap = createH2CServer((dec, enc) -> new Http2EventAdapter() { @Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception { enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise()); ctx.flush(); } }, upgrade); ChannelFuture s = bootstrap.bind(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT).sync(); try { client.close(); client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(upgrade)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp1 -> { HttpConnection conn = resp1.request().connection(); assertEquals(HttpVersion.HTTP_2, resp1.version()); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp2 -> { assertSame(((HttpClientConnection)conn).channel(), ((HttpClientConnection)resp2.request().connection()).channel()); testComplete(); })).exceptionHandler(this::fail).end(); })).exceptionHandler(this::fail).end(); await(); } finally { s.channel().close().sync(); } }
startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(400, resp.statusCode());
@Test public void testClientDoesNotSupportAlpn() throws Exception { waitFor(2); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_1, req.version()); req.response().end(); complete(); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setProtocolVersion(HttpVersion.HTTP_1_1).setUseAlpn(false)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(HttpVersion.HTTP_1_1, resp.version()); complete(); })).exceptionHandler(this::fail).end(); await(); }
options.setUseAlpn(true);
options.setMaxWaitQueueSize(maxWaitQueueSize); options.setInitialSettings(initialSettings); options.setUseAlpn(useAlpn); options.setSslEngineOptions(sslEngine); options.setAlpnVersions(alpnVersions);
.setSslEngineOptions(engine) .setSsl(true) .setUseAlpn(useAlpn) .setTrustAll(true) .setProtocolVersion(version));
@Override public void setUp() throws Exception { eventLoopGroups.clear(); super.setUp(); clientOptions = new HttpClientOptions(). setUseAlpn(true). setSsl(true). setTrustStoreOptions(Trust.SERVER_JKS.get()). setProtocolVersion(HttpVersion.HTTP_2); client = vertx.createHttpClient(clientOptions); }
@Test public void testClientMakeRequestHttp2WithSSLWithoutAlpn() throws Exception { client.close(); client = vertx.createHttpClient(createBaseClientOptions().setUseAlpn(false)); try { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI); fail(); } catch (IllegalArgumentException ignore) { // Expected } }
@Test public void testIdleTimeoutClearTextUpgrade() throws Exception { testIdleTimeout(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTPS_HOST), clientOptions.setDefaultPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(true)); }
@Test public void testIdleTimeoutClearTextDirect() throws Exception { testIdleTimeout(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTPS_HOST), clientOptions.setDefaultPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setHttp2ClearTextUpgrade(false)); }
assertEquals(options, options.setUseAlpn(true)); assertEquals(true, options.isUseAlpn());