Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate original request user-agent in proxy CONNECT requests #1742

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
if (connect) {
// assign proxy-auth as configured on request
headers.set(PROXY_AUTHORIZATION, request.getHeaders().getAll(PROXY_AUTHORIZATION));
headers.set(USER_AGENT, request.getHeaders().getAll(USER_AGENT));

} else {
// assign headers as configured on request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static io.netty.handler.codec.http.HttpHeaderNames.PROXY_AUTHENTICATE;
import static io.netty.handler.codec.http.HttpHeaderNames.PROXY_AUTHORIZATION;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static org.asynchttpclient.Dsl.*;
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
import static org.asynchttpclient.config.AsyncHttpClientConfigDefaults.*;

/**
* Test that validates that when having an HTTP proxy and trying to access an HTTPS through the proxy the proxy credentials should be passed during the CONNECT request.
* Test that validates that when having an HTTP proxy and trying to access an HTTPS
* through the proxy the proxy credentials and a custom user-agent (if set) should be passed during the CONNECT request.
*/
public class BasicHttpProxyToHttpsTest {

private static final Logger LOGGER = LoggerFactory.getLogger(BasicHttpProxyToHttpsTest.class);
private static final String CUSTOM_USER_AGENT = "custom-user-agent";

private int httpPort;
private int proxyPort;
Expand All @@ -66,13 +68,24 @@ public void setUpGlobal() throws Exception {
ConnectHandler connectHandler = new ConnectHandler() {

@Override
// This proxy receives a CONNECT request from the client before making the real request for the target host.
protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {

// If the userAgent of the CONNECT request is the same as the default userAgent,
// then the custom userAgent was not properly propagated and the test should fail.
String userAgent = request.getHeader(USER_AGENT.toString());
if(userAgent.equals(defaultUserAgent())) {
return false;
}

// If the authentication failed, the test should also fail.
String authorization = request.getHeader(PROXY_AUTHORIZATION.toString());
if (authorization == null) {
response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
response.setHeader(PROXY_AUTHENTICATE.toString(), "Basic realm=\"Fake Realm\"");
return false;
} else if (authorization.equals("Basic am9obmRvZTpwYXNz")) {
}
else if (authorization.equals("Basic am9obmRvZTpwYXNz")) {
return true;
}
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Expand All @@ -98,6 +111,7 @@ public void nonPreemptiveProxyAuthWithHttpsTarget() throws IOException, Interrup
String targetUrl = "https://localhost:" + httpPort + "/foo/bar";
Request request = get(targetUrl)
.setProxyServer(proxyServer("127.0.0.1", proxyPort).setRealm(realm(AuthScheme.BASIC, "johndoe", "pass")))
.setHeader("user-agent", CUSTOM_USER_AGENT)
// .setRealm(realm(AuthScheme.BASIC, "user", "passwd"))
.build();
Future<Response> responseFuture = client.executeRequest(request);
Expand All @@ -107,4 +121,4 @@ public void nonPreemptiveProxyAuthWithHttpsTarget() throws IOException, Interrup
Assert.assertEquals("/foo/bar", response.getHeader("X-pathInfo"));
}
}
}
}