src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58630 6b93cc7741ba
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
     2
 * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5199
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.www.protocol.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
    28
import java.security.PrivilegedAction;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
    29
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.ProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.HttpRetryException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.PasswordAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.Authenticator;
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
    36
import java.net.HttpCookie;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.SocketTimeoutException;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
    40
import java.net.SocketPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.ProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.CookieHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.ResponseCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.net.CacheResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.SecureCacheResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.net.CacheRequest;
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
    50
import java.net.URLPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.net.Authenticator.RequestorType;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
    52
import java.security.AccessController;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
    53
import java.security.PrivilegedExceptionAction;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
    54
import java.security.PrivilegedActionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.*;
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
    56
import java.util.ArrayList;
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
    57
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.Iterator;
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
    64
import java.util.HashSet;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
    65
import java.util.HashMap;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
    66
import java.util.Set;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    67
import java.util.StringJoiner;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52169
diff changeset
    68
import jdk.internal.access.JavaNetHttpCookieAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52169
diff changeset
    69
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.net.*;
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
    71
import sun.net.util.IPAddressUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.net.www.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.net.www.http.HttpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.net.www.http.PosterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.net.www.http.ChunkedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.net.www.http.ChunkedOutputStream;
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
    77
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import java.nio.ByteBuffer;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
    82
import java.util.Objects;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
    83
import java.util.Properties;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    84
import static sun.net.www.protocol.http.AuthScheme.BASIC;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    85
import static sun.net.www.protocol.http.AuthScheme.DIGEST;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    86
import static sun.net.www.protocol.http.AuthScheme.NTLM;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    87
import static sun.net.www.protocol.http.AuthScheme.NEGOTIATE;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    88
import static sun.net.www.protocol.http.AuthScheme.KERBEROS;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
    89
import static sun.net.www.protocol.http.AuthScheme.UNKNOWN;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
    90
