jdk/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
author asaha
Wed, 01 Jul 2009 09:59:16 -0700
changeset 3464 4ef40ee318ef
parent 3456 c8f6e154868b
parent 3061 419b6dba7ccf
child 3465 96252ea98a6a
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
     2
 * Copyright 1995-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.protocol.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.ProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.HttpRetryException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.PasswordAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.Authenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.SocketTimeoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.ProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.CookieHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.ResponseCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.CacheResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.SecureCacheResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.CacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.Authenticator.RequestorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.net.www.http.HttpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.net.www.http.PosterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.net.www.http.ChunkedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.net.www.http.ChunkedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * A class to represent an HTTP connection to a remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class HttpURLConnection extends java.net.HttpURLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
    76
    static String HTTP_CONNECT = "CONNECT";
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final String version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final String userAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* max # of allowed re-directs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int defaultmaxRedirects = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final int maxRedirects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* Not all servers support the (Proxy)-Authentication-Info headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * By default, we don't require them to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final boolean validateProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final boolean validateServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private StreamingOutputStream strOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final static String RETRY_MSG1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        "cannot retry due to proxy authentication, in streaming mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final static String RETRY_MSG2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        "cannot retry due to server authentication, in streaming mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static String RETRY_MSG3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        "cannot retry due to redirection, in streaming mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * System properties related to error stream handling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * sun.net.http.errorstream.enableBuffering = <boolean>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * With the above system property set to true (default is false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * when the response code is >=400, the HTTP handler will try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * buffer the response body (up to a certain amount and within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * time limit). Thus freeing up the underlying socket connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * for reuse. The rationale behind this is that usually when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * server responds with a >=400 error (client error or server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * error, such as 404 file not found), the server will send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * small response body to explain who to contact and what to do to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * recover. With this property set to true, even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * application doesn't call getErrorStream(), read the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * body, and then call close(), the underlying socket connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * can still be kept-alive and reused. The following two system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * properties provide further control to the error stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * buffering behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * sun.net.http.errorstream.timeout = <int>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *     the timeout (in millisec) waiting the error stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *     to be buffered; default is 300 ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * sun.net.http.errorstream.bufferSize = <int>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *     the size (in bytes) to use for the buffering the error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *     default is 4k
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* Should we enable buffering of error streams? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static boolean enableESBuffer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* timeout waiting for read for buffered error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static int timeout4ESBuffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /* buffer size for buffered error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static int bufSize4ES = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        maxRedirects = java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   142
            new sun.security.action.GetIntegerAction(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   143
                "http.maxRedirects", defaultmaxRedirects)).intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        version = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    new sun.security.action.GetPropertyAction("java.version"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String agent = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    new sun.security.action.GetPropertyAction("http.agent"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (agent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            agent = "Java/"+version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            agent = agent + " Java/"+version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        userAgent = agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        validateProxy = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                new sun.security.action.GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    "http.auth.digest.validateProxy")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        validateServer = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                new sun.security.action.GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    "http.auth.digest.validateServer")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        enableESBuffer = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                new sun.security.action.GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    "sun.net.http.errorstream.enableBuffering")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        timeout4ESBuffer = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                new sun.security.action.GetIntegerAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    "sun.net.http.errorstream.timeout", 300)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (timeout4ESBuffer <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            timeout4ESBuffer = 300; // use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        bufSize4ES = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                new sun.security.action.GetIntegerAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "sun.net.http.errorstream.bufferSize", 4096)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (bufSize4ES <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            bufSize4ES = 4096; // use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static final String httpVersion = "HTTP/1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static final String acceptString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // the following http request headers should NOT have their values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // returned for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private static final String[] EXCLUDE_HEADERS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            "Proxy-Authorization",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            "Authorization"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected HttpClient http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    protected Proxy instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private CookieHandler cookieHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private ResponseCache cacheHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // the cached response, and cached response headers and body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected CacheResponse cachedResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private MessageHeader cachedHeaders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private InputStream cachedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /* output stream to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected PrintStream ps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /* buffered error stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private InputStream errorStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /* User set Cookies */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private boolean setUserCookies = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private String userCookies = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /* We only have a single static authenticator for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * REMIND:  backwards compatibility with JDK 1.1.  Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * eliminated for JDK 2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static HttpAuthenticator defaultAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /* all the headers we send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * NOTE: do *NOT* dump out the content of 'requests' in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * output or stacktrace since it may contain security-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * headers such as those defined in EXCLUDE_HEADERS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private MessageHeader requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* The following two fields are only used with Digest Authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    String domain;      /* The list of authentication domains */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    DigestAuthentication.Parameters digestparams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /* Current credentials in use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    AuthenticationInfo  currentProxyCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    AuthenticationInfo  currentServerCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    boolean             needToCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private boolean doingNTLM2ndStage = false; /* doing the 2nd stage of an NTLM server authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private boolean doingNTLMp2ndStage = false; /* doing the 2nd stage of an NTLM proxy authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /* try auth without calling Authenticator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private boolean tryTransparentNTLMServer = NTLMAuthentication.supportsTransparentAuth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private boolean tryTransparentNTLMProxy = NTLMAuthentication.supportsTransparentAuth();
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
   240
    /* Used by Windows specific code */
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
   241
    Object authObj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /* Set if the user is manually setting the Authorization or Proxy-Authorization headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    boolean isUserServerAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    boolean isUserProxyAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /* Progress source */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    protected ProgressSource pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /* all the response headers we get back */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private MessageHeader responses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /* the stream _from_ the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private InputStream inputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /* post stream _to_ the server, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private PosterOutputStream poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /* Indicates if the std. request headers have been set in requests. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private boolean setRequests=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /* Indicates whether a request has already failed or not */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private boolean failedOnce=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /* Remembered Exception, we will throw it again if somebody
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
       calls getInputStream after disconnect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private Exception rememberedException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /* If we decide we want to reuse a client, we put it here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private HttpClient reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   270
    /* Tunnel states */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   271
    enum TunnelState {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   272
        /* No tunnel */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   273
        NONE,
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   274
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   275
        /* Setting up a tunnel */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   276
        SETUP,
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   277
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   278
        /* Tunnel has been successfully setup */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   279
        TUNNELING
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   280
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   281
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   282
    private TunnelState tunnelState = TunnelState.NONE;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   283
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /* Redefine timeouts from java.net.URLConnection as we nee -1 to mean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * not set. This is to ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private int connectTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private int readTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * privileged request password authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private static PasswordAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            final String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            final InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            final String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            final String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            final String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            final URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            final RequestorType authType) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   304
        return java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   305
            new java.security.PrivilegedAction<PasswordAuthentication>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   306
                public PasswordAuthentication run() {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   307
                    if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   308
                        logger.finest("Requesting Authentication: host =" + host + " url = " + url);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   309
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   310
                    PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        host, addr, port, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        prompt, scheme, url, authType);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   313
                    if (pass != null && logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   314
                        logger.finest("Authentication returned: " + pass.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   315
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   316
                    return pass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * checks the validity of http message header and throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * IllegalArgumentException if invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void checkMessageHeader(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        char LF = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int index = key.indexOf(LF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                "Illegal character(s) in message header field: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            index = value.indexOf(LF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            while (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (index < value.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    char c = value.charAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    if ((c==' ') || (c=='\t')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        // ok, check the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        index = value.indexOf(LF, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    "Illegal character(s) in message header value: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /* adds the standard key/val pairs to reqests if necessary & write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * given PrintStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private void writeRequests() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        /* print all message headers in the MessageHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * onto the wire - all the ones we've set and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * others that have been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // send any pre-emptive authentication
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   363
        if (http.usingProxy && tunnelState() != TunnelState.TUNNELING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            setPreemptiveProxyAuthentication(requests);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (!setRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            /* We're very particular about the order in which we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
             * set the request headers here.  The order should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             * matter, but some careless CGI programs have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
             * written to expect a very particular order of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
             * standard headers.  To name names, the order in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
             * Navigator3.0 sends them.  In particular, we make *sure*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             * to send Content-type: <> and Content-length:<> second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * to last and last, respectively, in the case of a POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             * request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (!failedOnce)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                requests.prepend(method + " " + http.getURLFile()+" "  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                 httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (!getUseCaches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                requests.setIfNotSet ("Cache-Control", "no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                requests.setIfNotSet ("Pragma", "no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            requests.setIfNotSet("User-Agent", userAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            requests.setIfNotSet("Host", host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            requests.setIfNotSet("Accept", acceptString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
             * For HTTP/1.1 the default behavior is to keep connections alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
             * However, we may be talking to a 1.0 server so we should set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
             * keep-alive just in case, except if we have encountered an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
             * or if keep alive is disabled via a system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // Try keep-alive only on first attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (!failedOnce && http.getHttpKeepAliveSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                if (http.usingProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    requests.setIfNotSet("Proxy-Connection", "keep-alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    requests.setIfNotSet("Connection", "keep-alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 * RFC 2616 HTTP/1.1 section 14.10 says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                 * HTTP/1.1 applications that do not support persistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                 * connections MUST include the "close" connection option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                 * in every message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                requests.setIfNotSet("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // Set modified since if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            long modTime = getIfModifiedSince();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (modTime != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                Date date = new Date(modTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                //use the preferred date format according to RFC 2068(HTTP1.1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // RFC 822 and RFC 1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                SimpleDateFormat fo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                  new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                requests.setIfNotSet("If-Modified-Since", fo.format(date));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // check for preemptive authorization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            AuthenticationInfo sauth = AuthenticationInfo.getServerAuth(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (sauth != null && sauth.supportsPreemptiveAuthorization() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                // Sets "Authorization"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                requests.setIfNotSet(sauth.getHeaderName(), sauth.getHeaderValue(url,method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                currentServerCredentials = sauth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (!method.equals("PUT") && (poster != null || streaming())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                requests.setIfNotSet ("Content-type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        "application/x-www-form-urlencoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (chunkLength != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    requests.set ("Transfer-Encoding", "chunked");
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   444
                } else { /* fixed content length */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   445
                    if (fixedContentLengthLong != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   446
                        requests.set ("Content-Length",
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   447
                                      String.valueOf(fixedContentLengthLong));
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   448
                    } else if (fixedContentLength != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   449
                        requests.set ("Content-Length",
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   450
                                      String.valueOf(fixedContentLength));
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   451
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            } else if (poster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                /* add Content-Length & POST/PUT data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                synchronized (poster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    /* close it, so no more data can be added */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    poster.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    requests.set("Content-Length",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                 String.valueOf(poster.size()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            // get applicable cookies based on the uri and request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            // add them to the existing request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            setRequests=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   469
        if (logger.isLoggable(Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            logger.fine(requests.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        http.writeRequests(requests, poster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (ps.checkError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            String proxyHost = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            int proxyPort = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                throw new IOException("Error writing to server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } else { // try once more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                failedOnce=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (proxyHost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    setProxiedClient(url, proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    setNewClient (url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                ps = (PrintStream) http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                connected=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                setRequests=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Create a new HttpClient object, bypassing the cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * HTTP client objects/connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    protected void setNewClient (URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        setNewClient(url, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Obtain a HttpsClient object. Use the cached copy if specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param useCache  whether the cached connection should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *        if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    protected void setNewClient (URL url, boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        http = HttpClient.New(url, null, -1, useCache, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Create a new HttpClient object, set up so that it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * per-instance proxying to the given HTTP proxy.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * bypasses the cache of HTTP client objects/connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param proxyHost the proxy host to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param proxyPort the proxy port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected void setProxiedClient (URL url, String proxyHost, int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        setProxiedClient(url, proxyHost, proxyPort, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Obtain a HttpClient object, set up so that it uses per-instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * proxying to the given HTTP proxy. Use the cached copy of HTTP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * client objects/connections if specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param proxyHost the proxy host to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param proxyPort the proxy port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @param useCache  whether the cached connection should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *        if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    protected void setProxiedClient (URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                           String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                           boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        proxiedConnect(url, proxyHost, proxyPort, useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    protected void proxiedConnect(URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                           String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                           boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        http = HttpClient.New (url, proxyHost, proxyPort, useCache, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    protected HttpURLConnection(URL u, Handler handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // we set proxy == null to distinguish this case with the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // when per connection proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        this(u, null, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public HttpURLConnection(URL u, String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        this(u, new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(host, port)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /** this constructor is used by other protocol handlers such as ftp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        that want to use http to fetch urls on their behalf.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public HttpURLConnection(URL u, Proxy p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        this(u, p, new Handler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    protected HttpURLConnection(URL u, Proxy p, Handler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        super(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        instProxy = p;
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   584
        if (instProxy instanceof sun.net.ApplicationProxy) {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   585
            /* Application set Proxies should not have access to cookies
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   586
             * in a secure environment unless explicitly allowed. */
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   587
            try {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   588
                cookieHandler = CookieHandler.getDefault();
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   589
            } catch (SecurityException se) { /* swallow exception */ }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   590
        } else {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   591
            cookieHandler = java.security.AccessController.doPrivileged(
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   592
                new java.security.PrivilegedAction<CookieHandler>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   593
                public CookieHandler run() {
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   594
                    return CookieHandler.getDefault();
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   595
                }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   596
            });
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   597
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   598
        cacheHandler = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   599
            new java.security.PrivilegedAction<ResponseCache>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   600
                public ResponseCache run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                return ResponseCache.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @deprecated.  Use java.net.Authenticator.setDefault() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public static void setDefaultAuthenticator(HttpAuthenticator a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        defaultAuth = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * opens a stream allowing redirects only to the same host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public static InputStream openConnectionCheckRedirects(URLConnection c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        boolean redir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        int redirects = 0;
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   621
        InputStream in;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            if (c instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                ((HttpURLConnection) c).setInstanceFollowRedirects(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // We want to open the input stream before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // getting headers, because getHeaderField()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            // et al swallow IOExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            in = c.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            redir = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (c instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                HttpURLConnection http = (HttpURLConnection) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                int stat = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (stat >= 300 && stat <= 307 && stat != 306 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        stat != HttpURLConnection.HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    URL base = http.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    String loc = http.getHeaderField("Location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    URL target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    if (loc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        target = new URL(base, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    if (target == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        || !base.getProtocol().equals(target.getProtocol())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        || base.getPort() != target.getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        || !hostsEqual(base, target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        || redirects >= 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        throw new SecurityException("illegal URL redirect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    redir = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    c = target.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } while (redir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    // Same as java.net.URL.hostsEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    private static boolean hostsEqual(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        final String h1 = u1.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        final String h2 = u2.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (h1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return h2 == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } else if (h2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        } else if (h1.equalsIgnoreCase(h2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // Have to resolve addresses before comparing, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        // names like tachyon and tachyon.eng would compare different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        final boolean result[] = {false};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   683
            new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   684
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    InetAddress a1 = InetAddress.getByName(h1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    InetAddress a2 = InetAddress.getByName(h2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    result[0] = a1.equals(a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                } catch(UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return result[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    // overridden in HTTPS subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public void connect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        plainConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    private boolean checkReuseConnection () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (reuseClient != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            http = reuseClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            http.setReadTimeout(getReadTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            http.reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    protected void plainConnect()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // try to see if request can be served from local cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (cacheHandler != null && getUseCaches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                if (uri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    cachedResponse = cacheHandler.get(uri, getRequestMethod(), requests.getHeaders(EXCLUDE_HEADERS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    if ("https".equalsIgnoreCase(uri.getScheme())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        && !(cachedResponse instanceof SecureCacheResponse)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        cachedResponse = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   734
                    if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   735
                        logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   736
                        if (cachedResponse != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   737
                            logger.finest("From cache: "+cachedResponse.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   738
                        }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   739
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    if (cachedResponse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        cachedInputStream = cachedResponse.getBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                // ignore and commence normal connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (cachedHeaders != null && cachedInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                cachedResponse = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            /* Try to open connections using the following scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
             * return on the first one that's successful:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
             * 1) if (instProxy != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
             *        connect to instProxy; raise exception if failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
             * 2) else use system default ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
             * 3) is 2) fails, make direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (instProxy == null) { // no instance Proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                 * Do we have to use a proxy?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                 */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   768
                ProxySelector sel =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   770
                        new java.security.PrivilegedAction<ProxySelector>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   771
                            public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                     return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                             });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    URI uri = sun.net.www.ParseUtil.toURI(url);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   777
                    if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   778
                        logger.finest("ProxySelector Request for " + uri);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   779
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    Iterator<Proxy> it = sel.select(uri).iterator();
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   781
                    Proxy p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        p = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                            if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                                http = getNewHttpClient(url, p, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                http = getNewHttpClient(url, p, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                            }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   794
                            if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   795
                                if (p != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   796
                                    logger.finest("Proxy used: " + p.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   797
                                }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   798
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                            if (p != Proxy.NO_PROXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                sel.connectFailed(uri, p.address(), ioex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                if (!it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                    // fallback to direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                    http = getNewHttpClient(url, null, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    // No proxy selector, create http client with no proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        http = getNewHttpClient(url, null, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                        http = getNewHttpClient(url, null, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    http = getNewHttpClient(url, instProxy, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    http = getNewHttpClient(url, instProxy, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // constructor to HTTP client calls openserver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    // subclass HttpsClient will overwrite & return an instance of HttpsClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return HttpClient.New(url, p, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    // subclass HttpsClient will overwrite & return an instance of HttpsClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    protected HttpClient getNewHttpClient(URL url, Proxy p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                          int connectTimeout, boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return HttpClient.New(url, p, connectTimeout, useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   860
    private void expect100Continue() throws IOException {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   861
            // Expect: 100-Continue was set, so check the return code for
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   862
            // Acceptance
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   863
            int oldTimeout = http.getReadTimeout();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   864
            boolean enforceTimeOut = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   865
            boolean timedOut = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   866
            if (oldTimeout <= 0) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   867
                // 5s read timeout in case the server doesn't understand
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   868
                // Expect: 100-Continue
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   869
                http.setReadTimeout(5000);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   870
                enforceTimeOut = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   871
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   872
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   873
            try {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   874
                http.parseHTTP(responses, pi, this);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   875
            } catch (SocketTimeoutException se) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   876
                if (!enforceTimeOut) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   877
                    throw se;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   878
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   879
                timedOut = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   880
                http.setIgnoreContinue(true);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   881
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   882
            if (!timedOut) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   883
                // Can't use getResponseCode() yet
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   884
                String resp = responses.getValue(0);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   885
                // Parse the response which is of the form:
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   886
                // HTTP/1.1 417 Expectation Failed
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   887
                // HTTP/1.1 100 Continue
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   888
                if (resp != null && resp.startsWith("HTTP/")) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   889
                    String[] sa = resp.split("\\s+");
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   890
                    responseCode = -1;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   891
                    try {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   892
                        // Response code is 2nd token on the line
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   893
                        if (sa.length > 1)
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   894
                            responseCode = Integer.parseInt(sa[1]);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   895
                    } catch (NumberFormatException numberFormatException) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   896
                    }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   897
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   898
                if (responseCode != 100) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   899
                    throw new ProtocolException("Server rejected operation");
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   900
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   901
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   902
            if (oldTimeout > 0) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   903
                http.setReadTimeout(oldTimeout);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   904
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   905
            responseCode = -1;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   906
            responses.reset();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   907
            // Proceed
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   908
    }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   909
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Allowable input/output sequences:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * [interpreted as POST/PUT]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * - get output, [write output,] get input, [read input]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * - get output, [write output]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * [interpreted as GET]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * - get input, [read input]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Disallowed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * - get input, [read input,] get output, [write output]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   921
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public synchronized OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            if (!doOutput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                throw new ProtocolException("cannot write to a URLConnection"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                               + " if doOutput=false - call setDoOutput(true)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (method.equals("GET")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                method = "POST"; // Backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (!"POST".equals(method) && !"PUT".equals(method) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                "http".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                throw new ProtocolException("HTTP method " + method +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                            " doesn't support output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            // if there's already an input stream open, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                throw new ProtocolException("Cannot write output after reading input.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   947
            boolean expectContinue = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   948
            String expects = requests.findValue("Expect");
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   949
            if ("100-Continue".equalsIgnoreCase(expects)) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   950
                http.setIgnoreContinue(false);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   951
                expectContinue = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   952
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            if (streaming() && strOutputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   957
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   958
            if (expectContinue) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   959
                expect100Continue();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   960
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                if (strOutputStream == null) {
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   964
                    if (chunkLength != -1) { /* chunked */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   965
                         strOutputStream = new StreamingOutputStream(
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   966
                               new ChunkedOutputStream(ps, chunkLength), -1L);
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   967
                    } else { /* must be fixed content length */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   968
                        long length = 0L;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   969
                        if (fixedContentLengthLong != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   970
                            length = fixedContentLengthLong;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   971
                        } else if (fixedContentLength != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   972
                            length = fixedContentLength;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   973
                        }
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   974
                        strOutputStream = new StreamingOutputStream(ps, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                return strOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                if (poster == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    poster = new PosterOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return poster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            throw e;
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   987
        } catch (ProtocolException e) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   988
            // Save the response code which may have been set while enforcing
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   989
            // the 100-continue. disconnectInternal() forces it to -1
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   990
            int i = responseCode;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   991
            disconnectInternal();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   992
            responseCode = i;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
   993
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    private boolean streaming () {
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1001
        return (fixedContentLength != -1) || (fixedContentLengthLong != -1) ||
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1002
               (chunkLength != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * get applicable cookies based on the uri and request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * add them to the existing request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    private void setCookieHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (cookieHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            // we only want to capture the user defined Cookies once, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            // they cannot be changed by user code after we are connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            // only internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (setUserCookies) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                int k = requests.getKey("Cookie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                if ( k != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    userCookies = requests.getValue(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                setUserCookies = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // remove old Cookie header before setting new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            requests.remove("Cookie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (uri != null) {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1026
                if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1027
                    logger.finest("CookieHandler request for " + uri);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1028
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1029
                Map<String, List<String>> cookies
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1030
                    = cookieHandler.get(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1031
                        uri, requests.getHeaders(EXCLUDE_HEADERS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                if (!cookies.isEmpty()) {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1033
                    if (logger.isLoggable(Level.FINEST)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1034
                        logger.finest("Cookies retrieved: " + cookies.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1035
                    }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1036
                    for (Map.Entry<String, List<String>> entry :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1037
                             cookies.entrySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1038
                        String key = entry.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        // ignore all entries that don't have "Cookie"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        // or "Cookie2" as keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        if (!"Cookie".equalsIgnoreCase(key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                            !"Cookie2".equalsIgnoreCase(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1045
                        List<String> l = entry.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        if (l != null && !l.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                            StringBuilder cookieValue = new StringBuilder();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1048
                            for (String value : l) {
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1049
                                cookieValue.append(value).append("; ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                            }
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1051
                            // strip off the trailing '; '
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                            try {
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1053
                                requests.add(key, cookieValue.substring(0, cookieValue.length() - 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                            } catch (StringIndexOutOfBoundsException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                                // no-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            if (userCookies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                if ((k = requests.getKey("Cookie")) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    requests.set("Cookie", requests.getValue(k) + ";" + userCookies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    requests.set("Cookie", userCookies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        } // end of getting cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1072
    @Override
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1073
    @SuppressWarnings("empty-statement")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    public synchronized InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        if (!doInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            throw new ProtocolException("Cannot read from URLConnection"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                   + " if doInput=false (call setDoInput(true))");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (rememberedException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (rememberedException instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                throw new RuntimeException(rememberedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                throw getChainedException((IOException)rememberedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (streaming() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            if (strOutputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            /* make sure stream is closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            strOutputStream.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            if (!strOutputStream.writtenOK()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                throw new IOException ("Incomplete output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        int redirects = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        int respCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        long cl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        AuthenticationInfo serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        AuthenticationInfo proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        AuthenticationHeader srvHdr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
         * Failed Negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
         * In some cases, the Negotiate auth is supported for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         * remote host but the negotiate process still fails (For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
         * example, if the web page is located on a backend server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         * and delegation is needed but fails). The authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
         * process will start again, and we need to detect this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
         * kind of failure and do proper fallback (say, to NTLM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
         * In order to achieve this, the inNegotiate flag is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
         * when the first negotiate challenge is met (and reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
         * if authentication is finished). If a fresh new negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * challenge (no parameter) is found while inNegotiate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         * set, we know there's a failed auth attempt recently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         * Here we'll ignore the header line so that fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         * can be practiced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         * inNegotiateProxy is for proxy authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        boolean inNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        boolean inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // If the user has set either of these headers then do not remove them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        isUserServerAuth = requests.getKey("Authorization") != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        isUserProxyAuth = requests.getKey("Proxy-Authorization") != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                if (cachedInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    return cachedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                // Check if URL should be metered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                if (meteredInput)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    pi = new ProgressSource(url, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    pi.beginTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                /* REMIND: This exists to fix the HttpsURLConnection subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                 * Hotjava needs to run on JDK1.1FCS.  Do proper fix once a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                 * proper solution for SSL can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                if (!streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                http.parseHTTP(responses, pi, this);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1165
                if (logger.isLoggable(Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    logger.fine(responses.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                inputStream = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                respCode = getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                if (respCode == HTTP_PROXY_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                    if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                        throw new HttpRetryException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                            RETRY_MSG1, HTTP_PROXY_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    boolean dontUseNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    Iterator iter = responses.multiValueIterator("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                        String value = ((String)iter.next()).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                            if (!inNegotiateProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                                inNegotiateProxy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                                proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    // changes: add a 3rd parameter to the constructor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    // AuthenticationHeader, so that NegotiateAuthentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    // isSupported can be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    // The other 2 appearances of "new AuthenticationHeader" is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    // altered in similar ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    AuthenticationHeader authhdr = new AuthenticationHeader (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                            "Proxy-Authenticate", responses,
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1204
                            new HttpCallerInfo(url, http.getProxyHostUsed(),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1205
                                http.getProxyPortUsed()),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1206
                            dontUseNegotiate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    if (!doingNTLMp2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                        proxyAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                            resetProxyAuthentication(proxyAuthentication, authhdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                        if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                            redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                        /* in this case, only one header field will be present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                        String raw = responses.findValue ("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                        if (!proxyAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                                                        authhdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                        if (serverAuthentication != null && srvHdr != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                                !serverAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                                                        srvHdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                            disconnectInternal ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                        }
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  1232
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                        doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                // cache proxy authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    // cache auth info on success, domain header not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    proxyAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                if (respCode == HTTP_UNAUTHORIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                        throw new HttpRetryException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                            RETRY_MSG2, HTTP_UNAUTHORIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    boolean dontUseNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    Iterator iter = responses.multiValueIterator("WWW-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                        String value = ((String)iter.next()).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                            if (!inNegotiate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                inNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                doingNTLM2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                                serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    srvHdr = new AuthenticationHeader (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                            "WWW-Authenticate", responses,
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1271
                            new HttpCallerInfo(url),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1272
                            dontUseNegotiate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    String raw = srvHdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    if (!doingNTLM2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                        if ((serverAuthentication != null)&&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                            !(serverAuthentication instanceof NTLMAuthentication)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                            if (serverAuthentication.isAuthorizationStale (raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                /* we can retry with the current credentials */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                                requests.set(serverAuthentication.getHeaderName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                            serverAuthentication.getHeaderValue(url, method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                                currentServerCredentials = serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                serverAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                        serverAuthentication = getServerAuthentication(srvHdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        currentServerCredentials = serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        if (serverAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                            redirects++; // don't let things loop ad nauseum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                            setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                        /* header not used for ntlm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                        if (!serverAuthentication.setHeaders(this, null, raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                        doingNTLM2ndStage = false;
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  1309
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                // cache server authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                if (serverAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    // cache auth info on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    if (!(serverAuthentication instanceof DigestAuthentication) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                        (domain == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                        if (serverAuthentication instanceof BasicAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                            // check if the path is shorter than the existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                            String npath = AuthenticationInfo.reducePath (url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                            String opath = serverAuthentication.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                            if (!opath.startsWith (npath) || npath.length() >= opath.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                /* npath is longer, there must be a common root */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                npath = BasicAuthentication.getRootPath (opath, npath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                            // remove the entry and create a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                            BasicAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                                (BasicAuthentication) serverAuthentication.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                            serverAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                            a.path = npath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                            serverAuthentication = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                        serverAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                        // what we cache is based on the domain list in the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        DigestAuthentication srv = (DigestAuthentication)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                            serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                        StringTokenizer tok = new StringTokenizer (domain," ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                        String realm = srv.realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                        PasswordAuthentication pw = srv.pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                        digestparams = srv.params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                        while (tok.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                            String path = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                                /* path could be an abs_path or a complete URI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                                URL u = new URL (url, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                                DigestAuthentication d = new DigestAuthentication (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                                   false, u, realm, "Digest", pw, digestparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                                d.addToCache ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                            } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                // some flags should be reset to its initialized form so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                // even after a redirect the necessary checks can still be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                // preformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                inNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                //serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                doingNTLM2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                if (!isUserServerAuth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    requests.remove("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                if (!isUserProxyAuth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    requests.remove("Proxy-Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                if (respCode == HTTP_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    checkResponseCredentials (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    needToCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                // a flag need to clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                needToCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                if (followRedirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    /* if we should follow a redirect, then the followRedirects()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                     * method will disconnect() and re-connect us to the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                     * location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    // redirecting HTTP response may have set cookie, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    // need to re-generate request header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    cl = Long.parseLong(responses.findValue("content-length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                } catch (Exception exc) { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                if (method.equals("HEAD") || cl == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    respCode == HTTP_NOT_MODIFIED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    respCode == HTTP_NO_CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                        pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                        pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    http.finished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    inputStream = new EmptyInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                if (respCode == 200 || respCode == 203 || respCode == 206 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                    respCode == 300 || respCode == 301 || respCode == 410) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    if (cacheHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                        // give cache a chance to save response in cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                        URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                        if (uri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                            URLConnection uconn = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                            if ("https".equalsIgnoreCase(uri.getScheme())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                                // use reflection to get to the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                                // HttpsURLConnection instance saved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                                // DelegateHttpsURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                                uconn = (URLConnection)this.getClass().getField("httpsURLConnection").get(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                                    // ignored; use 'this'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                                } catch (NoSuchFieldException nsfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                                    // ignored; use 'this'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                            CacheRequest cacheRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                                cacheHandler.put(uri, uconn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                            if (cacheRequest != null && http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                                http.setCacheRequest(cacheRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                                inputStream = new HttpInputStream(inputStream, cacheRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                if (!(inputStream instanceof HttpInputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    inputStream = new HttpInputStream(inputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                if (respCode >= 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                    if (respCode == 404 || respCode == 410) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                        throw new FileNotFoundException(url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                        throw new java.io.IOException("Server returned HTTP" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                              " response code: " + respCode + " for URL: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                              url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                strOutputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            } while (redirects < maxRedirects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            throw new ProtocolException("Server redirected too many " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                        " times ("+ redirects + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            rememberedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            rememberedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            // buffer the error stream if bytes < 4k
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            // and it can be buffered within 1 second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            String te = responses.findValue("Transfer-Encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            if (http != null && http.isKeepingAlive() && enableESBuffer &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                (cl > 0 || (te != null && te.equalsIgnoreCase("chunked")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                errorStream = ErrorStream.getErrorStream(inputStream, cl, http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            if (respCode == HTTP_PROXY_AUTH && proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                proxyAuthentication.endAuthRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            else if (respCode == HTTP_UNAUTHORIZED && serverAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                serverAuthentication.endAuthRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * Creates a chained exception that has the same type as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * original exception and with the same message. Right now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * there is no convenient APIs for doing so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1490
    private IOException getChainedException(final IOException rememberedException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1492
            final Object[] args = { rememberedException.getMessage() };
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1493
            IOException chainedException =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1494
                java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1495
                    new java.security.PrivilegedExceptionAction<IOException>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1496
                        public IOException run() throws Exception {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1497
                            return (IOException)
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1498
                                rememberedException.getClass()
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1499
                                .getConstructor(new Class[] { String.class })
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1500
                                .newInstance(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                    });
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1503
            chainedException.initCause(rememberedException);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            return chainedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        } catch (Exception ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            return rememberedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1510
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    public InputStream getErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        if (connected && responseCode >= 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            // Client Error 4xx and Server Error 5xx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            if (errorStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                return errorStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            } else if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * set or reset proxy authentication info in request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * after receiving a 407 error. In the case of NTLM however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * receiving a 407 is normal and we just skip the stale check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * because ntlm does not support this feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    private AuthenticationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        resetProxyAuthentication(AuthenticationInfo proxyAuthentication, AuthenticationHeader auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        if ((proxyAuthentication != null )&& ! (proxyAuthentication instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                                                        NTLMAuthentication)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            String raw = auth.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            if (proxyAuthentication.isAuthorizationStale (raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                /* we can retry with the current credentials */
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1536
                String value;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1537
                if (tunnelState() == TunnelState.SETUP &&
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1538
                      proxyAuthentication instanceof DigestAuthentication) {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1539
                    value = ((DigestAuthentication)proxyAuthentication)
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1540
                            .getHeaderValue(connectRequestURI(url), HTTP_CONNECT);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1541
                } else {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1542
                    value = proxyAuthentication.getHeaderValue(url, method);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1543
                }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1544
                requests.set(proxyAuthentication.getHeaderName(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                currentProxyCredentials = proxyAuthentication;
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1546
                return proxyAuthentication;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                proxyAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        proxyAuthentication = getHttpProxyAuthentication(auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        currentProxyCredentials = proxyAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        return  proxyAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    /**
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1557
     * Returns the tunnel state.
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1558
     *
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1559
     * @return  the state
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1560
     */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1561
    TunnelState tunnelState() {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1562
        return tunnelState;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1563
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1564
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1565
    /**
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1566
     * Set the tunneling status.
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1567
     *
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1568
     * @param  the state
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1569
     */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1570
    void setTunnelState(TunnelState tunnelState) {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1571
        this.tunnelState = tunnelState;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1572
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1573
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1574
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * establish a tunnel through proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    public synchronized void doTunneling() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        int retryTunnel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        String statusLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        int respCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        AuthenticationInfo proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        String proxyHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        int proxyPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        // save current requests so that they can be restored after tunnel is setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        MessageHeader savedRequests = requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        boolean inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        try {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1593
            /* Actively setting up a tunnel */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1594
            setTunnelState(TunnelState.SETUP);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1595
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                if (!checkReuseConnection()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    proxiedConnect(url, proxyHost, proxyPort, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                // send the "CONNECT" request to establish a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                // through proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                sendCONNECTRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                responses.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                // There is no need to track progress in HTTP Tunneling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                // so ProgressSource is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                http.parseHTTP(responses, null, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  1609
                /* Log the response to the CONNECT */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1610
                if (logger.isLoggable(Level.FINE)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1611
                    logger.fine(responses.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1612
                }
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  1613
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                statusLine = responses.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                StringTokenizer st = new StringTokenizer(statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                respCode = Integer.parseInt(st.nextToken().trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                if (respCode == HTTP_PROXY_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    boolean dontUseNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    Iterator iter = responses.multiValueIterator("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                        String value = ((String)iter.next()).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                            if (!inNegotiateProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                                inNegotiateProxy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                                proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                    AuthenticationHeader authhdr = new AuthenticationHeader (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                            "Proxy-Authenticate", responses,
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1639
                            new HttpCallerInfo(url, http.getProxyHostUsed(),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1640
                                http.getProxyPortUsed()),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1641
                            dontUseNegotiate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    if (!doingNTLMp2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                        proxyAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                            resetProxyAuthentication(proxyAuthentication, authhdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                            proxyHost = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                            proxyPort = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                            retryTunnel++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                        String raw = responses.findValue ("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                        if (!proxyAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                                                authhdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                        }
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  1661
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                        doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                // cache proxy authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    // cache auth info on success, domain header not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    proxyAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                if (respCode == HTTP_OK) {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1673
                    setTunnelState(TunnelState.TUNNELING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                // we don't know how to deal with other response code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                // so disconnect and report error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                disconnectInternal();
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1679
                setTunnelState(TunnelState.NONE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            } while (retryTunnel < maxRedirects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            if (retryTunnel >= maxRedirects || (respCode != HTTP_OK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                throw new IOException("Unable to tunnel through proxy."+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                                      " Proxy returns \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                                      statusLine + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        } finally  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            if (respCode == HTTP_PROXY_AUTH && proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                proxyAuthentication.endAuthRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        // restore original request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        requests = savedRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // reset responses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        responses.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1701
    static String connectRequestURI(URL url) {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1702
        String host = url.getHost();
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1703
        int port = url.getPort();
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1704
        port = port != -1 ? port : url.getDefaultPort();
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1705
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1706
        return host + ":" + port;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1707
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1708
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * send a CONNECT request for establishing a tunnel to proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    private void sendCONNECTRequest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        // setRequests == true indicates the std. request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        // have been set in (previous) requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        // so the first one must be the http method (GET, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        // we need to set it to CONNECT soon, remove this one first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        // otherwise, there may have 2 http methods in headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        if (setRequests) requests.set(0, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1722
        requests.prepend(HTTP_CONNECT + " " + connectRequestURI(url)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                         + " " + httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        requests.setIfNotSet("User-Agent", userAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        requests.setIfNotSet("Host", host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        // Not really necessary for a tunnel, but can't hurt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        requests.setIfNotSet("Accept", acceptString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        setPreemptiveProxyAuthentication(requests);
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  1736
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  1737
         /* Log the CONNECT request */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1738
        if (logger.isLoggable(Level.FINE)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1739
            logger.fine(requests.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1740
        }
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  1741
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        http.writeRequests(requests, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        // remove CONNECT header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        requests.set(0, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * Sets pre-emptive proxy authentication in header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    private void setPreemptiveProxyAuthentication(MessageHeader requests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        AuthenticationInfo pauth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            = AuthenticationInfo.getProxyAuth(http.getProxyHostUsed(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                                              http.getProxyPortUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        if (pauth != null && pauth.supportsPreemptiveAuthorization()) {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1755
            String value;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1756
            if (tunnelState() == TunnelState.SETUP &&
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1757
                    pauth instanceof DigestAuthentication) {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1758
                value = ((DigestAuthentication)pauth)
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1759
                        .getHeaderValue(connectRequestURI(url), HTTP_CONNECT);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1760
            } else {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1761
                value = pauth.getHeaderValue(url, method);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1762
            }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1763
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            // Sets "Proxy-authorization"
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  1765
            requests.set(pauth.getHeaderName(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            currentProxyCredentials = pauth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * Gets the authentication for an HTTP proxy, and applies it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    private AuthenticationInfo getHttpProxyAuthentication (AuthenticationHeader authhdr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        /* get authorization from authenticator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        AuthenticationInfo ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        String raw = authhdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        String host = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        int port = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        if (host != null && authhdr.isPresent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            HeaderParser p = authhdr.headerParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            String realm = p.findValue("realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            String scheme = authhdr.scheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            char schemeID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            if ("basic".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                schemeID = BasicAuthentication.BASIC_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            } else if ("digest".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                schemeID = DigestAuthentication.DIGEST_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            } else if ("ntlm".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                schemeID = NTLMAuthentication.NTLM_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            } else if ("Kerberos".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                schemeID = NegotiateAuthentication.KERBEROS_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            } else if ("Negotiate".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                schemeID = NegotiateAuthentication.NEGOTIATE_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                schemeID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if (realm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                realm = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            ret = AuthenticationInfo.getProxyAuth(host, port, realm, schemeID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                if (schemeID == BasicAuthentication.BASIC_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                    InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                        final String finalHost = host;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1809
                        addr = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1810
                            new java.security.PrivilegedExceptionAction<InetAddress>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1811
                                public InetAddress run()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                                    throws java.net.UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                                    return InetAddress.getByName(finalHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                    } catch (java.security.PrivilegedActionException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                        // User will have an unknown host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                        privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                    host, addr, port, "http",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                    realm, scheme, url, RequestorType.PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                        ret = new BasicAuthentication(true, host, port, realm, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                } else if (schemeID == DigestAuthentication.DIGEST_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                        privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                                    host, null, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                    realm, scheme, url, RequestorType.PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                        DigestAuthentication.Parameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                            new DigestAuthentication.Parameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                        ret = new DigestAuthentication(true, host, port, realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                                                            scheme, a, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                } else if (schemeID == NTLMAuthentication.NTLM_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    PasswordAuthentication a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                    if (!tryTransparentNTLMProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                        a = privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                                            host, null, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                                            "", scheme, url, RequestorType.PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                    /* If we are not trying transparent authentication then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                     * we need to have a PasswordAuthentication instance. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                     * transparent authentication (Windows only) the username
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                     * and password will be picked up from the current logged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                     * on users credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    if (tryTransparentNTLMProxy ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                          (!tryTransparentNTLMProxy && a != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                        ret = new NTLMAuthentication(true, host, port, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                    tryTransparentNTLMProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                } else if (schemeID == NegotiateAuthentication.NEGOTIATE_AUTH) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1857
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                } else if (schemeID == NegotiateAuthentication.KERBEROS_AUTH) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1859
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            // For backwards compatibility, we also try defaultAuth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            // REMIND:  Get rid of this for JDK2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            if (ret == null && defaultAuth != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                && defaultAuth.schemeSupported(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    URL u = new URL("http", host, port, "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    String a = defaultAuth.authString(u, scheme, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                        ret = new BasicAuthentication (true, host, port, realm, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                        // not in cache by default - cache on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                } catch (java.net.MalformedURLException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            if (ret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                if (!ret.setHeaders(this, p, raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1883
        if (logger.isLoggable(Level.FINER)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1884
            logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + ret.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1885
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * Gets the authentication for an HTTP server, and applies it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * @param authHdr the AuthenticationHeader which tells what auth scheme is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * prefered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    private AuthenticationInfo getServerAuthentication (AuthenticationHeader authhdr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        /* get authorization from authenticator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        AuthenticationInfo ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        String raw = authhdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        /* When we get an NTLM auth from cache, don't set any special headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        if (authhdr.isPresent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            HeaderParser p = authhdr.headerParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            String realm = p.findValue("realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            String scheme = authhdr.scheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            char schemeID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            if ("basic".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                schemeID = BasicAuthentication.BASIC_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            } else if ("digest".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                schemeID = DigestAuthentication.DIGEST_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            } else if ("ntlm".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                schemeID = NTLMAuthentication.NTLM_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            } else if ("Kerberos".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                schemeID = NegotiateAuthentication.KERBEROS_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            } else if ("Negotiate".equalsIgnoreCase(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                schemeID = NegotiateAuthentication.NEGOTIATE_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                schemeID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            domain = p.findValue ("domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            if (realm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                realm = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            ret = AuthenticationInfo.getServerAuth(url, realm, schemeID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                    addr = InetAddress.getByName(url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                } catch (java.net.UnknownHostException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                    // User will have addr = null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            // replacing -1 with default port for a protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                port = url.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                if (schemeID == NegotiateAuthentication.KERBEROS_AUTH) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1940
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                if (schemeID == NegotiateAuthentication.NEGOTIATE_AUTH) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1943
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                if (schemeID == BasicAuthentication.BASIC_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                        privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                            url.getHost(), addr, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                            realm, scheme, url, RequestorType.SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                        ret = new BasicAuthentication(false, url, realm, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                if (schemeID == DigestAuthentication.DIGEST_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                        privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                            url.getHost(), addr, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                            realm, scheme, url, RequestorType.SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                        digestparams = new DigestAuthentication.Parameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                        ret = new DigestAuthentication(false, url, realm, scheme, a, digestparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                if (schemeID == NTLMAuthentication.NTLM_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                    URL url1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                        url1 = new URL (url, "/"); /* truncate the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                        url1 = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    PasswordAuthentication a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    if (!tryTransparentNTLMServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                        a = privilegedRequestPasswordAuthentication(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                            url.getHost(), addr, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                            "", scheme, url, RequestorType.SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    /* If we are not trying transparent authentication then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                     * we need to have a PasswordAuthentication instance. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                     * transparent authentication (Windows only) the username
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                     * and password will be picked up from the current logged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                     * on users credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    if (tryTransparentNTLMServer ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                          (!tryTransparentNTLMServer && a != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                        ret = new NTLMAuthentication(false, url1, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                    tryTransparentNTLMServer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            // For backwards compatibility, we also try defaultAuth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            // REMIND:  Get rid of this for JDK2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            if (ret == null && defaultAuth != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                && defaultAuth.schemeSupported(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                String a = defaultAuth.authString(url, scheme, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                    ret = new BasicAuthentication (false, url, realm, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    // not in cache by default - cache on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            if (ret != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                if (!ret.setHeaders(this, p, raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2013
        if (logger.isLoggable(Level.FINER)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2014
            logger.finer("Server Authentication for " + authhdr.toString() +" returned " + ret.toString());
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2015
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    /* inclose will be true if called from close(), in which case we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * force the call to check because this is the last chance to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * If not in close(), then the authentication info could arrive in a trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * field, which we have not read yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    private void checkResponseCredentials (boolean inClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            if (!needToCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            if (validateProxy && currentProxyCredentials != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                String raw = responses.findValue ("Proxy-Authentication-Info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                if (inClose || (raw != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    currentProxyCredentials.checkResponse (raw, method, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                    currentProxyCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            if (validateServer && currentServerCredentials != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                String raw = responses.findValue ("Authentication-Info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                if (inClose || (raw != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                    currentServerCredentials.checkResponse (raw, method, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                    currentServerCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            if ((currentServerCredentials==null) && (currentProxyCredentials == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                needToCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    /* Tells us whether to follow a redirect.  If so, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * closes the connection (break any keep-alive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * resets the url, re-connects, and resets the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    private boolean followRedirect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        if (!getInstanceFollowRedirects()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        int stat = getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        if (stat < 300 || stat > 307 || stat == 306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                                || stat == HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        String loc = getHeaderField("Location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            /* this should be present - if not, we have no choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
             * but to go forward w/ the response we got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        URL locUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            locUrl = new URL(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            if (!url.getProtocol().equalsIgnoreCase(locUrl.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        } catch (MalformedURLException mue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
          // treat loc as a relative URI to conform to popular browsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
          locUrl = new URL(url, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            throw new HttpRetryException (RETRY_MSG3, stat, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        }
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2089
        if (logger.isLoggable(Level.FINE)) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2090
            logger.fine("Redirected from " + url + " to " + locUrl);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2091
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        // clear out old response headers!!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        if (stat == HTTP_USE_PROXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            /* This means we must re-request the resource through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
             * proxy denoted in the "Location:" field of the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
             * Judging by the spec, the string in the Location header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
             * _should_ denote a URL - let's hope for "http://my.proxy.org"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
             * Make a new HttpClient to the proxy, using HttpClient's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
             * Instance-specific proxy fields, but note we're still fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
             * the same URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            String proxyHost = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            int proxyPort = locUrl.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                security.checkConnect(proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            setProxiedClient (url, proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            requests.set(0, method + " " + http.getURLFile()+" "  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                             httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            // maintain previous headers, just change the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            // of the file we're getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            url = locUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            if (method.equals("POST") && !Boolean.getBoolean("http.strictPostRedirect") && (stat!=307)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                /* The HTTP/1.1 spec says that a redirect from a POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                 * *should not* be immediately turned into a GET, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                 * that some HTTP/1.0 clients incorrectly did this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                 * Correct behavior redirects a POST to another POST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                 * Unfortunately, since most browsers have this incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                 * behavior, the web works this way now.  Typical usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                 * seems to be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                 *   POST a login code or passwd to a web page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                 *   after validation, the server redirects to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                 *     (welcome) page
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                 *   The second request is (erroneously) expected to be GET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                 * We will do the incorrect thing (POST-->GET) by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                 * We will provide the capability to do the "right" thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                 * (POST-->POST) by a system property, "http.strictPostRedirect=true"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                setRequests = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                setRequestMethod("GET");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                    connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                /* Even after a connect() call, http variable still can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                 * null, if a ResponseCache has been installed and it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                 * a non-null CacheResponse instance. So check nullity before using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                 * And further, if http is null, there's no need to do anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                 * about request headers because successive http session will use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                 * cachedInputStream/cachedHeaders anyway, which is returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                 * CacheResponse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                    requests.set(0, method + " " + http.getURLFile()+" "  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                                 httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                    String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                    if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    requests.set("Host", host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    /* dummy byte buffer for reading off socket prior to closing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    byte[] cdata = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * Reset (without disconnecting the TCP conn) in order to do another transaction with this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
    private void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        http.reuse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        /* must save before calling close */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        reuseClient = http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        InputStream is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        if (!method.equals("HEAD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                /* we want to read the rest of the response without using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                 * hurry mechanism, because that would close the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                 * if everything is not available immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                if ((is instanceof ChunkedInputStream) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    (is instanceof MeteredStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    /* reading until eof will not block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    while (is.read (cdata) > 0) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    /* raw stream, which will block on read, so only read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                     * the expected number of bytes, probably 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                     */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2196
                    long cl = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2197
                    int n = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2198
                    String cls = responses.findValue ("Content-Length");
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2199
                    if (cls != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2200
                        try {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2201
                            cl = Long.parseLong (cls);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2202
                        } catch (NumberFormatException e) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2203
                            cl = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2204
                        }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2205
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2206
                    for (long i=0; i<cl; ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        if ((n = is.read (cdata)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                            i+= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                http.reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                if (is instanceof MeteredStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        responseCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * Disconnect from the server (for internal use)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    private void disconnectInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        responseCode = -1;
78
65353d0d0e31 6660405: HttpURLConnection returns the wrong InputStream
jccollet
parents: 74
diff changeset
  2236
        inputStream = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            http.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * Disconnect from the server (public API)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        responseCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
             * If we have an input stream this means we received a response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
             * from the server. That stream may have been read to EOF and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
             * dependening on the stream type may already be closed or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
             * the http client may be returned to the keep-alive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
             * If the http client has been returned to the keep-alive cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
             * it may be closed (idle timeout) or may be allocated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
             * another request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
             * In other to avoid timing issues we close the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
             * which will either close the underlying connection or return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
             * the client to the cache. If there's a possibility that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
             * client has been returned to the cache (ie: stream is a keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
             * alive stream or a chunked input stream) then we remove an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
             * idle connection to the server. Note that this approach
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
             * can be considered an approximation in that we may close a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
             * different idle connection to that used by the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
             * Additionally it's possible that we close two connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
             * - the first becuase it wasn't an EOF (and couldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
             * hurried) - the second, another idle connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
             * same server. The is okay because "disconnect" is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
             * indication that the application doesn't intend to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
             * this http server for a while.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                HttpClient hc = http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                // un-synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                boolean ka = hc.isKeepingAlive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                    inputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                // if the connection is persistent it may have been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                // or returned to the keep-alive cache. If it's been returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                // to the keep-alive cache then we would like to close it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                // but it may have been allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                if (ka) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                    hc.closeIdleConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                // We are deliberatly being disconnected so HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                // should not try to resend the request no matter what stage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                // of the connection we are in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                http.setDoNotRetry(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                http.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            //      poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        cachedInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            cachedHeaders.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    public boolean usingProxy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            return (http.getProxyHostUsed() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * Gets a header field by name. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @param name the name of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2335
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    public String getHeaderField(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            return cachedHeaders.findValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        return responses.findValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * Returns an unmodifiable Map of the header fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     * The Map keys are Strings that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * response-header field names. Each Map value is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * unmodifiable List of Strings that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * the corresponding field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * @return a Map of header fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2358
    @Override
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2359
    public Map<String, List<String>> getHeaderFields() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            return cachedHeaders.getHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        return responses.getHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * Gets a header field by index. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * @param n the index of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2375
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    public String getHeaderField(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
           return cachedHeaders.getValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        return responses.getValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * Gets a header field by index. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * @param n the index of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2391
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    public String getHeaderFieldKey(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            return cachedHeaders.getKey(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        return responses.getKey(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * Sets request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * @param value the value to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2409
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    public void setRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        checkMessageHeader(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        requests.set(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * Adds a general request property specified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * key-value pair.  This method will not overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * existing values associated with the same key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * @param   value  the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * @see #getRequestProperties(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2431
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
    public void addRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        checkMessageHeader(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        requests.add(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    // Set a property for authentication.  This can safely disregard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    // the connected test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    void setAuthenticationProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        checkMessageHeader(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        requests.set(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2451
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    public String getRequestProperty (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
        // don't return headers containing security sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            for (int i=0; i < EXCLUDE_HEADERS.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        return requests.findValue(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * Returns an unmodifiable Map of general request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * properties for this connection. The Map keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * are Strings that represent the request-header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * field names. Each Map value is a unmodifiable List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * of Strings that represents the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * @return  a Map of the general request properties for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2476
    @Override
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2477
    public Map<String, List<String>> getRequestProperties() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        // exclude headers containing security-sensitive info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        return requests.getHeaders(EXCLUDE_HEADERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2485
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * Returns setting for connect timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * 0 return implies that the option is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * @return an <code>int</code> that indicates the connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     * @see java.net.URLConnection#setConnectTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * @see java.net.URLConnection#connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2505
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * Sets the read timeout to a specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     * milliseconds. A non-zero value specifies the timeout when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * reading from Input stream when a connection is established to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * resource. If the timeout expires before there is data available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * for read, a java.net.SocketTimeoutException is raised. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     * timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     * <p> Some non-standard implementation of this method ignores the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * specified timeout. To see the read timeout set, please call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * getReadTimeout().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * @param timeout an <code>int</code> that specifies the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * value to be used in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * @throws IllegalArgumentException if the timeout parameter is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * @see java.net.URLConnectiongetReadTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * @see java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2530
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     * Returns setting for read timeout. 0 return implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * @return an <code>int</code> that indicates the read timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
     * @see java.net.URLConnection#setReadTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * @see java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2548
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    String getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2557
    private MessageHeader mapToMessageHeader(Map<String, List<String>> map) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        MessageHeader headers = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        if (map == null || map.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2562
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2563
            String key = entry.getKey();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2564
            List<String> values = entry.getValue();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2565
            for (String value : values) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    headers.prepend(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                    headers.add(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    /* The purpose of this wrapper is just to capture the close() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * so we can check authentication information that may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     * arrived in a Trailer field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    class HttpInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        private CacheRequest cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        private OutputStream outputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        private boolean marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        private int inCache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        private int markCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        public HttpInputStream (InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            super (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            this.cacheRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            this.outputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        public HttpInputStream (InputStream is, CacheRequest cacheRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            super (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            this.cacheRequest = cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                this.outputStream = cacheRequest.getBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                this.cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                this.cacheRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                this.outputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
         * Marks the current position in this input stream. A subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
         * call to the <code>reset</code> method repositions this stream at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
         * the last marked position so that subsequent reads re-read the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
         * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
         * The <code>readlimit</code> argument tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
         * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
         * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
         * This method simply performs <code>in.mark(readlimit)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
         * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
         *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
         * @see     java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
         * @see     java.io.FilterInputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
         */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2622
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        public synchronized void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            super.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                markCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
         * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
         * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
         * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
         * simply performs <code>in.reset()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
         * Stream marks are intended to be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
         * situations where you need to read ahead a little to see what's in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
         * the stream. Often this is most easily done by invoking some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
         * general parser. If the stream is of the type handled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
         * parse, it just chugs along happily. If the stream is not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
         * that type, the parser should toss an exception when it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
         * If this happens within readlimit bytes, it allows the outer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
         * code to reset the stream and try another parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
         * @exception  IOException  if the stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
         *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
         * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
         * @see        java.io.FilterInputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
         */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2652
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                inCache += markCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2661
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                int ret = read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                return (ret == -1? ret : (b[0] & 0x00FF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2675
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2680
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                int newLen = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                int nWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                // write to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                if (inCache > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                    if (inCache >= newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                        inCache -= newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                        nWrite = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                        nWrite = newLen - inCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                        inCache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                    nWrite = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                if (nWrite > 0 && outputStream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                    outputStream.write(b, off + (newLen-nWrite), nWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                if (marked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                    markCount += newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2711
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                if (outputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                    if (read() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                        cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                        outputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                super.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                HttpURLConnection.this.http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                checkResponseCredentials (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
    class StreamingOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2736
        long expected;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2737
        long written;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        boolean error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        IOException errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
         * expectedLength == -1 if the stream is chunked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
         * expectedLength > 0 if the stream is fixed content-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
         *    In the 2nd case, we make sure the expected number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
         *    of bytes are actually written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
         */
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2748
        StreamingOutputStream (OutputStream os, long expectedLength) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            super (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            expected = expectedLength;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2751
            written = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2756
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        public void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            checkError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            written ++;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2760
            if (expected != -1L && written > expected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                throw new IOException ("too many bytes written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            out.write (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2766
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        public void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2771
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        public void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            checkError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            written += len;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2775
            if (expected != -1L && written > expected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                out.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                throw new IOException ("too many bytes written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
            out.write (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
        void checkError () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
            if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                throw errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
            if (((PrintStream)out).checkError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                throw new IOException("Error writing request body to server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        /* this is called to check that all the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
         * that were supposed to be written were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
         * and that the stream is now closed().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        boolean writtenOK () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            return closed && ! error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2802
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
            closed = true;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  2808
            if (expected != -1L) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                /* not chunked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                if (written != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                    errorExcp = new IOException ("insufficient data written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                    out.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                    throw errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                super.flush(); /* can't close the socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                /* chunked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                super.close (); /* force final chunk to be written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                /* trailing \r\n */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                OutputStream o = http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                o.write ('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                o.write ('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                o.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    static class ErrorStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        ByteBuffer buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        private ErrorStream(ByteBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
            is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        private ErrorStream(ByteBuffer buf, InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
            this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        // when this method is called, it's either the case that cl > 0, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        // if chunk-encoded, cl = -1; in other words, cl can't be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        public static InputStream getErrorStream(InputStream is, long cl, HttpClient http) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            // cl can't be 0; this following is here for extra precaution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            if (cl == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                // set SO_TIMEOUT to 1/5th of the total timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                // remember the old timeout value so that we can restore it
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  2856
                int oldTimeout = http.getReadTimeout();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  2857
                http.setReadTimeout(timeout4ESBuffer/5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                long expected = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                boolean isChunked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                // the chunked case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                if (cl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                    expected = bufSize4ES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                    isChunked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                    expected = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                if (expected <= bufSize4ES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                    int exp = (int) expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                    byte[] buffer = new byte[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                    int count = 0, time = 0, len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                            len = is.read(buffer, count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                                             buffer.length - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                            if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                                if (isChunked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                                    // chunked ended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                                    // if chunked ended prematurely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                                    // an IOException would be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                                // the server sends less than cl bytes of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                                throw new IOException("the server closes"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                                                      " before sending "+cl+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                                                      " bytes of data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                            count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                        } catch (SocketTimeoutException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                            time += timeout4ESBuffer/5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                    } while (count < exp && time < timeout4ESBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                    // reset SO_TIMEOUT to old value
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  2895
                    http.setReadTimeout(oldTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    // if count < cl at this point, we will not try to reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                    // the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                    if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                        // since we haven't read anything,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                        // we will return the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                        // inputstream back to the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                    }  else if ((count == expected && !(isChunked)) || (isChunked && len <0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                        // put the connection into keep-alive cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                        // the inputstream will try to do the right thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                        return new ErrorStream(ByteBuffer.wrap(buffer, 0, count));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                        // we read part of the response body
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                        return new ErrorStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                                      ByteBuffer.wrap(buffer, 0, count), is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                // ioex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2922
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
            if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                return buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                return buffer.remaining()+is.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
            int ret = read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            return (ret == -1? ret : (b[0] & 0x00FF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2937
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2942
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            int rem = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            if (rem > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                int ret = rem < len? rem : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                buffer.get(b, off, ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                    return is.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2958
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
/** An input stream that just returns EOF.  This is for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
 * HTTP URLConnections that are KeepAlive && use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
 * HEAD method - i.e., stream not dead, but nothing to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
class EmptyInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  2975
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
    public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    public int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
}