src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 53018 8bf9268df0e2
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    66 import java.util.Set;
    66 import java.util.Set;
    67 import java.util.StringJoiner;
    67 import java.util.StringJoiner;
    68 import jdk.internal.access.JavaNetHttpCookieAccess;
    68 import jdk.internal.access.JavaNetHttpCookieAccess;
    69 import jdk.internal.access.SharedSecrets;
    69 import jdk.internal.access.SharedSecrets;
    70 import sun.net.*;
    70 import sun.net.*;
       
    71 import sun.net.util.IPAddressUtil;
    71 import sun.net.www.*;
    72 import sun.net.www.*;
    72 import sun.net.www.http.HttpClient;
    73 import sun.net.www.http.HttpClient;
    73 import sun.net.www.http.PosterOutputStream;
    74 import sun.net.www.http.PosterOutputStream;
    74 import sun.net.www.http.ChunkedInputStream;
    75 import sun.net.www.http.ChunkedInputStream;
    75 import sun.net.www.http.ChunkedOutputStream;
    76 import sun.net.www.http.ChunkedOutputStream;
   866         if (u != null) {
   867         if (u != null) {
   867             if (u.toExternalForm().indexOf('\n') > -1) {
   868             if (u.toExternalForm().indexOf('\n') > -1) {
   868                 throw new MalformedURLException("Illegal character in URL");
   869                 throw new MalformedURLException("Illegal character in URL");
   869             }
   870             }
   870         }
   871         }
       
   872         String s = IPAddressUtil.checkAuthority(u);
       
   873         if (s != null) {
       
   874             throw new MalformedURLException(s);
       
   875         }
   871         return u;
   876         return u;
   872     }
   877     }
       
   878 
   873     protected HttpURLConnection(URL u, Proxy p, Handler handler)
   879     protected HttpURLConnection(URL u, Proxy p, Handler handler)
   874             throws IOException {
   880             throws IOException {
   875         super(checkURL(u));
   881         super(checkURL(u));
   876         requests = new MessageHeader();
   882         requests = new MessageHeader();
   877         responses = new MessageHeader();
   883         responses = new MessageHeader();
  1170                 if (sel != null) {
  1176                 if (sel != null) {
  1171                     URI uri = sun.net.www.ParseUtil.toURI(url);
  1177                     URI uri = sun.net.www.ParseUtil.toURI(url);
  1172                     if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
  1178                     if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
  1173                         logger.finest("ProxySelector Request for " + uri);
  1179                         logger.finest("ProxySelector Request for " + uri);
  1174                     }
  1180                     }
  1175                     Iterator<Proxy> it = sel.select(uri).iterator();
  1181                     final List<Proxy> proxies;
       
  1182                     try {
       
  1183                         proxies = sel.select(uri);
       
  1184                     } catch (IllegalArgumentException iae) {
       
  1185                         throw new IOException("Failed to select a proxy", iae);
       
  1186                     }
       
  1187                     final Iterator<Proxy> it = proxies.iterator();
  1176                     Proxy p;
  1188                     Proxy p;
  1177                     while (it.hasNext()) {
  1189                     while (it.hasNext()) {
  1178                         p = it.next();
  1190                         p = it.next();
  1179                         try {
  1191                         try {
  1180                             if (!failedOnce) {
  1192                             if (!failedOnce) {
  2157                 setTunnelState(TunnelState.NONE);
  2169                 setTunnelState(TunnelState.NONE);
  2158                 break;
  2170                 break;
  2159             } while (retryTunnel < maxRedirects);
  2171             } while (retryTunnel < maxRedirects);
  2160 
  2172 
  2161             if (retryTunnel >= maxRedirects || (respCode != HTTP_OK)) {
  2173             if (retryTunnel >= maxRedirects || (respCode != HTTP_OK)) {
       
  2174                 if (respCode != HTTP_PROXY_AUTH) {
       
  2175                     // remove all but authenticate responses
       
  2176                     responses.reset();
       
  2177                 }
  2162                 throw new IOException("Unable to tunnel through proxy."+
  2178                 throw new IOException("Unable to tunnel through proxy."+
  2163                                       " Proxy returns \"" +
  2179                                       " Proxy returns \"" +
  2164                                       statusLine + "\"");
  2180                                       statusLine + "\"");
  2165             }
  2181             }
  2166         } finally  {
  2182         } finally  {
  2257         String host = http.getProxyHostUsed();
  2273         String host = http.getProxyHostUsed();
  2258         int port = http.getProxyPortUsed();
  2274         int port = http.getProxyPortUsed();
  2259         if (host != null && authhdr.isPresent()) {
  2275         if (host != null && authhdr.isPresent()) {
  2260             HeaderParser p = authhdr.headerParser();
  2276             HeaderParser p = authhdr.headerParser();
  2261             String realm = p.findValue("realm");
  2277             String realm = p.findValue("realm");
       
  2278             String charset = p.findValue("charset");
       
  2279             boolean isUTF8 = (charset != null && charset.equalsIgnoreCase("UTF-8"));
  2262             String scheme = authhdr.scheme();
  2280             String scheme = authhdr.scheme();
  2263             AuthScheme authScheme = UNKNOWN;
  2281             AuthScheme authScheme = UNKNOWN;
  2264             if ("basic".equalsIgnoreCase(scheme)) {
  2282             if ("basic".equalsIgnoreCase(scheme)) {
  2265                 authScheme = BASIC;
  2283                 authScheme = BASIC;
  2266             } else if ("digest".equalsIgnoreCase(scheme)) {
  2284             } else if ("digest".equalsIgnoreCase(scheme)) {
  2302                                     authenticator,
  2320                                     authenticator,
  2303                                     host, addr, port, "http",
  2321                                     host, addr, port, "http",
  2304                                     realm, scheme, url, RequestorType.PROXY);
  2322                                     realm, scheme, url, RequestorType.PROXY);
  2305                     if (a != null) {
  2323                     if (a != null) {
  2306                         ret = new BasicAuthentication(true, host, port, realm, a,
  2324                         ret = new BasicAuthentication(true, host, port, realm, a,
  2307                                              getAuthenticatorKey());
  2325                                              isUTF8, getAuthenticatorKey());
  2308                     }
  2326                     }
  2309                     break;
  2327                     break;
  2310                 case DIGEST:
  2328                 case DIGEST:
  2311                     a = privilegedRequestPasswordAuthentication(
  2329                     a = privilegedRequestPasswordAuthentication(
  2312                                     authenticator,
  2330                                     authenticator,
  2420         /* When we get an NTLM auth from cache, don't set any special headers */
  2438         /* When we get an NTLM auth from cache, don't set any special headers */
  2421         if (authhdr.isPresent()) {
  2439         if (authhdr.isPresent()) {
  2422             HeaderParser p = authhdr.headerParser();
  2440             HeaderParser p = authhdr.headerParser();
  2423             String realm = p.findValue("realm");
  2441             String realm = p.findValue("realm");
  2424             String scheme = authhdr.scheme();
  2442             String scheme = authhdr.scheme();
       
  2443             String charset = p.findValue("charset");
       
  2444             boolean isUTF8 = (charset != null && charset.equalsIgnoreCase("UTF-8"));
  2425             AuthScheme authScheme = UNKNOWN;
  2445             AuthScheme authScheme = UNKNOWN;
  2426             if ("basic".equalsIgnoreCase(scheme)) {
  2446             if ("basic".equalsIgnoreCase(scheme)) {
  2427                 authScheme = BASIC;
  2447                 authScheme = BASIC;
  2428             } else if ("digest".equalsIgnoreCase(scheme)) {
  2448             } else if ("digest".equalsIgnoreCase(scheme)) {
  2429                 authScheme = DIGEST;
  2449                 authScheme = DIGEST;
  2471                             authenticator,
  2491                             authenticator,
  2472                             url.getHost(), addr, port, url.getProtocol(),
  2492                             url.getHost(), addr, port, url.getProtocol(),
  2473                             realm, scheme, url, RequestorType.SERVER);
  2493                             realm, scheme, url, RequestorType.SERVER);
  2474                     if (a != null) {
  2494                     if (a != null) {
  2475                         ret = new BasicAuthentication(false, url, realm, a,
  2495                         ret = new BasicAuthentication(false, url, realm, a,
  2476                                     getAuthenticatorKey());
  2496                                     isUTF8, getAuthenticatorKey());
  2477                     }
  2497                     }
  2478                     break;
  2498                     break;
  2479                 case DIGEST:
  2499                 case DIGEST:
  2480                     a = privilegedRequestPasswordAuthentication(
  2500                     a = privilegedRequestPasswordAuthentication(
  2481                             authenticator,
  2501                             authenticator,