Issue #1785 minor cleanups

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-05-04 10:20:15 +10:00
parent ddaafc5360
commit 03d96ae538
2 changed files with 63 additions and 82 deletions

View File

@ -76,6 +76,7 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.AttributesMap;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.StringUtil;
@ -402,30 +403,18 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
*/
public void addVirtualHosts(String[] virtualHosts)
{
if (virtualHosts == null) // since this is add, we don't null the old ones
{
if (virtualHosts == null || virtualHosts.length==0) // since this is add, we don't null the old ones
return;
if (_vhosts==null)
{
setVirtualHosts(virtualHosts);
}
else
{
List<String> currentVirtualHosts = null;
if (_vhosts != null)
{
currentVirtualHosts = new ArrayList<String>(Arrays.asList(getVirtualHosts()));
}
else
{
currentVirtualHosts = new ArrayList<String>();
}
for (int i = 0; i < virtualHosts.length; i++)
{
String normVhost = normalizeHostname(virtualHosts[i]);
if (!currentVirtualHosts.contains(normVhost))
{
currentVirtualHosts.add(normVhost);
}
}
Set<String> currentVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts()));
for (String vh : virtualHosts)
currentVirtualHosts.add(normalizeHostname(vh));
setVirtualHosts(currentVirtualHosts.toArray(new String[0]));
}
}
@ -444,36 +433,16 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
*/
public void removeVirtualHosts(String[] virtualHosts)
{
if (virtualHosts == null)
{
if (virtualHosts == null || virtualHosts.length==0 || _vhosts == null || _vhosts.length == 0)
return; // do nothing
}
else if (_vhosts == null || _vhosts.length == 0)
{
return; // do nothing
}
Set<String> existingVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts()));
for (String vh : virtualHosts)
existingVirtualHosts.remove(normalizeHostname(vh));
if (existingVirtualHosts.isEmpty())
setVirtualHosts(null); // if we ended up removing them all, just null out _vhosts
else
{
List<String> existingVirtualHosts = new ArrayList<String>(Arrays.asList(getVirtualHosts()));
for (int i = 0; i < virtualHosts.length; i++)
{
String toRemoveVirtualHost = normalizeHostname(virtualHosts[i]);
if (existingVirtualHosts.contains(toRemoveVirtualHost))
{
existingVirtualHosts.remove(toRemoveVirtualHost);
}
}
if (existingVirtualHosts.isEmpty())
{
setVirtualHosts(null); // if we ended up removing them all, just null out _vhosts
}
else
{
setVirtualHosts(existingVirtualHosts.toArray(new String[0]));
}
}
setVirtualHosts(existingVirtualHosts.toArray(new String[0]));
}
/* ------------------------------------------------------------ */
@ -500,14 +469,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
{
StringBuilder sb = new StringBuilder();
if (_vhostswildcard[i])
sb.append("*.");
sb.append("*");
if (_vhosts[i] != null)
sb.append(_vhosts[i]);
if (_vconnectors[i] != null)
sb.append("@").append(_vconnectors[i]);
vhosts[i] = sb.toString();
}
return _vhosts;
return vhosts;
}
/* ------------------------------------------------------------ */
@ -1052,46 +1021,46 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
/* ------------------------------------------------------------ */
public boolean checkVirtualHost(final Request baseRequest)
{
boolean match = false;
if (_vhosts == null || _vhosts.length == 0)
return true;
if (_vhosts != null && _vhosts.length > 0)
String vhost = normalizeHostname(baseRequest.getServerName());
String connectorName = baseRequest.getHttpChannel().getConnector().getName();
for (int i = 0; i < _vhosts.length; i++)
{
String vhost = normalizeHostname(baseRequest.getServerName());
String connectorName = baseRequest.getHttpChannel().getConnector().getName();
String contextVhost = _vhosts[i];
String contextVConnector = _vconnectors[i];
for (int i = 0; i < _vhosts.length; i++)
if (contextVConnector!=null)
{
String contextVhost = _vhosts[i];
String contextVConnector = _vconnectors[i];
boolean connectorMatch = contextVConnector == null || contextVConnector.equalsIgnoreCase(connectorName);
if (contextVhost == null)
{
if (connectorMatch && contextVConnector != null)
return true;
else
continue;
}
else if (!connectorMatch)
if (!contextVConnector.equalsIgnoreCase(connectorName))
continue;
if (contextVhost==null)
{
return true;
}
}
if (contextVhost!=null)
{
if (_vhostswildcard[i])
{
// wildcard only at the beginning, and only for one additional subdomain level
int index = vhost.indexOf(".");
if (index >= 0)
match = vhost.substring(index).equalsIgnoreCase(contextVhost);
if (index >= 0 && vhost.substring(index).equalsIgnoreCase(contextVhost))
{
return true;
}
}
else if (vhost.equalsIgnoreCase(contextVhost))
{
return true;
}
else
match = vhost.equalsIgnoreCase(contextVhost);
if (match)
break;
}
return match;
}
return true;
return false;
}
/* ------------------------------------------------------------ */
@ -1813,7 +1782,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
}
}
b.append(getClass().getSimpleName()).append('@').append(Integer.toString(hashCode(),16));
b.append('{').append(getContextPath()).append(',').append(getBaseResource()).append(',').append(_availability);
b.append('{');
if (getDisplayName()!=null)
b.append(getDisplayName()).append(',');
b.append(getContextPath()).append(',').append(getBaseResource()).append(',').append(_availability);
if (vhosts != null && vhosts.length > 0)
b.append(',').append(vhosts[0]);

