8345625: Better HTTP connections

Reviewed-by: mbalao, andrew
This commit is contained in:
Alexey Bakhtin 2025-06-04 13:13:47 -07:00 committed by Andrew John Hughes
parent 1e912a129c
commit 181b252c79
6 changed files with 71 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -60,6 +60,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import sun.net.ftp.FtpDirEntry;
@ -69,6 +70,7 @@ import sun.net.ftp.FtpReplyCode;
import sun.net.util.IPAddressUtil;
import sun.util.logging.PlatformLogger;
import static sun.net.util.ProxyUtil.copyProxy;
public class FtpClient extends sun.net.ftp.FtpClient {
@ -996,7 +998,7 @@ public class FtpClient extends sun.net.ftp.FtpClient {
}
public sun.net.ftp.FtpClient setProxy(Proxy p) {
proxy = p;
proxy = copyProxy(p);
return this;
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.util;
import sun.net.ApplicationProxy;
import java.net.Proxy;
public final class ProxyUtil {
private ProxyUtil() {}
/**
* Creates a new {@link Proxy} instance for the given proxy iff it is
* neither null, {@link Proxy#NO_PROXY Proxy.NO_PROXY}, an
* {@link ApplicationProxy} instance, nor already a {@code Proxy} instance.
*/
public static Proxy copyProxy(Proxy proxy) {
return proxy == null
|| proxy.getClass() == Proxy.class
|| proxy instanceof ApplicationProxy
? proxy
: new Proxy(proxy.type(), proxy.address());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,6 +37,8 @@ import sun.net.www.MeteredStream;
import sun.net.www.ParseUtil;
import sun.net.www.protocol.http.HttpURLConnection;
import sun.util.logging.PlatformLogger;
import static sun.net.util.ProxyUtil.copyProxy;
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
/**
@ -231,7 +233,7 @@ public class HttpClient extends NetworkClient {
}
protected HttpClient(URL url, Proxy p, int to) throws IOException {
proxy = (p == null) ? Proxy.NO_PROXY : p;
proxy = p == null ? Proxy.NO_PROXY : copyProxy(p);
this.host = url.getHost();
this.url = url;
port = url.getPort();
@ -296,9 +298,7 @@ public class HttpClient extends NetworkClient {
public static HttpClient New(URL url, Proxy p, int to, boolean useCache,
HttpURLConnection httpuc) throws IOException
{
if (p == null) {
p = Proxy.NO_PROXY;
}
p = p == null ? Proxy.NO_PROXY : copyProxy(p);
HttpClient ret = null;
/* see if one's already around */
if (useCache) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -47,6 +47,7 @@ import java.net.ProxySelector;
import java.util.StringTokenizer;
import java.util.Iterator;
import java.security.Permission;
import sun.net.NetworkClient;
import sun.net.util.IPAddressUtil;
import sun.net.www.MessageHeader;
@ -60,6 +61,7 @@ import sun.net.ProgressMonitor;
import sun.net.www.ParseUtil;
import sun.security.action.GetPropertyAction;
import static sun.net.util.ProxyUtil.copyProxy;
/**
* This class Opens an FTP input (or output) stream given a URL.
@ -242,7 +244,7 @@ public class FtpURLConnection extends URLConnection {
URI uri = sun.net.www.ParseUtil.toURI(url);
Iterator<Proxy> it = sel.select(uri).iterator();
while (it.hasNext()) {
p = it.next();
p = copyProxy(it.next());
if (p == null || p == Proxy.NO_PROXY ||
p.type() == Proxy.Type.SOCKS) {
break;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,10 +32,7 @@ package sun.net.www.protocol.ftp;
import java.io.IOException;
import java.net.URL;
import java.net.Proxy;
import java.util.Map;
import java.util.HashMap;
import sun.net.ftp.FtpClient;
import sun.net.www.protocol.http.HttpURLConnection;
import static sun.net.util.ProxyUtil.copyProxy;
/** open an ftp connection given a URL */
public class Handler extends java.net.URLStreamHandler {
@ -56,8 +53,8 @@ public class Handler extends java.net.URLStreamHandler {
return openConnection(u, null);
}
protected java.net.URLConnection openConnection(URL u, Proxy p)
protected java.net.URLConnection openConnection(URL u, Proxy proxy)
throws IOException {
return new FtpURLConnection(u, p);
return new FtpURLConnection(u, copyProxy(proxy));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -77,6 +77,8 @@ import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.net.MalformedURLException;
import java.nio.ByteBuffer;
import static sun.net.util.ProxyUtil.copyProxy;
import static sun.net.www.protocol.http.AuthScheme.BASIC;
import static sun.net.www.protocol.http.AuthScheme.DIGEST;
import static sun.net.www.protocol.http.AuthScheme.NTLM;
@ -880,7 +882,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
responses = new MessageHeader(maxHeaderSize);
userHeaders = new MessageHeader();
this.handler = handler;
instProxy = p;
instProxy = copyProxy(p);
if (instProxy instanceof sun.net.ApplicationProxy) {
/* Application set Proxies should not have access to cookies
* in a secure environment unless explicitly allowed. */
@ -1171,7 +1173,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
Iterator<Proxy> it = sel.select(uri).iterator();
Proxy p;
while (it.hasNext()) {
p = it.next();
p = copyProxy(it.next());
try {
if (!failedOnce) {
http = getNewHttpClient(url, p, connectTimeout);