Skip to content

Commit

Permalink
Merge pull request square#2417 from square/jwilson_0319_proxy_regression
Browse files Browse the repository at this point in the history
Fix a regression in authenticating HTTP proxies.
  • Loading branch information
swankjesse committed Mar 20, 2016
2 parents 806c0e3 + 8e6aeb4 commit 704ce92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
28 changes: 28 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,34 @@ private InetSocketAddress startNullServer() throws IOException {
assertNull(get.getHeader("Proxy-Authorization"));
}

/** Confirm that the proxy authenticator works for unencrypted HTTP proxies. */
@Test public void httpProxyAuthenticate() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(407)
.addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
server.enqueue(new MockResponse()
.setBody("response body"));

client = client.newBuilder()
.proxy(server.toProxyAddress())
.proxyAuthenticator(new RecordingOkAuthenticator("password"))
.build();

Request request = new Request.Builder()
.url("http://android.com/foo")
.build();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());

RecordedRequest get1 = server.takeRequest();
assertEquals("GET http://android.com/foo HTTP/1.1", get1.getRequestLine());
assertNull(get1.getHeader("Proxy-Authorization"));

RecordedRequest get2 = server.takeRequest();
assertEquals("GET http://android.com/foo HTTP/1.1", get2.getRequestLine());
assertEquals("password", get2.getHeader("Proxy-Authorization"));
}

/**
* Confirm that we don't send the Proxy-Authorization header from the request to the proxy server.
* We used to have that behavior but it is problematic because unrelated requests end up sharing
Expand Down
9 changes: 5 additions & 4 deletions okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1596,15 +1596,16 @@ private List<String> authCallsForHeader(String authHeader) throws IOException {
.setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);

urlFactory.setClient(urlFactory.client().newBuilder()
.authenticator(new JavaNetAuthenticator())
.build());
if (proxy) {
urlFactory.setClient(urlFactory.client().newBuilder()
.proxy(server.toProxyAddress())
.proxyAuthenticator(new JavaNetAuthenticator())
.build());
connection = urlFactory.open(new URL("http://android.com"));
connection = urlFactory.open(new URL("http://android.com/"));
} else {
urlFactory.setClient(urlFactory.client().newBuilder()
.authenticator(new JavaNetAuthenticator())
.build());
connection = urlFactory.open(server.url("/").url());
}
assertEquals(responseCode, connection.getResponseCode());
Expand Down
3 changes: 2 additions & 1 deletion okhttp/src/main/java/okhttp3/internal/http/HttpEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ public Request followUpRequest() throws IOException {
if (selectedProxy.type() != Proxy.Type.HTTP) {
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
}
// fall-through
return client.proxyAuthenticator().authenticate(route, userResponse);

case HTTP_UNAUTHORIZED:
return client.authenticator().authenticate(route, userResponse);

Expand Down

0 comments on commit 704ce92

Please sign in to comment.