View File

@ -25,6 +25,7 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -125,40 +126,48 @@ public class ContextHandlerTest
server.setConnectors(new Connector[] { connector, connectorN });
ContextHandler contextA = new ContextHandler("/");
contextA.setDisplayName("A");
contextA.setVirtualHosts(new String[]{"www.example.com" });
IsHandledHandler handlerA = new IsHandledHandler();
contextA.setHandler(handlerA);
ContextHandler contextB = new ContextHandler("/");
contextB.setDisplayName("B");
IsHandledHandler handlerB = new IsHandledHandler();
contextB.setHandler(handlerB);
contextB.setVirtualHosts(new String[]{ "@name" });
ContextHandler contextC = new ContextHandler("/");
contextC.setDisplayName("C");
IsHandledHandler handlerC = new IsHandledHandler();
contextC.setHandler(handlerC);
ContextHandler contextD = new ContextHandler("/");
contextD.setDisplayName("D");
IsHandledHandler handlerD = new IsHandledHandler();
contextD.setHandler(handlerD);
contextD.setVirtualHosts(new String[]{ "www.example.com@name" });
ContextHandler contextE = new ContextHandler("/");
contextE.setDisplayName("E");
IsHandledHandler handlerE = new IsHandledHandler();
contextE.setHandler(handlerE);
contextE.setVirtualHosts(new String[]{ "*.example.com" });
ContextHandler contextF = new ContextHandler("/");
contextF.setDisplayName("F");
IsHandledHandler handlerF = new IsHandledHandler();
contextF.setHandler(handlerF);
contextF.setVirtualHosts(new String[]{ "*.example.com@name" });
ContextHandler contextG = new ContextHandler("/");
contextG.setDisplayName("G");
IsHandledHandler handlerG = new IsHandledHandler();
contextG.setHandler(handlerG);
contextG.setVirtualHosts(new String[]{ "*.com@name" });
ContextHandler contextH = new ContextHandler("/");
contextH.setDisplayName("H");
IsHandledHandler handlerH = new IsHandledHandler();
contextH.setHandler(handlerH);
contextH.setVirtualHosts(new String[]{ "*.com" });
@ -536,7 +545,7 @@ public class ContextHandlerTest
Assert.assertEquals(1, context.getVirtualHosts().length);
// test adding two more
context.addVirtualHosts(new String[] { "www.example2.com", "www.example3.com"});
context.addVirtualHosts(new String[] { "foo.com@connector1", "*.example2.com"});
Assert.assertEquals(3, context.getVirtualHosts().length);
// test adding existing context
@ -544,7 +553,7 @@ public class ContextHandlerTest
Assert.assertEquals(3, context.getVirtualHosts().length);
// test removing existing
context.removeVirtualHosts(new String[] { "www.example3.com" });
context.removeVirtualHosts(new String[] { "*.example2.com" });
Assert.assertEquals(2, context.getVirtualHosts().length);
// test removing non-existent
@ -552,7 +561,7 @@ public class ContextHandlerTest
Assert.assertEquals(2, context.getVirtualHosts().length);
// test removing all remaining and resets to null
context.removeVirtualHosts(new String[] { "www.example.com", "www.example2.com" });
context.removeVirtualHosts(new String[] { "www.example.com", "foo.com@connector1" });
Assert.assertArrayEquals(null, context.getVirtualHosts());
}