src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 52725 c470f977ade8
child 54116 d21803f47416
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49435
5fd26ab491fa 8144300: http.nonProxyHosts value having wildcard * both at end and start are not honored
rpatil
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018 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;
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    33
import java.util.Collections;
7982
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.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
    38
import java.util.StringJoiner;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
    39
import java.util.regex.Pattern;
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    40
import java.util.stream.Stream;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    41
import sun.net.NetProperties;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    42
import sun.net.SocksProxy;
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
    43
import static java.util.regex.Pattern.quote;
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    44
import static java.util.stream.Collectors.collectingAndThen;
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    45
import static java.util.stream.Collectors.toList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Supports proxy settings using system properties This proxy selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * provides backward compatibility with the old http protocol handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * as far as how proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Most of the implementation copied from the old http protocol handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class DefaultProxySelector extends ProxySelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * This is where we define all the valid System Properties we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * support for each given protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The format of this 2 dimensional array is :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * - 1 row per protocol (http, ftp, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * - 1st element of each row is the protocol name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * - subsequent elements are prefixes for Host & Port properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *   listed in order of priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * means for FTP we try in that oder:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *          + ftp.proxyHost & ftp.proxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *          + ftpProxyHost & ftpProxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *          + proxyHost & proxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *          + socksProxyHost & socksProxyPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Note that the socksProxy should *always* be the last on the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31529
diff changeset
    77
    static final String[][] props = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * protocol, Property prefix 1, Property prefix 2, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        {"http", "http.proxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        {"https", "https.proxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        {"socket", "socksProxy"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    87
    private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 5506
diff changeset
    88
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static boolean hasSystemProxies = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    91
    private static final List<Proxy> NO_PROXY_LIST = List.of(Proxy.NO_PROXY);
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        final String key = "java.net.useSystemProxies";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    95
        Boolean b = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    96
            new PrivilegedAction<Boolean>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    97
                public Boolean run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    return NetProperties.getBoolean(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (b != null && b.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   102
                new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   103
                    public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   104
                        System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   105
                        return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   106
                    }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12047
diff changeset
   107
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            hasSystemProxies = init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   112
    public static int socksProxyVersion() {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   113
        return AccessController.doPrivileged(
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   114
                new PrivilegedAction<Integer>() {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   115
                    @Override public Integer run() {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   116
                        return NetProperties.getInteger(SOCKS_PROXY_VERSION, 5);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   117
                    }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   118
                });
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   119
    }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 25859
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * How to deal with "non proxy hosts":
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   123
     * since we do have to generate a pattern we don't want to do that if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * it's not necessary. Therefore we do cache the result, on a per-protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * basis, and change it only when the "source", i.e. the system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * did change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static class NonProxyInfo {
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   130
        // 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
   131
        // by excluding localhost and its litteral notations.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   132
        static final String defStringVal = "localhost|127.*|[::1]|0.0.0.0|[::0]";
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String hostsSource;
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   135
        Pattern pattern;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   136
        final String property;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   137
        final String defaultVal;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   138
        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
   139
        static NonProxyInfo httpNonProxyInfo = new NonProxyInfo("http.nonProxyHosts", null, null, defStringVal);
16737
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 14342
diff changeset
   140
        static NonProxyInfo socksNonProxyInfo = new NonProxyInfo("socksNonProxyHosts", null, null, defStringVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   142
        NonProxyInfo(String p, String s, Pattern pattern, String d) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            property = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            hostsSource = s;
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   145
            this.pattern = pattern;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   146
            defaultVal = d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * select() method. Where all the hard work is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Build a list of proxies depending on URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Since we're only providing compatibility with the system properties
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   155
     * from previous releases (see list above), that list will typically
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   156
     * contain one single proxy, default being NO_PROXY.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   157
     * If we can get a system proxy it might contain more entries.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public java.util.List<Proxy> select(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (uri == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IllegalArgumentException("URI can't be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String protocol = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // This is a hack to ensure backward compatibility in two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // cases: 1. hostnames contain non-ascii characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // internationalized domain names. in which case, URI will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // return null, see BugID 4957669; 2. Some hostnames can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // contain '_' chars even though it's not supposed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // legal, in which case URI will return null for getHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // but not for getAuthority() See BugID 4913253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            String auth = uri.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (auth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                i = auth.indexOf('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    auth = auth.substring(i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                i = auth.lastIndexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    auth = auth.substring(0,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                host = auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (protocol == null || host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        NonProxyInfo pinfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ("http".equalsIgnoreCase(protocol)) {
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   196
            pinfo = NonProxyInfo.httpNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else if ("https".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // HTTPS uses the same property as HTTP, for backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // compatibility
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   200
            pinfo = NonProxyInfo.httpNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else if ("ftp".equalsIgnoreCase(protocol)) {
2429
8ea224e457c6 6819122: DefaultProxySelector should lazily initialize the Pattern object and the NonProxyInfo objects
mchung
parents: 715
diff changeset
   202
            pinfo = NonProxyInfo.ftpNonProxyInfo;
16737
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 14342
diff changeset
   203
        } else if ("socket".equalsIgnoreCase(protocol)) {
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 14342
diff changeset
   204
            pinfo = NonProxyInfo.socksNonProxyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * Let's check the System properties for that protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final String proto = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        final NonProxyInfo nprop = pinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        final String urlhost = host.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * This is one big doPrivileged call, but we're trying to optimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * the code as much as possible. Since we're checking quite a few
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * System properties it does help having only 1 call to doPrivileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * Be mindful what you do in here though!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         */
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   220
        Proxy[] proxyArray = AccessController.doPrivileged(
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   221
            new PrivilegedAction<Proxy[]>() {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   222
                public Proxy[] run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    String phost =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    int pport = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    String nphosts =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    InetSocketAddress saddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    // Then let's walk the list of protocols in our array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    for (i=0; i<props.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        if (props[i][0].equalsIgnoreCase(proto)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            for (j = 1; j < props[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                /* System.getProp() will give us an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                 * String, "" for a defined but "empty"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                 * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                phost =  NetProperties.get(props[i][j]+"Host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                if (phost != null && phost.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52725
diff changeset
   241
                            if (phost == null || phost.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                 * No system property defined for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                 * protocol. Let's check System Proxy
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   245
                                 * settings (Gnome, MacOsX & Windows) if
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   246
                                 * we were instructed to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                if (hasSystemProxies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                    String sproto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                    if (proto.equalsIgnoreCase("socket"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                        sproto = "socks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                        sproto = proto;
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   254
                                    return getSystemProxies(sproto, urlhost);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                }
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   256
                                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            // If a Proxy Host is defined for that protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            // Let's get the NonProxyHosts property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            if (nprop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                nphosts = NetProperties.get(nprop.property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                synchronized (nprop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                    if (nphosts == null) {
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   264
                                        if (nprop.defaultVal != null) {
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   265
                                            nphosts = nprop.defaultVal;
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   266
                                        } else {
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   267
                                            nprop.hostsSource = null;
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   268
                                            nprop.pattern = null;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   269
                                        }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52725
diff changeset
   270
                                    } else if (!nphosts.isEmpty()) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   271
                                        // add the required default patterns
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   272
                                        // but only if property no set. If it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   273
                                        // is empty, leave empty.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   274
                                        nphosts += "|" + NonProxyInfo
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   275
                                                         .defStringVal;
3855
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   276
                                    }
653df3a7256d 6737819: sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
jccollet
parents: 2429
diff changeset
   277
                                    if (nphosts != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                        if (!nphosts.equals(nprop.hostsSource)) {
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   279
                                            nprop.pattern = toPattern(nphosts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                            nprop.hostsSource = nphosts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                    }
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   283
                                    if (shouldNotUseProxyFor(nprop.pattern, urlhost)) {
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   284
                                        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            // We got a host, let's check for port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            pport = NetProperties.getInteger(props[i][j]+"Port", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            if (pport == 0 && j < (props[i].length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                // Can't find a port with same prefix as Host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                // AND it's not a SOCKS proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                // Let's try the other prefixes for that proto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                for (int k = 1; k < (props[i].length - 1); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                    if ((k != j) && (pport == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                        pport = NetProperties.getInteger(props[i][k]+"Port", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            // Still couldn't find a port, let's use default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            if (pport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                if (j == (props[i].length - 1)) // SOCKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                    pport = defaultPort("socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                    pport = defaultPort(proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            // We did find a proxy definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            // Let's create the address, but don't resolve it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            // as this will be done at connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            saddr = InetSocketAddress.createUnresolved(phost, pport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            // Socks is *always* the last on the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            if (j == (props[i].length - 1)) {
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   314
                                return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                            }
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   316
                            return new Proxy[] {new Proxy(Proxy.Type.HTTP, saddr)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   319
                    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   323
        if (proxyArray != null) {
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   324
            // Remove duplicate entries, while preserving order.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   325
            return Stream.of(proxyArray).distinct().collect(
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   326
                    collectingAndThen(toList(), Collections::unmodifiableList));
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   327
        }
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   328
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   329
        // If no specific proxy was found, return a standard list containing
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   330
        // only one NO_PROXY entry.
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   331
        return NO_PROXY_LIST;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (uri == null || sa == null || ioe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw new IllegalArgumentException("Arguments can't be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // ignored
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 int defaultPort(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if ("http".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else if ("https".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return 443;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else if ("ftp".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } else if ("socket".equalsIgnoreCase(protocol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return 1080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31529
diff changeset
   356
    private static native boolean init();
43501
243c346dc905 8170868: DefaultProxySelector should use system defaults on Windows, MacOS and Gnome
clanger
parents: 32649
diff changeset
   357
    private synchronized native Proxy[] getSystemProxies(String protocol, String host);
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   358
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   359
    /**
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   360
     * @return {@code true} if given this pattern for non-proxy hosts and this
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   361
     *         urlhost the proxy should NOT be used to access this urlhost
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   362
     */
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   363
    static boolean shouldNotUseProxyFor(Pattern pattern, String urlhost) {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   364
        if (pattern == null || urlhost.isEmpty())
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   365
            return false;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   366
        boolean matches = pattern.matcher(urlhost).matches();
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   367
        return matches;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   368
    }
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   369
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   370
    /**
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   371
     * @param mask non-null mask
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   372
     * @return {@link java.util.regex.Pattern} corresponding to this mask
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   373
     *         or {@code null} in case mask should not match anything
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   374
     */
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   375
    static Pattern toPattern(String mask) {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   376
        boolean disjunctionEmpty = true;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   377
        StringJoiner joiner = new StringJoiner("|");
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   378
        for (String disjunct : mask.split("\\|")) {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   379
            if (disjunct.isEmpty())
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   380
                continue;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   381
            disjunctionEmpty = false;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   382
            String regex = disjunctToRegex(disjunct.toLowerCase());
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   383
            joiner.add(regex);
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   384
        }
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   385
        return disjunctionEmpty ? null : Pattern.compile(joiner.toString());
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   386
    }
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   387
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   388
    /**
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   389
     * @param disjunct non-null mask disjunct
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   390
     * @return java regex string corresponding to this mask
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   391
     */
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   392
    static String disjunctToRegex(String disjunct) {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   393
        String regex;
49435
5fd26ab491fa 8144300: http.nonProxyHosts value having wildcard * both at end and start are not honored
rpatil
parents: 47216
diff changeset
   394
        if (disjunct.startsWith("*") && disjunct.endsWith("*")) {
5fd26ab491fa 8144300: http.nonProxyHosts value having wildcard * both at end and start are not honored
rpatil
parents: 47216
diff changeset
   395
            regex = ".*" + quote(disjunct.substring(1, disjunct.length() - 1)) + ".*";
5fd26ab491fa 8144300: http.nonProxyHosts value having wildcard * both at end and start are not honored
rpatil
parents: 47216
diff changeset
   396
        } else if (disjunct.startsWith("*")) {
23583
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   397
            regex = ".*" + quote(disjunct.substring(1));
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   398
        } else if (disjunct.endsWith("*")) {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   399
            regex = quote(disjunct.substring(0, disjunct.length() - 1)) + ".*";
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   400
        } else {
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   401
            regex = quote(disjunct);
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   402
        }
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   403
        return regex;
1bee92aa2f1f 8035158: Remove dependency on sun.misc.RegexpPool and friends
chegar
parents: 23010
diff changeset
   404
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
}