Skip to content

Commit

Permalink
Fix null host when checking virtual host #10922
Browse files Browse the repository at this point in the history
Use the `Request.getServerName` static and check for null host.
  • Loading branch information
gregw committed Nov 27, 2023
1 parent a35b0a2 commit a88ef6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,25 +325,7 @@ public void setVirtualHosts(List<String> vhosts)
{
if (vhost == null)
continue;
boolean wild = false;
String connector = null;
int at = vhost.indexOf('@');
if (at >= 0)
{
connector = vhost.substring(at + 1);
vhost = vhost.substring(0, at);
}

if (StringUtil.isBlank(vhost))
{
vhost = null;
}
else if (vhost.startsWith("*."))
{
vhost = vhost.substring(1);
wild = true;
}
_vhosts.add(new VHost(normalizeHostname(vhost), wild, connector));
_vhosts.add(new VHost(vhost));
}
}
}
Expand Down Expand Up @@ -387,9 +369,7 @@ public void removeVirtualHosts(String... virtualHosts)
return; // do nothing

for (String vh : virtualHosts)
{
vhosts.remove(normalizeHostname(vh));
}
_vhosts.remove(new VHost(vh));
}

/**
Expand Down Expand Up @@ -719,7 +699,9 @@ public boolean checkVirtualHost(Request request)
if (_vhosts.isEmpty())
return true;

String host = Request.getServerName(request);
// TODO check why normalizeVirtualHostname is necessary to pass ContextHandlerCollectionTest
// specifically is it valid for a request hostname to end with '.'
String host = normalizeVirtualHostname(Request.getServerName(request));
String connectorName = request.getConnectionMetaData().getConnector().getName();

for (VHost vhost : _vhosts)
Expand Down Expand Up @@ -1101,24 +1083,12 @@ public String toString()
return b.toString();
}

private String normalizeHostname(String host)
private static String normalizeVirtualHostname(String host)
{
// TODO is this needed? if so, should be it somewhere eles?
if (host == null)
return null;
int connectorIndex = host.indexOf('@');
String connector = null;
if (connectorIndex > 0)
{
host = host.substring(0, connectorIndex);
connector = host.substring(connectorIndex);
}

if (host.endsWith("."))
host = host.substring(0, host.length() - 1);
if (connector != null)
host += connector;

return host;
}

Expand Down Expand Up @@ -1331,11 +1301,30 @@ private static class VHost
private final boolean _wild;
private final String _vConnector;

private VHost(String vHost, boolean wild, String vConnector)
public VHost(String vhost)
{
_vHost = vHost;
boolean wild = false;
String connector = null;
int at = vhost.indexOf('@');
if (at >= 0)
{
connector = vhost.substring(at + 1);
vhost = vhost.substring(0, at);
}

if (StringUtil.isBlank(vhost))
{
vhost = null;
}
else if (vhost.startsWith("*."))
{
vhost = vhost.substring(1);
wild = true;
}

_vHost = normalizeVirtualHostname(vhost);
_wild = wild;
_vConnector = vConnector;
_vConnector = connector;
}

String getVHost()
Expand All @@ -1351,6 +1340,21 @@ String getName()
return _vHost;
}

@Override
public int hashCode()
{
return Objects.hash(_vHost, _wild, _vConnector);
}

@Override
public boolean equals(Object o)
{
return o instanceof VHost vhost &&
Objects.equals(_vHost, vhost._vHost) &&
Objects.equals(_wild, vhost._wild) &&
Objects.equals(_vConnector, vhost._vConnector);
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1599,26 +1599,6 @@ public Set<String> getResourcePaths(String path)
return Collections.emptySet();
}

private String normalizeHostname(String host)
{
if (host == null)
return null;
int connectorIndex = host.indexOf('@');
String connector = null;
if (connectorIndex > 0)
{
host = host.substring(0, connectorIndex);
connector = host.substring(connectorIndex);
}

if (host.endsWith("."))
host = host.substring(0, host.length() - 1);
if (connector != null)
host += connector;

return host;
}

/**
* Add an AliasCheck instance to possibly permit aliased resources
*
Expand Down

0 comments on commit a88ef6b

Please sign in to comment.