From 084fa33870fd40d3bdedfe16dbcf773128e0709c Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 12 Nov 2012 12:32:47 -0800 Subject: [PATCH] Don't explode if DNS fails or if the address is malformed. We had a bug where we were trying to report a proxy problem before the connection was even created. --- .../net/http/HttpURLConnectionImpl.java | 6 +++++- .../libcore/net/http/URLConnectionTest.java | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/libcore/net/http/HttpURLConnectionImpl.java b/src/main/java/libcore/net/http/HttpURLConnectionImpl.java index f5cc49e74..94678481a 100644 --- a/src/main/java/libcore/net/http/HttpURLConnectionImpl.java +++ b/src/main/java/libcore/net/http/HttpURLConnectionImpl.java @@ -338,7 +338,11 @@ public class HttpURLConnectionImpl extends OkHttpConnection { return true; } catch (IOException e) { RouteSelector routeSelector = httpEngine.routeSelector; - routeSelector.connectFailed(httpEngine.connection, e); + if (routeSelector == null) { + throw e; // Without a route selector, we can't retry. + } else if (httpEngine.connection != null) { + routeSelector.connectFailed(httpEngine.connection, e); + } // The connection failure isn't fatal if there's another route to attempt. OutputStream requestBody = httpEngine.getRequestBody(); diff --git a/src/test/java/libcore/net/http/URLConnectionTest.java b/src/test/java/libcore/net/http/URLConnectionTest.java index 0c5e13099..48a6a5dcf 100644 --- a/src/test/java/libcore/net/http/URLConnectionTest.java +++ b/src/test/java/libcore/net/http/URLConnectionTest.java @@ -133,8 +133,6 @@ public final class URLConnectionTest extends TestCase { // TODO: test that request bodies are retransmitted on IP address failures // TODO: pooled proxy failures are not reported to the proxy selector - // TODO: a URI with no host should fail in Address creation. - // TODO: make HttpURLConnection.connect() include a loop around execute public void testRequestHeaders() throws IOException, InterruptedException { server.enqueue(new MockResponse()); @@ -1936,6 +1934,24 @@ public final class URLConnectionTest extends TestCase { } } + public void testDnsFailureThrowsIOException() throws IOException { + OkHttpConnection connection = openConnection(new URL("http://host.unlikelytld")); + try { + connection.connect(); + fail(); + } catch (IOException expected) { + } + } + + public void testMalformedUrlThrowsUnknownHostException() throws IOException { + OkHttpConnection connection = openConnection(new URL("http:///foo.html")); + try { + connection.connect(); + fail(); + } catch (UnknownHostException expected) { + } + } + public void SUPPRESSED_testGetKeepAlive() throws Exception { MockWebServer server = new MockWebServer(); server.enqueue(new MockResponse().setBody("ABC"));