Skip to content

Commit 8ce1eb1

Browse files
committed
Allow an HTTP client request to have a null authority.
Motivation: In some cases it is desirable to send an HTTP request that does not carry the http2 pseudo authority header, nor the host header.
1 parent 12a0fd3 commit 8ce1eb1

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ private HttpRequest createRequest(
187187
}
188188
}
189189
if (!headers.contains(HOST)) {
190-
request.headers().set(HOST, authority.toString(ssl));
190+
if (authority != null) {
191+
request.headers().set(HOST, authority.toString(ssl));
192+
}
191193
} else {
192194
headers.remove(TRANSFER_ENCODING);
193195
}

vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientRequestBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public synchronized HttpClientRequest setURI(String uri) {
117117

118118
@Override
119119
public synchronized HttpClientRequest authority(HostAndPort authority) {
120-
Objects.requireNonNull(authority);
121120
this.authority = authority;
122121
return this;
123122
}

vertx-core/src/test/java/io/vertx/tests/http/Http1xTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5431,7 +5431,7 @@ private void testEmptyHostPortionOfHostHeader(String hostHeader, int expectedPor
54315431
}
54325432

54335433
@Test
5434-
public void testMissingHostHeader() throws Exception {
5434+
public void testServerMissingHostHeader() throws Exception {
54355435
server.requestHandler(req -> {
54365436
assertEquals(null, req.authority());
54375437
assertFalse(((HttpServerRequestInternal) req).isValidAuthority());
@@ -5445,6 +5445,23 @@ public void testMissingHostHeader() throws Exception {
54455445
await();
54465446
}
54475447

5448+
@Test
5449+
public void testClientMissingHostHeader() throws Exception {
5450+
server.requestHandler(req -> {
5451+
assertEquals(null, req.authority());
5452+
assertFalse(((HttpServerRequestInternal) req).isValidAuthority());
5453+
req.response().end();
5454+
});
5455+
startServer(testAddress);
5456+
client.request(requestOptions)
5457+
.compose(request -> request
5458+
.authority(null)
5459+
.send()
5460+
.expecting(HttpResponseExpectation.SC_OK)
5461+
.compose(HttpClientResponse::end))
5462+
.await();
5463+
}
5464+
54485465
@Test
54495466
public void testCanUpgradeToWebSocket() throws Exception {
54505467
UnaryOperator<RequestOptions> config = ro -> ro.setMethod(HttpMethod.GET)

vertx-core/src/test/java/io/vertx/tests/http/Http2ClientTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,31 @@ public void testOverrideAuthority() throws Exception {
378378
await();
379379
}
380380

381+
@Test
382+
public void testNoAuthority() throws Exception {
383+
ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() {
384+
@Override
385+
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
386+
vertx.runOnContext(v -> {
387+
assertNull(headers.authority());
388+
encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise());
389+
ctx.flush();
390+
});
391+
}
392+
});
393+
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
394+
client.request(new RequestOptions().setServer(testAddress)
395+
.setPort(4444)
396+
.setHost("localhost")
397+
)
398+
.compose(request -> {
399+
request.authority(null);
400+
return request.send();
401+
})
402+
.onComplete(onSuccess(resp -> testComplete()));
403+
await();
404+
}
405+
381406
@Test
382407
public void testTrailers() throws Exception {
383408
server.requestHandler(req -> {

0 commit comments

Comments
 (0)