35
35
*
36
36
* @author cstamas
37
37
*/
38
- public class ProxyUtils {
38
+ public final class ProxyUtils {
39
39
40
40
private final static Logger log = LoggerFactory .getLogger (ProxyUtils .class );
41
41
@@ -71,6 +71,9 @@ public class ProxyUtils {
71
71
*/
72
72
public static final String PROXY_PASSWORD = PROPERTY_PREFIX + "password" ;
73
73
74
+ private ProxyUtils () {
75
+ }
76
+
74
77
/**
75
78
* @param config the global config
76
79
* @param request the request
@@ -88,19 +91,25 @@ public static ProxyServer getProxyServer(AsyncHttpClientConfig config, Request r
88
91
}
89
92
90
93
/**
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)
99
95
*/
100
96
public static boolean avoidProxy (final ProxyServer proxyServer , final Request request ) {
101
97
return avoidProxy (proxyServer , AsyncHttpProviderUtils .getHost (request .getOriginalURI ()));
102
98
}
103
99
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
+
104
113
/**
105
114
* Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go directly to
106
115
* 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
116
125
if (hostname == null )
117
126
throw new NullPointerException ("hostname" );
118
127
119
- final String targetHost = hostname .toLowerCase (Locale .ENGLISH );
120
-
121
128
List <String > nonProxyHosts = proxyServer .getNonProxyHosts ();
122
129
123
130
if (isNonEmpty (nonProxyHosts )) {
124
131
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 ))
131
133
return true ;
132
- } else if (nonProxyHost .equalsIgnoreCase (targetHost )) {
133
- return true ;
134
- }
135
134
}
136
135
}
137
136
0 commit comments