8183369: RFC unconformity of HttpURLConnection with proxy

HttpURLConnection retried with proxy if the connection fails on first attempt as per RFC

Reviewed-by: chegar, dfuchs, vtewari
This commit is contained in:
Ravi Reddy 2020-03-05 03:27:17 -05:00 committed by David Buck
parent d1818942b0
commit b2f1f73e75
2 changed files with 59 additions and 11 deletions

View File

@ -1209,7 +1209,12 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
if (p != Proxy.NO_PROXY) { if (p != Proxy.NO_PROXY) {
sel.connectFailed(uri, p.address(), ioex); sel.connectFailed(uri, p.address(), ioex);
if (!it.hasNext()) { if (!it.hasNext()) {
throw ioex; if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Retrying with proxy: " + p.toString());
}
http = getNewHttpClient(url, p, connectTimeout, false);
http.setReadTimeout(readTimeout);
break;
} }
} else { } else {
throw ioex; throw ioex;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8161016 * @bug 8161016 8183369
* @library /test/lib * @library /test/lib
* @summary When proxy is set HttpURLConnection should not use DIRECT connection. * @summary When proxy is set HttpURLConnection should not use DIRECT connection.
* @run main/othervm HttpURLConWithProxy * @run main/othervm HttpURLConWithProxy
@ -37,10 +37,14 @@ import java.net.ServerSocket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jdk.test.lib.net.URIBuilder; import jdk.test.lib.net.URIBuilder;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogRecord;
public class HttpURLConWithProxy { public class HttpURLConWithProxy {
@ -50,10 +54,15 @@ public class HttpURLConWithProxy {
System.setProperty("http.proxyHost", "1.1.1.1"); System.setProperty("http.proxyHost", "1.1.1.1");
System.setProperty("http.proxyPort", "1111"); System.setProperty("http.proxyPort", "1111");
String HTTPLOG = "sun.net.www.protocol.http.HttpURLConnection";
Logger.getLogger(HTTPLOG).setLevel(Level.ALL);
Handler h = new ProxyHandler();
h.setLevel(Level.ALL);
Logger.getLogger(HTTPLOG).addHandler(h);
ServerSocket ss; ServerSocket ss;
URL url; URL url;
URLConnection con; HttpURLConnection con;
InetAddress loopback = InetAddress.getLoopbackAddress(); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0); InetSocketAddress address = new InetSocketAddress(loopback, 0);
@ -66,16 +75,24 @@ public class HttpURLConWithProxy {
.loopback() .loopback()
.port(ss.getLocalPort()) .port(ss.getLocalPort())
.toURL(); .toURL();
con = url.openConnection(); con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(10 * 1000); con.setConnectTimeout(10 * 1000);
con.connect(); con.connect();
throw new RuntimeException("Shouldn't use DIRECT connection " if(con.usingProxy()){
+ "when proxy is invalid/down"); System.out.println("Test1 Passed with: Connection succeeded with proxy");
} else {
throw new RuntimeException("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down");
}
} catch (IOException ie) { } catch (IOException ie) {
if(!ProxyHandler.proxyRetried) {
throw new RuntimeException("Connection not retried with proxy");
}
System.out.println("Test1 Passed with: " + ie.getMessage()); System.out.println("Test1 Passed with: " + ie.getMessage());
} }
// Test2: using custom ProxySelector implementation // Test2: using custom ProxySelector implementation
ProxyHandler.proxyRetried = false;
MyProxySelector myProxySel = new MyProxySelector(); MyProxySelector myProxySel = new MyProxySelector();
ProxySelector.setDefault(myProxySel); ProxySelector.setDefault(myProxySel);
try { try {
@ -86,12 +103,19 @@ public class HttpURLConWithProxy {
.loopback() .loopback()
.port(ss.getLocalPort()) .port(ss.getLocalPort())
.toURL(); .toURL();
con = url.openConnection(); con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(10 * 1000); con.setConnectTimeout(10 * 1000);
con.connect(); con.connect();
throw new RuntimeException("Shouldn't use DIRECT connection " if(con.usingProxy()){
+ "when proxy is invalid/down"); System.out.println("Test2 Passed with: Connection succeeded with proxy");
} else {
throw new RuntimeException("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down");
}
} catch (IOException ie) { } catch (IOException ie) {
if(!ProxyHandler.proxyRetried) {
throw new RuntimeException("Connection not retried with proxy");
}
System.out.println("Test2 Passed with: " + ie.getMessage()); System.out.println("Test2 Passed with: " + ie.getMessage());
} }
} }
@ -119,3 +143,22 @@ class MyProxySelector extends ProxySelector {
// System.out.println("MyProxySelector.connectFailed(): "+sa); // System.out.println("MyProxySelector.connectFailed(): "+sa);
} }
} }
class ProxyHandler extends Handler {
public static boolean proxyRetried = false;
@Override
public void publish(LogRecord record) {
if (record.getMessage().contains("Retrying with proxy")) {
proxyRetried = true;
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}