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