Skip to content
Permalink
Browse files
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
  • Loading branch information
raviniitw2012 authored and David Buck committed Mar 5, 2020
1 parent d181894 commit b2f1f73e7524acc0e38f454255052a991ee76da2
Showing with 59 additions and 11 deletions.
  1. +6 −1 src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java
  2. +53 −10 test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java
@@ -1209,7 +1209,12 @@ public ProxySelector run() {
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;
@@ -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.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 static void main(String... arg) throws Exception {

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 static void main(String... arg) throws Exception {
.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 static void main(String... arg) throws Exception {
.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 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
// 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 {
}
}

0 comments on commit b2f1f73

Please sign in to comment.