Skip to content

Commit 233cbfa

Browse files
Alexey Bakhtingnu-andrew
authored andcommitted
8345625: Better HTTP connections
Reviewed-by: mbalao, andrew
1 parent e00605f commit 233cbfa

File tree

9 files changed

+114
-43
lines changed

9 files changed

+114
-43
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2017, 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,7 +24,6 @@
2424
*/
2525
package sun.net.ftp.impl;
2626

27-
2827
import java.io.BufferedInputStream;
2928
import java.io.BufferedOutputStream;
3029
import java.io.BufferedReader;
@@ -62,13 +61,15 @@
6261
import java.util.regex.Pattern;
6362
import javax.net.ssl.SSLSocket;
6463
import javax.net.ssl.SSLSocketFactory;
64+
6565
import sun.net.ftp.FtpDirEntry;
6666
import sun.net.ftp.FtpDirParser;
6767
import sun.net.ftp.FtpProtocolException;
6868
import sun.net.ftp.FtpReplyCode;
6969
import sun.net.util.IPAddressUtil;
7070
import sun.util.logging.PlatformLogger;
7171

72+
import static sun.net.util.ProxyUtil.copyProxy;
7273

7374
public class FtpClient extends sun.net.ftp.FtpClient {
7475

@@ -996,7 +997,7 @@ public int getReadTimeout() {
996997
}
997998

998999
public sun.net.ftp.FtpClient setProxy(Proxy p) {
999-
proxy = p;
1000+
proxy = copyProxy(p);
10001001
return this;
10011002
}
10021003

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, 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
@@ -40,6 +40,8 @@
4040
import sun.net.www.protocol.http.AuthenticatorKeys;
4141
import sun.net.www.protocol.http.HttpURLConnection;
4242
import sun.util.logging.PlatformLogger;
43+
44+
import static sun.net.util.ProxyUtil.copyProxy;
4345
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
4446
import sun.security.action.GetPropertyAction;
4547

@@ -264,7 +266,7 @@ public HttpClient(URL url, String proxyHost, int proxyPort)
264266
}
265267

