Skip to content

Commit 16b1c2d

Browse files
author
Stephane Landelle
committed
Minor ProxyUtils.matchNonProxyHost clean up, still very incomplete
1 parent 7a61453 commit 16b1c2d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/main/java/com/ning/http/util/ProxyUtils.java

Lines changed: 19 additions & 17 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
@@ -122,15 +131,8 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final String hos
122131

123132
if (nonProxyHosts != null) {
124133
for (String nonProxyHost : nonProxyHosts) {
125-
if (nonProxyHost.startsWith("*") && nonProxyHost.length() > 1
126-
&& targetHost.endsWith(nonProxyHost.substring(1).toLowerCase(Locale.ENGLISH))) {
127-
return true;
128-
} else if (nonProxyHost.endsWith("*") && nonProxyHost.length() > 1
129-
&& targetHost.startsWith(nonProxyHost.substring(0, nonProxyHost.length() - 1).toLowerCase(Locale.ENGLISH))) {
134+
if (matchNonProxyHost(targetHost, nonProxyHost))
130135
return true;
131-
} else if (nonProxyHost.equalsIgnoreCase(targetHost)) {
132-
return true;
133-
}
134136
}
135137
}
136138

0 commit comments

Comments
 (0)