Skip to content

Commit 01e61a1

Browse files
author
Stephane Landelle
committed
Minor ProxyUtils.matchNonProxyHost clean up, still very incomplete
1 parent 02f069d commit 01e61a1

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

api/src/main/java/org/asynchttpclient/util/ProxyUtils.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* @author cstamas
3737
*/
38-
public class ProxyUtils {
38+
public final class ProxyUtils {
3939

4040
private final static Logger log = LoggerFactory.getLogger(ProxyUtils.class);
4141

@@ -71,6 +71,9 @@ public class ProxyUtils {
7171
*/
7272
public static final String PROXY_PASSWORD = PROPERTY_PREFIX + "password";
7373

74+
private ProxyUtils() {
75+
}
76+
7477
/**
7578
* @param config the global config
7679
* @param request the request
@@ -88,19 +91,25 @@ public static ProxyServer getProxyServer(AsyncHttpClientConfig config, Request r
8891
}
8992

9093
/**
91-
* Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go directly to
92-
* target host. If <code>null</code> proxy is passed in, this method returns true -- since there is NO proxy, we
93-
* should avoid to use it. Simple hostname pattern matching using "*" are supported, but only as prefixes.
94-
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
95-
*
96-
* @param proxyServer
97-
* @param request
98-
* @return true if we have to avoid proxy use (obeying non-proxy hosts settings), false otherwise.
94+
* @see #avoidProxy(ProxyServer, String)
9995
*/
10096
public static boolean avoidProxy(final ProxyServer proxyServer, final Request request) {
10197
return avoidProxy(proxyServer, AsyncHttpProviderUtils.getHost(request.getOriginalURI()));
10298
}
10399

100+
private static boolean matchNonProxyHost(String targetHost, String nonProxyHost) {
101+
102+
if (nonProxyHost.length() > 1) {
103+
if (nonProxyHost.charAt(0) == '*')
104+
return targetHost.regionMatches(true, targetHost.length() - nonProxyHost.length() + 1, nonProxyHost, 1,
105+
nonProxyHost.length() - 1);
106+
else if (nonProxyHost.charAt(nonProxyHost.length() - 1) == '*')
107+
return targetHost.regionMatches(true, 0, nonProxyHost, 0, nonProxyHost.length() - 1);
108+
}
109+
110+
return nonProxyHost.equalsIgnoreCase(targetHost);
111+
}
112+
104113
/**
105114
* Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go directly to
106115
* target host. If <code>null</code> proxy is passed in, this method returns true -- since there is NO proxy, we
@@ -116,22 +125,12 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final String hos
116125
if (hostname == null)
117126
throw new NullPointerException("hostname");
118127

119-
final String targetHost = hostname.toLowerCase(Locale.ENGLISH);
120-
121128
List<String> nonProxyHosts = proxyServer.getNonProxyHosts();
122129

123130
if (isNonEmpty(nonProxyHosts)) {
124131
for (String nonProxyHost : nonProxyHosts) {
125-
// FIXME use regionMatches instead
126-
if (nonProxyHost.startsWith("*") && nonProxyHost.length() > 1
127-
&& targetHost.endsWith(nonProxyHost.substring(1).toLowerCase(Locale.ENGLISH))) {
128-
return true;
129-
} else if (nonProxyHost.endsWith("*") && nonProxyHost.length() > 1
130-
&& targetHost.startsWith(nonProxyHost.substring(0, nonProxyHost.length() - 1).toLowerCase(Locale.ENGLISH))) {
132+
if (matchNonProxyHost(hostname, nonProxyHost))
131133
return true;
132-
} else if (nonProxyHost.equalsIgnoreCase(targetHost)) {
133-
return true;
134-
}
135134
}
136135
}
137136

0 commit comments

Comments
 (0)