< prev index next >

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

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.spi;
  27 
  28 import java.net.InetSocketAddress;
  29 import java.net.Proxy;
  30 import java.net.ProxySelector;
  31 import java.net.SocketAddress;
  32 import java.net.URI;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 import java.io.IOException;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import java.util.StringJoiner;
  39 import java.util.regex.Pattern;

  40 import sun.net.NetProperties;
  41 import sun.net.SocksProxy;
  42 import static java.util.regex.Pattern.quote;


  43 
  44 /**
  45  * Supports proxy settings using system properties This proxy selector
  46  * provides backward compatibility with the old http protocol handler
  47  * as far as how proxy is set
  48  *
  49  * Most of the implementation copied from the old http protocol handler
  50  *
  51  * Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
  52  * proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
  53  * NOTE: need to do gopher as well
  54  */
  55 public class DefaultProxySelector extends ProxySelector {
  56 
  57     /**
  58      * This is where we define all the valid System Properties we have to
  59      * support for each given protocol.
  60      * The format of this 2 dimensional array is :
  61      * - 1 row per protocol (http, ftp, ...)
  62      * - 1st element of each row is the protocol name


 301                             if (pport == 0) {
 302                                 if (j == (props[i].length - 1)) // SOCKS
 303                                     pport = defaultPort("socket");
 304                                 else
 305                                     pport = defaultPort(proto);
 306                             }
 307                             // We did find a proxy definition.
 308                             // Let's create the address, but don't resolve it
 309                             // as this will be done at connection time
 310                             saddr = InetSocketAddress.createUnresolved(phost, pport);
 311                             // Socks is *always* the last on the list.
 312                             if (j == (props[i].length - 1)) {
 313                                 return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())};
 314                             }
 315                             return new Proxy[] {new Proxy(Proxy.Type.HTTP, saddr)};
 316                         }
 317                     }
 318                     return null;
 319                 }});
 320 
 321         // If no specific proxy was found we return our standard list containing







 322         // only one NO_PROXY entry.
 323         return proxyArray == null ? NO_PROXY_LIST : List.of(proxyArray);
 324     }
 325 
 326     public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
 327         if (uri == null || sa == null || ioe == null) {
 328             throw new IllegalArgumentException("Arguments can't be null.");
 329         }
 330         // ignored
 331     }
 332 
 333 
 334     private int defaultPort(String protocol) {
 335         if ("http".equalsIgnoreCase(protocol)) {
 336             return 80;
 337         } else if ("https".equalsIgnoreCase(protocol)) {
 338             return 443;
 339         } else if ("ftp".equalsIgnoreCase(protocol)) {
 340             return 80;
 341         } else if ("socket".equalsIgnoreCase(protocol)) {
 342             return 1080;
 343         } else if ("gopher".equalsIgnoreCase(protocol)) {




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.spi;
  27 
  28 import java.net.InetSocketAddress;
  29 import java.net.Proxy;
  30 import java.net.ProxySelector;
  31 import java.net.SocketAddress;
  32 import java.net.URI;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.io.IOException;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import java.util.StringJoiner;
  39 import java.util.regex.Pattern;
  40 import java.util.stream.Stream;
  41 import sun.net.NetProperties;
  42 import sun.net.SocksProxy;
  43 import static java.util.regex.Pattern.quote;
  44 import static java.util.stream.Collectors.collectingAndThen;
  45 import static java.util.stream.Collectors.toList;
  46 
  47 /**
  48  * Supports proxy settings using system properties This proxy selector
  49  * provides backward compatibility with the old http protocol handler
  50  * as far as how proxy is set
  51  *
  52  * Most of the implementation copied from the old http protocol handler
  53  *
  54  * Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
  55  * proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
  56  * NOTE: need to do gopher as well
  57  */
  58 public class DefaultProxySelector extends ProxySelector {
  59 
  60     /**
  61      * This is where we define all the valid System Properties we have to
  62      * support for each given protocol.
  63      * The format of this 2 dimensional array is :
  64      * - 1 row per protocol (http, ftp, ...)
  65      * - 1st element of each row is the protocol name


 304                             if (pport == 0) {
 305                                 if (j == (props[i].length - 1)) // SOCKS
 306                                     pport = defaultPort("socket");
 307                                 else
 308                                     pport = defaultPort(proto);
 309                             }
 310                             // We did find a proxy definition.
 311                             // Let's create the address, but don't resolve it
 312                             // as this will be done at connection time
 313                             saddr = InetSocketAddress.createUnresolved(phost, pport);
 314                             // Socks is *always* the last on the list.
 315                             if (j == (props[i].length - 1)) {
 316                                 return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())};
 317                             }
 318                             return new Proxy[] {new Proxy(Proxy.Type.HTTP, saddr)};
 319                         }
 320                     }
 321                     return null;
 322                 }});
 323 
 324 
 325         if (proxyArray != null) {
 326             // Remove duplicate entries, while preserving order.
 327             return Stream.of(proxyArray).distinct().collect(
 328                     collectingAndThen(toList(), Collections::unmodifiableList));
 329         }
 330 
 331         // If no specific proxy was found, return a standard list containing
 332         // only one NO_PROXY entry.
 333         return NO_PROXY_LIST;
 334     }
 335 
 336     public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
 337         if (uri == null || sa == null || ioe == null) {
 338             throw new IllegalArgumentException("Arguments can't be null.");
 339         }
 340         // ignored
 341     }
 342 
 343 
 344     private int defaultPort(String protocol) {
 345         if ("http".equalsIgnoreCase(protocol)) {
 346             return 80;
 347         } else if ("https".equalsIgnoreCase(protocol)) {
 348             return 443;
 349         } else if ("ftp".equalsIgnoreCase(protocol)) {
 350             return 80;
 351         } else if ("socket".equalsIgnoreCase(protocol)) {
 352             return 1080;
 353         } else if ("gopher".equalsIgnoreCase(protocol)) {


< prev index next >