Skip to content

Commit a8f51b2

Browse files
Alexey BakhtinRealCLanger
authored andcommitted
8345625: Better HTTP connections
Reviewed-by: mbalao, andrew Backport-of: 2ad3d59e4eb8882ee46a70c70cc75c9c0d008211
1 parent 42a0772 commit a8f51b2

File tree

9 files changed

+111
-44
lines changed

9 files changed

+111
-44
lines changed

src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,8 +24,6 @@
2424
*/
2525
package sun.net.ftp.impl;
2626

27-
28-
2927
import java.io.BufferedInputStream;
3028
import java.io.BufferedOutputStream;
3129
import java.io.BufferedReader;
@@ -64,13 +62,15 @@
6462
import java.util.regex.Pattern;
6563
import javax.net.ssl.SSLSocket;
6664
import javax.net.ssl.SSLSocketFactory;
65+
6766
import sun.net.ftp.FtpDirEntry;
6867
import sun.net.ftp.FtpDirParser;
6968
import sun.net.ftp.FtpProtocolException;
7069
import sun.net.ftp.FtpReplyCode;
7170
import sun.net.util.IPAddressUtil;
7271
import sun.util.logging.PlatformLogger;
7372

73+
import static sun.net.util.ProxyUtil.copyProxy;
7474

7575
public class FtpClient extends sun.net.ftp.FtpClient {
7676

@@ -982,7 +982,7 @@ public int getReadTimeout() {
982982
}
983983

984984
public sun.net.ftp.FtpClient setProxy(Proxy p) {
985-
proxy = p;
985+
proxy = copyProxy(p);
986986
return this;
987987
}
988988

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package sun.net.util;
27+
28+
import sun.net.ApplicationProxy;
29+
30+
import java.net.Proxy;
31+
32+
public final class ProxyUtil {
33+
34+
private ProxyUtil() {}
35+
36+
/**
37+
* Creates a new {@link Proxy} instance for the given proxy iff it is
38+
* neither null, {@link Proxy#NO_PROXY Proxy.NO_PROXY}, an
39+
* {@link ApplicationProxy} instance, nor already a {@code Proxy} instance.
40+
*/
41+
public static Proxy copyProxy(Proxy proxy) {
42+
return proxy == null
43+
|| proxy.getClass() == Proxy.class
44+
|| proxy instanceof ApplicationProxy
45+
? proxy
46+
: new Proxy(proxy.type(), proxy.address());
47+
}
48+
49+
}

src/java.base/share/classes/sun/net/www/http/HttpClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,8 @@
4242
import sun.net.www.protocol.http.AuthenticatorKeys;
4343
import sun.net.www.protocol.http.HttpURLConnection;
4444
import sun.util.logging.PlatformLogger;
45+
46+
import static sun.net.util.ProxyUtil.copyProxy;
4547
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
4648
import sun.security.action.GetPropertyAction;
4749

@@ -268,7 +270,7 @@ public HttpClient(URL url, String proxyHost, int proxyPort)
268270
}
269271

