src/java.base/share/classes/sun/net/www/protocol/https/HttpsClient.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 53018 8bf9268df0e2
permissions -rw-r--r--
JDK-8229867: first prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
     2
 * Copyright (c) 2001, 2019, 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: 5162
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: 5162
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: 5162
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.net.www.protocol.https;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.BufferedOutputStream;
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    33
import java.net.InetAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.Socket;
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
    35
import java.net.SocketException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.*;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
    42
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Vector;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
    45
import java.util.concurrent.locks.Lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.net.www.http.HttpClient;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
    48
import sun.net.www.protocol.http.AuthenticatorKeys;
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    49
import sun.net.www.protocol.http.HttpURLConnection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.security.util.HostnameChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.security.ssl.SSLSocketImpl;
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    53
import sun.util.logging.PlatformLogger;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    54
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * This class provides HTTPS client URL support, building on the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * "sun.net.www" HTTP protocol handler.  HTTPS is the same protocol as HTTP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * but differs in the transport layer which it uses:  <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      <LI>There's a <em>Secure Sockets Layer</em> between TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      and the HTTP protocol code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      <LI>It uses a different default TCP port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      <LI>It doesn't use application level proxies, which can see and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *      manipulate HTTP user level data, compromising privacy.  It uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *      low level tunneling instead, which hides HTTP protocol and data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      from all third parties.  (Traffic analysis is still possible).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      <LI>It does basic server authentication, to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      against "URL spoofing" attacks.  This involves deciding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      whether the X.509 certificate chain identifying the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      is trusted, and verifying that the name of the server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      found in the certificate.  (The application may enable an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      anonymous SSL cipher suite, and such checks are not done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      for anonymous ciphers.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      <LI>It exposes key SSL session attributes, specifically the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      cipher suite in use and the server's X509 certificates, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      application software which knows about this protocol handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <P> System properties used include:  <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *      <LI><em>https.proxyHost</em> ... the host supporting SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *      tunneling using the conventional CONNECT syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      <LI><em>https.proxyPort</em> ... port to use on proxyHost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *      <LI><em>https.cipherSuites</em> ... comma separated list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      SSL cipher suite names to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      <LI><em>http.nonProxyHosts</em> ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
// final for export control reasons (access to APIs); remove with care
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
final class HttpsClient extends HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    implements HandshakeCompletedListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // STATIC STATE and ACCESSORS THERETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // HTTPS uses a different default port number than HTTP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int    httpsPortNumber = 443;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   113
    // default HostnameVerifier class canonical name
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   114
    private static final String defaultHVCanonicalName =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   115
            "javax.net.ssl.HttpsURLConnection.DefaultHostnameVerifier";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   116
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** Returns the default HTTPS port (443) */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   118
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected int getDefaultPort() { return httpsPortNumber; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private HostnameVerifier hv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private SSLSocketFactory sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // HttpClient.proxyDisabled will always be false, because we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // use an application-level HTTP proxy.  We might tunnel through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // our http proxy, though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // INSTANCE DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // last negotiated SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private SSLSession  session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private String [] getCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // If ciphers are assigned, sort them into an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String ciphers [];
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   139
        String cipherString =
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   140
                GetPropertyAction.privilegedGetProperty("https.cipherSuites");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52474
diff changeset
   142
        if (cipherString == null || cipherString.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ciphers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            StringTokenizer     tokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Vector<String>      v = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            tokenizer = new StringTokenizer(cipherString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            while (tokenizer.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                v.addElement(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            ciphers = new String [v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            for (int i = 0; i < ciphers.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                ciphers [i] = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return ciphers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private String [] getProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // If protocols are assigned, sort them into an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        String protocols [];
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   163
        String protocolString =
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   164
                GetPropertyAction.privilegedGetProperty("https.protocols");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52474
diff changeset
   166
        if (protocolString == null || protocolString.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            protocols = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            StringTokenizer     tokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Vector<String>      v = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            tokenizer = new StringTokenizer(protocolString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            while (tokenizer.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                v.addElement(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            protocols = new String [v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            for (int i = 0; i < protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                protocols [i] = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return protocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private String getUserAgent() {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   184
        String userAgent =
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   185
                GetPropertyAction.privilegedGetProperty("https.agent");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52474
diff changeset
   186
        if (userAgent == null || userAgent.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            userAgent = "JSSE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return userAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // CONSTRUCTOR, FACTORY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Create an HTTPS client URL.  Traffic will be tunneled through any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * intermediate nodes rather than proxied, so that confidentiality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * of data exchanged can be preserved.  However, note that all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * anonymous SSL flavors are subject to "person-in-the-middle"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * attacks against confidentiality.  If you enable use of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * flavors, you may be giving up the protection you get through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * SSL tunneling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Use New to get new HttpsClient. This constructor is meant to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * used only by New method. New properly checks for URL spoofing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   207
     * @param url https URL with which a connection must be established
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private HttpsClient(SSLSocketFactory sf, URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // HttpClient-level proxying is always disabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // because we override doConnect to do tunneling instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this(sf, url, (String)null, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *  Create an HTTPS client URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the specified proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    HttpsClient(SSLSocketFactory sf, URL url, String proxyHost, int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this(sf, url, proxyHost, proxyPort, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *  Create an HTTPS client URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the specified proxy server, with a connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    HttpsClient(SSLSocketFactory sf, URL url, String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this(sf, url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             (proxyHost == null? null:
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   235
                HttpClient.newHttpProxy(proxyHost, proxyPort, "https")),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *  Same as previous constructor except using a Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    HttpsClient(SSLSocketFactory sf, URL url, Proxy proxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        throws IOException {
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   245
        PlatformLogger logger = HttpURLConnection.getHttpLogger();
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   246
        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   247
             logger.finest("Creating new HttpsClient with url:" + url + " and proxy:" + proxy +
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   248
             " with connect timeout:" + connectTimeout);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   249
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this.proxy = proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        setSSLSocketFactory(sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        this.proxyDisabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            port = getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        openServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // This code largely ripped off from HttpClient.New, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // it uses the same keepalive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   268
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   269
                          HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws IOException {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   271
        return HttpsClient.New(sf, url, hv, true, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /** See HttpClient for the model for this method. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static HttpClient New(SSLSocketFactory sf, URL url,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   276
            HostnameVerifier hv, boolean useCache,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   277
            HttpURLConnection httpuc) throws IOException {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   278
        return HttpsClient.New(sf, url, hv, (String)null, -1, useCache, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Get a HTTPS client to the URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the specified proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   286
                           String proxyHost, int proxyPort,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   287
                           HttpURLConnection httpuc) throws IOException {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   288
        return HttpsClient.New(sf, url, hv, proxyHost, proxyPort, true, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   292
                           String proxyHost, int proxyPort, boolean useCache,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   293
                           HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throws IOException {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   295
        return HttpsClient.New(sf, url, hv, proxyHost, proxyPort, useCache, -1,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   296
                               httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                          String proxyHost, int proxyPort, boolean useCache,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   301
                          int connectTimeout, HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return HttpsClient.New(sf, url, hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                               (proxyHost == null? null :
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   306
                                HttpClient.newHttpProxy(proxyHost, proxyPort, "https")),
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   307
                               useCache, connectTimeout, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                          Proxy p, boolean useCache,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   312
                          int connectTimeout, HttpURLConnection httpuc)
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   313
        throws IOException
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   314
    {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   315
        if (p == null) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   316
            p = Proxy.NO_PROXY;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   317
        }
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   318
        PlatformLogger logger = HttpURLConnection.getHttpLogger();
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   319
        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   320
            logger.finest("Looking for HttpClient for URL " + url +
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   321
                " and proxy value of " + p);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   322
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        HttpsClient ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (useCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            /* see if one's already around */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            ret = (HttpsClient) kac.get(url, sf);
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   327
            if (ret != null && httpuc != null &&
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   328
                httpuc.streaming() &&
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   329
                httpuc.getRequestMethod() == "POST") {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   330
                if (!ret.available())
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   331
                    ret = null;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   332
            }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   333
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (ret != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   335
                String ak = httpuc == null ? AuthenticatorKeys.DEFAULT
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   336
                     : httpuc.getAuthenticatorKey();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   337
                boolean compatible = ((ret.proxy != null && ret.proxy.equals(p)) ||
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   338
                    (ret.proxy == null && p == Proxy.NO_PROXY))
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   339
                     && Objects.equals(ret.getAuthenticatorKey(), ak);
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   340
                Lock lock = ret.clientLock;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   341
                if (compatible) {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   342
                    lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   343
                    try {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   344
                        ret.cachedHttpClient = true;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   345
                        assert ret.inCache;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   346
                        ret.inCache = false;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   347
                        if (httpuc != null && ret.needsTunneling())
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   348
                            httpuc.setTunnelState(TUNNELING);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16499
diff changeset
   349
                        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   350
                            logger.finest("KeepAlive stream retrieved from the cache, " + ret);
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   351
                        }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   352
                    } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   353
                        lock.unlock();
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   354
                    }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   355
                } else {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   356
                    // We cannot return this connection to the cache as it's
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   357
                    // KeepAliveTimeout will get reset. We simply close the connection.
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   358
                    // This should be fine as it is very rare that a connection
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   359
                    // to the same host will not use the same proxy.
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   360
                    lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   361
                    try {
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   362
                        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   363
                            logger.finest("Not returning this connection to cache: " + ret);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   364
                        }
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   365
                        ret.inCache = false;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   366
                        ret.closeServer();
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   367
                    } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53018
diff changeset
   368
                        lock.unlock();
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   369
                    }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   370
                    ret = null;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   371
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            ret = new HttpsClient(sf, url, p, connectTimeout);
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   376
            if (httpuc != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   377
                ret.authenticatorKey = httpuc.getAuthenticatorKey();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   378
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (security != null) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   382
                if (ret.proxy == Proxy.NO_PROXY || ret.proxy == null) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   383
                    security.checkConnect(InetAddress.getByName(url.getHost()).getHostAddress(), url.getPort());
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   384
                } else {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   385
                    security.checkConnect(url.getHost(), url.getPort());
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   386
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            ret.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        ret.setHostnameVerifier(hv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    // METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    void setHostnameVerifier(HostnameVerifier hv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        this.hv = hv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    void setSSLSocketFactory(SSLSocketFactory sf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        sslSocketFactory = sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    SSLSocketFactory getSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   408
    /**
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   409
     * The following method, createSocket, is defined in NetworkClient
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   410
     * and overridden here so that the socket facroty is used to create
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   411
     * new sockets.
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   412
     */
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   413
    @Override
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   414
    protected Socket createSocket() throws IOException {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   415
        try {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   416
            return sslSocketFactory.createSocket();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   417
        } catch (SocketException se) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   418
            //
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   419
            // bug 6771432
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   420
            // javax.net.SocketFactory throws a SocketException with an
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   421
            // UnsupportedOperationException as its cause to indicate that
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   422
            // unconnected sockets have not been implemented.
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   423
            //
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   424
            Throwable t = se.getCause();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   425
            if (t != null && t instanceof UnsupportedOperationException) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   426
                return super.createSocket();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   427
            } else {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   428
                throw se;
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   429
            }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   430
        }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   431
    }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   432
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   433
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   434
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public boolean needsTunneling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return (proxy != null && proxy.type() != Proxy.Type.DIRECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                && proxy.type() != Proxy.Type.SOCKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void afterConnect() throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (!isCachedConnection()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            SSLSocket s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            SSLSocketFactory factory = sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (!(serverSocket instanceof SSLSocket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    s = (SSLSocket)factory.createSocket(serverSocket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                                        host, port, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    s = (SSLSocket)serverSocket;
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   451
                    if (s instanceof SSLSocketImpl) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   452
                        ((SSLSocketImpl)s).setHost(host);
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   453
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // If we fail to connect through the tunnel, try it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                // locally, as a last resort.  If this doesn't work,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                // throw the original exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    s = (SSLSocket)factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                } catch (IOException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // Force handshaking, so that we get any authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            // Register a handshake callback so our session state tracks any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // later session renegotiations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            String [] protocols = getProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            String [] ciphers = getCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (protocols != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                s.setEnabledProtocols(protocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (ciphers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                s.setEnabledCipherSuites(ciphers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            s.addHandshakeCompletedListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   481
            // We have two hostname verification approaches. One is in
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   482
            // SSL/TLS socket layer, where the algorithm is configured with
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   483
            // SSLParameters.setEndpointIdentificationAlgorithm(), and the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   484
            // hostname verification is done by X509ExtendedTrustManager when
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   485
            // the algorithm is "HTTPS". The other one is in HTTPS layer,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   486
            // where the algorithm is customized by
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   487
            // HttpsURLConnection.setHostnameVerifier(), and the hostname
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   488
            // verification is done by HostnameVerifier when the default
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   489
            // rules for hostname verification fail.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   490
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   491
            // The relationship between two hostname verification approaches
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   492
            // likes the following:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   493
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   494
            //               |             EIA algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   495
            //               +----------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   496
            //               |     null      |   HTTPS    |   LDAP/other   |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   497
            // -------------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   498
            //     |         |1              |2           |3               |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   499
            // HNV | default | Set HTTPS EIA | use EIA    | HTTPS          |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   500
            //     |--------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   501
            //     | non -   |4              |5           |6               |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   502
            //     | default | HTTPS/HNV     | use EIA    | HTTPS/HNV      |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   503
            // -------------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   504
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   505
            // Abbreviation:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   506
            //     EIA: the endpoint identification algorithm in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   507
            //           socket layer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   508
            //     HNV: the hostname verification object in HTTPS layer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   509
            // Notes:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   510
            //     case 1. default HNV and EIA is null
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   511
            //           Set EIA as HTTPS, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   512
            //           layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   513
            //     case 2. default HNV and EIA is HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   514
            //           Use existing EIA, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   515
            //           layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   516
            //     case 3. default HNV and EIA is other than HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   517
            //           Use existing EIA, EIA check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   518
            //           layer, then do HTTPS check in HTTPS layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   519
            //     case 4. non-default HNV and EIA is null
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   520
            //           No EIA, no EIA check done in SSL/TLS layer, then do
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   521
            //           HTTPS check in HTTPS layer using HNV as override.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   522
            //     case 5. non-default HNV and EIA is HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   523
            //           Use existing EIA, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   524
            //           layer. No HNV override possible. We will review this
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   525
            //           decision and may update the architecture for JDK 7.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   526
            //     case 6. non-default HNV and EIA is other than HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   527
            //           Use existing EIA, EIA check done in SSL/TLS layer,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   528
            //           then do HTTPS check in HTTPS layer as override.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   529
            boolean needToCheckSpoofing = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   530
            String identification =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   531
                s.getSSLParameters().getEndpointIdentificationAlgorithm();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   532
            if (identification != null && identification.length() != 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   533
                if (identification.equalsIgnoreCase("HTTPS")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   534
                    // Do not check server identity again out of SSLSocket,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   535
                    // the endpoint will be identified during TLS handshaking
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   536
                    // in SSLSocket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   537
                    needToCheckSpoofing = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   538
                }   // else, we don't understand the identification algorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   539
                    // need to check URL spoofing here.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   540
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   541
                boolean isDefaultHostnameVerifier = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   542
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   543
                // We prefer to let the SSLSocket do the spoof checks, but if
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   544
                // the application has specified a HostnameVerifier (HNV),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   545
                // we will always use that.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   546
                if (hv != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   547
                    String canonicalName = hv.getClass().getCanonicalName();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   548
                    if (canonicalName != null &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   549
                    canonicalName.equalsIgnoreCase(defaultHVCanonicalName)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   550
                        isDefaultHostnameVerifier = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   551
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   552
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   553
                    // Unlikely to happen! As the behavior is the same as the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   554
                    // default hostname verifier, so we prefer to let the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   555
                    // SSLSocket do the spoof checks.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   556
                    isDefaultHostnameVerifier = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   557
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   558
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   559
                if (isDefaultHostnameVerifier) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   560
                    // If the HNV is the default from HttpsURLConnection, we
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   561
                    // will do the spoof checks in SSLSocket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   562
                    SSLParameters paramaters = s.getSSLParameters();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   563
                    paramaters.setEndpointIdentificationAlgorithm("HTTPS");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   564
                    s.setSSLParameters(paramaters);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   565
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   566
                    needToCheckSpoofing = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   567
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            s.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            session = s.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            // change the serverSocket and serverOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            serverSocket = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                serverOutput = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    new BufferedOutputStream(serverSocket.getOutputStream()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    false, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            } catch (UnsupportedEncodingException e) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   579
                throw new InternalError(encoding+" encoding not found");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            // check URL spoofing if it has not been checked under handshaking
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   583
            if (needToCheckSpoofing) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                checkURLSpoofing(hv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // if we are reusing a cached https session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            // we don't need to do handshaking etc. But we do need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // set the ssl session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            session = ((SSLSocket)serverSocket).getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    // Server identity checking is done according to RFC 2818: HTTP over TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    // Section 3.1 Server Identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    private void checkURLSpoofing(HostnameVerifier hostnameVerifier)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   597
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // Get authenticated server name, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        // if IPv6 strip off the "[]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (host != null && host.startsWith("[") && host.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            host = host.substring(1, host.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        Certificate[] peerCerts = null;
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 98
diff changeset
   609
        String cipher = session.getCipherSuite();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            HostnameChecker checker = HostnameChecker.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                                HostnameChecker.TYPE_TLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   614
            // get the subject's certificate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   615
            peerCerts = session.getPeerCertificates();
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   616
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   617
            X509Certificate peerCert;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   618
            if (peerCerts[0] instanceof
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   619
                    java.security.cert.X509Certificate) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   620
                peerCert = (java.security.cert.X509Certificate)peerCerts[0];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   621
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   622
                throw new SSLPeerUnverifiedException("");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   624
            checker.match(host, peerCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // if it doesn't throw an exception, we passed. Return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        } catch (SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // client explicitly changed default policy and enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            // anonymous ciphers; we can't check the standard policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        } catch (java.security.cert.CertificateException cpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if ((cipher != null) && (cipher.indexOf("_anon_") != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } else if ((hostnameVerifier != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                   (hostnameVerifier.verify(host, session))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        throw new IOException("HTTPS hostname wrong:  should be <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                              + url.getHost() + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   654
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    protected void putInKeepAliveCache() {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   656
        if (inCache) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   657
            assert false : "Duplicate put to keep alive cache";
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   658
            return;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   659
        }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   660
        inCache = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        kac.put(url, sslSocketFactory, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   664
    /*
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   665
     * Close an idle connection to this URL (if it exists in the cache).
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   666
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   667
    @Override
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   668
    public void closeIdleConnection() {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   669
        HttpClient http = kac.get(url, sslSocketFactory);
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   670
        if (http != null) {
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   671
            http.closeServer();
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   672
        }
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   673
    }
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   674
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Returns the cipher suite in use on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    String getCipherSuite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return session.getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Returns the certificate chain the client sent to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * server, or null if the client did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public java.security.cert.Certificate [] getLocalCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Returns the certificate chain with which the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * authenticated itself, or throw a SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * if the server did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    java.security.cert.Certificate [] getServerCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            throws SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Returns the principal with which the server authenticated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * itself, or throw a SSLPeerUnverifiedException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * server did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    Principal getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            throws SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            principal = session.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            // if the provider does not support it, fallback to peer certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            // return the X500Principal of the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            java.security.cert.Certificate[] certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        session.getPeerCertificates();
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   717
            principal = ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Returns the principal the client sent to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * server, or null if the client did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    Principal getLocalPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            principal = session.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // if the provider does not support it, fallback to local certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            // return the X500Principal of the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            java.security.cert.Certificate[] certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                        session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (certs != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   738
                principal = ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
52474
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   745
     * Returns the {@code SSLSession} in use on this connection.
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   746
     */
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   747
    SSLSession getSSLSession() {
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   748
        return session;
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   749
    }
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   750
13266dac5fdb 8212261: Add SSLSession accessors to HttpsURLConnection and SecureCacheResponse
xuelei
parents: 50768
diff changeset
   751
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * This method implements the SSL HandshakeCompleted callback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * remembering the resulting session so that it may be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * for the current cipher suite and peer certificates.  Servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * sometimes re-initiate handshaking, so the session in use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * a given connection may change.  When sessions change, so may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * peer identities and cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public void handshakeCompleted(HandshakeCompletedEvent event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        session = event.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return the proxy host being used for this client, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          if we're not going through a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   768
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public String getProxyHostUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (!needsTunneling()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } else {
5160
c0e0c9a9d338 6632169: HttpClient and HttpsClient should not try to reverse lookup IP address of a proxy server
chegar
parents: 4236
diff changeset
   773
            return super.getProxyHostUsed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return the proxy port being used for this client.  Meaningless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *          if getProxyHostUsed() gives null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   781
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    public int getProxyPortUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        return (proxy == null || proxy.type() == Proxy.Type.DIRECT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                proxy.type() == Proxy.Type.SOCKS)? -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            ((InetSocketAddress)proxy.address()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}