src/java.base/share/classes/sun/net/www/http/HttpClient.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 53563 a4b7ea85d668
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: 53563
diff changeset
     2
 * Copyright (c) 1994, 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: 3866
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: 3866
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: 3866
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3866
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3866
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
    30
import java.util.Locale;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
    31
import java.util.Objects;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
    32
import java.util.Properties;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
    33
import java.util.concurrent.locks.Lock;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
    34
import java.util.concurrent.locks.ReentrantLock;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
    35
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.NetworkClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.ProgressSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.net.www.MeteredStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.net.www.ParseUtil;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
    42
import sun.net.www.protocol.http.AuthenticatorKeys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.net.www.protocol.http.HttpURLConnection;
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3288
diff changeset
    44
import sun.util.logging.PlatformLogger;
14008
b572c1f3a7ad 7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents: 14007
diff changeset
    45
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
    46
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Dave Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class HttpClient extends NetworkClient {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
    53
    protected final ReentrantLock clientLock = new ReentrantLock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // whether this httpclient comes from the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected boolean cachedHttpClient = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 16477
diff changeset
    58
    protected boolean inCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Http requests we send
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    MessageHeader requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Http data we send with the headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    PosterOutputStream poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
6673
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
    66
    // true if we are in streaming mode (fixed length or chunked)
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
    67
    boolean streaming;
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // if we've had one io error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    boolean failedOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Response code for CONTINUE */
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
    73
    private boolean ignoreContinue = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int    HTTP_CONTINUE = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** Default port number for http daemons. REMIND: make these private */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final int    httpPortNumber = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** return default port number (subclasses may override) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected int getDefaultPort () { return httpPortNumber; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
    82
    private static int getDefaultPort(String proto) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ("http".equalsIgnoreCase(proto))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if ("https".equalsIgnoreCase(proto))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return 443;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* All proxying (generic as well as instance-specific) may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * disabled through use of this flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected boolean proxyDisabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // are we using proxy in this instance?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public boolean usingProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // target host, port for the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* where we cache currently open, persistent connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected static KeepAliveCache kac = new KeepAliveCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static boolean keepAliveProp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // retryPostProp is true by default so as to preserve behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // from previous releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static boolean retryPostProp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   110
    /* Value of the system property jdk.ntlm.cache;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   111
       if false, then NTLM connections will not be cached.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   112
       The default value is 'true'. */
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   113
    private static final boolean cacheNTLMProp;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   114
    /* Value of the system property jdk.spnego.cache;
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   115
       if false, then connections authentified using the Negotiate/Kerberos
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   116
       scheme will not be cached.
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   117
       The default value is 'true'. */
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   118
    private static final boolean cacheSPNEGOProp;
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   119
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 32649
diff changeset
   120
    volatile boolean keepingAlive;    /* this is a keep-alive connection */
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   121
    volatile boolean disableKeepAlive;/* keep-alive has been disabled for this
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   122
                                         connection - this will be used when
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   123
                                         recomputing the value of keepingAlive */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int keepAliveConnections = -1;    /* number of keep-alives left */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**Idle timeout value, in milliseconds. Zero means infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * iff keepingAlive=true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Unfortunately, we can't always believe this one.  If I'm connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * through a Netscape proxy to a server that sent me a keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * time of 15 sec, the proxy unilaterally terminates my connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * after 5 sec.  So we have to hard code our effective timeout to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * 4 sec for the case where we're using a proxy. *SIGH*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    int keepAliveTimeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** whether the response is to be cached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private CacheRequest cacheRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /** Url being fetched. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected URL       url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* if set, the client will be reused and must not be put in cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   145
    // Traffic capture tool, if configured. See HttpCapture class for info
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   146
    private HttpCapture capture = null;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   147
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   148
    private static final PlatformLogger logger = HttpURLConnection.getHttpLogger();
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   149
    private static void logFinest(String msg) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16499
diff changeset
   150
        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   151
            logger.finest(msg);
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   152
        }
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   153
    }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   154
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   155
    protected volatile String authenticatorKey;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   156
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * A NOP method kept for backwards binary compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @deprecated -- system properties are no longer cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static synchronized void resetProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    int getKeepAliveTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return keepAliveTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   170
        Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
   171
        String keepAlive = props.getProperty("http.keepAlive");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
   172
        String retryPost = props.getProperty("sun.net.http.retryPost");
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   173
        String cacheNTLM = props.getProperty("jdk.ntlm.cache");
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   174
        String cacheSPNEGO = props.getProperty("jdk.spnego.cache");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (keepAlive != null) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
   177
            keepAliveProp = Boolean.parseBoolean(keepAlive);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            keepAliveProp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (retryPost != null) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34943
