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
@@ -122,15 +131,8 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final String hos
122
131
123
132
if (nonProxyHosts != null ) {
124
133
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 ))
130
135
return true ;
131
- } else if (nonProxyHost .equalsIgnoreCase (targetHost )) {
132
- return true ;
133
- }
134
136
}
135
137
}
136
138
0 commit comments