14
14
import java .net .URL ;
15
15
import java .net .URLDecoder ;
16
16
import java .nio .charset .StandardCharsets ;
17
- import java .util .Arrays ;
18
- import java .util .List ;
19
17
import java .util .Objects ;
20
- import java .util .function .BiFunction ;
21
18
import java .util .regex .Pattern ;
22
19
23
20
/**
@@ -30,9 +27,9 @@ public class ProxyOptions {
30
27
private static final String INVALID_AZURE_PROXY_URL = "Configuration {} is an invalid URL and is being ignored." ;
31
28
32
29
/*
33
- * This indicates whether Java environment proxy configurations are allowed to be used.
30
+ * This indicates whether system proxy configurations (HTTPS_PROXY, HTTP_PROXY) are allowed to be used.
34
31
*/
35
- private static final String JAVA_PROXY_PREREQUISITE = "java.net.useSystemProxies" ;
32
+ private static final String JAVA_SYSTEM_PROXY_PREREQUISITE = "java.net.useSystemProxies" ;
36
33
37
34
/*
38
35
* Java environment variables related to proxies. The protocol is removed since these are the same for 'https' and
@@ -50,17 +47,6 @@ public class ProxyOptions {
50
47
private static final String HTTP = "http" ;
51
48
private static final int DEFAULT_HTTP_PORT = 80 ;
52
49
53
- private static final List <BiFunction <Configuration , Boolean , ProxyOptions >> ENVIRONMENT_LOAD_ORDER = Arrays .asList (
54
- (configuration , resolveProxy )
55
- -> attemptToLoadAzureProxy (configuration , resolveProxy , Configuration .PROPERTY_HTTPS_PROXY ),
56
- (configuration , resolveProxy )
57
- -> attemptToLoadAzureProxy (configuration , resolveProxy , Configuration .PROPERTY_HTTP_PROXY ),
58
- (configuration , resolveProxy )
59
- -> attemptToLoadJavaProxy (configuration , resolveProxy , HTTPS ),
60
- (configuration , resolveProxy )
61
- -> attemptToLoadJavaProxy (configuration , resolveProxy , HTTP )
62
- );
63
-
64
50
private final InetSocketAddress address ;
65
51
private final Type type ;
66
52
private String username ;
@@ -93,7 +79,6 @@ public ProxyOptions setCredentials(String username, String password) {
93
79
94
80
/**
95
81
* Sets the hosts which bypass the proxy.
96
- *
97
82
* <p>
98
83
* The expected format of the passed string is a {@code '|'} delimited list of hosts which should bypass the proxy.
99
84
* Individual host strings may contain regex characters such as {@code '*'}.
@@ -188,7 +173,6 @@ public static ProxyOptions fromConfiguration(Configuration configuration) {
188
173
*
189
174
* Azure proxy configurations will be preferred over Java proxy configurations as they are more closely scoped to
190
175
* the purpose of the SDK. Additionally, more secure protocols, HTTPS vs HTTP, will be preferred.
191
- *
192
176
* <p>
193
177
* {@code null} will be returned if no proxy was found in the environment.
194
178
*
@@ -209,17 +193,44 @@ public static ProxyOptions fromConfiguration(Configuration configuration, boolea
209
193
? Configuration .getGlobalConfiguration ()
210
194
: configuration ;
211
195
212
- for (BiFunction <Configuration , Boolean , ProxyOptions > loader : ENVIRONMENT_LOAD_ORDER ) {
213
- ProxyOptions proxyOptions = loader .apply (proxyConfiguration , createUnresolved );
196
+ return attemptToLoadProxy (proxyConfiguration , createUnresolved );
197
+ }
198
+
199
+ private static ProxyOptions attemptToLoadProxy (Configuration configuration , boolean createUnresolved ) {
200
+ ProxyOptions proxyOptions ;
201
+
202
+ // Only use system proxies when the prerequisite property is 'true'.
203
+ if (Boolean .parseBoolean (configuration .get (JAVA_SYSTEM_PROXY_PREREQUISITE ))) {
204
+ proxyOptions = attemptToLoadSystemProxy (configuration , createUnresolved ,
205
+ Configuration .PROPERTY_HTTPS_PROXY );
206
+ if (proxyOptions != null ) {
207
+ LOGGER .verbose ("Using proxy created from HTTPS_PROXY environment variable." );
208
+ return proxyOptions ;
209
+ }
210
+
211
+ proxyOptions = attemptToLoadSystemProxy (configuration , createUnresolved , Configuration .PROPERTY_HTTP_PROXY );
214
212
if (proxyOptions != null ) {
213
+ LOGGER .verbose ("Using proxy created from HTTP_PROXY environment variable." );
215
214
return proxyOptions ;
216
215
}
217
216
}
218
217
218
+ proxyOptions = attemptToLoadJavaProxy (configuration , createUnresolved , HTTPS );
219
+ if (proxyOptions != null ) {
220
+ LOGGER .verbose ("Using proxy created from JVM HTTPS system properties." );
221
+ return proxyOptions ;
222
+ }
223
+
224
+ proxyOptions = attemptToLoadJavaProxy (configuration , createUnresolved , HTTP );
225
+ if (proxyOptions != null ) {
226
+ LOGGER .verbose ("Using proxy created from JVM HTTP system properties." );
227
+ return proxyOptions ;
228
+ }
229
+
219
230
return null ;
220
231
}
221
232
222
- private static ProxyOptions attemptToLoadAzureProxy (Configuration configuration , boolean createUnresolved ,
233
+ private static ProxyOptions attemptToLoadSystemProxy (Configuration configuration , boolean createUnresolved ,
223
234
String proxyProperty ) {
224
235
String proxyConfiguration = configuration .get (proxyProperty );
225
236
@@ -236,7 +247,7 @@ private static ProxyOptions attemptToLoadAzureProxy(Configuration configuration,
236
247
? InetSocketAddress .createUnresolved (proxyUrl .getHost (), port )
237
248
: new InetSocketAddress (proxyUrl .getHost (), port );
238
249
239
- ProxyOptions proxyOptions = new ProxyOptions (Type .HTTP , socketAddress );
250
+ ProxyOptions proxyOptions = new ProxyOptions (ProxyOptions . Type .HTTP , socketAddress );
240
251
241
252
String nonProxyHostsString = configuration .get (Configuration .PROPERTY_NO_PROXY );
242
253
if (!CoreUtils .isNullOrEmpty (nonProxyHostsString )) {
@@ -325,11 +336,6 @@ private static String sanitizeNoProxy(String noProxyString) {
325
336
326
337
private static ProxyOptions attemptToLoadJavaProxy (Configuration configuration , boolean createUnresolved ,
327
338
String type ) {
328
- // Not allowed to use Java proxies
329
- if (!Boolean .parseBoolean (configuration .get (JAVA_PROXY_PREREQUISITE ))) {
330
- return null ;
331
- }
332
-
333
339
String host = configuration .get (type + "." + JAVA_PROXY_HOST );
334
340
335
341
// No proxy configuration setup.
@@ -348,11 +354,11 @@ private static ProxyOptions attemptToLoadJavaProxy(Configuration configuration,
348
354
? InetSocketAddress .createUnresolved (host , port )
349
355
: new InetSocketAddress (host , port );
350
356
351
- ProxyOptions proxyOptions = new ProxyOptions (Type .HTTP , socketAddress );
357
+ ProxyOptions proxyOptions = new ProxyOptions (ProxyOptions . Type .HTTP , socketAddress );
352
358
353
359
String nonProxyHostsString = configuration .get (JAVA_NON_PROXY_HOSTS );
354
360
if (!CoreUtils .isNullOrEmpty (nonProxyHostsString )) {
355
- proxyOptions .nonProxyHosts = sanitizeJavaHttpNonProxyHosts (nonProxyHostsString );
361
+ proxyOptions .setNonProxyHosts (nonProxyHostsString );
356
362
}
357
363
358
364
String username = configuration .get (type + "." + JAVA_PROXY_USER );
0 commit comments