270272
protected HttpClient(URL url, Proxy p, int to) throws IOException {
271-
proxy = (p == null) ? Proxy.NO_PROXY : p;
273+
proxy = p == null ? Proxy.NO_PROXY : copyProxy(p);
272274
this.host = url.getHost();
273275
this.url = url;
274276
port = url.getPort();
@@ -333,9 +335,7 @@ public static HttpClient New(URL url, boolean useCache)
333335
public static HttpClient New(URL url, Proxy p, int to, boolean useCache,
334336
HttpURLConnection httpuc) throws IOException
335337
{
336-
if (p == null) {
337-
p = Proxy.NO_PROXY;
338-
}
338+
p = p == null ? Proxy.NO_PROXY : copyProxy(p);
339339
HttpClient ret = null;
340340
/* see if one's already around */
341341
if (useCache) {

src/java.base/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,6 +49,7 @@
4949
import java.util.Iterator;
5050
import java.security.Permission;
5151
import java.util.Properties;
52+
5253
import sun.net.NetworkClient;
5354
import sun.net.util.IPAddressUtil;
5455
import sun.net.www.MessageHeader;
@@ -62,6 +63,7 @@
6263
import sun.net.www.ParseUtil;
6364
import sun.security.action.GetPropertyAction;
6465

66+
import static sun.net.util.ProxyUtil.copyProxy;
6567

6668
/**
6769
* This class Opens an FTP input (or output) stream given a URL.
@@ -252,7 +254,7 @@ public ProxySelector run() {
252254
}
253255
final Iterator<Proxy> it = proxies.iterator();
254256
while (it.hasNext()) {
255-
p = it.next();
257+
p = copyProxy(it.next());
256258
if (p == null || p == Proxy.NO_PROXY ||
257259
p.type() == Proxy.Type.SOCKS) {
258260
break;

src/java.base/share/classes/sun/net/www/protocol/ftp/Handler.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,11 +32,9 @@
3232
import java.io.IOException;
3333
import java.net.URL;
3434
import java.net.Proxy;
35-
import java.util.Map;
36-
import java.util.HashMap;
3735
import java.util.Objects;
38-
import sun.net.ftp.FtpClient;
39-
import sun.net.www.protocol.http.HttpURLConnection;
36+
37+
import static sun.net.util.ProxyUtil.copyProxy;
4038

4139
/** open an ftp connection given a URL */
4240
public class Handler extends java.net.URLStreamHandler {
@@ -56,8 +54,8 @@ protected java.net.URLConnection openConnection(URL u)
5654
return openConnection(u, null);
5755
}
5856

59-
protected java.net.URLConnection openConnection(URL u, Proxy p)
57+
protected java.net.URLConnection openConnection(URL u, Proxy proxy)
6058
throws IOException {
61-
return new FtpURLConnection(u, p);
59+
return new FtpURLConnection(u, copyProxy(proxy));
6260
}
6361
}

src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -83,6 +83,7 @@
8383
import java.util.Properties;
8484
import java.util.concurrent.locks.ReentrantLock;
8585

86+
import static sun.net.util.ProxyUtil.copyProxy;
8687
import static sun.net.www.protocol.http.AuthScheme.BASIC;
8788
import static sun.net.www.protocol.http.AuthScheme.DIGEST;
8889
import static sun.net.www.protocol.http.AuthScheme.NTLM;
@@ -936,7 +937,7 @@ protected HttpURLConnection(URL u, Proxy p, Handler handler)
936937
responses = new MessageHeader(maxHeaderSize);
937938
userHeaders = new MessageHeader();
938939
this.handler = handler;
939-
instProxy = p;
940+
instProxy = copyProxy(p);
940941
if (instProxy instanceof sun.net.ApplicationProxy) {
941942
/* Application set Proxies should not have access to cookies
942943
* in a secure environment unless explicitly allowed. */
@@ -1251,7 +1252,7 @@ public ProxySelector run() {
12511252
final Iterator<Proxy> it = proxies.iterator();
12521253
Proxy p;
12531254
while (it.hasNext()) {
1254-
p = it.next();
1255+
p = copyProxy(it.next());
12551256
try {
12561257
if (!failedOnce) {
12571258
http = getNewHttpClient(url, p, connectTimeout);

src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,6 +49,7 @@
4949

5050
import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
5151
import static jdk.internal.net.http.common.Utils.ProxyHeaders;
52+
import static jdk.internal.net.http.common.Utils.copyProxy;
5253

5354
public class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
5455

@@ -129,15 +130,7 @@ public HttpRequestImpl(HttpRequest request, ProxySelector ps) {
129130
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
130131
}
131132
this.uri = requestURI;
132-
if (isWebSocket) {
133-
// WebSocket determines and sets the proxy itself
134-
this.proxy = ((HttpRequestImpl) request).proxy;
135-
} else {
136-
if (ps != null)
137-
this.proxy = retrieveProxy(ps, uri);
138-
else
139-
this.proxy = null;
140-
}
133+
this.proxy = retrieveProxy(request, ps, uri);
141134
this.expectContinue = request.expectContinue();
142135
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
143136
this.requestPublisher = request.bodyPublisher().orElse(null);
@@ -296,16 +289,27 @@ void setH2Upgrade(Exchange<?> exchange) {
296289
@Override
297290
public boolean expectContinue() { return expectContinue; }
298291

299-
/** Retrieves the proxy, from the given ProxySelector, if there is one. */
300-
private static Proxy retrieveProxy(ProxySelector ps, URI uri) {
301-
Proxy proxy = null;
302-
List<Proxy> pl = ps.select(uri);
303-
if (!pl.isEmpty()) {
304-
Proxy p = pl.get(0);
305-
if (p.type() == Proxy.Type.HTTP)
306-
proxy = p;
292+
/** Retrieves a copy of the proxy either from the given {@link HttpRequest} or {@link ProxySelector}, if there is one. */
293+
private static Proxy retrieveProxy(HttpRequest request, ProxySelector ps, URI uri) {
294+
295+
// WebSocket determines and sets the proxy itself
296+
if (request instanceof HttpRequestImpl requestImpl && requestImpl.isWebSocket) {
297+
return requestImpl.proxy;
298+
}
299+
300+
// Try to find a matching one from the `ProxySelector`
301+
if (ps != null) {
302+
List<Proxy> pl = ps.select(uri);
303+
if (!pl.isEmpty()) {
304+
Proxy p = pl.get(0);
305+
if (p.type() == Proxy.Type.HTTP) {
306+
return copyProxy(p);
307+
}
308+
}
307309
}
308-
return proxy;
310+
311+
return null;
312+
309313
}
310314

311315
InetSocketAddress proxy() {
@@ -321,7 +325,7 @@ InetSocketAddress proxy() {
321325
@Override
322326
public void setProxy(Proxy proxy) {
323327
assert isWebSocket;
324-
this.proxy = proxy;
328+
this.proxy = copyProxy(proxy);
325329
}
326330

327331
@Override

src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,7 @@
4242
import java.lang.System.Logger.Level;
4343
import java.net.ConnectException;
4444
import java.net.InetSocketAddress;
45+
import java.net.Proxy;
4546
import java.net.URI;
4647
import java.net.URLPermission;
4748
import java.net.http.HttpClient;
@@ -273,6 +274,17 @@ public static boolean proxyHasDisabledSchemes(boolean tunnel) {
273274
: ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty();
274275
}
275276

277+
/**
278+
* Creates a new {@link Proxy} instance for the given proxy iff it is
279+
* neither null, {@link Proxy#NO_PROXY Proxy.NO_PROXY}, nor already a
280+
* {@code Proxy} instance.
281+
*/
282+
public static Proxy copyProxy(Proxy proxy) {
283+
return proxy == null || proxy.getClass() == Proxy.class
284+
? proxy
285+
: new Proxy(proxy.type(), proxy.address());
286+
}
287+
276288
// WebSocket connection Upgrade headers
277289
private static final String HEADER_CONNECTION = "Connection";
278290
private static final String HEADER_UPGRADE = "Upgrade";

src/java.net.http/share/classes/jdk/internal/net/http/websocket/OpeningHandshake.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -66,6 +66,7 @@
6666
import java.util.stream.Stream;
6767

6868
import static java.lang.String.format;
69+
import static jdk.internal.net.http.common.Utils.copyProxy;
6970
import static jdk.internal.net.http.common.Utils.isValidName;
7071
import static jdk.internal.net.http.common.Utils.permissionForProxy;
7172
import static jdk.internal.net.http.common.Utils.stringOf;
@@ -375,7 +376,7 @@ private static Proxy proxyFor(Optional<ProxySelector> selector, URI uri) {
375376
if (proxy.type() != Proxy.Type.HTTP) {
376377
return null;
377378
}
378-
return proxy;
379+
return copyProxy(proxy);
379380
}
380381

381382
/**

0 commit comments

Comments
 (0)