@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. vertx.createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true)).getNow(4443, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); } }
@Test public void testHttp2() { vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setSslEngineOptions(new OpenSSLEngineOptions()) .setKeyCertOptions(Cert.SERVER_JKS.get()) ) .requestHandler(req -> req.response().end("OK")) .listen(8443, "localhost", onSuccess(s -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions() .setSsl(true) .setSslEngineOptions(new OpenSSLEngineOptions()) .setTrustStoreOptions(Trust.SERVER_JKS.get())); client.getNow(8443, "localhost", "/somepath", onSuccess(resp -> { resp.bodyHandler(buff -> { assertEquals("OK", buff.toString()); testComplete(); }); })); })); await(); } }
@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"))); }); } }
.setSsl(true) .setUseAlpn(useAlpn) .setTrustAll(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); 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(); } }
options.setReuseAddress(reuseAddress); options.setTrafficClass(trafficClass); options.setSsl(ssl); options.setTcpNoDelay(tcpNoDelay); options.setTcpKeepAlive(tcpKeepAlive);
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); }
setSsl(true). setUseAlpn(true). setProtocolVersion(HttpVersion.HTTP_2).
@Test public void testClientOptionsCopiedBeforeUse() { client.close(); server.requestHandler(req -> { req.response().end(); }); server.listen(ar -> { assertTrue(ar.succeeded()); HttpClientOptions options = new HttpClientOptions(); client = vertx.createHttpClient(options); // Now change something - but server should ignore this options.setSsl(true); client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/uri", onSuccess(res -> { assertEquals(200, res.statusCode()); testComplete(); })).end(); }); await(); }
@Test public void testCrlInvalidPath() throws Exception { HttpClientOptions clientOptions = new HttpClientOptions(); clientOptions.setTrustOptions(Trust.SERVER_PEM_ROOT_CA.get()); clientOptions.setSsl(true); clientOptions.addCrlPath("/invalid.pem"); try { vertx.createHttpClient(clientOptions); fail("Was expecting a failure"); } catch (VertxException e) { assertNotNull(e.getCause()); assertEquals(NoSuchFileException.class, e.getCause().getCause().getClass()); } }
@Override public void start() throws Exception { HttpClientRequest request = vertx.createHttpClient(new HttpClientOptions() .setSsl(true) .setTrustAll(true) .setVerifyHost(false) .setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost("localhost") .setPort(8080))) .put(8282, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); request.setChunked(true); for (int i = 0; i < 10; i++) { request.write("client-chunk-" + i); } request.end(); } }
@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 proxyTest(int error, String username, String url, Handler<AsyncResult<HttpClientResponse>> assertResponse) throws Exception { startProxy(error, username); final HttpClientOptions options = new HttpClientOptions() .setSsl(url.startsWith("https")) .setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost("localhost") .setPort(proxy.getPort())); HttpClient client = vertx.createHttpClient(options); client.getAbs(url, assertResponse).end(); await(); }
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 testNetServerInternalTLS() throws Exception { server.close(); server = vertx.createNetServer(new NetServerOptions() .setPort(1234) .setHost("localhost") .setSsl(true) .setKeyStoreOptions(Cert.SERVER_JKS.get())); testNetServerInternal_(new HttpClientOptions() .setSsl(true) .setTrustStoreOptions(Trust.SERVER_JKS.get()) , true); }
@Test public void testHttpProxyRequestOverrideClientSsl() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setSsl(true).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); testHttpProxyRequest2(handler -> client.get(new RequestOptions().setSsl(false).setHost("localhost").setPort(8080), handler)); }
Function<HttpClient, ReadStream<WebSocket>> wsProvider) throws Exception { HttpClientOptions options = new HttpClientOptions(); options.setSsl(clientSsl); options.setTrustAll(clientTrustAll); if (clientUsesCrl) {