Skip to content

Commit

Permalink
Merge "Fix a bug in tunnel construction and 'Connection: close' heade…
Browse files Browse the repository at this point in the history
…rs."
  • Loading branch information
bdcgoogle authored and Gerrit Code Review committed Oct 5, 2012
2 parents 7ae5f93 + 4bedacd commit 3c63620
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 9 additions & 7 deletions luni/src/main/java/libcore/net/http/HttpEngine.java
Expand Up @@ -491,8 +491,15 @@ public final void release(boolean reusable) {
reusable = false;
}

// If the headers specify that the connection shouldn't be reused, don't reuse it.
if (hasConnectionCloseHeader()) {
// 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 (requestHeaders.hasConnectionClose() && method != CONNECT) {
reusable = false;
}

// If the response specified that the connection shouldn't be reused, don't reuse it.
if (responseHeaders != null && responseHeaders.hasConnectionClose()) {
reusable = false;
}

Expand Down Expand Up @@ -764,11 +771,6 @@ protected final String getDefaultUserAgent() {
return agent != null ? agent : ("Java" + System.getProperty("java.version"));
}

private boolean hasConnectionCloseHeader() {
return (responseHeaders != null && responseHeaders.hasConnectionClose())
|| requestHeaders.hasConnectionClose();
}

protected final String getOriginAddress(URL url) {
int port = url.getPort();
String result = url.getHost();
Expand Down
21 changes: 21 additions & 0 deletions luni/src/test/java/libcore/java/net/URLConnectionTest.java
Expand Up @@ -795,6 +795,27 @@ public void testProxyAuthenticateOnConnect() throws Exception {
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 {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.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");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(
server.toProxyAddress());
connection.setRequestProperty("Connection", "close");
connection.setSSLSocketFactory(testSSLContext.clientContext.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();
Expand Down

0 comments on commit 3c63620

Please sign in to comment.