jdk/src/share/classes/sun/net/spi/DefaultProxySelector.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7982 65f5328a67a2
child 12047 320a714614e9
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7982
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3855
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3855
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3855
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3855
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3855
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    28
import java.net.InetSocketAddress;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    29
import java.net.Proxy;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    30
import java.net.ProxySelector;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    31
import java.net.SocketAddress;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    32
import java.net.URI;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    33
import java.util.ArrayList;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    34
import java.util.List;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    35
import java.util.StringTokenizer;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    36
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.misc.RegexpPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedAction;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    40
import sun.net.NetProperties;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    41
import sun.net.SocksProxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Supports proxy settings using system properties This proxy selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * provides backward compatibility with the old http protocol handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * as far as how proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Most of the implementation copied from the old http protocol handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * NOTE: need to do gopher as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class DefaultProxySelector extends ProxySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * This is where we define all the valid System Properties we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * support for each given protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The format of this 2 dimensional array is :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * - 1 row per protocol (http, ftp, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * - 1st element of each row is the protocol name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * - subsequent elements are prefixes for Host & Port properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *   listed in order of priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * means for FTP we try in that oder:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *          + ftp.proxyHost & ftp.proxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *          + ftpProxyHost & ftpProxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *          + proxyHost & proxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *          + socksProxyHost & socksProxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Note that the socksProxy should *always* be the last on the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    final static String[][] props = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * protocol, Property prefix 1, Property prefix 2, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        {"http", "http.proxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        {"https", "https.proxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        {"gopher", "gopherProxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        {"socket", "socksProxy"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    85
    private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static boolean hasSystemProxies = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        final String key = "java.net.useSystemProxies";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    91
        Boolean b = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    92
            new PrivilegedAction<Boolean>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    93
                public Boolean run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return NetProperties.getBoolean(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (b != null && b.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                      new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            hasSystemProxies = init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * How to deal with "non proxy hosts":
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * since we do have to generate a RegexpPool we don't want to do that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * it's not necessary. Therefore we do cache the result, on a per-protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * basis, and change it only when the "source", i.e. the system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * did change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static class NonProxyInfo {
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   112
        // Default value for nonProxyHosts, this provides backward compatibility
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   113
        // by excluding localhost and its litteral notations.
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   114
        static final String defStringVal = "localhost|127.*|[::1]";
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        String hostsSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        RegexpPool hostsPool;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   118
        final String property;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   119
        final String defaultVal;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   120
        static NonProxyInfo ftpNonProxyInfo = new NonProxyInfo("ftp.nonProxyHosts", null, null, defStringVal);
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   121
        static NonProxyInfo httpNonProxyInfo = new NonProxyInfo("http.nonProxyHosts", null, null, defStringVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   123
        NonProxyInfo(String p, String s, RegexpPool pool, String d) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            property = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            hostsSource = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            hostsPool = pool;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   127
            defaultVal = d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * select() method. Where all the hard work is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Build a list of proxies depending on URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Since we're only providing compatibility with the system properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * from previous releases (see list above), that list will always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * contain 1 single proxy, default being NO_PROXY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public java.util.List<Proxy> select(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (uri == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new IllegalArgumentException("URI can't be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String protocol = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // This is a hack to ensure backward compatibility in two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // cases: 1. hostnames contain non-ascii characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // internationalized domain names. in which case, URI will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // return null, see BugID 4957669; 2. Some hostnames can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // contain '_' chars even though it's not supposed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // legal, in which case URI will return null for getHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // but not for getAuthority() See BugID 4913253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            String auth = uri.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (auth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                i = auth.indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    auth = auth.substring(i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                i = auth.lastIndexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    auth = auth.substring(0,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                host = auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (protocol == null || host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        List<Proxy> proxyl = new ArrayList<Proxy>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        NonProxyInfo pinfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if ("http".equalsIgnoreCase(protocol)) {
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   177
            pinfo = NonProxyInfo.httpNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else if ("https".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // HTTPS uses the same property as HTTP, for backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // compatibility
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   181
            pinfo = NonProxyInfo.httpNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else if ("ftp".equalsIgnoreCase(protocol)) {
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   183
            pinfo = NonProxyInfo.ftpNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * Let's check the System properties for that protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        final String proto = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        final NonProxyInfo nprop = pinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        final String urlhost = host.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * This is one big doPrivileged call, but we're trying to optimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * the code as much as possible. Since we're checking quite a few
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * System properties it does help having only 1 call to doPrivileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * Be mindful what you do in here though!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   199
        Proxy p = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   200
            new PrivilegedAction<Proxy>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   201
                public Proxy run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    String phost =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    int pport = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    String nphosts =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    InetSocketAddress saddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    // Then let's walk the list of protocols in our array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    for (i=0; i<props.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        if (props[i][0].equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            for (j = 1; j < props[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                /* System.getProp() will give us an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                 * String, "" for a defined but "empty"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                 * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                phost =  NetProperties.get(props[i][j]+"Host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                if (phost != null && phost.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            if (phost == null || phost.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                 * No system property defined for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                 * protocol. Let's check System Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                 * settings (Gnome & Windows) if we were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                 * instructed to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                if (hasSystemProxies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                    String sproto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                    if (proto.equalsIgnoreCase("socket"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        sproto = "socks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                        sproto = proto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                    Proxy sproxy = getSystemProxy(sproto, urlhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                    if (sproxy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                        return sproxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                return Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            // If a Proxy Host is defined for that protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            // Let's get the NonProxyHosts property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            if (nprop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                nphosts = NetProperties.get(nprop.property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                synchronized (nprop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                    if (nphosts == null) {
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   246
                                        if (nprop.defaultVal != null) {
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   247
                                            nphosts = nprop.defaultVal;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   248
                                        } else {
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   249
                                            nprop.hostsSource = null;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   250
                                            nprop.hostsPool = null;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   251
                                        }
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   252
                                    }
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   253
                                    if (nphosts != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                        if (!nphosts.equals(nprop.hostsSource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                            RegexpPool pool = new RegexpPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                            StringTokenizer st = new StringTokenizer(nphosts, "|", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                                while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                    pool.add(st.nextToken().toLowerCase(), Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                            } catch (sun.misc.REException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                            nprop.hostsPool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                            nprop.hostsSource = nphosts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                    if (nprop.hostsPool != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        nprop.hostsPool.match(urlhost) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                        return Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            // We got a host, let's check for port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            pport = NetProperties.getInteger(props[i][j]+"Port", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                            if (pport == 0 && j < (props[i].length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                // Can't find a port with same prefix as Host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                // AND it's not a SOCKS proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                // Let's try the other prefixes for that proto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                for (int k = 1; k < (props[i].length - 1); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                    if ((k != j) && (pport == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                        pport = NetProperties.getInteger(props[i][k]+"Port", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            // Still couldn't find a port, let's use default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            if (pport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                if (j == (props[i].length - 1)) // SOCKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                    pport = defaultPort("socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                    pport = defaultPort(proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            // We did find a proxy definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            // Let's create the address, but don't resolve it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            // as this will be done at connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            saddr = InetSocketAddress.createUnresolved(phost, pport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            // Socks is *always* the last on the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if (j == (props[i].length - 1)) {
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
   299
                                int version = NetProperties.getInteger(SOCKS_PROXY_VERSION, 5).intValue();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
   300
                                return SocksProxy.create(saddr, version);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                return new Proxy(Proxy.Type.HTTP, saddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    return Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        proxyl.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * If no specific property was set for that URI, we should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * returning an iterator to an empty List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return proxyl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (uri == null || sa == null || ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new IllegalArgumentException("Arguments can't be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private int defaultPort(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if ("http".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else if ("https".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return 443;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else if ("ftp".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else if ("socket".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return 1080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else if ("gopher".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private native static boolean init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private native Proxy getSystemProxy(String protocol, String host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}