Skip to content

Commit 4791a8a

Browse files
j-whitevietj
authored andcommitted
allow the 'host' header to be used on HTTP/2 requests if the ':authority' header if missing
see #5425 Signed-off-by: Jesse White <1379749+j-white@users.noreply.github.com>
1 parent 70e8535 commit 4791a8a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/io/vertx/core/http/impl/Http2ServerConnection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,21 @@ private Http2ServerStream createStream(Http2Headers headers, boolean streamEnded
137137
authorityHeaderAsString = authorityHeader.toString();
138138
authority = HostAndPort.parseAuthority(authorityHeaderAsString, -1);
139139
}
140+
CharSequence hostHeader = null;
141+
if (authority == null) {
142+
hostHeader = headers.getAndRemove(HttpHeaders.HOST);
143+
if (hostHeader != null) {
144+
authority = HostAndPort.parseAuthority(hostHeader.toString(), -1);
145+
}
146+
}
140147
CharSequence pathHeader = headers.getAndRemove(HttpHeaders.PSEUDO_PATH);
141148
CharSequence methodHeader = headers.getAndRemove(HttpHeaders.PSEUDO_METHOD);
142149
return new Http2ServerStream(
143150
this,
144151
streamContextSupplier.get(),
145152
headers,
146153
schemeHeader != null ? schemeHeader.toString() : null,
147-
authorityHeader != null,
154+
authorityHeader != null || hostHeader != null,
148155
authority,
149156
methodHeader != null ? HttpMethod.valueOf(methodHeader.toString()) : null,
150157
pathHeader != null ? pathHeader.toString() : null,

src/test/java/io/vertx/core/http/Http2ServerTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import io.netty.handler.codec.http2.Http2Exception;
4040
import io.netty.handler.codec.http2.Http2Flags;
4141
import io.netty.handler.codec.http2.Http2FrameAdapter;
42+
import io.netty.handler.codec.http2.Http2FrameListener;
4243
import io.netty.handler.codec.http2.Http2Headers;
4344
import io.netty.handler.codec.http2.Http2Settings;
4445
import io.netty.handler.codec.http2.Http2Stream;
@@ -1278,6 +1279,27 @@ public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promi
12781279
await();
12791280
}
12801281

1282+
@Test
1283+
public void testHostHeaderInsteadOfAuthorityPseudoHeader() throws Exception {
1284+
// build the HTTP/2 headers, omit the ":authority" pseudo-header and include the "host" header instead
1285+
Http2Headers headers = new DefaultHttp2Headers().method("GET").scheme("https").path("/").set("host", DEFAULT_HTTPS_HOST_AND_PORT);
1286+
server.requestHandler(req -> {
1287+
// validate that the authority is properly populated
1288+
assertEquals(DEFAULT_HTTPS_HOST, req.authority().host());
1289+
assertEquals(DEFAULT_HTTPS_PORT, req.authority().port());
1290+
testComplete();
1291+
});
1292+
startServer();
1293+
TestClient client = new TestClient();
1294+
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
1295+
int id = request.nextStreamId();
1296+
Http2ConnectionEncoder encoder = request.encoder;
1297+
encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
1298+
});
1299+
fut.sync();
1300+
await();
1301+
}
1302+
12811303
@Test
12821304
public void testMissingMethodPseudoHeader() throws Exception {
12831305
testMalformedRequestHeaders(new DefaultHttp2Headers().scheme("http").path("/"));

0 commit comments

Comments
 (0)