@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(); } }
}); })); req.pushHandler(pushedReq -> { Context current = Vertx.currentContext(); if (ctx.get() == null) {
@Test public void testResetPendingPushPromise() throws Exception { server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertFalse(ar.succeeded()); testComplete(); }); }); startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L))); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(resp -> {})); req.pushHandler(pushedReq -> { pushedReq.reset(Http2Error.CANCEL.code()); }); req.end(); await(); }
@Test public void testResetActivePushPromise() throws Exception { server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertTrue(ar.succeeded()); HttpServerResponse response = ar.result(); response.exceptionHandler(err -> { if (err instanceof StreamResetException) { assertEquals(Http2Error.CANCEL.code(), ((StreamResetException) err).getCode()); testComplete(); } }); response.setChunked(true).write("some_content"); }); }); startServer(); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(resp -> { })); req.pushHandler(pushedReq -> { pushedReq.handler(onSuccess(pushedResp -> { pushedResp.handler(buff -> { pushedReq.reset(Http2Error.CANCEL.code()); }); })); }); req.end(); await(); }
complete(); })); request.pushHandler(pushReq -> { complete(); pushReq.handler(onSuccess(pushResp -> {
complete(); }); request.pushHandler(pushReq -> { complete(); pushReq.handler(onSuccess(pushResp -> {
HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }); req.pushHandler(pushedReq -> { HttpClientMetric metric = metrics.getMetric(pushedReq); assertNotNull(metric);
/** * Set a push handler for this request.<p/> * * The handler is called when the client receives a <i>push promise</i> from the server. The handler can be called * multiple times, for each push promise.<p/> * * The handler is called with a <i>read-only</i> {@link io.vertx.rxjava.core.http.HttpClientRequest}, the following methods can be called:<p/> * * <ul> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#method}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#uri}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#headers}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#getHost}</li> * </ul> * * In addition the handler should call the {@link io.vertx.rxjava.core.http.HttpClientRequest#handler} method to set an handler to * process the response.<p/> * @param handler the handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.http.HttpClientRequest pushHandler(Handler<io.vertx.rxjava.core.http.HttpClientRequest> handler) { delegate.pushHandler(new Handler<io.vertx.core.http.HttpClientRequest>() { public void handle(io.vertx.core.http.HttpClientRequest event) { handler.handle(io.vertx.rxjava.core.http.HttpClientRequest.newInstance(event)); } }); return this; }
/** * Set a push handler for this request.<p/> * * The handler is called when the client receives a <i>push promise</i> from the server. The handler can be called * multiple times, for each push promise.<p/> * * The handler is called with a <i>read-only</i> {@link io.vertx.rxjava.core.http.HttpClientRequest}, the following methods can be called:<p/> * * <ul> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#method}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#uri}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#headers}</li> * <li>{@link io.vertx.rxjava.core.http.HttpClientRequest#getHost}</li> * </ul> * * In addition the handler should call the {@link io.vertx.rxjava.core.http.HttpClientRequest#handler} method to set an handler to * process the response.<p/> * @param handler the handler * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.http.HttpClientRequest pushHandler(Handler<io.vertx.rxjava.core.http.HttpClientRequest> handler) { delegate.pushHandler(new Handler<io.vertx.core.http.HttpClientRequest>() { public void handle(io.vertx.core.http.HttpClientRequest event) { handler.handle(io.vertx.rxjava.core.http.HttpClientRequest.newInstance(event)); } }); return this; }
}); }); req.pushHandler(pushedReq -> { Context current = Vertx.currentContext(); if (ctx.get() == null) {
})); CountDownLatch latch = new CountDownLatch(2); request.pushHandler(pushedReq -> pushedReq.handler(onSuccess(pushedResp -> { assertNotNull(pushedResp); pushedResp.bodyHandler(this::assertNotNull);
@Test public void testResetPendingPushPromise() throws Exception { server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertFalse(ar.succeeded()); testComplete(); }); }); startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L))); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { fail(); }); req.pushHandler(pushedReq -> { pushedReq.reset(Http2Error.CANCEL.code()); }); req.end(); await(); }
@Test public void testResetActivePushPromise() throws Exception { server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertTrue(ar.succeeded()); HttpServerResponse response = ar.result(); response.exceptionHandler(err -> { if (err instanceof StreamResetException) { assertEquals(Http2Error.CANCEL.code(), ((StreamResetException) err).getCode()); testComplete(); } }); response.setChunked(true).write("some_content"); }); }); startServer(); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { fail(); }); req.pushHandler(pushedReq -> { pushedReq.handler(pushedResp -> { pushedResp.handler(buff -> { pushedReq.reset(Http2Error.CANCEL.code()); }); }); }); req.end(); await(); }
complete(); }); request.pushHandler(pushReq -> { complete(); pushReq.handler(pushResp -> {
complete(); }); request.pushHandler(pushReq -> { complete(); pushReq.handler(pushResp -> {
HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }); req.pushHandler(pushedReq -> { HttpClientMetric metric = metrics.getMetric(pushedReq); assertNotNull(metric);
@Test public void testNoHttp2Push() throws Exception { stat.setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))); http2Server.requestHandler(router).listen(8443); HttpClientOptions options = new HttpClientOptions() .setSsl(true) .setUseAlpn(true) .setProtocolVersion(HttpVersion.HTTP_2) .setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem")); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(HttpVersion.HTTP_2, resp.version()); resp.bodyHandler(this::assertNotNull); testComplete(); })); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { fail(); })); request.end(); await(); }
}); CountDownLatch latch = new CountDownLatch(2); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { assertNotNull(pushedResp); pushedResp.bodyHandler(this::assertNotNull);
@Test public void testNoHttp2Push() throws Exception { stat.setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))); http2Server.requestHandler(router).listen(8443); HttpClientOptions options = new HttpClientOptions() .setSsl(true) .setUseAlpn(true) .setProtocolVersion(HttpVersion.HTTP_2) .setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem")); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> { assertEquals(200, resp.statusCode()); assertEquals(HttpVersion.HTTP_2, resp.version()); resp.bodyHandler(this::assertNotNull); testComplete(); }); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { fail(); })); request.end(); await(); }