diff --git a/src/main/java/libcore/net/http/HttpTransport.java b/src/main/java/libcore/net/http/HttpTransport.java index d30734978..dacffb94f 100644 --- a/src/main/java/libcore/net/http/HttpTransport.java +++ b/src/main/java/libcore/net/http/HttpTransport.java @@ -211,10 +211,16 @@ final class HttpTransport implements Transport { return false; } - // If the headers specify that the connection shouldn't be reused, don't reuse it. + // If the request specified that the connection shouldn't be reused, + // don't reuse it. This advice doesn't apply to CONNECT requests because + // the "Connection: close" header goes the origin server, not the proxy. if (httpEngine.requestHeaders.hasConnectionClose() - || (httpEngine.responseHeaders != null - && httpEngine.responseHeaders.hasConnectionClose())) { + && httpEngine.method != HttpEngine.CONNECT) { + return false; + } + + // If the response specified that the connection shouldn't be reused, don't reuse it. + if (httpEngine.responseHeaders != null && httpEngine.responseHeaders.hasConnectionClose()) { return false; } diff --git a/src/test/java/libcore/net/http/URLConnectionTest.java b/src/test/java/libcore/net/http/URLConnectionTest.java index ae4abd2b5..723e32788 100644 --- a/src/test/java/libcore/net/http/URLConnectionTest.java +++ b/src/test/java/libcore/net/http/URLConnectionTest.java @@ -748,6 +748,26 @@ public final class URLConnectionTest extends TestCase { assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*"); } + // Don't disconnect after building a tunnel with CONNECT + // http://code.google.com/p/android/issues/detail?id=37221 + public void testProxyWithConnectionClose() throws IOException { + server.useHttps(sslContext.getSocketFactory(), true); + server.enqueue(new MockResponse() + .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) + .clearHeaders()); + server.enqueue(new MockResponse().setBody("this response comes via a proxy")); + server.play(); + + URL url = new URL("https://android.com/foo"); + OkHttpsConnection connection = (OkHttpsConnection) openConnection( + url, server.toProxyAddress()); + connection.setRequestProperty("Connection", "close"); + connection.setSSLSocketFactory(sslContext.getSocketFactory()); + connection.setHostnameVerifier(new RecordingHostnameVerifier()); + + assertContent("this response comes via a proxy", connection); + } + public void testDisconnectedConnection() throws IOException { server.enqueue(new MockResponse().setBody("ABCDEFGHIJKLMNOPQR")); server.play();