import sun.security.action.GetIntegerAction;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
    91
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * A class to represent an HTTP connection to a remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
public class HttpURLConnection extends java.net.HttpURLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   100
    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
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static final String version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final String userAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* max # of allowed re-directs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static final int defaultmaxRedirects = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static final int maxRedirects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* Not all servers support the (Proxy)-Authentication-Info headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * By default, we don't require them to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static final boolean validateProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static final boolean validateServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   115
    /** A, possibly empty, set of authentication schemes that are disabled
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   116
     *  when proxying plain HTTP ( not HTTPS ). */
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   117
    static final Set<String> disabledProxyingSchemes;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   118
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   119
    /** A, possibly empty, set of authentication schemes that are disabled
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   120
     *  when setting up a tunnel for HTTPS ( HTTP CONNECT ). */
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   121
    static final Set<String> disabledTunnelingSchemes;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private StreamingOutputStream strOutputStream;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
   124
    private static final String RETRY_MSG1 =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        "cannot retry due to proxy authentication, in streaming mode";
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
   126
    private static final String RETRY_MSG2 =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        "cannot retry due to server authentication, in streaming mode";
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
   128
    private static final String RETRY_MSG3 =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        "cannot retry due to redirection, in streaming mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * System properties related to error stream handling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * sun.net.http.errorstream.enableBuffering = <boolean>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * With the above system property set to true (default is false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * when the response code is >=400, the HTTP handler will try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * buffer the response body (up to a certain amount and within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * time limit). Thus freeing up the underlying socket connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * for reuse. The rationale behind this is that usually when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * server responds with a >=400 error (client error or server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * error, such as 404 file not found), the server will send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * small response body to explain who to contact and what to do to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * recover. With this property set to true, even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * application doesn't call getErrorStream(), read the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * body, and then call close(), the underlying socket connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * can still be kept-alive and reused. The following two system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * properties provide further control to the error stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * buffering behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * sun.net.http.errorstream.timeout = <int>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *     the timeout (in millisec) waiting the error stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *     to be buffered; default is 300 ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * sun.net.http.errorstream.bufferSize = <int>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *     the size (in bytes) to use for the buffering the error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *     default is 4k
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /* Should we enable buffering of error streams? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static boolean enableESBuffer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /* timeout waiting for read for buffered error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static int timeout4ESBuffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /* buffer size for buffered error stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static int bufSize4ES = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   172
    /*
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   173
     * Restrict setting of request headers through the public api
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   174
     * consistent with JavaScript XMLHttpRequest2 with a few
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   175
     * exceptions. Disallowed headers are silently ignored for
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   176
     * backwards compatibility reasons rather than throwing a
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   177
     * SecurityException. For example, some applets set the
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   178
     * Host header since old JREs did not implement HTTP 1.1.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   179
     * Additionally, any header starting with Sec- is
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   180
     * disallowed.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   181
     *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   182
     * The following headers are allowed for historical reasons:
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   183
     *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   184
     * Accept-Charset, Accept-Encoding, Cookie, Cookie2, Date,
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   185
     * Referer, TE, User-Agent, headers beginning with Proxy-.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   186
     *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   187
     * The following headers are allowed in a limited form:
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   188
     *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   189
     * Connection: close
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   190
     *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   191
     * See http://www.w3.org/TR/XMLHttpRequest2.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   192
     */
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   193
    private static final boolean allowRestrictedHeaders;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   194
    private static final Set<String> restrictedHeaderSet;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   195
    private static final String[] restrictedHeaders = {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   196
        /* Restricted by XMLHttpRequest2 */
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   197
        //"Accept-Charset",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   198
        //"Accept-Encoding",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   199
        "Access-Control-Request-Headers",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   200
        "Access-Control-Request-Method",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   201
        "Connection", /* close is allowed */
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   202
        "Content-Length",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   203
        //"Cookie",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   204
        //"Cookie2",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   205
        "Content-Transfer-Encoding",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   206
        //"Date",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   207
        //"Expect",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   208
        "Host",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   209
        "Keep-Alive",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   210
        "Origin",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   211
        // "Referer",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   212
        // "TE",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   213
        "Trailer",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   214
        "Transfer-Encoding",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   215
        "Upgrade",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   216
        //"User-Agent",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   217
        "Via"
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   218
    };
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   219
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   220
    private static String getNetProperty(String name) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   221
        PrivilegedAction<String> pa = () -> NetProperties.get(name);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   222
        return AccessController.doPrivileged(pa);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   223
    }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   224
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   225
    private static Set<String> schemesListToSet(String list) {
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   226
        if (list == null || list.isEmpty())
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   227
            return Collections.emptySet();
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   228
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   229
        Set<String> s = new HashSet<>();
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   230
        String[] parts = list.split("\\s*,\\s*");
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   231
        for (String part : parts)
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   232
            s.add(part.toLowerCase(Locale.ROOT));
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   233
        return s;
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   234
    }
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   235
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   237
        Properties props = GetPropertyAction.privilegedGetProperties();
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   238
        maxRedirects = GetIntegerAction.privilegedGetProperty(
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   239
                "http.maxRedirects", defaultmaxRedirects);
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   240
        version = props.getProperty("java.version");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   241
        String agent = props.getProperty("http.agent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (agent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            agent = "Java/"+version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            agent = agent + " Java/"+version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        userAgent = agent;
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   248
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   249
        // A set of net properties to control the use of authentication schemes
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
   250
        // when proxying/tunneling.
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   251
        String p = getNetProperty("jdk.http.auth.tunneling.disabledSchemes");
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   252
        disabledTunnelingSchemes = schemesListToSet(p);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   253
        p = getNetProperty("jdk.http.auth.proxying.disabledSchemes");
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   254
        disabledProxyingSchemes = schemesListToSet(p);
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
   255
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   256
        validateProxy = Boolean.parseBoolean(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   257
                props.getProperty("http.auth.digest.validateProxy"));
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   258
        validateServer = Boolean.parseBoolean(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   259
                props.getProperty("http.auth.digest.validateServer"));
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   260
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   261
        enableESBuffer = Boolean.parseBoolean(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   262
                props.getProperty("sun.net.http.errorstream.enableBuffering"));
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   263
        timeout4ESBuffer = GetIntegerAction.privilegedGetProperty(
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   264
                "sun.net.http.errorstream.timeout", 300);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (timeout4ESBuffer <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            timeout4ESBuffer = 300; // use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   269
        bufSize4ES = GetIntegerAction.privilegedGetProperty(
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   270
                "sun.net.http.errorstream.bufferSize", 4096);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (bufSize4ES <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            bufSize4ES = 4096; // use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   275
        allowRestrictedHeaders = Boolean.parseBoolean(
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 33292
diff changeset
   276
                props.getProperty("sun.net.http.allowRestrictedHeaders"));
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   277
        if (!allowRestrictedHeaders) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
   278
            restrictedHeaderSet = new HashSet<>(restrictedHeaders.length);
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   279
            for (int i=0; i < restrictedHeaders.length; i++) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   280
                restrictedHeaderSet.add(restrictedHeaders[i].toLowerCase());
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   281
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   282
        } else {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   283
            restrictedHeaderSet = null;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   284
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    static final String httpVersion = "HTTP/1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static final String acceptString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // the following http request headers should NOT have their values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // returned for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static final String[] EXCLUDE_HEADERS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            "Proxy-Authorization",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            "Authorization"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    };
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   297
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   298
    // also exclude system cookies when any might be set
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   299
    private static final String[] EXCLUDE_HEADERS2= {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   300
            "Proxy-Authorization",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   301
            "Authorization",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   302
            "Cookie",
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   303
            "Cookie2"
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   304
    };
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   305
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    protected HttpClient http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    protected Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    protected Proxy instProxy;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   309
    protected volatile Authenticator authenticator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   310
    protected volatile String authenticatorKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private CookieHandler cookieHandler;
11839
53065c5f535d 7133367: ResponseCache.put should not be called when setUseCaches(false)
chegar
parents: 11528
diff changeset
   313
    private final ResponseCache cacheHandler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    // the cached response, and cached response headers and body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    protected CacheResponse cachedResponse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private MessageHeader cachedHeaders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private InputStream cachedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /* output stream to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    protected PrintStream ps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /* buffered error stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private InputStream errorStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /* User set Cookies */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private boolean setUserCookies = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private String userCookies = null;
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   330
    private String userCookies2 = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /* We only have a single static authenticator for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * REMIND:  backwards compatibility with JDK 1.1.  Should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * eliminated for JDK 2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
   336
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private static HttpAuthenticator defaultAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /* all the headers we send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * NOTE: do *NOT* dump out the content of 'requests' in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * output or stacktrace since it may contain security-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * headers such as those defined in EXCLUDE_HEADERS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private MessageHeader requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   346
    /* The headers actually set by the user are recorded here also
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   347
     */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   348
    private MessageHeader userHeaders;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   349
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   350
    /* Headers and request method cannot be changed
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   351
     * once this flag is set in :-
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   352
     *     - getOutputStream()
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   353
     *     - getInputStream())
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   354
     *     - connect()
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   355
     * Access synchronized on this.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   356
     */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   357
    private boolean connecting = false;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /* The following two fields are only used with Digest Authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    String domain;      /* The list of authentication domains */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    DigestAuthentication.Parameters digestparams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /* Current credentials in use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    AuthenticationInfo  currentProxyCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    AuthenticationInfo  currentServerCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    boolean             needToCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private boolean doingNTLM2ndStage = false; /* doing the 2nd stage of an NTLM server authentication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private boolean doingNTLMp2ndStage = false; /* doing the 2nd stage of an NTLM proxy authentication */
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
   369
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
   370
    /* try auth without calling Authenticator. Used for transparent NTLM authentication */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
   371
    private boolean tryTransparentNTLMServer = true;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
   372
    private boolean tryTransparentNTLMProxy = true;
28545
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
   373
    private boolean useProxyResponseCode = false;
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
   374
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
   375
    /* Used by Windows specific code */
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   376
    private Object authObj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /* Set if the user is manually setting the Authorization or Proxy-Authorization headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    boolean isUserServerAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    boolean isUserProxyAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
   382
    String serverAuthKey, proxyAuthKey;
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
   383
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /* Progress source */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    protected ProgressSource pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /* all the response headers we get back */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private MessageHeader responses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /* the stream _from_ the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private InputStream inputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /* post stream _to_ the server, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private PosterOutputStream poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /* Indicates if the std. request headers have been set in requests. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private boolean setRequests=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /* Indicates whether a request has already failed or not */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private boolean failedOnce=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /* Remembered Exception, we will throw it again if somebody
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
       calls getInputStream after disconnect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private Exception rememberedException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /* If we decide we want to reuse a client, we put it here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private HttpClient reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   407
    /* Tunnel states */
14008
b572c1f3a7ad 7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents: 14007
diff changeset
   408
    public enum TunnelState {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   409
        /* No tunnel */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   410
        NONE,
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   411
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   412
        /* Setting up a tunnel */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   413
        SETUP,
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   414
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   415
        /* 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
   416
        TUNNELING
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   417
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   418
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
   419
    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
   420
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
   421
    /* Redefine timeouts from java.net.URLConnection as we need -1 to mean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * not set. This is to ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
   424
    private int connectTimeout = NetworkClient.DEFAULT_CONNECT_TIMEOUT;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
   425
    private int readTimeout = NetworkClient.DEFAULT_READ_TIMEOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
   427
    /* A permission converted from a URLPermission */
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   428
    private SocketPermission socketPermission;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   429
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   430
    /* Logging support */
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   431
    private static final PlatformLogger logger =
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   432
            PlatformLogger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   433
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * privileged request password authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private static PasswordAuthentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   440
                            final Authenticator authenticator,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            final String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            final InetAddress addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            final String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            final String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            final String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            final URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                            final RequestorType authType) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   449
        return java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
   450
            new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   451
                public PasswordAuthentication run() {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
   452
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   453
                        logger.finest("Requesting Authentication: host =" + host + " url = " + url);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   454
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   455
                    PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   456
                        authenticator, host, addr, port, protocol,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        prompt, scheme, url, authType);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
   458
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   459
                        logger.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   460
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   461
                    return pass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   466
    private boolean isRestrictedHeader(String key, String value) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   467
        if (allowRestrictedHeaders) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   468
            return false;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   469
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   470
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   471
        key = key.toLowerCase();
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   472
        if (restrictedHeaderSet.contains(key)) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   473
            /*
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   474
             * Exceptions to restricted headers:
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   475
             *
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   476
             * Allow "Connection: close".
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   477
             */
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   478
            if (key.equals("connection") && value.equalsIgnoreCase("close")) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   479
                return false;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   480
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   481
            return true;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   482
        } else if (key.startsWith("sec-")) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   483
            return true;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   484
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   485
        return false;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   486
    }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   487
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   488
    /*
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   489
     * Checks the validity of http message header and whether the header
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   490
     * is restricted and throws IllegalArgumentException if invalid or
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   491
     * restricted.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   492
     */
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   493
    private boolean isExternalMessageHeaderAllowed(String key, String value) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   494
        checkMessageHeader(key, value);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   495
        if (!isRestrictedHeader(key, value)) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   496
            return true;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   497
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   498
        return false;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   499
    }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
   500
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   501
    /* Logging support */
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   502
    public static PlatformLogger getHttpLogger() {
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   503
        return logger;
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   504
    }
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   505
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   506
    /* Used for Windows NTLM implementation */
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   507
    public Object authObj() {
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   508
        return authObj;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   509
    }
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   510
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   511
    public void authObj(Object authObj) {
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   512
        this.authObj = authObj;
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   513
    }
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
   514
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   515
    @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   516
    public synchronized void setAuthenticator(Authenticator auth) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   517
        if (connecting || connected) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   518
            throw new IllegalStateException(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   519
                  "Authenticator must be set before connecting");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   520
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   521
        authenticator = Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   522
        authenticatorKey = AuthenticatorKeys.getKey(authenticator);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   523
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   524
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   525
    public String getAuthenticatorKey() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   526
        String k = authenticatorKey;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   527
        if (k == null) return AuthenticatorKeys.getKey(authenticator);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   528
        return k;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   529
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   530
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * checks the validity of http message header and throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * IllegalArgumentException if invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    private void checkMessageHeader(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        char LF = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int index = key.indexOf(LF);
33292
4d2290a6ce1f 8130193: Improve HTTP connections
michaelm
parents: 32834
diff changeset
   538
        int index1 = key.indexOf(':');
4d2290a6ce1f 8130193: Improve HTTP connections
michaelm
parents: 32834
diff changeset
   539
        if (index != -1 || index1 != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                "Illegal character(s) in message header field: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            index = value.indexOf(LF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            while (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (index < value.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    char c = value.charAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    if ((c==' ') || (c=='\t')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        // ok, check the next occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        index = value.indexOf(LF, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    "Illegal character(s) in message header value: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   565
    public synchronized void setRequestMethod(String method)
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   566
                        throws ProtocolException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   567
        if (connecting) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   568
            throw new IllegalStateException("connect in progress");
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   569
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   570
        super.setRequestMethod(method);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   571
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   572
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /* adds the standard key/val pairs to reqests if necessary & write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * given PrintStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    private void writeRequests() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        /* print all message headers in the MessageHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         * onto the wire - all the ones we've set and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * others that have been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // 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
   582
        if (http.usingProxy && tunnelState() != TunnelState.TUNNELING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            setPreemptiveProxyAuthentication(requests);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (!setRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            /* We're very particular about the order in which we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
             * set the request headers here.  The order should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
             * matter, but some careless CGI programs have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
             * written to expect a very particular order of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
             * standard headers.  To name names, the order in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
             * Navigator3.0 sends them.  In particular, we make *sure*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
             * to send Content-type: <> and Content-length:<> second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
             * to last and last, respectively, in the case of a POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
             * request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
             */
22348
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   597
            if (!failedOnce) {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   598
                checkURLFile();
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
   599
                requests.prepend(method + " " + getRequestURI()+" "  +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                 httpVersion, null);
22348
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   601
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (!getUseCaches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                requests.setIfNotSet ("Cache-Control", "no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                requests.setIfNotSet ("Pragma", "no-cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            requests.setIfNotSet("User-Agent", userAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            int port = url.getPort();
24869
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
   608
            String host = stripIPv6ZoneId(url.getHost());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
22348
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   612
            String reqHost = requests.findValue("Host");
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   613
            if (reqHost == null ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   614
                (!reqHost.equalsIgnoreCase(host) && !checkSetHost()))
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   615
            {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   616
                requests.set("Host", host);
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   617
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            requests.setIfNotSet("Accept", acceptString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
             * For HTTP/1.1 the default behavior is to keep connections alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
             * However, we may be talking to a 1.0 server so we should set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
             * keep-alive just in case, except if we have encountered an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
             * or if keep alive is disabled via a system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // Try keep-alive only on first attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (!failedOnce && http.getHttpKeepAliveSet()) {
4923
cd2829ef32e2 6365587: Proxy-Connection header sent through tunnel
chegar
parents: 4158
diff changeset
   629
                if (http.usingProxy && tunnelState() != TunnelState.TUNNELING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    requests.setIfNotSet("Proxy-Connection", "keep-alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    requests.setIfNotSet("Connection", "keep-alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                 * RFC 2616 HTTP/1.1 section 14.10 says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                 * HTTP/1.1 applications that do not support persistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                 * connections MUST include the "close" connection option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                 * in every message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                requests.setIfNotSet("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            // Set modified since if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            long modTime = getIfModifiedSince();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (modTime != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                Date date = new Date(modTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                //use the preferred date format according to RFC 2068(HTTP1.1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                // RFC 822 and RFC 1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                SimpleDateFormat fo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                  new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                requests.setIfNotSet("If-Modified-Since", fo.format(date));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // check for preemptive authorization
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   655
            AuthenticationInfo sauth = AuthenticationInfo.getServerAuth(url,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   656
                                             getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (sauth != null && sauth.supportsPreemptiveAuthorization() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                // Sets "Authorization"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                requests.setIfNotSet(sauth.getHeaderName(), sauth.getHeaderValue(url,method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                currentServerCredentials = sauth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (!method.equals("PUT") && (poster != null || streaming())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                requests.setIfNotSet ("Content-type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        "application/x-www-form-urlencoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
6873
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   668
            boolean chunked = false;
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   669
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                if (chunkLength != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    requests.set ("Transfer-Encoding", "chunked");
6873
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   673
                    chunked = true;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   674
                } else { /* fixed content length */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   675
                    if (fixedContentLengthLong != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   676
                        requests.set ("Content-Length",
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   677
                                      String.valueOf(fixedContentLengthLong));
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   678
                    } else if (fixedContentLength != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   679
                        requests.set ("Content-Length",
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   680
                                      String.valueOf(fixedContentLength));
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
   681
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            } else if (poster != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                /* add Content-Length & POST/PUT data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                synchronized (poster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    /* close it, so no more data can be added */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    poster.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    requests.set("Content-Length",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                 String.valueOf(poster.size()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
6873
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   693
            if (!chunked) {
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   694
                if (requests.findValue("Transfer-Encoding") != null) {
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   695
                    requests.remove("Transfer-Encoding");
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
   696
                    if (logger.isLoggable(PlatformLogger.Level.WARNING)) {
6873
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   697
                        logger.warning(
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   698
                            "use streaming mode for chunked encoding");
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   699
                    }
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   700
                }
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   701
            }
7fe1f614001e 6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents: 5979
diff changeset
   702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            // get applicable cookies based on the uri and request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            // add them to the existing request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            setRequests=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
   709
        if (logger.isLoggable(PlatformLogger.Level.FINE)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
   710
            logger.fine(requests.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
6673
3674dbc66612 6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents: 6292
diff changeset
   712
        http.writeRequests(requests, poster, streaming());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (ps.checkError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            String proxyHost = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            int proxyPort = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                throw new IOException("Error writing to server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            } else { // try once more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                failedOnce=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (proxyHost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    setProxiedClient(url, proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    setNewClient (url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                ps = (PrintStream) http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                connected=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                setRequests=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
22348
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   735
    private boolean checkSetHost() {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   736
        SecurityManager s = System.getSecurityManager();
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   737
        if (s != null) {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   738
            String name = s.getClass().getName();
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   739
            if (name.equals("sun.plugin2.applet.AWTAppletSecurityManager") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   740
                name.equals("sun.plugin2.applet.FXAppletSecurityManager") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   741
                name.equals("com.sun.javaws.security.JavaWebStartSecurity") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   742
                name.equals("sun.plugin.security.ActivatorSecurityManager"))
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   743
            {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   744
                int CHECK_SET_HOST = -2;
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   745
                try {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   746
                    s.checkConnect(url.toExternalForm(), CHECK_SET_HOST);
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   747
                } catch (SecurityException ex) {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   748
                    return false;
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   749
                }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   750
            }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   751
        }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   752
        return true;
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   753
    }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   754
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   755
    private void checkURLFile() {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   756
        SecurityManager s = System.getSecurityManager();
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   757
        if (s != null) {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   758
            String name = s.getClass().getName();
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   759
            if (name.equals("sun.plugin2.applet.AWTAppletSecurityManager") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   760
                name.equals("sun.plugin2.applet.FXAppletSecurityManager") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   761
                name.equals("com.sun.javaws.security.JavaWebStartSecurity") ||
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   762
                name.equals("sun.plugin.security.ActivatorSecurityManager"))
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   763
            {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   764
                int CHECK_SUBPATH = -3;
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   765
                try {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   766
                    s.checkConnect(url.toExternalForm(), CHECK_SUBPATH);
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   767
                } catch (SecurityException ex) {
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   768
                    throw new SecurityException("denied access outside a permitted URL subpath", ex);
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   769
                }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   770
            }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   771
        }
8e8c84d9535f 8004562: Better support for crossdomain.xml
nikgor
parents: 21952
diff changeset
   772
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Create a new HttpClient object, bypassing the cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * HTTP client objects/connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    protected void setNewClient (URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        setNewClient(url, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Obtain a HttpsClient object. Use the cached copy if specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @param useCache  whether the cached connection should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *        if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    protected void setNewClient (URL url, boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
   794
        http = HttpClient.New(url, null, -1, useCache, connectTimeout, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Create a new HttpClient object, set up so that it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * per-instance proxying to the given HTTP proxy.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * bypasses the cache of HTTP client objects/connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @param proxyHost the proxy host to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param proxyPort the proxy port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    protected void setProxiedClient (URL url, String proxyHost, int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        setProxiedClient(url, proxyHost, proxyPort, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Obtain a HttpClient object, set up so that it uses per-instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * proxying to the given HTTP proxy. Use the cached copy of HTTP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * client objects/connections if specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param url       the URL being accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param proxyHost the proxy host to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param proxyPort the proxy port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param useCache  whether the cached connection should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *        if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    protected void setProxiedClient (URL url,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   825
                                     String proxyHost, int proxyPort,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   826
                                     boolean useCache)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        proxiedConnect(url, proxyHost, proxyPort, useCache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    protected void proxiedConnect(URL url,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   832
                                  String proxyHost, int proxyPort,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   833
                                  boolean useCache)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
   835
        http = HttpClient.New (url, proxyHost, proxyPort, useCache,
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
   836
            connectTimeout, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    protected HttpURLConnection(URL u, Handler handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // we set proxy == null to distinguish this case with the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // when per connection proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        this(u, null, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
47411
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   847
    private static String checkHost(String h) throws IOException {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   848
        if (h != null) {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   849
            if (h.indexOf('\n') > -1) {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   850
                throw new MalformedURLException("Illegal character in host");
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   851
            }
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   852
        }
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   853
        return h;
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   854
    }
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   855
    public HttpURLConnection(URL u, String host, int port) throws IOException {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   856
        this(u, new Proxy(Proxy.Type.HTTP,
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   857
                InetSocketAddress.createUnresolved(checkHost(host), port)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /** this constructor is used by other protocol handlers such as ftp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        that want to use http to fetch urls on their behalf.*/
47411
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   862
    public HttpURLConnection(URL u, Proxy p) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        this(u, p, new Handler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
47411
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   866
    private static URL checkURL(URL u) throws IOException {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   867
        if (u != null) {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   868
            if (u.toExternalForm().indexOf('\n') > -1) {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   869
                throw new MalformedURLException("Illegal character in URL");
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   870
            }
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   871
        }
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   872
        String s = IPAddressUtil.checkAuthority(u);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   873
        if (s != null) {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   874
            throw new MalformedURLException(s);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   875
        }
47411
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   876
        return u;
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   877
    }
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   878
47411
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   879
    protected HttpURLConnection(URL u, Proxy p, Handler handler)
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   880
            throws IOException {
45a84b0b59b9 8176751: Better URL connections
dfuchs
parents: 47216
diff changeset
   881
        super(checkURL(u));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        responses = new MessageHeader();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
   884
        userHeaders = new MessageHeader();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        instProxy = p;
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   887
        if (instProxy instanceof sun.net.ApplicationProxy) {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   888
            /* 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
   889
             * 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
   890
            try {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   891
                cookieHandler = CookieHandler.getDefault();
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   892
            } catch (SecurityException se) { /* swallow exception */ }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   893
        } else {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   894
            cookieHandler = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
   895
                new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   896
                public CookieHandler run() {
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   897
                    return CookieHandler.getDefault();
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   898
                }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   899
            });
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 1576
diff changeset
   900
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   901
        cacheHandler = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
   902
            new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   903
                public ResponseCache run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                return ResponseCache.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @deprecated.  Use java.net.Authenticator.setDefault() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
   912
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public static void setDefaultAuthenticator(HttpAuthenticator a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        defaultAuth = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * opens a stream allowing redirects only to the same host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public static InputStream openConnectionCheckRedirects(URLConnection c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        boolean redir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        int redirects = 0;
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
   925
        InputStream in;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   926
        Authenticator a = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            if (c instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                ((HttpURLConnection) c).setInstanceFollowRedirects(false);
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   931
                if (a == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   932
                    a = ((HttpURLConnection) c).authenticator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   933
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // We want to open the input stream before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            // getting headers, because getHeaderField()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            // et al swallow IOExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            in = c.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            redir = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            if (c instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                HttpURLConnection http = (HttpURLConnection) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                int stat = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                if (stat >= 300 && stat <= 307 && stat != 306 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        stat != HttpURLConnection.HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    URL base = http.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    String loc = http.getHeaderField("Location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    URL target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    if (loc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        target = new URL(base, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    if (target == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                        || !base.getProtocol().equals(target.getProtocol())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                        || base.getPort() != target.getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        || !hostsEqual(base, target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                        || redirects >= 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                        throw new SecurityException("illegal URL redirect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    redir = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    c = target.openConnection();
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   964
                    if (a != null && c instanceof HttpURLConnection) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   965
                        ((HttpURLConnection)c).setAuthenticator(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
   966
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        } while (redir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    // Same as java.net.URL.hostsEqual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    private static boolean hostsEqual(URL u1, URL u2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        final String h1 = u1.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        final String h2 = u2.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (h1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return h2 == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        } else if (h2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        } else if (h1.equalsIgnoreCase(h2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        // Have to resolve addresses before comparing, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        // names like tachyon and tachyon.eng would compare different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        final boolean result[] = {false};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
   994
            new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   995
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    InetAddress a1 = InetAddress.getByName(h1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    InetAddress a2 = InetAddress.getByName(h2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    result[0] = a1.equals(a2);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1000
                } catch(UnknownHostException | SecurityException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                return null;
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
        return result[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    // overridden in HTTPS subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public void connect() throws IOException {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1012
        synchronized (this) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1013
            connecting = true;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1014
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        plainConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    private boolean checkReuseConnection () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        if (reuseClient != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            http = reuseClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            http.setReadTimeout(getReadTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            http.reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1033
    private String getHostAndPort(URL url) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1034
        String host = url.getHost();
21344
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1035
        final String hostarg = host;
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1036
        try {
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1037
            // lookup hostname and use IP address if available
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1038
            host = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1039
                new PrivilegedExceptionAction<>() {
21344
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1040
                    public String run() throws IOException {
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1041
                            InetAddress addr = InetAddress.getByName(hostarg);
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1042
                            return addr.getHostAddress();
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1043
                    }
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1044
                }
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1045
            );
13091b742137 8025734: Use literal IP address where possible in SocketPermission generated by HttpURLPermission
michaelm
parents: 20787
diff changeset
  1046
        } catch (PrivilegedActionException e) {}
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1047
        int port = url.getPort();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1048
        if (port == -1) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1049
            String scheme = url.getProtocol();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1050
            if ("http".equals(scheme)) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1051
                return host + ":80";
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1052
            } else { // scheme must be https
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1053
                return host + ":443";
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1054
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1055
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1056
        return host + ":" + Integer.toString(port);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1057
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1058
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    protected void plainConnect()  throws IOException {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1060
        synchronized (this) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1061
            if (connected) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1062
                return;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1063
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1064
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1065
        SocketPermission p = URLtoSocketPermission(this.url);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1066
        if (p != null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1067
            try {
30685
5806d3adbd6b 8077155: LoginContext Subject ignored by jdk8 sun.net.www.protocol.http.HttpURLConnection
robm
parents: 29986
diff changeset
  1068
                AccessController.doPrivilegedWithCombiner(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1069
                    new PrivilegedExceptionAction<>() {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1070
                        public Void run() throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1071
                            plainConnect0();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1072
                            return null;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1073
                        }
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1074
                    }, null, p
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1075
                );
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1076
            } catch (PrivilegedActionException e) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1077
                    throw (IOException) e.getException();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1078
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1079
        } else {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1080
            // run without additional permission
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1081
            plainConnect0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1083
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1084
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1085
    /**
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1086
     *  if the caller has a URLPermission for connecting to the
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1087
     *  given URL, then return a SocketPermission which permits
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1088
     *  access to that destination. Return null otherwise. The permission
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1089
     *  is cached in a field (which can only be changed by redirects)
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1090
     */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1091
    SocketPermission URLtoSocketPermission(URL url) throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1092
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1093
        if (socketPermission != null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1094
            return socketPermission;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1095
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1096
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1097
        SecurityManager sm = System.getSecurityManager();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1098
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1099
        if (sm == null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1100
            return null;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1101
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1102
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1103
        // the permission, which we might grant
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1104
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1105
        SocketPermission newPerm = new SocketPermission(
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1106
            getHostAndPort(url), "connect"
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1107
        );
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1108
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1109
        String actions = getRequestMethod()+":" +
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1110
                getUserSetHeaders().getHeaderNamesInList();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1111
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1112
        String urlstring = url.getProtocol() + "://" + url.getAuthority()
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1113
                + url.getPath();
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1114
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1115
        URLPermission p = new URLPermission(urlstring, actions);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1116
        try {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1117
            sm.checkPermission(p);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1118
            socketPermission = newPerm;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1119
            return socketPermission;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1120
        } catch (SecurityException e) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1121
            // fall thru
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1122
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1123
        return null;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1124
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1125
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1126
    protected void plainConnect0()  throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        // try to see if request can be served from local cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        if (cacheHandler != null && getUseCaches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (uri != null) {
27177
3717db2c3bfe 8042622: Check for CRL results in IllegalArgumentException "white space not allowed"
michaelm
parents: 26219
diff changeset
  1132
                    cachedResponse = cacheHandler.get(uri, getRequestMethod(), getUserSetHeaders().getHeaders());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    if ("https".equalsIgnoreCase(uri.getScheme())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                        && !(cachedResponse instanceof SecureCacheResponse)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                        cachedResponse = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1137
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1138
                        logger.finest("Cache Request for " + uri + " / " + getRequestMethod());
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1139
                        logger.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null"));
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1140
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    if (cachedResponse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                        cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        cachedInputStream = cachedResponse.getBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                // ignore and commence normal connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            if (cachedHeaders != null && cachedInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                cachedResponse = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            /* Try to open connections using the following scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
             * return on the first one that's successful:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
             * 1) if (instProxy != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
             *        connect to instProxy; raise exception if failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
             * 2) else use system default ProxySelector
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents: 37781
diff changeset
  1162
             * 3) else make a direct connection if ProxySelector is not present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            if (instProxy == null) { // no instance Proxy is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                 * Do we have to use a proxy?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                 */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1169
                ProxySelector sel =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1171
                        new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1172
                            public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                                     return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                             });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    URI uri = sun.net.www.ParseUtil.toURI(url);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1178
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1179
                        logger.finest("ProxySelector Request for " + uri);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1180
                    }
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1181
                    final List<Proxy> proxies;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1182
                    try {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1183
                        proxies = sel.select(uri);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1184
                    } catch (IllegalArgumentException iae) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1185
                        throw new IOException("Failed to select a proxy", iae);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1186
                    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 57838
diff changeset
  1187
                    final Iterator<Proxy> it = proxies.iterator();
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1188
                    Proxy p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                    while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                        p = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                            if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                                http = getNewHttpClient(url, p, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                                http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                                // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                                // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                                http = getNewHttpClient(url, p, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                                http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                            }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1201
                            if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1202
                                if (p != null) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1203
                                    logger.finest("Proxy used: " + p.toString());
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1204
                                }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1205
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                        } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                            if (p != Proxy.NO_PROXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                sel.connectFailed(uri, p.address(), ioex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                if (!it.hasNext()) {
40737
9b5eee5d7a26 8161016: Strange behavior of URLConnection with proxy
rpatil
parents: 37781
diff changeset
  1211
                                    throw ioex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    // No proxy selector, create http client with no proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                        http = getNewHttpClient(url, null, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                        // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                        http = getNewHttpClient(url, null, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                        http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                if (!failedOnce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    http = getNewHttpClient(url, instProxy, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    // make sure to construct new connection if first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    // attempt failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    http = getNewHttpClient(url, instProxy, connectTimeout, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        // constructor to HTTP client calls openserver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    // subclass HttpsClient will overwrite & return an instance of HttpsClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
  1254
        return HttpClient.New(url, p, connectTimeout, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    // subclass HttpsClient will overwrite & return an instance of HttpsClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    protected HttpClient getNewHttpClient(URL url, Proxy p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                          int connectTimeout, boolean useCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        throws IOException {
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
  1261
        return HttpClient.New(url, p, connectTimeout, useCache, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1264
    private void expect100Continue() throws IOException {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1265
            // 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
  1266
            // Acceptance
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1267
            int oldTimeout = http.getReadTimeout();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1268
            boolean enforceTimeOut = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1269
            boolean timedOut = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1270
            if (oldTimeout <= 0) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1271
                // 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
  1272
                // Expect: 100-Continue
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1273
                http.setReadTimeout(5000);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1274
                enforceTimeOut = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1275
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1276
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1277
            try {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1278
                http.parseHTTP(responses, pi, this);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1279
            } catch (SocketTimeoutException se) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1280
                if (!enforceTimeOut) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1281
                    throw se;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1282
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1283
                timedOut = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1284
                http.setIgnoreContinue(true);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1285
            }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1286
            if (!timedOut) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1287
                // Can't use getResponseCode() yet
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1288
                String resp = responses.getValue(0);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1289
                // Parse the response which is of the form:
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1290
                // HTTP/1.1 417 Expectation Failed
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1291
                // HTTP/1.1 100 Continue
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1292
                if (resp != null && resp.startsWith("HTTP/")) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1293
                    String[] sa = resp.split("\\s+");
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1294
                    responseCode = -1;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1295
                    try {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1296
                        // Response code is 2nd token on the line
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1297
                        if (sa.length > 1)
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1298
                            responseCode = Integer.parseInt(sa[1]);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1299
                    } catch (NumberFormatException numberFormatException) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1300
                    }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1301
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1302
                if (responseCode != 100) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1303
                    throw new ProtocolException("Server rejected operation");
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1304
                }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1305
            }
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
  1306
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
  1307
            http.setReadTimeout(oldTimeout);
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 6879
diff changeset
  1308
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1309
            responseCode = -1;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1310
            responses.reset();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1311
            // Proceed
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1312
    }
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1313
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * Allowable input/output sequences:
18541
a974731db859 7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents: 18178
diff changeset
  1316
     * [interpreted as request entity]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * - get output, [write output,] get input, [read input]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * - get output, [write output]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * [interpreted as GET]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * - get input, [read input]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * Disallowed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * - get input, [read input,] get output, [write output]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1325
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    public synchronized OutputStream getOutputStream() throws IOException {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1327
        connecting = true;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1328
        SocketPermission p = URLtoSocketPermission(this.url);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1329
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1330
        if (p != null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1331
            try {
30685
5806d3adbd6b 8077155: LoginContext Subject ignored by jdk8 sun.net.www.protocol.http.HttpURLConnection
robm
parents: 29986
diff changeset
  1332
                return AccessController.doPrivilegedWithCombiner(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1333
                    new PrivilegedExceptionAction<>() {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1334
                        public OutputStream run() throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1335
                            return getOutputStream0();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1336
                        }
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1337
                    }, null, p
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1338
                );
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1339
            } catch (PrivilegedActionException e) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1340
                throw (IOException) e.getException();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1341
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1342
        } else {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1343
            return getOutputStream0();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1344
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1345
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1346
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1347
    private synchronized OutputStream getOutputStream0() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            if (!doOutput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                throw new ProtocolException("cannot write to a URLConnection"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                               + " if doOutput=false - call setDoOutput(true)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            if (method.equals("GET")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                method = "POST"; // Backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            }
18541
a974731db859 7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents: 18178
diff changeset
  1357
            if ("TRACE".equals(method) && "http".equals(url.getProtocol())) {
a974731db859 7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents: 18178
diff changeset
  1358
                throw new ProtocolException("HTTP method TRACE" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                                            " doesn't support output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            // if there's already an input stream open, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                throw new ProtocolException("Cannot write output after reading input.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1370
            boolean expectContinue = false;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1371
            String expects = requests.findValue("Expect");
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents: 17473
diff changeset
  1372
            if ("100-Continue".equalsIgnoreCase(expects) && streaming()) {
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1373
                http.setIgnoreContinue(false);
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1374
                expectContinue = true;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1375
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            if (streaming() && strOutputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            }
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1380
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1381
            if (expectContinue) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1382
                expect100Continue();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1383
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                if (strOutputStream == null) {
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1387
                    if (chunkLength != -1) { /* chunked */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1388
                         strOutputStream = new StreamingOutputStream(
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1389
                               new ChunkedOutputStream(ps, chunkLength), -1L);
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1390
                    } else { /* must be fixed content length */
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1391
                        long length = 0L;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1392
                        if (fixedContentLengthLong != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1393
                            length = fixedContentLengthLong;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1394
                        } else if (fixedContentLength != -1) {
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1395
                            length = fixedContentLength;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1396
                        }
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1397
                        strOutputStream = new StreamingOutputStream(ps, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                return strOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                if (poster == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    poster = new PosterOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                return poster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            throw e;
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1410
        } catch (ProtocolException e) {
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1411
            // 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
  1412
            // the 100-continue. disconnectInternal() forces it to -1
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1413
            int i = responseCode;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1414
            disconnectInternal();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1415
            responseCode = i;
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  1416
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
14007
2fde9d6d874a 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents: 11839
diff changeset
  1423
    public boolean streaming () {
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1424
        return (fixedContentLength != -1) || (fixedContentLengthLong != -1) ||
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  1425
               (chunkLength != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * get applicable cookies based on the uri and request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * add them to the existing request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    private void setCookieHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (cookieHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            // we only want to capture the user defined Cookies once, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            // they cannot be changed by user code after we are connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            // only internally.
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1437
            synchronized (this) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1438
                if (setUserCookies) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1439
                    int k = requests.getKey("Cookie");
6879
chegar
parents: 6685 6877
diff changeset
  1440
                    if (k != -1)
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1441
                        userCookies = requests.getValue(k);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1442
                    k = requests.getKey("Cookie2");
6879
chegar
parents: 6685 6877
diff changeset
  1443
                    if (k != -1)
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1444
                        userCookies2 = requests.getValue(k);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1445
                    setUserCookies = false;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1446
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            // remove old Cookie header before setting new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            requests.remove("Cookie");
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1451
            requests.remove("Cookie2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            if (uri != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1455
                if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1456
                    logger.finest("CookieHandler request for " + uri);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1457
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1458
                Map<String, List<String>> cookies
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1459
                    = cookieHandler.get(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1460
                        uri, requests.getHeaders(EXCLUDE_HEADERS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                if (!cookies.isEmpty()) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1462
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1463
                        logger.finest("Cookies retrieved: " + cookies.toString());
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  1464
                    }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1465
                    for (Map.Entry<String, List<String>> entry :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1466
                             cookies.entrySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1467
                        String key = entry.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                        // ignore all entries that don't have "Cookie"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                        // or "Cookie2" as keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                        if (!"Cookie".equalsIgnoreCase(key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                            !"Cookie2".equalsIgnoreCase(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1474
                        List<String> l = entry.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                        if (l != null && !l.isEmpty()) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1476
                            StringJoiner cookieValue = new StringJoiner("; ");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1477
                            for (String value : l) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1478
                                cookieValue.add(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                            }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1480
                            requests.add(key, cookieValue.toString());
2
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
            if (userCookies != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                int k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                if ((k = requests.getKey("Cookie")) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    requests.set("Cookie", requests.getValue(k) + ";" + userCookies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                    requests.set("Cookie", userCookies);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1492
            if (userCookies2 != null) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1493
                int k;
6877
80aa31f1b638 6986400: Change Cookie to Cookie2 in 6980004 fix
michaelm
parents: 6876
diff changeset
  1494
                if ((k = requests.getKey("Cookie2")) != -1)
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1495
                    requests.set("Cookie2", requests.getValue(k) + ";" + userCookies2);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1496
                else
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1497
                    requests.set("Cookie2", userCookies2);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  1498
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        } // end of getting cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1503
    @Override
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1504
    public synchronized InputStream getInputStream() throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1505
        connecting = true;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1506
        SocketPermission p = URLtoSocketPermission(this.url);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1507
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1508
        if (p != null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1509
            try {
30685
5806d3adbd6b 8077155: LoginContext Subject ignored by jdk8 sun.net.www.protocol.http.HttpURLConnection
robm
parents: 29986
diff changeset
  1510
                return AccessController.doPrivilegedWithCombiner(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1511
                    new PrivilegedExceptionAction<>() {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1512
                        public InputStream run() throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1513
                            return getInputStream0();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1514
                        }
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  1515
                    }, null, p
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1516
                );
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1517
            } catch (PrivilegedActionException e) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1518
                throw (IOException) e.getException();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1519
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1520
        } else {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1521
            return getInputStream0();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1522
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1523
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1524
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1525
    @SuppressWarnings("empty-statement")
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  1526
    private synchronized InputStream getInputStream0() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        if (!doInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            throw new ProtocolException("Cannot read from URLConnection"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                   + " if doInput=false (call setDoInput(true))");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        if (rememberedException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            if (rememberedException instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                throw new RuntimeException(rememberedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                throw getChainedException((IOException)rememberedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        if (streaming() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            if (strOutputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            /* make sure stream is closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            strOutputStream.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            if (!strOutputStream.writtenOK()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                throw new IOException ("Incomplete output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        int redirects = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        int respCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        long cl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        AuthenticationInfo serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        AuthenticationInfo proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        AuthenticationHeader srvHdr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         * Failed Negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
         * In some cases, the Negotiate auth is supported for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
         * remote host but the negotiate process still fails (For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
         * example, if the web page is located on a backend server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
         * and delegation is needed but fails). The authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         * process will start again, and we need to detect this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         * kind of failure and do proper fallback (say, to NTLM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         * In order to achieve this, the inNegotiate flag is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * when the first negotiate challenge is met (and reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         * if authentication is finished). If a fresh new negotiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         * challenge (no parameter) is found while inNegotiate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         * set, we know there's a failed auth attempt recently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
         * Here we'll ignore the header line so that fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         * can be practiced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         * inNegotiateProxy is for proxy authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        boolean inNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        boolean inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        // If the user has set either of these headers then do not remove them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        isUserServerAuth = requests.getKey("Authorization") != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        isUserProxyAuth = requests.getKey("Proxy-Authorization") != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                    connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                if (cachedInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    return cachedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                // Check if URL should be metered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                if (meteredInput)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    pi = new ProgressSource(url, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                    pi.beginTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                /* REMIND: This exists to fix the HttpsURLConnection subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                 * Hotjava needs to run on JDK1.1FCS.  Do proper fix once a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                 * proper solution for SSL can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                ps = (PrintStream)http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                if (!streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    writeRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                http.parseHTTP(responses, pi, this);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1617
                if (logger.isLoggable(PlatformLogger.Level.FINE)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  1618
                    logger.fine(responses.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                }
14766
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1620
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1621
                boolean b1 = responses.filterNTLMResponses("WWW-Authenticate");
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1622
                boolean b2 = responses.filterNTLMResponses("Proxy-Authenticate");
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1623
                if (b1 || b2) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  1624
                    if (logger.isLoggable(PlatformLogger.Level.FINE)) {
14766
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1625
                        logger.fine(">>>> Headers are filtered");
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1626
                        logger.fine(responses.toString());
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1627
                    }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1628
                }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  1629
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                inputStream = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                respCode = getResponseCode();
4047
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents: 3952
diff changeset
  1633
                if (respCode == -1) {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents: 3952
diff changeset
  1634
                    disconnectInternal();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents: 3952
diff changeset
  1635
                    throw new IOException ("Invalid Http response");
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents: 3952
diff changeset
  1636
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                if (respCode == HTTP_PROXY_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                    if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                        throw new HttpRetryException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                            RETRY_MSG1, HTTP_PROXY_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    boolean dontUseNegotiate = false;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1646
                    Iterator<String> iter = responses.multiValueIterator("Proxy-Authenticate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    while (iter.hasNext()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1648
                        String value = iter.next().trim();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                            if (!inNegotiateProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                                inNegotiateProxy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                                proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    // changes: add a 3rd parameter to the constructor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    // AuthenticationHeader, so that NegotiateAuthentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    // isSupported can be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                    // The other 2 appearances of "new AuthenticationHeader" is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    // altered in similar ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    AuthenticationHeader authhdr = new AuthenticationHeader (
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1669
                            "Proxy-Authenticate",
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1670
                            responses,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1671
                            new HttpCallerInfo(url,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1672
                                               http.getProxyHostUsed(),
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  1673
                                               http.getProxyPortUsed(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  1674
                                               authenticator),
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1675
                            dontUseNegotiate,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  1676
                            disabledProxyingSchemes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                    if (!doingNTLMp2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                        proxyAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                            resetProxyAuthentication(proxyAuthentication, authhdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                        if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                            redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                        /* in this case, only one header field will be present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        String raw = responses.findValue ("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                        if (!proxyAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                                                        authhdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                        if (serverAuthentication != null && srvHdr != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                                !serverAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                                        srvHdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                            disconnectInternal ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                        }
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  1702
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                        doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    }
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1706
                } else {
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1707
                    inNegotiateProxy = false;
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1708
                    doingNTLMp2ndStage = false;
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1709
                    if (!isUserProxyAuth)
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1710
                        requests.remove("Proxy-Authorization");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                // cache proxy authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    // cache auth info on success, domain header not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    proxyAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                if (respCode == HTTP_UNAUTHORIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                        throw new HttpRetryException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                            RETRY_MSG2, HTTP_UNAUTHORIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    boolean dontUseNegotiate = false;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1728
                    Iterator<String> iter = responses.multiValueIterator("WWW-Authenticate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    while (iter.hasNext()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1730
                        String value = iter.next().trim();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                            if (!inNegotiate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                                inNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                                doingNTLM2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                                serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    srvHdr = new AuthenticationHeader (
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                            "WWW-Authenticate", responses,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  1746
                            new HttpCallerInfo(url, authenticator),
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  1747
                            dontUseNegotiate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    String raw = srvHdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                    if (!doingNTLM2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                        if ((serverAuthentication != null)&&
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  1753
                            serverAuthentication.getAuthScheme() != NTLM) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                            if (serverAuthentication.isAuthorizationStale (raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                                /* we can retry with the current credentials */
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1756
                                disconnectWeb();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                                redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                                requests.set(serverAuthentication.getHeaderName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                                            serverAuthentication.getHeaderValue(url, method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                                currentServerCredentials = serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                                setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                                serverAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                        serverAuthentication = getServerAuthentication(srvHdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                        currentServerCredentials = serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                        if (serverAuthentication != null) {
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1771
                            disconnectWeb();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                            redirects++; // don't let things loop ad nauseum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                            setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                        /* header not used for ntlm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                        if (!serverAuthentication.setHeaders(this, null, raw)) {
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  1780
                            disconnectWeb();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                        doingNTLM2ndStage = false;
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  1784
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                        setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                // cache server authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                if (serverAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    // cache auth info on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                    if (!(serverAuthentication instanceof DigestAuthentication) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                        (domain == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                        if (serverAuthentication instanceof BasicAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                            // check if the path is shorter than the existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                            String npath = AuthenticationInfo.reducePath (url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                            String opath = serverAuthentication.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                            if (!opath.startsWith (npath) || npath.length() >= opath.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                                /* npath is longer, there must be a common root */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                                npath = BasicAuthentication.getRootPath (opath, npath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                            // remove the entry and create a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                            BasicAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                                (BasicAuthentication) serverAuthentication.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                            serverAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                            a.path = npath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                            serverAuthentication = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                        serverAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                        // what we cache is based on the domain list in the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                        DigestAuthentication srv = (DigestAuthentication)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                            serverAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                        StringTokenizer tok = new StringTokenizer (domain," ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                        String realm = srv.realm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                        PasswordAuthentication pw = srv.pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                        digestparams = srv.params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                        while (tok.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                            String path = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                /* path could be an abs_path or a complete URI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                URL u = new URL (url, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                DigestAuthentication d = new DigestAuthentication (
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  1824
                                                   false, u, realm, "Digest", pw,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  1825
                                                   digestparams, srv.authenticatorKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                                d.addToCache ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                            } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                // some flags should be reset to its initialized form so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                // even after a redirect the necessary checks can still be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                // preformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                inNegotiate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                //serverAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                doingNTLM2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                if (!isUserServerAuth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                    requests.remove("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                if (!isUserProxyAuth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                    requests.remove("Proxy-Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                if (respCode == HTTP_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    checkResponseCredentials (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                    needToCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                // a flag need to clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                needToCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                if (followRedirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                    /* if we should follow a redirect, then the followRedirects()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                     * method will disconnect() and re-connect us to the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                     * location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                    redirects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    // redirecting HTTP response may have set cookie, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                    // need to re-generate request header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                    setCookieHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    cl = Long.parseLong(responses.findValue("content-length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                } catch (Exception exc) { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                if (method.equals("HEAD") || cl == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    respCode == HTTP_NOT_MODIFIED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    respCode == HTTP_NO_CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                        pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                        pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    http.finished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                    http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                    inputStream = new EmptyInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                if (respCode == 200 || respCode == 203 || respCode == 206 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    respCode == 300 || respCode == 301 || respCode == 410) {
11839
53065c5f535d 7133367: ResponseCache.put should not be called when setUseCaches(false)
chegar
parents: 11528
diff changeset
  1889
                    if (cacheHandler != null && getUseCaches()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        // give cache a chance to save response in cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                        URI uri = ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                        if (uri != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                            URLConnection uconn = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                            if ("https".equalsIgnoreCase(uri.getScheme())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                                // use reflection to get to the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                                // HttpsURLConnection instance saved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                                // DelegateHttpsURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                                uconn = (URLConnection)this.getClass().getField("httpsURLConnection").get(this);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1900
                                } catch (IllegalAccessException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  1901
                                         NoSuchFieldException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                                    // ignored; use 'this'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                            CacheRequest cacheRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                                cacheHandler.put(uri, uconn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                            if (cacheRequest != null && http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                                http.setCacheRequest(cacheRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                                inputStream = new HttpInputStream(inputStream, cacheRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                if (!(inputStream instanceof HttpInputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                    inputStream = new HttpInputStream(inputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                if (respCode >= 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                    if (respCode == 404 || respCode == 410) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                        throw new FileNotFoundException(url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                        throw new java.io.IOException("Server returned HTTP" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                              " response code: " + respCode + " for URL: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                              url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                strOutputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            } while (redirects < maxRedirects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            throw new ProtocolException("Server redirected too many " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                                        " times ("+ redirects + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            rememberedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            rememberedException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            // buffer the error stream if bytes < 4k
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            // and it can be buffered within 1 second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            String te = responses.findValue("Transfer-Encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            if (http != null && http.isKeepingAlive() && enableESBuffer &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                (cl > 0 || (te != null && te.equalsIgnoreCase("chunked")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                errorStream = ErrorStream.getErrorStream(inputStream, cl, http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        } finally {
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  1951
            if (proxyAuthKey != null) {
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  1952
                AuthenticationInfo.endAuthRequest(proxyAuthKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            }
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  1954
            if (serverAuthKey != null) {
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  1955
                AuthenticationInfo.endAuthRequest(serverAuthKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * Creates a chained exception that has the same type as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * original exception and with the same message. Right now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * there is no convenient APIs for doing so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1965
    private IOException getChainedException(final IOException rememberedException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1967
            final Object[] args = { rememberedException.getMessage() };
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1968
            IOException chainedException =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1969
                java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  1970
                    new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1971
                        public IOException run() throws Exception {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1972
                            return (IOException)
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1973
                                rememberedException.getClass()
10701
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 10596
diff changeset
  1974
                                .getConstructor(new Class<?>[] { String.class })
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1975
                                .newInstance(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                    });
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1978
            chainedException.initCause(rememberedException);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            return chainedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        } catch (Exception ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            return rememberedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  1985
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    public InputStream getErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        if (connected && responseCode >= 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            // Client Error 4xx and Server Error 5xx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            if (errorStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                return errorStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            } else if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                return inputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * set or reset proxy authentication info in request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * after receiving a 407 error. In the case of NTLM however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * receiving a 407 is normal and we just skip the stale check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * because ntlm does not support this feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    private AuthenticationInfo
4150
74dfe4a968c6 6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents: 4047
diff changeset
  2005
        resetProxyAuthentication(AuthenticationInfo proxyAuthentication, AuthenticationHeader auth) throws IOException {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2006
        if ((proxyAuthentication != null )&&
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2007
             proxyAuthentication.getAuthScheme() != NTLM) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            String raw = auth.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            if (proxyAuthentication.isAuthorizationStale (raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                /* 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
  2011
                String value;
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2012
                if (proxyAuthentication instanceof DigestAuthentication) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2013
                    DigestAuthentication digestProxy = (DigestAuthentication)
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2014
                        proxyAuthentication;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2015
                    if (tunnelState() == TunnelState.SETUP) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2016
                        value = digestProxy.getHeaderValue(connectRequestURI(url), HTTP_CONNECT);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2017
                    } else {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2018
                        value = digestProxy.getHeaderValue(getRequestURI(), method);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2019
                    }
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2020
                } else {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2021
                    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
  2022
                }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2023
                requests.set(proxyAuthentication.getHeaderName(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                currentProxyCredentials = proxyAuthentication;
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2025
                return proxyAuthentication;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                proxyAuthentication.removeFromCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        proxyAuthentication = getHttpProxyAuthentication(auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        currentProxyCredentials = proxyAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        return  proxyAuthentication;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    /**
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2036
     * Returns the tunnel state.
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2037
     *
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2038
     * @return  the state
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2039
     */
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2040
    TunnelState tunnelState() {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2041
        return tunnelState;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2042
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2043
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2044
    /**
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2045
     * Set the tunneling status.
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2046
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 30685
diff changeset
  2047
     * @param tunnelState the state
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2048
     */
14008
b572c1f3a7ad 7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents: 14007
diff changeset
  2049
    public void setTunnelState(TunnelState tunnelState) {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2050
        this.tunnelState = tunnelState;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2051
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2052
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2053
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * establish a tunnel through proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    public synchronized void doTunneling() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        int retryTunnel = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        String statusLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        int respCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        AuthenticationInfo proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        String proxyHost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        int proxyPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        // save current requests so that they can be restored after tunnel is setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        MessageHeader savedRequests = requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        boolean inNegotiateProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        try {
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2072
            /* 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
  2073
            setTunnelState(TunnelState.SETUP);
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2074
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                if (!checkReuseConnection()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                    proxiedConnect(url, proxyHost, proxyPort, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                // send the "CONNECT" request to establish a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                // through proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                sendCONNECTRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                responses.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                // There is no need to track progress in HTTP Tunneling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                // so ProgressSource is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                http.parseHTTP(responses, null, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  2088
                /* Log the response to the CONNECT */
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2089
                if (logger.isLoggable(PlatformLogger.Level.FINE)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2090
                    logger.fine(responses.toString());
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2091
                }
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  2092
14766
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2093
                if (responses.filterNTLMResponses("Proxy-Authenticate")) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2094
                    if (logger.isLoggable(PlatformLogger.Level.FINE)) {
14766
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2095
                        logger.fine(">>>> Headers are filtered");
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2096
                        logger.fine(responses.toString());
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2097
                    }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2098
                }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 14008
diff changeset
  2099
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                statusLine = responses.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                StringTokenizer st = new StringTokenizer(statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                respCode = Integer.parseInt(st.nextToken().trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                if (respCode == HTTP_PROXY_AUTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    // Read comments labeled "Failed Negotiate" for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                    boolean dontUseNegotiate = false;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2107
                    Iterator<String> iter = responses.multiValueIterator("Proxy-Authenticate");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                    while (iter.hasNext()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2109
                        String value = iter.next().trim();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        if (value.equalsIgnoreCase("Negotiate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                                value.equalsIgnoreCase("Kerberos")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                            if (!inNegotiateProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                                inNegotiateProxy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                                dontUseNegotiate = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                                doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                                proxyAuthentication = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2123
                    AuthenticationHeader authhdr = new AuthenticationHeader(
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2124
                            "Proxy-Authenticate",
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2125
                            responses,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2126
                            new HttpCallerInfo(url,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2127
                                               http.getProxyHostUsed(),
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2128
                                               http.getProxyPortUsed(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2129
                                               authenticator),
41579
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2130
                            dontUseNegotiate,
c0fe2e6364d9 8160838: Better HTTP service
chegar
parents: 40737
diff changeset
  2131
                            disabledTunnelingSchemes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                    if (!doingNTLMp2ndStage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        proxyAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                            resetProxyAuthentication(proxyAuthentication, authhdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                        if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                            proxyHost = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                            proxyPort = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                            retryTunnel++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        String raw = responses.findValue ("Proxy-Authenticate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                        reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        if (!proxyAuthentication.setHeaders(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                                                authhdr.headerParser(), raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                            throw new IOException ("Authentication failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                        }
3061
419b6dba7ccf 6855297: Windows build breaks after 6811297
jccollet
parents: 3059
diff changeset
  2151
                        authObj = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                        doingNTLMp2ndStage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                // cache proxy authentication info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                if (proxyAuthentication != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                    // cache auth info on success, domain header not relevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    proxyAuthentication.addToCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                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
  2163
                    setTunnelState(TunnelState.TUNNELING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                // we don't know how to deal with other response code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                // so disconnect and report error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                disconnectInternal();
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2169
                setTunnelState(TunnelState.NONE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            } while (retryTunnel < maxRedirects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            if (retryTunnel >= maxRedirects || (respCode != HTTP_OK)) {
58630
6b93cc7741ba 8225298: Improve TLS connection support
chegar
parents: 57879
diff changeset
  2174
                if (respCode != HTTP_PROXY_AUTH) {
6b93cc7741ba 8225298: Improve TLS connection support
chegar
parents: 57879
diff changeset
  2175
                    // remove all but authenticate responses
6b93cc7741ba 8225298: Improve TLS connection support
chegar
parents: 57879
diff changeset
  2176
                    responses.reset();
6b93cc7741ba 8225298: Improve TLS connection support
chegar
parents: 57879
diff changeset
  2177
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                throw new IOException("Unable to tunnel through proxy."+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                                      " Proxy returns \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                                      statusLine + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        } finally  {
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  2183
            if (proxyAuthKey != null) {
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  2184
                AuthenticationInfo.endAuthRequest(proxyAuthKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        // restore original request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        requests = savedRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        // reset responses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        responses.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2195
    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
  2196
        String host = url.getHost();
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2197
        int port = url.getPort();
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2198
        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
  2199
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2200
        return host + ":" + port;
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2201
    }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2202
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * send a CONNECT request for establishing a tunnel to proxy server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    private void sendCONNECTRequest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
10136
af9631156b25 6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents: 9035
diff changeset
  2209
        requests.set(0, HTTP_CONNECT + " " + connectRequestURI(url)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                         + " " + httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        requests.setIfNotSet("User-Agent", userAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        requests.setIfNotSet("Host", host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        // Not really necessary for a tunnel, but can't hurt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        requests.setIfNotSet("Accept", acceptString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5979
diff changeset
  2222
        if (http.getHttpKeepAliveSet()) {
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5979
diff changeset
  2223
            requests.setIfNotSet("Proxy-Connection", "keep-alive");
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5979
diff changeset
  2224
        }
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5979
diff changeset
  2225
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        setPreemptiveProxyAuthentication(requests);
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  2227
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  2228
         /* Log the CONNECT request */
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2229
        if (logger.isLoggable(PlatformLogger.Level.FINE)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2230
            logger.fine(requests.toString());
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2231
        }
478
5adf3e3f1ff3 6659779: HttpURLConnections logger should log tunnel requests
chegar
parents: 94
diff changeset
  2232
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        http.writeRequests(requests, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * Sets pre-emptive proxy authentication in header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     */
4150
74dfe4a968c6 6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents: 4047
diff changeset
  2239
    private void setPreemptiveProxyAuthentication(MessageHeader requests) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        AuthenticationInfo pauth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            = AuthenticationInfo.getProxyAuth(http.getProxyHostUsed(),
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2242
                                              http.getProxyPortUsed(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2243
                                              getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        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
  2245
            String value;
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2246
            if (pauth instanceof DigestAuthentication) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2247
                DigestAuthentication digestProxy = (DigestAuthentication) pauth;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2248
                if (tunnelState() == TunnelState.SETUP) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2249
                    value = digestProxy
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2250
                        .getHeaderValue(connectRequestURI(url), HTTP_CONNECT);
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2251
                } else {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2252
                    value = digestProxy.getHeaderValue(getRequestURI(), method);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2253
                }
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2254
            } else {
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2255
                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
  2256
            }
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            // Sets "Proxy-authorization"
479
c6ddcfc7ff4d 6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents: 478
diff changeset
  2259
            requests.set(pauth.getHeaderName(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            currentProxyCredentials = pauth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * Gets the authentication for an HTTP proxy, and applies it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2268
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    private AuthenticationInfo getHttpProxyAuthentication (AuthenticationHeader authhdr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        /* get authorization from authenticator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        AuthenticationInfo ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        String raw = authhdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        String host = http.getProxyHostUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        int port = http.getProxyPortUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        if (host != null && authhdr.isPresent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            HeaderParser p = authhdr.headerParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            String realm = p.findValue("realm");
57838
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2278
            String charset = p.findValue("charset");
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2279
            boolean isUTF8 = (charset != null && charset.equalsIgnoreCase("UTF-8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            String scheme = authhdr.scheme();
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2281
            AuthScheme authScheme = UNKNOWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            if ("basic".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2283
                authScheme = BASIC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            } else if ("digest".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2285
                authScheme = DIGEST;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            } else if ("ntlm".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2287
                authScheme = NTLM;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            } else if ("Kerberos".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2290
                authScheme = KERBEROS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            } else if ("Negotiate".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2293
                authScheme = NEGOTIATE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                doingNTLMp2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2296
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            if (realm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                realm = "";
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2299
            proxyAuthKey = AuthenticationInfo.getProxyAuthKey(host, port, realm,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2300
                                authScheme, getAuthenticatorKey());
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  2301
            ret = AuthenticationInfo.getProxyAuth(proxyAuthKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            if (ret == null) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2303
                switch (authScheme) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2304
                case BASIC:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                        final String finalHost = host;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2308
                        addr = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  2309
                            new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2310
                                public InetAddress run()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                                    throws java.net.UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                                    return InetAddress.getByName(finalHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                    } catch (java.security.PrivilegedActionException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                        // User will have an unknown host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                        privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2320
                                    authenticator,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                                    host, addr, port, "http",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                                    realm, scheme, url, RequestorType.PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                    if (a != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2324
                        ret = new BasicAuthentication(true, host, port, realm, a,
57838
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2325
                                             isUTF8, getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2327
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2328
                case DIGEST:
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2329
                    a = privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2330
                                    authenticator,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                                    host, null, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                                    realm, scheme, url, RequestorType.PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                        DigestAuthentication.Parameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                            new DigestAuthentication.Parameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                        ret = new DigestAuthentication(true, host, port, realm,
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2337
                                             scheme, a, params,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2338
                                             getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2340
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2341
                case NTLM:
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2342
                    if (NTLMAuthenticationProxy.supported) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2343
                        /* tryTransparentNTLMProxy will always be true the first
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2344
                         * time around, but verify that the platform supports it
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2345
                         * otherwise don't try. */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2346
                        if (tryTransparentNTLMProxy) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2347
                            tryTransparentNTLMProxy =
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2348
                                    NTLMAuthenticationProxy.supportsTransparentAuth;
28545
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2349
                            /* If the platform supports transparent authentication
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2350
                             * then normally it's ok to do transparent auth to a proxy
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2351
                                         * because we generally trust proxies (chosen by the user)
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2352
                                         * But not in the case of 305 response where the server
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2353
                             * chose it. */
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2354
                            if (tryTransparentNTLMProxy && useProxyResponseCode) {
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2355
                                tryTransparentNTLMProxy = false;
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2356
                            }
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2357
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2358
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2359
                        a = null;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2360
                        if (tryTransparentNTLMProxy) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2361
                            logger.finest("Trying Transparent NTLM authentication");
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2362
                        } else {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2363
                            a = privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2364
                                                authenticator,
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2365
                                                host, null, port, url.getProtocol(),
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2366
                                                "", scheme, url, RequestorType.PROXY);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2367
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2368
                        /* If we are not trying transparent authentication then
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2369
                         * we need to have a PasswordAuthentication instance. For
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2370
                         * transparent authentication (Windows only) the username
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2371
                         * and password will be picked up from the current logged
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2372
                         * on users credentials.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2373
                        */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2374
                        if (tryTransparentNTLMProxy ||
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2375
                              (!tryTransparentNTLMProxy && a != null)) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2376
                            ret = NTLMAuthenticationProxy.proxy.create(true, host,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2377
                                    port, a, getAuthenticatorKey());
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2378
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2379
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2380
                        /* set to false so that we do not try again */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2381
                        tryTransparentNTLMProxy = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2383
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2384
                case NEGOTIATE:
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  2385
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2386
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2387
                case KERBEROS:
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  2388
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2389
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2390
                case UNKNOWN:
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2391
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 16007
diff changeset
  2392
                        logger.finest("Unknown/Unsupported authentication scheme: " + scheme);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 16007
diff changeset
  2393
                    }
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2394
                /*fall through*/
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2395
                default:
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2396
                    throw new AssertionError("should not reach here");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            // For backwards compatibility, we also try defaultAuth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            // REMIND:  Get rid of this for JDK2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            if (ret == null && defaultAuth != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                && defaultAuth.schemeSupported(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                    URL u = new URL("http", host, port, "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                    String a = defaultAuth.authString(u, scheme, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                    if (a != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2408
                        ret = new BasicAuthentication (true, host, port, realm, a,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2409
                                  getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                        // not in cache by default - cache on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                } catch (java.net.MalformedURLException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            if (ret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                if (!ret.setHeaders(this, p, raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2421
        if (logger.isLoggable(PlatformLogger.Level.FINER)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2422
            logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2423
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * Gets the authentication for an HTTP server, and applies it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * @param authHdr the AuthenticationHeader which tells what auth scheme is
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20787
diff changeset
  2431
     * preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2433
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    private AuthenticationInfo getServerAuthentication (AuthenticationHeader authhdr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        /* get authorization from authenticator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        AuthenticationInfo ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        String raw = authhdr.raw();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        /* When we get an NTLM auth from cache, don't set any special headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        if (authhdr.isPresent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            HeaderParser p = authhdr.headerParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            String realm = p.findValue("realm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            String scheme = authhdr.scheme();
57838
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2443
            String charset = p.findValue("charset");
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2444
            boolean isUTF8 = (charset != null && charset.equalsIgnoreCase("UTF-8"));
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2445
            AuthScheme authScheme = UNKNOWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            if ("basic".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2447
                authScheme = BASIC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            } else if ("digest".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2449
                authScheme = DIGEST;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            } else if ("ntlm".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2451
                authScheme = NTLM;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            } else if ("Kerberos".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2454
                authScheme = KERBEROS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            } else if ("Negotiate".equalsIgnoreCase(scheme)) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2457
                authScheme = NEGOTIATE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                doingNTLM2ndStage = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2460
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            domain = p.findValue ("domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            if (realm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                realm = "";
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2464
            serverAuthKey = AuthenticationInfo.getServerAuthKey(url, realm, authScheme,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2465
                                               getAuthenticatorKey());
5199
98b1778f0fd1 6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents: 5161
diff changeset
  2466
            ret = AuthenticationInfo.getServerAuth(serverAuthKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                    addr = InetAddress.getByName(url.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                } catch (java.net.UnknownHostException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                    // User will have addr = null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            // replacing -1 with default port for a protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
            if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                port = url.getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            if (ret == null) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2481
                switch(authScheme) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2482
                case KERBEROS:
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  2483
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2484
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2485
                case NEGOTIATE:
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2928
diff changeset
  2486
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2487
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2488
                case BASIC:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                    PasswordAuthentication a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                        privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2491
                            authenticator,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                            url.getHost(), addr, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                            realm, scheme, url, RequestorType.SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                    if (a != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2495
                        ret = new BasicAuthentication(false, url, realm, a,
57838
78844dceede6 8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents: 55711
diff changeset
  2496
                                    isUTF8, getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2498
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2499
                case DIGEST:
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2500
                    a = privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2501
                            authenticator,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                            url.getHost(), addr, port, url.getProtocol(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                            realm, scheme, url, RequestorType.SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                        digestparams = new DigestAuthentication.Parameters();
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2506
                        ret = new DigestAuthentication(false, url, realm, scheme,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2507
                                    a, digestparams,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2508
                                    getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2510
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2511
                case NTLM:
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2512
                    if (NTLMAuthenticationProxy.supported) {
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2513
                        URL url1;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2514
                        try {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2515
                            url1 = new URL (url, "/"); /* truncate the path */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2516
                        } catch (Exception e) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2517
                            url1 = url;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2518
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2520
                        /* tryTransparentNTLMServer will always be true the first
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2521
                         * time around, but verify that the platform supports it
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2522
                         * otherwise don't try. */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2523
                        if (tryTransparentNTLMServer) {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2524
                            tryTransparentNTLMServer =
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2525
                                    NTLMAuthenticationProxy.supportsTransparentAuth;
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2526
                            /* If the platform supports transparent authentication
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2527
                             * then check if we are in a secure environment
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2528
                             * whether, or not, we should try transparent authentication.*/
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2529
                            if (tryTransparentNTLMServer) {
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2530
                                tryTransparentNTLMServer =
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2531
                                        NTLMAuthenticationProxy.isTrustedSite(url);
8815
9a3f042d307e 6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents: 7022
diff changeset
  2532
                            }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2533
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2534
                        a = null;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2535
                        if (tryTransparentNTLMServer) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2536
                            logger.finest("Trying Transparent NTLM authentication");
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2537
                        } else {
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2538
                            a = privilegedRequestPasswordAuthentication(
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2539
                                authenticator,
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2540
                                url.getHost(), addr, port, url.getProtocol(),
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2541
                                "", scheme, url, RequestorType.SERVER);
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2542
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2543
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2544
                        /* If we are not trying transparent authentication then
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2545
                         * we need to have a PasswordAuthentication instance. For
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2546
                         * transparent authentication (Windows only) the username
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2547
                         * and password will be picked up from the current logged
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2548
                         * on users credentials.
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2549
                         */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2550
                        if (tryTransparentNTLMServer ||
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2551
                              (!tryTransparentNTLMServer && a != null)) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2552
                            ret = NTLMAuthenticationProxy.proxy.create(false,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2553
                                     url1, a, getAuthenticatorKey());
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2554
                        }
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2555
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2556
                        /* set to false so that we do not try again */
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2557
                        tryTransparentNTLMServer = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                    }
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2559
                    break;
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2560
                case UNKNOWN:
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2561
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 16007
diff changeset
  2562
                        logger.finest("Unknown/Unsupported authentication scheme: " + scheme);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 16007
diff changeset
  2563
                    }
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  2564
                /*fall through*/
3859
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2565
                default:
8b82336dedb3 6882594: Remove static dependancy on NTLM authentication
chegar
parents: 3465
diff changeset
  2566
                    throw new AssertionError("should not reach here");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            // For backwards compatibility, we also try defaultAuth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
            // REMIND:  Get rid of this for JDK2.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            if (ret == null && defaultAuth != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                && defaultAuth.schemeSupported(scheme)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                String a = defaultAuth.authString(url, scheme, realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                if (a != null) {
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2577
                    ret = new BasicAuthentication (false, url, realm, a,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents: 41579
diff changeset
  2578
                                    getAuthenticatorKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                    // not in cache by default - cache on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            if (ret != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                if (!ret.setHeaders(this, p, raw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2589
        if (logger.isLoggable(PlatformLogger.Level.FINER)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2590
            logger.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null"));
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2591
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    /* inclose will be true if called from close(), in which case we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     * force the call to check because this is the last chance to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * If not in close(), then the authentication info could arrive in a trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * field, which we have not read yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
    private void checkResponseCredentials (boolean inClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            if (!needToCheck)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                return;
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2604
            if ((validateProxy && currentProxyCredentials != null) &&
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2605
                (currentProxyCredentials instanceof DigestAuthentication)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                String raw = responses.findValue ("Proxy-Authentication-Info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                if (inClose || (raw != null)) {
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2608
                    DigestAuthentication da = (DigestAuthentication)
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2609
                        currentProxyCredentials;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2610
                    da.checkResponse (raw, method, getRequestURI());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                    currentProxyCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            }
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2614
            if ((validateServer && currentServerCredentials != null) &&
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2615
                (currentServerCredentials instanceof DigestAuthentication)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                String raw = responses.findValue ("Authentication-Info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                if (inClose || (raw != null)) {
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2618
                    DigestAuthentication da = (DigestAuthentication)
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2619
                        currentServerCredentials;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2620
                    da.checkResponse (raw, method, url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                    currentServerCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            if ((currentServerCredentials==null) && (currentProxyCredentials == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                needToCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2634
   /* The request URI used in the request line for this request.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2635
    * Also, needed for digest authentication
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2636
    */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2637
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2638
    String requestURI = null;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2639
4150
74dfe4a968c6 6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents: 4047
diff changeset
  2640
    String getRequestURI() throws IOException {
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2641
        if (requestURI == null) {
4150
74dfe4a968c6 6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents: 4047
diff changeset
  2642
            requestURI = http.getURLFile();
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2643
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2644
        return requestURI;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2645
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2646
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
    /* Tells us whether to follow a redirect.  If so, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     * closes the connection (break any keep-alive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     * resets the url, re-connects, and resets the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
    private boolean followRedirect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        if (!getInstanceFollowRedirects()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2657
        final int stat = getResponseCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        if (stat < 300 || stat > 307 || stat == 306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                                || stat == HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        }
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2662
        final String loc = getHeaderField("Location");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            /* this should be present - if not, we have no choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
             * but to go forward w/ the response we got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        }
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2669
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        URL locUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
            locUrl = new URL(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            if (!url.getProtocol().equalsIgnoreCase(locUrl.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        } catch (MalformedURLException mue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
          // treat loc as a relative URI to conform to popular browsers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
          locUrl = new URL(url, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        }
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2681
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2682
        final URL locUrl0 = locUrl;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2683
        socketPermission = null; // force recalculation
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2684
        SocketPermission p = URLtoSocketPermission(locUrl);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2685
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2686
        if (p != null) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2687
            try {
30685
5806d3adbd6b 8077155: LoginContext Subject ignored by jdk8 sun.net.www.protocol.http.HttpURLConnection
robm
parents: 29986
diff changeset
  2688
                return AccessController.doPrivilegedWithCombiner(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28545
diff changeset
  2689
                    new PrivilegedExceptionAction<>() {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2690
                        public Boolean run() throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2691
                            return followRedirect0(loc, stat, locUrl0);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2692
                        }
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 18577
diff changeset
  2693
                    }, null, p
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2694
                );
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2695
            } catch (PrivilegedActionException e) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2696
                throw (IOException) e.getException();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2697
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2698
        } else {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2699
            // run without additional permission
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2700
            return followRedirect0(loc, stat, locUrl);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2701
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2702
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2703
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2704
    /* Tells us whether to follow a redirect.  If so, it
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2705
     * closes the connection (break any keep-alive) and
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2706
     * resets the url, re-connects, and resets the request
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2707
     * property.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2708
     */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2709
    private boolean followRedirect0(String loc, int stat, URL locUrl)
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2710
        throws IOException
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  2711
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        if (streaming()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
            throw new HttpRetryException (RETRY_MSG3, stat, loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17952
diff changeset
  2716
        if (logger.isLoggable(PlatformLogger.Level.FINE)) {
3866
34cd368bd2dd 6882384: Update http protocol handler to use PlatformLogger
chegar
parents: 3859
diff changeset
  2717
            logger.fine("Redirected from " + url + " to " + locUrl);
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2718
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        // clear out old response headers!!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        if (stat == HTTP_USE_PROXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            /* This means we must re-request the resource through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
             * proxy denoted in the "Location:" field of the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
             * Judging by the spec, the string in the Location header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
             * _should_ denote a URL - let's hope for "http://my.proxy.org"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
             * Make a new HttpClient to the proxy, using HttpClient's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
             * Instance-specific proxy fields, but note we're still fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
             * the same URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            String proxyHost = locUrl.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            int proxyPort = locUrl.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                security.checkConnect(proxyHost, proxyPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
            setProxiedClient (url, proxyHost, proxyPort);
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2740
            requests.set(0, method + " " + getRequestURI()+" "  +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                             httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            connected = true;
28545
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2743
            // need to remember this in case NTLM proxy authentication gets
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2744
            // used. We can't use transparent authentication when user
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2745
            // doesn't know about proxy.
1628787df68a 8048035: Ensure proper proxy protocols
michaelm
parents: 27177
diff changeset
  2746
            useProxyResponseCode = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        } else {
52169
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2748
            final URL prevURL = url;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2749
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            // maintain previous headers, just change the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            // of the file we're getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            url = locUrl;
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2753
            requestURI = null; // force it to be recalculated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            if (method.equals("POST") && !Boolean.getBoolean("http.strictPostRedirect") && (stat!=307)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                /* The HTTP/1.1 spec says that a redirect from a POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                 * *should not* be immediately turned into a GET, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                 * that some HTTP/1.0 clients incorrectly did this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                 * Correct behavior redirects a POST to another POST.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                 * Unfortunately, since most browsers have this incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                 * behavior, the web works this way now.  Typical usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                 * seems to be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                 *   POST a login code or passwd to a web page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                 *   after validation, the server redirects to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                 *     (welcome) page
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                 *   The second request is (erroneously) expected to be GET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                 * We will do the incorrect thing (POST-->GET) by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                 * We will provide the capability to do the "right" thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                 * (POST-->POST) by a system property, "http.strictPostRedirect=true"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                setRequests = false;
21952
6fdca3d54613 8029127: Redirected POST request throws IllegalStateException on HttpURLConnection.getInputStream
michaelm
parents: 21428
diff changeset
  2774
                super.setRequestMethod("GET"); // avoid the connecting check
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                    connect();
52169
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2778
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2779
                if (!sameDestination(prevURL, url)) {
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2780
                    // Ensures pre-redirect user-set cookie will not be reset.
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2781
                    // CookieHandler, if any, will be queried to determine
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2782
                    // cookies for redirected URL, if any.
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2783
                    userCookies = null;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2784
                    userCookies2 = null;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2785
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                if (!checkReuseConnection())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                    connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                /* Even after a connect() call, http variable still can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                 * null, if a ResponseCache has been installed and it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                 * a non-null CacheResponse instance. So check nullity before using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                 * And further, if http is null, there's no need to do anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                 * about request headers because successive http session will use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                 * cachedInputStream/cachedHeaders anyway, which is returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                 * CacheResponse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                if (http != null) {
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents: 3866
diff changeset
  2799
                    requests.set(0, method + " " + getRequestURI()+" "  +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                                 httpVersion, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                    int port = url.getPort();
24869
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  2802
                    String host = stripIPv6ZoneId(url.getHost());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                    if (port != -1 && port != url.getDefaultPort()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                        host += ":" + String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                    requests.set("Host", host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                }
52169
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2808
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2809
                if (!sameDestination(prevURL, url)) {
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2810
                    // Redirecting to a different destination will drop any
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2811
                    // security-sensitive headers, regardless of whether
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2812
                    // they are user-set or not. CookieHandler, if any, will be
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2813
                    // queried to determine cookies for redirected URL, if any.
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2814
                    userCookies = null;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2815
                    userCookies2 = null;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2816
                    requests.remove("Cookie");
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2817
                    requests.remove("Cookie2");
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2818
                    requests.remove("Authorization");
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2819
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2820
                    // check for preemptive authorization
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2821
                    AuthenticationInfo sauth =
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2822
                            AuthenticationInfo.getServerAuth(url, getAuthenticatorKey());
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2823
                    if (sauth != null && sauth.supportsPreemptiveAuthorization() ) {
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2824
                        // Sets "Authorization"
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2825
                        requests.setIfNotSet(sauth.getHeaderName(), sauth.getHeaderValue(url,method));
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2826
                        currentServerCredentials = sauth;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2827
                    }
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2828
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
52169
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2834
    /* Returns true iff the given URLs have the same host and effective port. */
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2835
    private static boolean sameDestination(URL firstURL, URL secondURL) {
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2836
        assert firstURL.getProtocol().equalsIgnoreCase(secondURL.getProtocol()):
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2837
               "protocols not equal: " + firstURL +  " - " + secondURL;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2838
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2839
        if (!firstURL.getHost().equalsIgnoreCase(secondURL.getHost()))
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2840
            return false;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2841
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2842
        int firstPort = firstURL.getPort();
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2843
        if (firstPort == -1)
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2844
            firstPort = firstURL.getDefaultPort();
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2845
        int secondPort = secondURL.getPort();
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2846
        if (secondPort == -1)
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2847
            secondPort = secondURL.getDefaultPort();
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2848
        if (firstPort != secondPort)
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2849
            return false;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2850
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2851
        return true;
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2852
    }
ca48ad1b6e21 8196902: Better HTTP Redirection
michaelm
parents: 47478
diff changeset
  2853
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
    /* dummy byte buffer for reading off socket prior to closing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
    byte[] cdata = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * Reset (without disconnecting the TCP conn) in order to do another transaction with this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
    private void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        http.reuse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        /* must save before calling close */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        reuseClient = http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        InputStream is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        if (!method.equals("HEAD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                /* we want to read the rest of the response without using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                 * hurry mechanism, because that would close the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                 * if everything is not available immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                if ((is instanceof ChunkedInputStream) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                    (is instanceof MeteredStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                    /* reading until eof will not block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                    while (is.read (cdata) > 0) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                    /* raw stream, which will block on read, so only read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                     * the expected number of bytes, probably 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                     */
3059
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2879
                    long cl = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2880
                    int n = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2881
                    String cls = responses.findValue ("Content-Length");
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2882
                    if (cls != null) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2883
                        try {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2884
                            cl = Long.parseLong (cls);
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2885
                        } catch (NumberFormatException e) {
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2886
                            cl = 0;
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2887
                        }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2888
                    }
2975ff687a6b 6811297: Add more logging to HTTP protocol handler
jccollet
parents: 2942
diff changeset
  2889
                    for (long i=0; i<cl; ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                        if ((n = is.read (cdata)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                            i+= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                http.reuse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                reuseClient = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                disconnectInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                if (is instanceof MeteredStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        responseCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        responses = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
    /**
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2915
     * Disconnect from the web server at the first 401 error. Do not
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2916
     * disconnect when using a proxy, a good proxy should have already
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2917
     * closed the connection to the web server.
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2918
     */
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2919
    private void disconnectWeb() throws IOException {
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
  2920
        if (usingProxy() && http.isKeepingAlive()) {
5161
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2921
            responseCode = -1;
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2922
            // clean up, particularly, skip the content part
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2923
            // of a 401 error response
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2924
            reset();
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2925
        } else {
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2926
            disconnectInternal();
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2927
        }
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2928
    }
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2929
bad56b1f38fb 6586707: NTLM authentication with proxy fails
weijun
parents: 4923
diff changeset
  2930
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     * Disconnect from the server (for internal use)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
    private void disconnectInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        responseCode = -1;
78
65353d0d0e31 6660405: HttpURLConnection returns the wrong InputStream
jccollet
parents: 74
diff changeset
  2935
        inputStream = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
            pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            http.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
     * Disconnect from the server (public API)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        responseCode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
             * If we have an input stream this means we received a response
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
             * from the server. That stream may have been read to EOF and
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47411
diff changeset
  2962
             * depending on the stream type may already be closed or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
             * the http client may be returned to the keep-alive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
             * If the http client has been returned to the keep-alive cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
             * it may be closed (idle timeout) or may be allocated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
             * another request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
             * In other to avoid timing issues we close the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
             * which will either close the underlying connection or return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
             * the client to the cache. If there's a possibility that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
             * client has been returned to the cache (ie: stream is a keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
             * alive stream or a chunked input stream) then we remove an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
             * idle connection to the server. Note that this approach
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
             * can be considered an approximation in that we may close a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
             * different idle connection to that used by the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
             * Additionally it's possible that we close two connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
             * - the first becuase it wasn't an EOF (and couldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
             * hurried) - the second, another idle connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
             * same server. The is okay because "disconnect" is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
             * indication that the application doesn't intend to access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
             * this http server for a while.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            if (inputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                HttpClient hc = http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                // un-synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                boolean ka = hc.isKeepingAlive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                    inputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
                // if the connection is persistent it may have been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                // or returned to the keep-alive cache. If it's been returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                // to the keep-alive cache then we would like to close it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                // but it may have been allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                if (ka) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                    hc.closeIdleConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                // We are deliberatly being disconnected so HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                // should not try to resend the request no matter what stage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                // of the connection we are in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                http.setDoNotRetry(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                http.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            //      poster = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        cachedInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
            cachedHeaders.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
    public boolean usingProxy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        if (http != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
            return (http.getProxyHostUsed() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3030
    // constant strings represent set-cookie header names
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
  3031
    private static final String SET_COOKIE = "set-cookie";
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31061
diff changeset
  3032
    private static final String SET_COOKIE2 = "set-cookie2";
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3033
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3034
    /**
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3035
     * Returns a filtered version of the given headers value.
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3036
     *
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3037
     * Note: The implementation currently only filters out HttpOnly cookies
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3038
     *       from Set-Cookie and Set-Cookie2 headers.
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3039
     */
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3040
    private String filterHeaderField(String name, String value) {
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3041
        if (value == null)
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3042
            return null;
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3043
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3044
        if (SET_COOKIE.equalsIgnoreCase(name) ||
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3045
            SET_COOKIE2.equalsIgnoreCase(name)) {
18577
f76979ce4bc4 8015799: HttpURLConnection.getHeaderFields() throws IllegalArgumentException
jzavgren
parents: 18541
diff changeset
  3046
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3047
            // Filtering only if there is a cookie handler. [Assumption: the
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3048
            // cookie handler will store/retrieve the HttpOnly cookies]
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52499
diff changeset
  3049
            if (cookieHandler == null || value.isEmpty())
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3050
                return value;
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3051
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
  3052
            JavaNetHttpCookieAccess access =
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
  3053
                    SharedSecrets.getJavaNetHttpCookieAccess();
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  3054
            StringJoiner retValue = new StringJoiner(",");  // RFC 2965, comma separated
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3055
            List<HttpCookie> cookies = access.parse(value);
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3056
            for (HttpCookie cookie : cookies) {
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3057
                // skip HttpOnly cookies
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  3058
                if (!cookie.isHttpOnly())
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  3059
                    retValue.add(access.header(cookie));
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3060
            }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  3061
            return retValue.toString();
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3062
        }
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3063
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3064
        return value;
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3065
    }
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3066
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3067
    // Cache the filtered response headers so that they don't need
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3068
    // to be generated for every getHeaderFields() call.
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3069
    private Map<String, List<String>> filteredHeaders;  // null
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3070
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3071
    private Map<String, List<String>> getFilteredHeaderFields() {
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3072
        if (filteredHeaders != null)
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3073
            return filteredHeaders;
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3074
11518
7e720dd82e9e 7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents: 11284
diff changeset
  3075
        Map<String, List<String>> headers, tmpMap = new HashMap<>();
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3076
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3077
        if (cachedHeaders != null)
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3078
            headers = cachedHeaders.getHeaders();
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3079
        else
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3080
            headers = responses.getHeaders();
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3081
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3082
        for (Map.Entry<String, List<String>> e: headers.entrySet()) {
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3083
            String key = e.getKey();
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3084
            List<String> values = e.getValue(), filteredVals = new ArrayList<>();
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3085
            for (String value : values) {
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3086
                String fVal = filterHeaderField(key, value);
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3087
                if (fVal != null)
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3088
                    filteredVals.add(fVal);
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3089
            }
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3090
            if (!filteredVals.isEmpty())
11518
7e720dd82e9e 7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents: 11284
diff changeset
  3091
                tmpMap.put(key, Collections.unmodifiableList(filteredVals));
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3092
        }
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3093
11518
7e720dd82e9e 7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents: 11284
diff changeset
  3094
        return filteredHeaders = Collections.unmodifiableMap(tmpMap);
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3095
    }
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3096
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
     * Gets a header field by name. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
     * @param name the name of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3101
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
    public String getHeaderField(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        if (cachedHeaders != null) {
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3108
            return filterHeaderField(name, cachedHeaders.findValue(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3111
        return filterHeaderField(name, responses.findValue(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     * Returns an unmodifiable Map of the header fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
     * The Map keys are Strings that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
     * response-header field names. Each Map value is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
     * unmodifiable List of Strings that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     * the corresponding field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * @return a Map of header fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3124
    @Override
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3125
    public Map<String, List<String>> getHeaderFields() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3130
        return getFilteredHeaderFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * Gets a header field by index. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     * @param n the index of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3137
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
    public String getHeaderField(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
        if (cachedHeaders != null) {
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3144
           return filterHeaderField(cachedHeaders.getKey(n),
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3145
                                    cachedHeaders.getValue(n));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        }
11284
2750cfd2352c 7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents: 10701
diff changeset
  3147
        return filterHeaderField(responses.getKey(n), responses.getValue(n));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
     * Gets a header field by index. Returns null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
     * @param n the index of the header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3154
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    public String getHeaderFieldKey(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
        if (cachedHeaders != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
            return cachedHeaders.getKey(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        return responses.getKey(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
     * Sets request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     * @param value the value to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3172
    @Override
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3173
    public synchronized void setRequestProperty(String key, String value) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3174
        if (connected || connecting)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3179
        if (isExternalMessageHeaderAllowed(key, value)) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3180
            requests.set(key, value);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3181
            if (!key.equalsIgnoreCase("Content-Type")) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3182
                userHeaders.set(key, value);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3183
            }
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3184
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3187
    MessageHeader getUserSetHeaders() {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3188
        return userHeaders;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3189
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * Adds a general request property specified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     * key-value pair.  This method will not overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * existing values associated with the same key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
     * @param   value  the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
     * @see #getRequestProperties(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3202
    @Override
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3203
    public synchronized void addRequestProperty(String key, String value) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3204
        if (connected || connecting)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3209
        if (isExternalMessageHeaderAllowed(key, value)) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3210
            requests.add(key, value);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3211
            if (!key.equalsIgnoreCase("Content-Type")) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3212
                    userHeaders.add(key, value);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 16839
diff changeset
  3213
            }
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3214
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
    // Set a property for authentication.  This can safely disregard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
    // the connected test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
    //
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 4047
diff changeset
  3221
    public void setAuthenticationProperty(String key, String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        checkMessageHeader(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        requests.set(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3226
    @Override
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3227
    public synchronized String getRequestProperty (String key) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3228
        if (key == null) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3229
            return null;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3230
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3231
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        // don't return headers containing security sensitive information
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3233
        for (int i=0; i < EXCLUDE_HEADERS.length; i++) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3234
            if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3235
                return null;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3236
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3237
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3238
        if (!setUserCookies) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3239
            if (key.equalsIgnoreCase("Cookie")) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3240
                return userCookies;
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3241
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3242
            if (key.equalsIgnoreCase("Cookie2")) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3243
                return userCookies2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        return requests.findValue(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * Returns an unmodifiable Map of general request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     * properties for this connection. The Map keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
     * are Strings that represent the request-header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     * field names. Each Map value is a unmodifiable List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * of Strings that represents the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * @return  a Map of the general request properties for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3261
    @Override
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3262
    public synchronized Map<String, List<String>> getRequestProperties() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        // exclude headers containing security-sensitive info
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3267
        if (setUserCookies) {
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3268
            return requests.getHeaders(EXCLUDE_HEADERS);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3269
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3270
        /*
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3271
         * The cookies in the requests message headers may have
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3272
         * been modified. Use the saved user cookies instead.
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3273
         */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  3274
        Map<String, List<String>> userCookiesMap = null;
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3275
        if (userCookies != null || userCookies2 != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  3276
            userCookiesMap = new HashMap<>();
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3277
            if (userCookies != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  3278
                userCookiesMap.put("Cookie", Arrays.asList(userCookies));
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3279
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3280
            if (userCookies2 != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10136
diff changeset
  3281
                userCookiesMap.put("Cookie2", Arrays.asList(userCookies2));
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3282
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3283
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 6873
diff changeset
  3284
        return requests.filterAndAddHeaders(EXCLUDE_HEADERS2, userCookiesMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3287
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     * Returns setting for connect timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     * 0 return implies that the option is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     * (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
     * @return an <code>int</code> that indicates the connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
     * @see java.net.URLConnection#setConnectTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
     * @see java.net.URLConnection#connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3307
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * Sets the read timeout to a specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     * milliseconds. A non-zero value specifies the timeout when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * reading from Input stream when a connection is established to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * resource. If the timeout expires before there is data available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * for read, a java.net.SocketTimeoutException is raised. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
     * <p> Some non-standard implementation of this method ignores the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
     * specified timeout. To see the read timeout set, please call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
     * getReadTimeout().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
     * @param timeout an <code>int</code> that specifies the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
     * value to be used in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     * @throws IllegalArgumentException if the timeout parameter is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
     * @see java.net.URLConnectiongetReadTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
     * @see java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3332
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     * Returns setting for read timeout. 0 return implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
     * @return an <code>int</code> that indicates the read timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
     * @see java.net.URLConnection#setReadTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     * @see java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
     */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3350
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
11528
638ca25aec87 7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents: 11518
diff changeset
  3355
    public CookieHandler getCookieHandler() {
638ca25aec87 7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents: 11518
diff changeset
  3356
        return cookieHandler;
638ca25aec87 7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents: 11518
diff changeset
  3357
    }
638ca25aec87 7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents: 11518
diff changeset
  3358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
    String getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3363
    private MessageHeader mapToMessageHeader(Map<String, List<String>> map) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        MessageHeader headers = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        if (map == null || map.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
            return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3368
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3369
            String key = entry.getKey();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3370
            List<String> values = entry.getValue();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  3371
            for (String value : values) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                    headers.prepend(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                    headers.add(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
24869
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3382
    /**
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3383
     * Returns the given host, without the IPv6 Zone Id, if present.
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3384
     * (e.g. [fe80::a00:27ff:aaaa:aaaa%eth0] -> [fe80::a00:27ff:aaaa:aaaa])
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3385
     *
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3386
     * @param host host address (not null, not empty)
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3387
     * @return host address without Zone Id
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3388
     */
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3389
    static String stripIPv6ZoneId(String host) {
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3390
        if (host.charAt(0) != '[') { // not an IPv6-literal
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3391
            return host;
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3392
        }
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3393
        int i = host.lastIndexOf('%');
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3394
        if (i == -1) { // doesn't contain zone_id
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3395
            return host;
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3396
        }
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3397
        return host.substring(0, i) + "]";
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3398
    }
6cdd5aa7e259 8027308: HttpURLConnection should better handle URLs with literal IPv6 addresses
prappo
parents: 22348
diff changeset
  3399
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
    /* The purpose of this wrapper is just to capture the close() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * so we can check authentication information that may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * arrived in a Trailer field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
    class HttpInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
        private CacheRequest cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
        private OutputStream outputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
        private boolean marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
        private int inCache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        private int markCount = 0;
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3410
        private boolean closed;  // false
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        public HttpInputStream (InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            super (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
            this.cacheRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
            this.outputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
        public HttpInputStream (InputStream is, CacheRequest cacheRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            super (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
            this.cacheRequest = cacheRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                this.outputStream = cacheRequest.getBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                this.cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                this.cacheRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                this.outputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
         * Marks the current position in this input stream. A subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
         * call to the <code>reset</code> method repositions this stream at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
         * the last marked position so that subsequent reads re-read the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
         * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
         * The <code>readlimit</code> argument tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
         * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
         * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
         * This method simply performs <code>in.mark(readlimit)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
         * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
         *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
         * @see     java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
         * @see     java.io.FilterInputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
         */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3447
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        public synchronized void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            super.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
            if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                markCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
         * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
         * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
         * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
         * simply performs <code>in.reset()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
         * Stream marks are intended to be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
         * situations where you need to read ahead a little to see what's in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
         * the stream. Often this is most easily done by invoking some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
         * general parser. If the stream is of the type handled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
         * parse, it just chugs along happily. If the stream is not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
         * that type, the parser should toss an exception when it fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
         * If this happens within readlimit bytes, it allows the outer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
         * code to reset the stream and try another parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
         * @exception  IOException  if the stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
         *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
         * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
         * @see        java.io.FilterInputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
         */
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3477
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
        public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
                inCache += markCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3486
        private void ensureOpen() throws IOException {
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3487
            if (closed)
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3488
                throw new IOException("stream is closed");
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3489
        }
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3490
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3491
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        public int read() throws IOException {
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3493
            ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                int ret = read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                return (ret == -1? ret : (b[0] & 0x00FF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3506
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3511
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        public int read(byte[] b, int off, int len) throws IOException {
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3513
            ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                int newLen = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
                int nWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
                // write to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
                if (inCache > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
                    if (inCache >= newLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                        inCache -= newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
                        nWrite = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                        nWrite = newLen - inCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                        inCache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                    nWrite = newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                if (nWrite > 0 && outputStream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                    outputStream.write(b, off + (newLen-nWrite), nWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                if (marked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                    markCount += newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                return newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3543
        /* skip() calls read() in order to ensure that entire response gets
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3544
         * cached. same implementation as InputStream.skip */
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3545
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3546
        private byte[] skipBuffer;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3547
        private static final int SKIP_BUFFER_SIZE = 8096;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3548
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3549
        @Override
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3550
        public long skip (long n) throws IOException {
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3551
            ensureOpen();
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3552
            long remaining = n;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3553
            int nr;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3554
            if (skipBuffer == null)
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3555
                skipBuffer = new byte[SKIP_BUFFER_SIZE];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3556
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3557
            byte[] localSkipBuffer = skipBuffer;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3558
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3559
            if (n <= 0) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3560
                return 0;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3561
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3562
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3563
            while (remaining > 0) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3564
                nr = read(localSkipBuffer, 0,
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3565
                          (int) Math.min(SKIP_BUFFER_SIZE, remaining));
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3566
                if (nr < 0) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3567
                    break;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3568
                }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3569
                remaining -= nr;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3570
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3571
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3572
            return n - remaining;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3573
        }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents: 6673
diff changeset
  3574
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3575
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
        public void close () throws IOException {
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3577
            if (closed)
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3578
                return;
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3579
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                if (outputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                    if (read() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                        cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
                        outputStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
                super.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
                if (cacheRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
                    cacheRequest.abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
                throw ioex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            } finally {
17952
a5f2fb0e7457 8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents: 17485
diff changeset
  3595
                closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
                HttpURLConnection.this.http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                checkResponseCredentials (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
    class StreamingOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3604
        long expected;
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3605
        long written;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
        boolean error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        IOException errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
         * expectedLength == -1 if the stream is chunked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
         * expectedLength > 0 if the stream is fixed content-length
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52427
diff changeset
  3613
         *    In the 2nd case, we make sure the expected number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
         *    of bytes are actually written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
         */
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3616
        StreamingOutputStream (OutputStream os, long expectedLength) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
            super (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
            expected = expectedLength;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3619
            written = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
            error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3624
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        public void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
            checkError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            written ++;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3628
            if (expected != -1L && written > expected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                throw new IOException ("too many bytes written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
            out.write (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3634
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
        public void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3639
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
        public void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
            checkError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
            written += len;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3643
            if (expected != -1L && written > expected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
                out.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
                throw new IOException ("too many bytes written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
            out.write (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
        void checkError () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                throw errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
            if (((PrintStream)out).checkError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                throw new IOException("Error writing request body to server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        /* this is called to check that all the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
         * that were supposed to be written were written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
         * and that the stream is now closed().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
        boolean writtenOK () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
            return closed && ! error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3670
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
            closed = true;
1576
b697b141012d 6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents: 715
diff changeset
  3676
            if (expected != -1L) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                /* not chunked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                if (written != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                    errorExcp = new IOException ("insufficient data written");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                    out.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                    throw errorExcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
                super.flush(); /* can't close the socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
                /* chunked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
                super.close (); /* force final chunk to be written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                /* trailing \r\n */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
                OutputStream o = http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
                o.write ('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
                o.write ('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
                o.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
    static class ErrorStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
        ByteBuffer buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
        InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        private ErrorStream(ByteBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        private ErrorStream(ByteBuffer buf, InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            this.is = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
        // when this method is called, it's either the case that cl > 0, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
        // if chunk-encoded, cl = -1; in other words, cl can't be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
        public static InputStream getErrorStream(InputStream is, long cl, HttpClient http) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            // cl can't be 0; this following is here for extra precaution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
            if (cl == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                // set SO_TIMEOUT to 1/5th of the total timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
                // 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
  3724
                int oldTimeout = http.getReadTimeout();
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  3725
                http.setReadTimeout(timeout4ESBuffer/5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
                long expected = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
                boolean isChunked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
                // the chunked case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
                if (cl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                    expected = bufSize4ES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
                    isChunked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
                    expected = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                if (expected <= bufSize4ES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                    int exp = (int) expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                    byte[] buffer = new byte[exp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                    int count = 0, time = 0, len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                            len = is.read(buffer, count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                                             buffer.length - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                            if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                                if (isChunked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                                    // chunked ended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                                    // if chunked ended prematurely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                                    // an IOException would be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                                // the server sends less than cl bytes of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                                throw new IOException("the server closes"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                                                      " before sending "+cl+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                                                      " bytes of data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                            count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                        } catch (SocketTimeoutException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                            time += timeout4ESBuffer/5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                    } while (count < exp && time < timeout4ESBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                    // reset SO_TIMEOUT to old value
2928
80b0b6c2d527 6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents: 1576
diff changeset
  3763
                    http.setReadTimeout(oldTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                    // if count < cl at this point, we will not try to reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
                    // the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
                    if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                        // since we haven't read anything,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
                        // we will return the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
                        // inputstream back to the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                    }  else if ((count == expected && !(isChunked)) || (isChunked && len <0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                        // put the connection into keep-alive cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
                        // the inputstream will try to do the right thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
                        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                        return new ErrorStream(ByteBuffer.wrap(buffer, 0, count));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                        // we read part of the response body
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                        return new ErrorStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
                                      ByteBuffer.wrap(buffer, 0, count), is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
            } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
                // ioex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3790
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
            if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                return buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                return buffer.remaining()+is.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
            int ret = read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
            return (ret == -1? ret : (b[0] & 0x00FF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3805
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
        public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
            return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3810
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
            int rem = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
            if (rem > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
                int ret = rem < len? rem : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
                buffer.get(b, off, ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
                    return is.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3826
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
            buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
/** An input stream that just returns EOF.  This is for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
 * HTTP URLConnections that are KeepAlive && use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
 * HEAD method - i.e., stream not dead, but nothing to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
class EmptyInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
  3843
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
    public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
    public int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
}