266268
protected HttpClient(URL url, Proxy p, int to) throws IOException {
267-
proxy = (p == null) ? Proxy.NO_PROXY : p;
269+
proxy = p == null ? Proxy.NO_PROXY : copyProxy(p);
268270
this.host = url.getHost();
269271
this.url = url;
270272
port = url.getPort();
@@ -329,9 +331,7 @@ public static HttpClient New(URL url, boolean useCache)
329331
public static HttpClient New(URL url, Proxy p, int to, boolean useCache,
330332
HttpURLConnection httpuc) throws IOException
331333
{
332-
if (p == null) {
333-
p = Proxy.NO_PROXY;
334-
}
334+
p = p == null ? Proxy.NO_PROXY : copyProxy(p);
335335
HttpClient ret = null;
336336
/* see if one's already around */
337337
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, 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
@@ -48,6 +48,7 @@
4848
import java.util.Iterator;
4949
import java.security.Permission;
5050
import java.util.Properties;
51+
5152
import sun.net.NetworkClient;
5253
import sun.net.util.IPAddressUtil;
5354
import sun.net.www.MessageHeader;
@@ -61,6 +62,7 @@
6162
import sun.net.www.ParseUtil;
6263
import sun.security.action.GetPropertyAction;
6364

65+
import static sun.net.util.ProxyUtil.copyProxy;
6466

6567
/**
6668
* This class Opens an FTP input (or output) stream given a URL.
@@ -244,7 +246,7 @@ public ProxySelector run() {
244246
URI uri = sun.net.www.ParseUtil.toURI(url);
245247
Iterator<Proxy> it = sel.select(uri).iterator();
246248
while (it.hasNext()) {
247-
p = it.next();
249+
p = copyProxy(it.next());
248250
if (p == null || p == Proxy.NO_PROXY ||
249251
p.type() == Proxy.Type.SOCKS) {
250252
break;

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2003, 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,10 +32,7 @@
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;
37-
import sun.net.ftp.FtpClient;
38-
import sun.net.www.protocol.http.HttpURLConnection;
35+
import static sun.net.util.ProxyUtil.copyProxy;
3936

4037
/** open an ftp connection given a URL */
4138
public class Handler extends java.net.URLStreamHandler {
@@ -56,8 +53,8 @@ protected java.net.URLConnection openConnection(URL u)
5653
return openConnection(u, null);
5754
}
5855

59-
protected java.net.URLConnection openConnection(URL u, Proxy p)
56+
protected java.net.URLConnection openConnection(URL u, Proxy proxy)
6057
throws IOException {
61-
return new FtpURLConnection(u, p);
58+
return new FtpURLConnection(u, copyProxy(proxy));
6259
}
6360
}

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

Lines changed: 5 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
@@ -81,6 +81,8 @@
8181
import java.nio.ByteBuffer;
8282
import java.util.Objects;
8383
import java.util.Properties;
84+
85+
import static sun.net.util.ProxyUtil.copyProxy;
8486
import static sun.net.www.protocol.http.AuthScheme.BASIC;
8587
import static sun.net.www.protocol.http.AuthScheme.DIGEST;
8688
import static sun.net.www.protocol.http.AuthScheme.NTLM;
@@ -898,7 +900,7 @@ protected HttpURLConnection(URL u, Proxy p, Handler handler)
898900
responses = new MessageHeader(maxHeaderSize);
899901
userHeaders = new MessageHeader();
900902
this.handler = handler;
901-
instProxy = p;
903+
instProxy = copyProxy(p);
902904
if (instProxy instanceof sun.net.ApplicationProxy) {
903905
/* Application set Proxies should not have access to cookies
904906
* in a secure environment unless explicitly allowed. */
@@ -1196,7 +1198,7 @@ public ProxySelector run() {
11961198
Iterator<Proxy> it = sel.select(uri).iterator();
11971199
Proxy p;
11981200
while (it.hasNext()) {
1199-
p = it.next();
1201+
p = copyProxy(it.next());
12001202
try {
12011203
if (!failedOnce) {
12021204
http = getNewHttpClient(url, p, connectTimeout);

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, 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
@@ -48,6 +48,7 @@
4848
import jdk.internal.net.http.websocket.WebSocketRequest;
4949

5050
import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
51+
import static jdk.internal.net.http.common.Utils.copyProxy;
5152

5253
public class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
5354

@@ -126,15 +127,7 @@ public HttpRequestImpl(HttpRequest request, ProxySelector ps) {
126127
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT);
127128
}
128129
this.uri = requestURI;
129-
if (isWebSocket) {
130-
// WebSocket determines and sets the proxy itself
131-
this.proxy = ((HttpRequestImpl) request).proxy;
132-
} else {
133-
if (ps != null)
134-
this.proxy = retrieveProxy(ps, uri);
135-
else
136-
this.proxy = null;
137-
}
130+
this.proxy = retrieveProxy(request, ps, uri);
138131
this.expectContinue = request.expectContinue();
139132
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
140133
this.requestPublisher = request.bodyPublisher().orElse(null);
@@ -292,16 +285,30 @@ void setH2Upgrade(Exchange<?> exchange) {
292285
@Override
293286
public boolean expectContinue() { return expectContinue; }
294287

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

307314
InetSocketAddress proxy() {
@@ -317,7 +324,7 @@ InetSocketAddress proxy() {
317324
@Override
318325
public void setProxy(Proxy proxy) {
319326
assert isWebSocket;
320-
this.proxy = proxy;
327+
this.proxy = copyProxy(proxy);
321328
}
322329

323330
@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, 2018, 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;
@@ -267,6 +268,17 @@ public static boolean proxyHasDisabledSchemes(boolean tunnel) {
267268
: ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty();
268269
}
269270

271+
/**
272+
* Creates a new {@link Proxy} instance for the given proxy iff it is
273+
* neither null, {@link Proxy#NO_PROXY Proxy.NO_PROXY}, nor already a
274+
* {@code Proxy} instance.
275+
*/
276+
public static Proxy copyProxy(Proxy proxy) {
277+
return proxy == null || proxy.getClass() == Proxy.class
278+
? proxy
279+
: new Proxy(proxy.type(), proxy.address());
280+
}
281+
270282
// WebSocket connection Upgrade headers
271283
private static final String HEADER_CONNECTION = "Connection";
272284
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, 2020, 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;
@@ -373,7 +374,7 @@ private static Proxy proxyFor(Optional<ProxySelector> selector, URI uri) {
373374
if (proxy.type() != Proxy.Type.HTTP) {
374375
return null;
375376
}
376-
return proxy;
377+
return copyProxy(proxy);
377378
}
378379

379380
/**

0 commit comments

Comments
 (0)