Improved HttpClient's proxy configuration by using Strings as

included and excluded addresses. This allows to pass in Strings such
as 127.0.0.1/8 or other wildcard formats.
This commit is contained in:
Simone Bordet 2013-10-17 09:18:05 +02:00
parent 4da6ddc94f
commit e408bd64c7
3 changed files with 106 additions and 8 deletions

View File

@ -58,8 +58,8 @@ public class ProxyConfiguration
public static abstract class Proxy
{
private final Set<Origin> included = new HashSet<>();
private final Set<Origin> excluded = new HashSet<>();
private final Set<String> included = new HashSet<>();
private final Set<String> excluded = new HashSet<>();
private final Origin.Address address;
private final boolean secure;
@ -87,16 +87,20 @@ public class ProxyConfiguration
/**
* @return the list of origins that must be proxied
* @see #matches(Origin)
* @see #getExcludedAddresses()
*/
public Set<Origin> getIncludedOrigins()
public Set<String> getIncludedAddresses()
{
return included;
}
/**
* @return the list of origins that must not be proxied.
* @see #matches(Origin)
* @see #getIncludedAddresses()
*/
public Set<Origin> getExcludedOrigins()
public Set<String> getExcludedAddresses()
{
return excluded;
}
@ -110,7 +114,7 @@ public class ProxyConfiguration
}
/**
* Matches the given {@code origin} with the included and excluded origins,
* Matches the given {@code origin} with the included and excluded addresses,
* returning true if the given {@code origin} is to be proxied.
*
* @param origin the origin to test for proxying
@ -118,7 +122,36 @@ public class ProxyConfiguration
*/
public boolean matches(Origin origin)
{
return included.contains(origin) || !excluded.contains(origin);
boolean result = included.isEmpty();
Origin.Address address = origin.getAddress();
for (String included : this.included)
{
if (matches(address, included))
{
result = true;
break;
}
}
for (String excluded : this.excluded)
{
if (matches(address, excluded))
{
result = false;
break;
}
}
return result;
}
private boolean matches(Origin.Address address, String pattern)
{
// TODO: add support for CIDR notation like 192.168.0.0/24, see DoSFilter
int colon = pattern.indexOf(':');
if (colon < 0)
return pattern.equals(address.getHost());
String host = pattern.substring(0, colon);
String port = pattern.substring(colon + 1);
return host.equals(address.getHost()) && port.equals(String.valueOf(address.getPort()));
}
/**

View File

@ -0,0 +1,66 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.client;
import org.junit.Assert;
import org.junit.Test;
public class ProxyConfigurationTest
{
@Test
public void testProxyMatchesWithoutIncludesWithoutExcludes() throws Exception
{
HttpProxy proxy = new HttpProxy("host", 0);
Assert.assertTrue(proxy.matches(new Origin("http", "any", 0)));
}
@Test
public void testProxyMatchesWithOnlyExcludes() throws Exception
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("1.2.3.4:5");
Assert.assertTrue(proxy.matches(new Origin("http", "any", 0)));
Assert.assertTrue(proxy.matches(new Origin("http", "1.2.3.4", 0)));
Assert.assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 5)));
}
@Test
public void testProxyMatchesWithOnlyIncludes() throws Exception
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getIncludedAddresses().add("1.2.3.4:5");
Assert.assertFalse(proxy.matches(new Origin("http", "any", 0)));
Assert.assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 0)));
Assert.assertTrue(proxy.matches(new Origin("http", "1.2.3.4", 5)));
}
@Test
public void testProxyMatchesWithIncludesAndExcludes() throws Exception
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getIncludedAddresses().add("1.2.3.4");
proxy.getExcludedAddresses().add("1.2.3.4:5");
Assert.assertFalse(proxy.matches(new Origin("http", "any", 0)));
Assert.assertTrue(proxy.matches(new Origin("http", "1.2.3.4", 0)));
Assert.assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 5)));
}
}

View File

@ -49,7 +49,6 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpContentResponse;
import org.eclipse.jetty.client.HttpProxy;
import org.eclipse.jetty.client.Origin;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
@ -632,7 +631,7 @@ public class ProxyServletTest
}
});
int port = serverConnector.getLocalPort();
client.getProxyConfiguration().getProxies().get(0).getExcludedOrigins().add(new Origin("http", "127.0.0.1", port));
client.getProxyConfiguration().getProxies().get(0).getExcludedAddresses().add("127.0.0.1:" + port);
// Try with a proxied host
ContentResponse response = client.newRequest("localhost", port)