8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
Reviewed-by: michaelm, chegar, dfuchs
Contributed-by: jai.forums2013@gmail.com
--- a/src/java.base/share/classes/java/net/ProxySelector.java Mon Aug 26 11:46:54 2019 +0100
+++ b/src/java.base/share/classes/java/net/ProxySelector.java Mon Aug 26 12:25:49 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2019, 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
@@ -143,7 +143,9 @@
* contain one element of type
* {@link java.net.Proxy Proxy}
* that represents a direct connection.
- * @throws IllegalArgumentException if the argument is null
+ * @throws IllegalArgumentException if the argument is null or if
+ * the protocol or host cannot be determined from the provided
+ * {@code uri}
*/
public abstract List<Proxy> select(URI uri);
--- a/src/java.base/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java Mon Aug 26 11:46:54 2019 +0100
+++ b/src/java.base/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java Mon Aug 26 12:25:49 2019 +0100
@@ -44,6 +44,7 @@
import java.net.URI;
import java.net.Proxy;
import java.net.ProxySelector;
+import java.util.List;
import java.util.StringTokenizer;
import java.util.Iterator;
import java.security.Permission;
@@ -242,7 +243,13 @@
});
if (sel != null) {
URI uri = sun.net.www.ParseUtil.toURI(url);
- Iterator<Proxy> it = sel.select(uri).iterator();
+ final List<Proxy> proxies;
+ try {
+ proxies = sel.select(uri);
+ } catch (IllegalArgumentException iae) {
+ throw new IOException("Failed to select a proxy", iae);
+ }
+ final Iterator<Proxy> it = proxies.iterator();
while (it.hasNext()) {
p = it.next();
if (p == null || p == Proxy.NO_PROXY ||
--- a/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java Mon Aug 26 11:46:54 2019 +0100
+++ b/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java Mon Aug 26 12:25:49 2019 +0100
@@ -1178,7 +1178,13 @@
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("ProxySelector Request for " + uri);
}
- Iterator<Proxy> it = sel.select(uri).iterator();
+ final List<Proxy> proxies;
+ try {
+ proxies = sel.select(uri);
+ } catch (IllegalArgumentException iae) {
+ throw new IOException("Failed to select a proxy", iae);
+ }
+ final Iterator<Proxy> it = proxies.iterator();
Proxy p;
while (it.hasNext()) {
p = it.next();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/HttpURLConnection/HttpURLProxySelectionTest.java Mon Aug 26 12:25:49 2019 +0100
@@ -0,0 +1,156 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
+import org.testng.Assert;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import sun.net.spi.DefaultProxySelector;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+
+/**
+ * @test
+ * @bug 6563286 6797318 8177648
+ * @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with
+ * sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException
+ * correctly
+ * @run testng HttpURLProxySelectionTest
+ * @modules java.base/sun.net.spi:+open
+ */
+public class HttpURLProxySelectionTest {
+
+ private static final String WEB_APP_CONTEXT = "/httpurlproxytest";
+
+ private HttpServer server;
+ private SimpleHandler handler;
+ private ProxySelector previousDefault;
+ private CustomProxySelector ourProxySelector = new CustomProxySelector();
+
+ @BeforeTest
+ public void beforeTest() throws Exception {
+ previousDefault = ProxySelector.getDefault();
+ ProxySelector.setDefault(ourProxySelector);
+ handler = new SimpleHandler();
+ server = createServer(handler);
+ }
+
+ @AfterTest
+ public void afterTest() {
+ try {
+ if (server != null) {
+ final int delaySeconds = 0;
+ server.stop(delaySeconds);
+ }
+ } finally {
+ ProxySelector.setDefault(previousDefault);
+ }
+ }
+
+ /**
+ * - Test initiates a HTTP request to server
+ * - Server receives request and sends a 301 redirect to an URI which doesn't have a "host"
+ * - Redirect is expected to fail with IOException (caused by IllegalArgumentException from DefaultProxySelector)
+ *
+ * @throws Exception
+ */
+ @Test
+ public void test() throws Exception {
+ final String targetURL = "http://" + server.getAddress().getHostName() + ":"
+ + server.getAddress().getPort() + WEB_APP_CONTEXT;
+ System.out.println("Sending request to " + targetURL);
+ final HttpURLConnection conn = (HttpURLConnection) new URL(targetURL).openConnection();
+ try {
+ conn.getResponseCode();
+ Assert.fail("Request to " + targetURL + " was expected to fail during redirect");
+ } catch (IOException ioe) {
+ // expected because of the redirect to an invalid URL, for which a proxy can't be selected
+
+ // make sure the it was indeed a redirect
+ Assert.assertTrue(handler.redirectSent, "Server was expected to send a redirect, but didn't");
+ Assert.assertTrue(ourProxySelector.selectorUsedForRedirect, "Proxy selector wasn't used for redirect");
+
+ // make sure the IOException was caused by an IllegalArgumentException
+ Assert.assertTrue(ioe.getCause() instanceof IllegalArgumentException, "Unexpected cause in the IOException");
+ }
+ }
+
+
+ private static HttpServer createServer(final HttpHandler handler) throws IOException {
+ final InetSocketAddress serverAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
+ final int backlog = -1;
+ final HttpServer server = HttpServer.create(serverAddr, backlog);
+ // setup the handler
+ server.createContext(WEB_APP_CONTEXT, handler);
+ // start the server
+ server.start();
+ System.out.println("Server started on " + server.getAddress());
+ return server;
+ }
+
+ private static class SimpleHandler implements HttpHandler {
+ private boolean redirectSent = false;
+
+ @Override
+ public void handle(final HttpExchange httpExchange) throws IOException {
+ final String redirectURL;
+ try {
+ redirectURL = new URI("http", "/irrelevant", null).toString();
+ } catch (URISyntaxException e) {
+ throw new IOException(e);
+ }
+ httpExchange.getResponseHeaders().add("Location", redirectURL);
+ final URI requestURI = httpExchange.getRequestURI();
+ System.out.println("Handling " + httpExchange.getRequestMethod() + " request "
+ + requestURI + " responding with redirect to " + redirectURL);
+ httpExchange.sendResponseHeaders(301, -1);
+ this.redirectSent = true;
+ }
+
+ }
+
+ private static class CustomProxySelector extends DefaultProxySelector {
+
+ private boolean selectorUsedForRedirect = false;
+
+ @Override
+ public List<Proxy> select(final URI uri) {
+ if (uri.toString().contains("/irrelevant")) {
+ this.selectorUsedForRedirect = true;
+ }
+ return super.select(uri);
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/net/spi/DefaultProxySelectorTest.java Mon Aug 26 12:25:49 2019 +0100
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import sun.net.spi.DefaultProxySelector;
+
+import java.net.ProxySelector;
+import java.net.URI;
+
+/**
+ * @test
+ * @bug 6563286 6797318 8177648
+ * @summary Tests sun.net.spi.DefaultProxySelector#select(URI)
+ * @run testng DefaultProxySelectorTest
+ * @modules java.base/sun.net.spi:+open
+ */
+public class DefaultProxySelectorTest {
+
+ /**
+ * Tests that {@link DefaultProxySelector#select(URI)} throws
+ * {@link IllegalArgumentException} when passed {@code null}
+ */
+ @Test
+ public void testIllegalArgForNull() {
+ final ProxySelector selector = new DefaultProxySelector();
+ try {
+ selector.select(null);
+ Assert.fail("select() was expected to fail for null URI");
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ }
+
+ /**
+ * Tests that {@link DefaultProxySelector} throws a {@link IllegalArgumentException}
+ * for URIs that don't have host information
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testIllegalArgForNoHost() throws Exception {
+ final ProxySelector selector = new DefaultProxySelector();
+ assertFailsWithIAE(selector, new URI("http", "/test", null));
+ assertFailsWithIAE(selector, new URI("https", "/test2", null));
+ assertFailsWithIAE(selector, new URI("ftp", "/test3", null));
+ }
+
+
+ /**
+ * Tests that {@link DefaultProxySelector} throws a {@link IllegalArgumentException}
+ * for URIs that don't have protocol/scheme information
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testIllegalArgForNoScheme() throws Exception {
+ final ProxySelector selector = new DefaultProxySelector();
+ assertFailsWithIAE(selector, new URI(null, "/test", null));
+ }
+
+ private static void assertFailsWithIAE(final ProxySelector selector, final URI uri) {
+ try {
+ selector.select(uri);
+ Assert.fail("select() was expected to fail for URI " + uri);
+ } catch (IllegalArgumentException iae) {
+ // expected
+ }
+ }
+}