diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 3dee12f0c6f5..081ffe3779a6 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -719,7 +719,7 @@ public boolean checkVirtualHost(Request request) if (_vhosts.isEmpty()) return true; - String host = normalizeHostname(request.getHttpURI().getHost()); + String host = Request.getServerName(request); String connectorName = request.getConnectionMetaData().getConnector().getName(); for (VHost vhost : _vhosts) @@ -733,21 +733,17 @@ public boolean checkVirtualHost(Request request) continue; if (contextVhost == null) - { return true; - } } - if (contextVhost != null) + if (contextVhost != null && host != null) { if (vhost._wild) { // wildcard only at the beginning, and only for one additional subdomain level int index = host.indexOf("."); if (index >= 0 && host.substring(index).equalsIgnoreCase(contextVhost)) - { return true; - } } else if (host.equalsIgnoreCase(contextVhost)) { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java index 8b0a71246777..38f575f99020 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java @@ -535,6 +535,11 @@ public String getName() HttpFields fields = HttpFields.build().asImmutable(); MockHttpStream stream = new MockHttpStream(channel); + channel.onRequest(new MetaData.Request("GET", HttpURI.from("/ctx/"), HttpVersion.HTTP_1_0, fields, 0)).run(); + assertThat(stream.isComplete(), is(true)); + assertThat(stream.getResponse().getStatus(), equalTo(404)); + + stream = new MockHttpStream(channel); channel.onRequest(new MetaData.Request("GET", HttpURI.from("http://localhost/ctx/"), HttpVersion.HTTP_1_1, fields, 0)).run(); assertThat(stream.isComplete(), is(true)); assertThat(stream.getResponse().getStatus(), equalTo(404));