diff --git a/src/main/java/io/vertx/core/http/HttpClientRequest.java b/src/main/java/io/vertx/core/http/HttpClientRequest.java index bcfe6d0ce..c040990df 100644 --- a/src/main/java/io/vertx/core/http/HttpClientRequest.java +++ b/src/main/java/io/vertx/core/http/HttpClientRequest.java @@ -239,15 +239,14 @@ public interface HttpClientRequest extends WriteStream, Future sendHead(); /** * Like {@link #sendHead()} but with an handler after headers have been sent. The handler will be called with * the {@link HttpVersion} if it can be determined or null otherwise.

*/ @Fluent - HttpClientRequest sendHead(Handler completionHandler); + HttpClientRequest sendHead(Handler> completionHandler); /** * Same as {@link #end(Buffer)} but writes a String in UTF-8 encoding diff --git a/src/main/java/io/vertx/core/http/impl/HttpClientRequestImpl.java b/src/main/java/io/vertx/core/http/impl/HttpClientRequestImpl.java index 7b726d0b0..95572cd0d 100644 --- a/src/main/java/io/vertx/core/http/impl/HttpClientRequestImpl.java +++ b/src/main/java/io/vertx/core/http/impl/HttpClientRequestImpl.java @@ -264,12 +264,14 @@ public class HttpClientRequestImpl extends HttpClientRequestBase implements Http } @Override - public HttpClientRequest sendHead() { - return sendHead(null); + public Future sendHead() { + Promise promise = context.promise(); + sendHead(promise); + return promise.future(); } @Override - public synchronized HttpClientRequest sendHead(Handler headersHandler) { + public synchronized HttpClientRequest sendHead(Handler> headersHandler) { checkEnded(); checkResponseHandler(); if (stream != null) { @@ -413,7 +415,7 @@ public class HttpClientRequestImpl extends HttpClientRequestBase implements Http return hostHeader != null ? hostHeader : super.hostHeader(); } - private synchronized void connect(Handler headersHandler) { + private synchronized void connect(Handler> headersHandler) { if (!connecting) { if (method == HttpMethod.OTHER && rawMethod == null) { @@ -457,7 +459,7 @@ public class HttpClientRequestImpl extends HttpClientRequestBase implements Http } } - private void connected(Handler headersHandler, HttpClientStream stream) { + private void connected(Handler> headersHandler, HttpClientStream stream) { synchronized (this) { this.stream = stream; @@ -481,17 +483,22 @@ public class HttpClientRequestImpl extends HttpClientRequestBase implements Http }; } } + if (headersHandler != null) { + Handler> others = handler; + handler = ar -> { + if (others != null) { + others.handle(ar); + } + headersHandler.handle(ar.map(stream.version())); + }; + } stream.writeHead(method, rawMethod, uri, headers, hostHeader(), chunked, pending, ended, priority, handler); if (ended) { - // we also need to write the head so optimize this and write all out in once tryComplete(); } this.connecting = false; this.stream = stream; } - if (headersHandler != null) { - headersHandler.handle(stream.version()); - } } @Override diff --git a/src/main/java/io/vertx/core/http/impl/HttpClientRequestPushPromise.java b/src/main/java/io/vertx/core/http/impl/HttpClientRequestPushPromise.java index bc94650a1..aba38525d 100644 --- a/src/main/java/io/vertx/core/http/impl/HttpClientRequestPushPromise.java +++ b/src/main/java/io/vertx/core/http/impl/HttpClientRequestPushPromise.java @@ -196,12 +196,12 @@ class HttpClientRequestPushPromise extends HttpClientRequestBase { } @Override - public HttpClientRequest sendHead() { + public Future sendHead() { throw new IllegalStateException(); } @Override - public HttpClientRequest sendHead(Handler completionHandler) { + public HttpClientRequest sendHead(Handler> completionHandler) { throw new IllegalStateException(); } diff --git a/src/test/java/io/vertx/core/http/Http1xTest.java b/src/test/java/io/vertx/core/http/Http1xTest.java index a11597043..968e501a9 100644 --- a/src/test/java/io/vertx/core/http/Http1xTest.java +++ b/src/test/java/io/vertx/core/http/Http1xTest.java @@ -4429,9 +4429,10 @@ public class Http1xTest extends HttpTest { startServer(testAddress); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); - HttpClientRequest req1 = client.request(HttpMethod.GET, testAddress, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { + HttpClientRequest req = client.request(HttpMethod.GET, testAddress, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { complete(); - }).sendHead(); + }); + req.sendHead(); client.request(HttpMethod.GET, testAddress, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { complete(); }).end(); @@ -4441,7 +4442,7 @@ public class Http1xTest extends HttpTest { // Need to wait a little so requests 2 and 3 are appended to the first request Thread.sleep(300); // This will end request 1 and make requests 2 and 3 progress - req1.end(); + req.end(); await(); } diff --git a/src/test/java/io/vertx/core/http/Http2ClientTest.java b/src/test/java/io/vertx/core/http/Http2ClientTest.java index 3f4b63190..a25273d3b 100644 --- a/src/test/java/io/vertx/core/http/Http2ClientTest.java +++ b/src/test/java/io/vertx/core/http/Http2ClientTest.java @@ -1460,11 +1460,11 @@ public class Http2ClientTest extends Http2TestBase { testComplete(); }); })); - req.sendHead(version -> { + req.sendHead(onSuccess(version -> { assertEquals(0, status.getAndIncrement()); assertSame(HttpVersion.HTTP_2, version); req.end(); - }); + })); await(); } @@ -1497,11 +1497,11 @@ public class Http2ClientTest extends Http2TestBase { testComplete(); }); })); - req.sendHead(version -> { + req.sendHead(onSuccess(version -> { assertSame(HttpVersion.HTTP_2, version); req.writeCustomFrame(10, 253, expectedSend); req.end(); - }); + })); await(); } diff --git a/src/test/java/io/vertx/core/http/Http2Test.java b/src/test/java/io/vertx/core/http/Http2Test.java index 441d514ed..281a48e4c 100644 --- a/src/test/java/io/vertx/core/http/Http2Test.java +++ b/src/test/java/io/vertx/core/http/Http2Test.java @@ -119,7 +119,9 @@ public class Http2Test extends HttpTest { HttpClientRequest req = client.request(HttpMethod.GET, testAddress, 8080, "localhost", "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); - })).setChunked(true).sendHead(); + })) + .setChunked(true); + req.sendHead(); awaitLatch(latch2); // The next write won't be buffered req.write("hello "); req.end("world"); diff --git a/src/test/java/io/vertx/core/http/HttpTest.java b/src/test/java/io/vertx/core/http/HttpTest.java index 7099f2e80..586eabb7e 100644 --- a/src/test/java/io/vertx/core/http/HttpTest.java +++ b/src/test/java/io/vertx/core/http/HttpTest.java @@ -96,7 +96,6 @@ public abstract class HttpTest extends HttpTestBase { server.listen(testAddress, onSuccess(server -> { HttpClientRequest req = client.request(HttpMethod.PUT, testAddress, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); assertTrue(req.setChunked(true) == req); - assertTrue(req.sendHead() == req); testComplete(); })); @@ -4088,8 +4087,8 @@ public abstract class HttpTest extends HttpTestBase { public void write(String chunk, Handler> handler) { throw new UnsupportedOperationException(); } public void write(String chunk, String enc, Handler> handler) { throw new UnsupportedOperationException(); } public HttpClientRequest continueHandler(@Nullable Handler handler) { throw new UnsupportedOperationException(); } - public HttpClientRequest sendHead() { throw new UnsupportedOperationException(); } - public HttpClientRequest sendHead(Handler completionHandler) { throw new UnsupportedOperationException(); } + public Future sendHead() { throw new UnsupportedOperationException(); } + public HttpClientRequest sendHead(Handler> completionHandler) { throw new UnsupportedOperationException(); } public Future end(String chunk) { throw new UnsupportedOperationException(); } public Future end(String chunk, String enc) { throw new UnsupportedOperationException(); } public void end(String chunk, Handler> handler) { throw new UnsupportedOperationException(); }