diff changeset
   183
            retryPostProp = Boolean.parseBoolean(retryPost);
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   184
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            retryPostProp = true;
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   186
        }
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   187
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   188
        if (cacheNTLM != null) {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   189
            cacheNTLMProp = Boolean.parseBoolean(cacheNTLM);
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   190
        } else {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   191
            cacheNTLMProp = true;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   194
        if (cacheSPNEGO != null) {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   195
            cacheSPNEGOProp = Boolean.parseBoolean(cacheSPNEGO);
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   196
        } else {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   197
            cacheSPNEGOProp = true;
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   198
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return true iff http keep alive is set (i.e. enabled).  Defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          to true if the system property http.keepAlive isn't set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public boolean getHttpKeepAliveSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return keepAliveProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected HttpClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private HttpClient(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this(url, (String)null, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected HttpClient(URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                         boolean proxyDisabled) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this(url, null, -1, proxyDisabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /* This package-only CTOR should only be used for FTP piggy-backed on HTTP
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 53018
diff changeset
   224
     * URL's that use this won't take advantage of keep-alive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Additionally, this constructor may be used as a last resort when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * first HttpClient gotten through New() failed (probably b/c of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Keep-Alive mismatch).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * XXX That documentation is wrong ... it's not package-private any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public HttpClient(URL url, String proxyHost, int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this(url, proxyHost, proxyPort, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    protected HttpClient(URL url, Proxy p, int to) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        proxy = (p == null) ? Proxy.NO_PROXY : p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            port = getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        setConnectTimeout(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   246
        capture = HttpCapture.getCapture(url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        openServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
   250
    protected static Proxy newHttpProxy(String proxyHost, int proxyPort,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                      String proto) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (proxyHost == null || proto == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int pport = proxyPort < 0 ? getDefaultPort(proto) : proxyPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        InetSocketAddress saddr = InetSocketAddress.createUnresolved(proxyHost, pport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return new Proxy(Proxy.Type.HTTP, saddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * This constructor gives "ultimate" flexibility, including the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * to bypass implicit proxying.  Sometimes we need to be using tunneling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * (transport or network level) instead of proxying (application level),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * for example when we don't want the application level data to become
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * visible to third parties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param url               the URL to which we're connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param proxy             proxy to use for this URL (e.g. forwarding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param proxyPort         proxy port to use for this URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param proxyDisabled     true to disable default proxying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private HttpClient(URL url, String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                       boolean proxyDisabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this(url, proxyDisabled ? Proxy.NO_PROXY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
             newHttpProxy(proxyHost, proxyPort, "http"), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public HttpClient(URL url, String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                       boolean proxyDisabled, int to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this(url, proxyDisabled ? Proxy.NO_PROXY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             newHttpProxy(proxyHost, proxyPort, "http"), to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /* This class has no public constructor for HTTP.  This method is used to
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18294
diff changeset
   286
     * get an HttpClient to the specified URL.  If there's currently an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * active HttpClient to that server/port, you'll get that one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public static HttpClient New(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   291
        return HttpClient.New(url, Proxy.NO_PROXY, -1, true, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public static HttpClient New(URL url, boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   296
        return HttpClient.New(url, Proxy.NO_PROXY, -1, useCache, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   299
    public static HttpClient New(URL url, Proxy p, int to, boolean useCache,
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   300
        HttpURLConnection httpuc) throws IOException
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   301
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            p = Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        HttpClient ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /* see if one's already around */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (useCache) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   308
            ret = kac.get(url, null);
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   309
            if (ret != null && httpuc != null &&
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   310
                httpuc.streaming() &&
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   311
                httpuc.getRequestMethod() == "POST") {
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   312
                if (!ret.available()) {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   313
                    ret.inCache = false;
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   314
                    ret.closeServer();
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   315
                    ret = null;
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   316
                }
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   317
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (ret != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   319
                String ak = httpuc == null ? AuthenticatorKeys.DEFAULT
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   320
                     : httpuc.getAuthenticatorKey();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   321
                boolean compatible = Objects.equals(ret.proxy, p)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   322
                     && Objects.equals(ret.getAuthenticatorKey(), ak);
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   323
                final Lock lock = ret.clientLock;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   324
                if (compatible) {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   325
                    lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   326
                    try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        ret.cachedHttpClient = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        assert ret.inCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        ret.inCache = false;
14008
b572c1f3a7ad 7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents: 14007
diff changeset
   330
                        if (httpuc != null && ret.needsTunneling())
b572c1f3a7ad 7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents: 14007
diff changeset
   331
                            httpuc.setTunnelState(TUNNELING);
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   332
                        logFinest("KeepAlive stream retrieved from the cache, " + ret);
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   333
                    } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   334
                        lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    // We cannot return this connection to the cache as it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    // KeepAliveTimeout will get reset. We simply close the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    // This should be fine as it is very rare that a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    // to the same host will not use the same proxy.
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   341
                    lock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   342
                    try  {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   343
                        ret.inCache = false;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   344
                        ret.closeServer();
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   345
                    } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   346
                        lock.unlock();
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   347
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            ret = new HttpClient(url, p, to);
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   354
            if (httpuc != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   355
                ret.authenticatorKey = httpuc.getAuthenticatorKey();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   356
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (security != null) {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   360
                if (ret.proxy == Proxy.NO_PROXY || ret.proxy == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   361
                    security.checkConnect(InetAddress.getByName(url.getHost()).getHostAddress(), url.getPort());
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   362
                } else {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   363
                    security.checkConnect(url.getHost(), url.getPort());
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   364
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            ret.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   371
    public static HttpClient New(URL url, Proxy p, int to,
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   372
        HttpURLConnection httpuc) throws IOException
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   373
    {
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   374
        return New(url, p, to, true, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public static HttpClient New(URL url, String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                 boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   380
        return New(url, newHttpProxy(proxyHost, proxyPort, "http"),
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   381
            -1, useCache, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public static HttpClient New(URL url, String proxyHost, int proxyPort,
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   385
                                 boolean useCache, int to,
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   386
                                 HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   388
        return New(url, newHttpProxy(proxyHost, proxyPort, "http"),
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   389
            to, useCache, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   392
    public final String getAuthenticatorKey() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   393
        String k = authenticatorKey;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   394
        if (k == null) return AuthenticatorKeys.DEFAULT;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   395
        return k;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   396
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 37781
diff changeset
   397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /* return it to the cache as still usable, if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * 1) It's keeping alive, AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * 2) It still has some connections left, AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * 3) It hasn't had a error (PrintStream.checkError())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * 4) It hasn't timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * If this client is not keepingAlive, it should have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * removed from the cache in the parseHeaders() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public void finished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (reuse) /* will be reused */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        keepAliveConnections--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (keepAliveConnections > 0 && isKeepingAlive() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
               !(serverOutput.checkError())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            /* This connection is keepingAlive && still valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
             * Return it to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            putInKeepAliveCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   424
    protected boolean available() {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   425
        boolean available = true;
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   426
        int old = -1;
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   427
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   428
        clientLock.lock();
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   429
        try {
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   430
            try {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   431
                old = serverSocket.getSoTimeout();
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   432
                serverSocket.setSoTimeout(1);
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   433
                BufferedInputStream tmpbuf =
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   434
                        new BufferedInputStream(serverSocket.getInputStream());
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   435
                int r = tmpbuf.read();
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   436
                if (r == -1) {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   437
                    logFinest("HttpClient.available(): " +
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   438
                            "read returned -1: not available");
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   439
                    available = false;
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   440
                }
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   441
            } catch (SocketTimeoutException e) {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   442
                logFinest("HttpClient.available(): " +
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   443
                        "SocketTimeout: its available");
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   444
            } finally {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   445
                if (old != -1)
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   446
                    serverSocket.setSoTimeout(old);
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   447
            }
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   448
        } catch (IOException e) {
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   449
            logFinest("HttpClient.available(): " +
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   450
                        "SocketException: not available");
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
   451
            available = false;
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   452
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   453
            clientLock.unlock();
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   454
        }
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   455
        return available;
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   456
    }
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11528
diff changeset
   457
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   458
    protected void putInKeepAliveCache() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   459
        clientLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   460
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   461
            if (inCache) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   462
                assert false : "Duplicate put to keep alive cache";
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   463
                return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   464
            }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   465
            inCache = true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   466
            kac.put(url, null, this);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   467
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   468
            clientLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   472
    protected boolean isInKeepAliveCache() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   473
        clientLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   474
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   475
            return inCache;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   476
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   477
            clientLock.unlock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   478
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Close an idle connection to this URL (if it exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * cache).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void closeIdleConnection() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   486
        HttpClient http = kac.get(url, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            http.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /* We're very particular here about what our InputStream to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * looks like for reasons that are apparent if you can decipher the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * method parseHTTP().  That's why this method is overidden from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   497
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void openServer(String server, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        serverSocket = doConnect(server, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        try {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   501
            OutputStream out = serverSocket.getOutputStream();
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   502
            if (capture != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   503
                out = new HttpCaptureOutputStream(out, capture);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   504
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            serverOutput = new PrintStream(
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   506
                new BufferedOutputStream(out),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                         false, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } catch (UnsupportedEncodingException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10136
diff changeset
   509
            throw new InternalError(encoding+" encoding not found", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        serverSocket.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns true if the http request should be tunneled through proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * An example where this is the case is Https.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public boolean needsTunneling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Returns true if this httpclient is from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   525
    public boolean isCachedConnection() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   526
        clientLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   527
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   528
            return cachedHttpClient;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   529
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   530
            clientLock.unlock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   531
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Finish any work left after the socket connection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * established.  In the normal http case, it's a NO-OP. Subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * may need to override this. An example is Https, where for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * direct connection to the origin server, ssl handshake needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * be done; for proxy tunneling, the socket needs to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * into an SSL socket before ssl handshake can take place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public void afterConnect() throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        // NO-OP. Needs to be overwritten by HttpsClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * call openServer in a privileged block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   549
    private void privilegedOpenServer(final InetSocketAddress server)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   552
        assert clientLock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27717
diff changeset
   555
                new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   556
                    public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    openServer(server.getHostString(), server.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw (IOException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * call super.openServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    private void superOpenServer(final String proxyHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                 final int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        throws IOException, UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        super.openServer(proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   578
    protected void openServer() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        SecurityManager security = System.getSecurityManager();
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   581
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   582
        clientLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   583
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   584
            if (security != null) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   585
                security.checkConnect(host, port);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   586
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   588
            if (keepingAlive) { // already opened
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   592
            if (url.getProtocol().equals("http") ||
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   593
                    url.getProtocol().equals("https")) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   594
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   595
                if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   596
                    sun.net.www.URLConnection.setProxiedHost(host);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   597
                    privilegedOpenServer((InetSocketAddress) proxy.address());
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   598
                    usingProxy = true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   599
                    return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   600
                } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   601
                    // make direct connection
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   602
                    openServer(host, port);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   603
                    usingProxy = false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   604
                    return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   605
                }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   606
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            } else {
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   608
                /* we're opening some other kind of url, most likely an
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   609
                 * ftp url.
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   610
                 */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   611
                if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   612
                    sun.net.www.URLConnection.setProxiedHost(host);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   613
                    privilegedOpenServer((InetSocketAddress) proxy.address());
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   614
                    usingProxy = true;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   615
                    return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   616
                } else {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   617
                    // make direct connection
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   618
                    super.openServer(host, port);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   619
                    usingProxy = false;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   620
                    return;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   621
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   623
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
   624
            clientLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public String getURLFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
18294
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   630
        String fileName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * proxyDisabled is set by subclass HttpsClient!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (usingProxy && !proxyDisabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            // Do not use URLStreamHandler.toExternalForm as the fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // should not be part of the RequestURI. It should be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            // absolute URI which does not have a fragment part.
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24045
diff changeset
   639
            StringBuilder result = new StringBuilder(128);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            result.append(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            result.append(":");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   642
            if (url.getAuthority() != null && !url.getAuthority().isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                result.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                result.append(url.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (url.getPath() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                result.append(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (url.getQuery() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                result.append('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                result.append(url.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
18294
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   654
            fileName = result.toString();
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   655
        } else {
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   656
            fileName = url.getFile();
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   657
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   658
            if ((fileName == null) || (fileName.isEmpty())) {
18294
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   659
                fileName = "/";
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   660
            } else if (fileName.charAt(0) == '?') {
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   661
                /* HTTP/1.1 spec says in 5.1.2. about Request-URI:
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   662
                 * "Note that the absolute path cannot be empty; if
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   663
                 * none is present in the original URI, it MUST be
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   664
                 * given as "/" (the server root)."  So if the file
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   665
                 * name here has only a query string, the path is
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   666
                 * empty and we also have to add a "/".
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   667
                 */
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   668
                fileName = "/" + fileName;
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   669
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
18294
364814dc7391 7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents: 18178
diff changeset
   671
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (fileName.indexOf('\n') == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            throw new java.net.MalformedURLException("Illegal character in URL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public void writeRequests(MessageHeader head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        requests = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        requests.print(serverOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        serverOutput.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    public void writeRequests(MessageHeader head,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                              PosterOutputStream pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        requests = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        requests.print(serverOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        poster = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (poster != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            poster.writeTo(serverOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        serverOutput.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
6673
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   698
    public void writeRequests(MessageHeader head,
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   699
                              PosterOutputStream pos,
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   700
                              boolean streaming) throws IOException {
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   701
        this.streaming = streaming;
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   702
        writeRequests(head, pos);
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   703
    }
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   704
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /** Parse the first line of the HTTP request.  It usually looks
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 29986
diff changeset
   706
        something like: {@literal "HTTP/1.0 <number> comment\r\n"}. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public boolean parseHTTP(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        /* If "HTTP/*" is found in the beginning, return true.  Let
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         * HttpURLConnection parse the mime header itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
         * If this isn't valid HTTP, then we don't try to parse a header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
         * out of the beginning of the response into the responses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * and instead just queue up the output stream to it's very beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * This seems most reasonable, and is what the NN browser does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            serverInput = serverSocket.getInputStream();
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   721
            if (capture != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   722
                serverInput = new HttpCaptureInputStream(serverInput, capture);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   723
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            serverInput = new BufferedInputStream(serverInput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return (parseHTTPHeader(responses, pi, httpuc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        } catch (SocketTimeoutException stex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            // We don't want to retry the request when the app. sets a timeout
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
   728
            // but don't close the server if timeout while waiting for 100-continue
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
   729
            if (ignoreContinue) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
   730
                closeServer();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
   731
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw stex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            cachedHttpClient = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (!failedOnce && requests != null) {
6673
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   737
                failedOnce = true;
27717
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   738
                if (getRequestMethod().equals("CONNECT")
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   739
                    || streaming
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   740
                    || (httpuc.getRequestMethod().equals("POST")
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   741
                        && !retryPostProp)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    // do not retry the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    // try once more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    openServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    if (needsTunneling()) {
24045
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents: 21278
diff changeset
   747
                        MessageHeader origRequests = requests;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                        httpuc.doTunneling();
24045
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents: 21278
diff changeset
   749
                        requests = origRequests;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    afterConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    writeRequests(requests, poster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    return parseHTTP(responses, pi, httpuc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        /* If "HTTP/*" is found in the beginning, return true.  Let
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * HttpURLConnection parse the mime header itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * If this isn't valid HTTP, then we don't try to parse a header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * out of the beginning of the response into the responses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * and instead just queue up the output stream to it's very beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         * This seems most reasonable, and is what the NN browser does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        keepAliveConnections = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        keepAliveTimeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        byte[] b = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            int nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            serverInput.mark(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            while (nread < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                int r = serverInput.read(b, nread, 8 - nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                if (r < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                nread += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            String keep=null;
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   789
            String authenticate=null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            ret = b[0] == 'H' && b[1] == 'T'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    && b[2] == 'T' && b[3] == 'P' && b[4] == '/' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                b[5] == '1' && b[6] == '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            serverInput.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (ret) { // is valid HTTP - response started w/ "HTTP/1."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                responses.parseHeader(serverInput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                // we've finished parsing http headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                // check if there are any applicable cookies to set (in cache)
11528
638ca25aec87 7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents: 10419
diff changeset
   799
                CookieHandler cookieHandler = httpuc.getCookieHandler();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                if (cookieHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    // NOTE: That cast from Map shouldn't be necessary but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    // a bug in javac is triggered under certain circumstances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    // So we do put the cast in as a workaround until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    // it is resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    if (uri != null)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   807
                        cookieHandler.put(uri, responses.getHeaders());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                /* decide if we're keeping alive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                 * This is a bit tricky.  There's a spec, but most current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                 * servers (10/1/96) that support this differ in dialects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                 * If the server/client misunderstand each other, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                 * protocol should fall back onto HTTP/1.0, no keep-alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                if (usingProxy) { // not likely a proxy will return this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    keep = responses.findValue("Proxy-Connection");
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   818
                    authenticate = responses.findValue("Proxy-Authenticate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                if (keep == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    keep = responses.findValue("Connection");
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   822
                    authenticate = responses.findValue("WWW-Authenticate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   824
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   825
                // 'disableKeepAlive' starts with the value false.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   826
                // It can transition from false to true, but once true
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   827
                // it stays true.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   828
                // If cacheNTLMProp is false, and disableKeepAlive is false,
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   829
                // then we need to examine the response headers to figure out
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   830
                // whether we are doing NTLM authentication. If we do NTLM,
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   831
                // and cacheNTLMProp is false, than we can't keep this connection
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   832
                // alive: we will switch disableKeepAlive to true.
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   833
                boolean canKeepAlive = !disableKeepAlive;
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   834
                if (canKeepAlive && (cacheNTLMProp == false || cacheSPNEGOProp == false)
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   835
                        && authenticate != null) {
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   836
                    authenticate = authenticate.toLowerCase(Locale.US);
44755
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   837
                    if (cacheNTLMProp == false) {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   838
                        canKeepAlive &= !authenticate.startsWith("ntlm ");
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   839
                    }
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   840
                    if (cacheSPNEGOProp == false) {
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   841
                        canKeepAlive &= !authenticate.startsWith("negotiate ");
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   842
                        canKeepAlive &= !authenticate.startsWith("kerberos ");
e16b506a60b2 8170814: Reuse cache entries (part II)
dfuchs
parents: 44753
diff changeset
   843
                    }
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   844
                }
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   845
                disableKeepAlive |= !canKeepAlive;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   846
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   847
                if (keep != null && keep.toLowerCase(Locale.US).equals("keep-alive")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    /* some servers, notably Apache1.1, send something like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                     * "Keep-Alive: timeout=15, max=1" which we should respect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                     */
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   851
                    if (disableKeepAlive) {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   852
                        keepAliveConnections = 1;
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   853
                    } else {
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   854
                        HeaderParser p = new HeaderParser(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                            responses.findValue("Keep-Alive"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        /* default should be larger in case of proxy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        keepAliveConnections = p.findInt("max", usingProxy?50:5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        keepAliveTimeout = p.findInt("timeout", usingProxy?60:5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                } else if (b[7] != '0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                     * We're talking 1.1 or later. Keep persistent until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                     * the server says to close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                     */
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   865
                    if (keep != null || disableKeepAlive) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                         * The only Connection token we understand is close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                         * Paranoia: if there is any Connection header then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                         * treat as non-persistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        keepAliveConnections = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        keepAliveConnections = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            } else if (nread != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                if (!failedOnce && requests != null) {
6673
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 5506
diff changeset
   878
                    failedOnce = true;
27717
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   879
                    if (getRequestMethod().equals("CONNECT")
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   880
                        || streaming
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   881
                        || (httpuc.getRequestMethod().equals("POST")
9d29bef916e1 8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents: 26720
diff changeset
   882
                            && !retryPostProp)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                        // do not retry the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                        closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                        cachedHttpClient = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                        openServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                        if (needsTunneling()) {
24045
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents: 21278
diff changeset
   889
                            MessageHeader origRequests = requests;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                            httpuc.doTunneling();
24045
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents: 21278
diff changeset
   891
                            requests = origRequests;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                        afterConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        writeRequests(requests, poster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        return parseHTTP(responses, pi, httpuc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                throw new SocketException("Unexpected end of file from server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                // we can't vouche for what this is....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                responses.set("Content-type", "unknown/unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        int code = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            String resp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            resp = responses.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            /* should have no leading/trailing LWS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
             * expedite the typical case by assuming it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
             * form "HTTP/1.x <WS> 2XX <mumble>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            int ind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            ind = resp.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            while(resp.charAt(ind) == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                ind++;
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   919
            code = Integer.parseInt(resp, ind, ind + 3, 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
   922
        if (code == HTTP_CONTINUE && ignoreContinue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            responses.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            return parseHTTPHeader(responses, pi, httpuc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        long cl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * Set things up to parse the entity body of the reply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * We should be smarter about avoid pointless work when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * the HTTP method and response code indicate there will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * no entity body to parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   935
        String te = responses.findValue("Transfer-Encoding");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (te != null && te.equalsIgnoreCase("chunked")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            serverInput = new ChunkedInputStream(serverInput, this, responses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
             * If keep alive not specified then close after the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
             * has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (keepAliveConnections <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                keepAliveConnections = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                keepingAlive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            } else {
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   947
                keepingAlive = !disableKeepAlive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            failedOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
             * If it's a keep alive connection then we will keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
             * (alive if :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
             * 1. content-length is specified, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
             * 2. "Not-Modified" or "No-Content" responses - RFC 2616 states that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
             *    204 or 304 response must not include a message body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
             */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   959
            String cls = responses.findValue("content-length");
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   960
            if (cls != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   961
                try {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   962
                    cl = Long.parseLong(cls);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   963
                } catch (NumberFormatException e) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   964
                    cl = -1;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   965
                }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2928
diff changeset
   966
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            String requestLine = requests.getKey(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            if ((requestLine != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                 (requestLine.startsWith("HEAD"))) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                code == HttpURLConnection.HTTP_NOT_MODIFIED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                code == HttpURLConnection.HTTP_NO_CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                cl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            if (keepAliveConnections > 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                (cl >= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                 code == HttpURLConnection.HTTP_NOT_MODIFIED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                 code == HttpURLConnection.HTTP_NO_CONTENT)) {
44753
37d4270a2a8d 8163520: Reuse cache entries
dfuchs
parents: 42351
diff changeset
   980
                keepingAlive = !disableKeepAlive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                failedOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            } else if (keepingAlive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                /* Previously we were keeping alive, and now we're not.  Remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                 * this from the cache (but only here, once) - otherwise we get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                 * multiple removes and the cache count gets messed up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                keepingAlive=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        /* wrap a KeepAliveStream/MeteredStream around it if appropriate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (cl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            // In this case, content length is well known, so it is okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            // to wrap the input stream with KeepAliveStream/MeteredStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                // Progress monitor is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                pi.setContentType(responses.findValue("content-type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
44759
b4a251d223e2 8174985: NTLM authentication doesn't work with IIS if NTLM cache is disabled
dfuchs
parents: 44755
diff changeset
  1002
            // If disableKeepAlive == true, the client will not be returned
b4a251d223e2 8174985: NTLM authentication doesn't work with IIS if NTLM cache is disabled
dfuchs
parents: 44755
diff changeset
  1003
            // to the cache. But we still need to use a keepalive stream to
b4a251d223e2 8174985: NTLM authentication doesn't work with IIS if NTLM cache is disabled
dfuchs
parents: 44755
diff changeset
  1004
            // allow the multi-message authentication exchange on the connection
b4a251d223e2 8174985: NTLM authentication doesn't work with IIS if NTLM cache is disabled
dfuchs
parents: 44755
diff changeset
  1005
            boolean useKeepAliveStream = isKeepingAlive() || disableKeepAlive;
b4a251d223e2 8174985: NTLM authentication doesn't work with IIS if NTLM cache is disabled
dfuchs
parents: 44755
diff changeset
  1006
            if (useKeepAliveStream)   {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                // Wrap KeepAliveStream if keep alive is enabled.
16477
15ee12718efe 8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents: 14008
diff changeset
  1008
                logFinest("KeepAlive stream used: " + url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                serverInput = new KeepAliveStream(serverInput, pi, cl, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                failedOnce = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            else        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                serverInput = new MeteredStream(serverInput, pi, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        else if (cl == -1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            // In this case, content length is unknown - the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            // stream would simply be a regular InputStream or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            // ChunkedInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                // Progress monitoring is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                pi.setContentType(responses.findValue("content-type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                // Wrap MeteredStream for tracking indeterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                // progress, even if the input stream is ChunkedInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                serverInput = new MeteredStream(serverInput, pi, cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            else    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                // Progress monitoring is disabled, and there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                // need to wrap an unknown length input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                // ** This is an no-op **
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        else    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if (pi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1045
    public InputStream getInputStream() {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1046
        clientLock.lock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1047
        try {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1048
            return serverInput;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1049
        } finally {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1050
            clientLock.unlock();
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 53563
diff changeset
  1051
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public OutputStream getOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return serverOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
  1058
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        return getClass().getName()+"("+url+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    public final boolean isKeepingAlive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        return getHttpKeepAliveSet() && keepingAlive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    public void setCacheRequest(CacheRequest cacheRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        this.cacheRequest = cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    CacheRequest getCacheRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
10136
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1075
    String getRequestMethod() {
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1076
        if (requests != null) {
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1077
            String requestLine = requests.getKey(0);
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1078
            if (requestLine != null) {
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1079
               return requestLine.split("\\s+")[0];
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1080
            }
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1081
        }
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1082
        return "";
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1083
    }
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9775
diff changeset
  1084
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public void setDoNotRetry(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // failedOnce is used to determine if a request should be retried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        failedOnce = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
  1090
    public void setIgnoreContinue(boolean value) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
  1091
        ignoreContinue = value;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
  1092
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    /* Use only on connections in error. */
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 715
diff changeset
  1095
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    public void closeServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            keepingAlive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @return the proxy host being used for this client, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *          if we're not going through a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public String getProxyHostUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (!usingProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            return ((InetSocketAddress)proxy.address()).getHostString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @return the proxy port being used for this client.  Meaningless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *          if getProxyHostUsed() gives null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public int getProxyPortUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (usingProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            return ((InetSocketAddress)proxy.address()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
}