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