< prev index next >

src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java

Print this page
rev 16207 : 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
Contributed-by: arno.zeller@sap.com

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -28,11 +28,11 @@
 import java.net.InetSocketAddress;
 import java.net.Proxy;
 import java.net.ProxySelector;
 import java.net.SocketAddress;
 import java.net.URI;
-import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.io.IOException;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.StringJoiner;

@@ -85,10 +85,12 @@
 
     private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
 
     private static boolean hasSystemProxies = false;
 
+    private static final List<Proxy> NO_PROXY_LIST = List.of(Proxy.NO_PROXY);
+
     static {
         final String key = "java.net.useSystemProxies";
         Boolean b = AccessController.doPrivileged(
             new PrivilegedAction<Boolean>() {
                 public Boolean run() {

@@ -147,12 +149,13 @@
 
     /**
      * select() method. Where all the hard work is done.
      * Build a list of proxies depending on URI.
      * Since we're only providing compatibility with the system properties
-     * from previous releases (see list above), that list will always
-     * contain 1 single proxy, default being NO_PROXY.
+     * from previous releases (see list above), that list will typically
+     * contain one single proxy, default being NO_PROXY.
+     * If we can get a system proxy it might contain more entries.
      */
     public java.util.List<Proxy> select(URI uri) {
         if (uri == null) {
             throw new IllegalArgumentException("URI can't be null.");
         }

@@ -183,11 +186,10 @@
         }
 
         if (protocol == null || host == null) {
             throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
         }
-        List<Proxy> proxyl = new ArrayList<Proxy>(1);
 
         NonProxyInfo pinfo = null;
 
         if ("http".equalsIgnoreCase(protocol)) {
             pinfo = NonProxyInfo.httpNonProxyInfo;

@@ -212,13 +214,13 @@
          * This is one big doPrivileged call, but we're trying to optimize
          * the code as much as possible. Since we're checking quite a few
          * System properties it does help having only 1 call to doPrivileged.
          * Be mindful what you do in here though!
          */
-        Proxy p = AccessController.doPrivileged(
-            new PrivilegedAction<Proxy>() {
-                public Proxy run() {
+        Proxy[] proxyArray = AccessController.doPrivileged(
+            new PrivilegedAction<Proxy[]>() {
+                public Proxy[] run() {
                     int i, j;
                     String phost =  null;
                     int pport = 0;
                     String nphosts =  null;
                     InetSocketAddress saddr = null;

@@ -237,25 +239,22 @@
                             }
                             if (phost == null || phost.length() == 0) {
                                 /**
                                  * No system property defined for that
                                  * protocol. Let's check System Proxy
-                                 * settings (Gnome & Windows) if we were
-                                 * instructed to.
+                                 * settings (Gnome, MacOsX & Windows) if
+                                 * we were instructed to.
                                  */
                                 if (hasSystemProxies) {
                                     String sproto;
                                     if (proto.equalsIgnoreCase("socket"))
                                         sproto = "socks";
                                     else
                                         sproto = proto;
-                                    Proxy sproxy = getSystemProxy(sproto, urlhost);
-                                    if (sproxy != null) {
-                                        return sproxy;
-                                    }
+                                    return getSystemProxies(sproto, urlhost);
                                 }
-                                return Proxy.NO_PROXY;
+                                return null;
                             }
                             // If a Proxy Host is defined for that protocol
                             // Let's get the NonProxyHosts property
                             if (nprop != null) {
                                 nphosts = NetProperties.get(nprop.property);

@@ -279,11 +278,11 @@
                                             nprop.pattern = toPattern(nphosts);
                                             nprop.hostsSource = nphosts;
                                         }
                                     }
                                     if (shouldNotUseProxyFor(nprop.pattern, urlhost)) {
-                                        return Proxy.NO_PROXY;
+                                        return null;
                                     }
                                 }
                             }
                             // We got a host, let's check for port
 

@@ -309,26 +308,21 @@
                             // Let's create the address, but don't resolve it
                             // as this will be done at connection time
                             saddr = InetSocketAddress.createUnresolved(phost, pport);
                             // Socks is *always* the last on the list.
                             if (j == (props[i].length - 1)) {
-                                return SocksProxy.create(saddr, socksProxyVersion());
-                            } else {
-                                return new Proxy(Proxy.Type.HTTP, saddr);
+                                return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())};
                             }
+                            return new Proxy[] {new Proxy(Proxy.Type.HTTP, saddr)};
                         }
                     }
-                    return Proxy.NO_PROXY;
+                    return null;
                 }});
 
-        proxyl.add(p);
-
-        /*
-         * If no specific property was set for that URI, we should be
-         * returning an iterator to an empty List.
-         */
-        return proxyl;
+        // If no specific proxy was found we return our standard list containing
+        // only one NO_PROXY entry.
+        return proxyArray == null ? NO_PROXY_LIST : List.of(proxyArray);
     }
 
     public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
         if (uri == null || sa == null || ioe == null) {
             throw new IllegalArgumentException("Arguments can't be null.");

@@ -352,11 +346,11 @@
             return -1;
         }
     }
 
     private static native boolean init();
-    private synchronized native Proxy getSystemProxy(String protocol, String host);
+    private synchronized native Proxy[] getSystemProxies(String protocol, String host);
 
     /**
      * @return {@code true} if given this pattern for non-proxy hosts and this
      *         urlhost the proxy should NOT be used to access this urlhost
      */
< prev index next >