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) {
sel.connectFailed(uri, p.address(), ioex);
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 {
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8161016
* @bug 8161016 8183369
* @library /test/lib
* @summary When proxy is set HttpURLConnection should not use DIRECT connection.
* @run main/othervm HttpURLConWithProxy
@ -37,10 +37,14 @@ import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
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 {
@ -50,10 +54,15 @@ public class HttpURLConWithProxy {
System.setProperty("http.proxyHost", "1.1.1.1");
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;
URL url;
URLConnection con;
HttpURLConnection con;
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress address = new InetSocketAddress(loopback, 0);
@ -66,16 +75,24 @@ public class HttpURLConWithProxy {
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection();
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(10 * 1000);
con.connect();
throw new RuntimeException("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down");
if(con.usingProxy()){
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) {
if(!ProxyHandler.proxyRetried) {
throw new RuntimeException("Connection not retried with proxy");
}
System.out.println("Test1 Passed with: " + ie.getMessage());
}
// Test2: using custom ProxySelector implementation
ProxyHandler.proxyRetried = false;
MyProxySelector myProxySel = new MyProxySelector();
ProxySelector.setDefault(myProxySel);
try {
@ -86,12 +103,19 @@ public class HttpURLConWithProxy {
.loopback()
.port(ss.getLocalPort())
.toURL();
con = url.openConnection();
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(10 * 1000);
con.connect();
throw new RuntimeException("Shouldn't use DIRECT connection "
+ "when proxy is invalid/down");
if(con.usingProxy()){
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) {
if(!ProxyHandler.proxyRetried) {
throw new RuntimeException("Connection not retried with proxy");
}
System.out.println("Test2 Passed with: " + ie.getMessage());
}
}
@ -119,3 +143,22 @@ class MyProxySelector extends ProxySelector {
// 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 {
}
}