Skip to content

Commit 7ce374a

Browse files
committed
Make Apache v4 interception reliable enough to intercept Gradle
Not really because Gradle is especially interesting, more that it's sufficiently common that it's likely to be a test case for many people, and it would be good if that worked! Previously it did basically work, AFAICT, but with loud errors during setup when HttpRoute couldn't be resolved. We now ensure that that's not required, so there's no errors: just perfectly intercepted HTTP(S).
1 parent c0b9dc8 commit 7ce374a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
package tech.httptoolkit.javaagent.advice.apacheclient;
22

33
import net.bytebuddy.asm.Advice;
4+
import net.bytebuddy.implementation.bytecode.assign.Assigner;
45
import org.apache.http.HttpHost;
56
import org.apache.http.conn.routing.HttpRoute;
6-
import tech.httptoolkit.javaagent.HttpProxyAgent;
7+
8+
import java.net.*;
79

810
public class ApacheV4ReturnProxyRouteAdvice {
911
@Advice.OnMethodExit
1012
public static void determineRoute(
11-
@Advice.Return(readOnly = false) HttpRoute returnValue
13+
// We type this dynamically, because in some cases (notably Gradle) we seemingly can't reach the
14+
// HttpRoute type from ByteBuddy, only at runtime.
15+
@Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object returnValue
1216
) {
17+
HttpRoute existingValue = (HttpRoute) returnValue;
18+
// We guarantee that the default proxy selector is always our own. This ensures that we can
19+
// always grab the proxy URL without needing to access our injected classes.
20+
Proxy proxy = ProxySelector.getDefault().select(URI.create("https://example.com")).get(0);
21+
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
22+
1323
returnValue = new HttpRoute(
14-
returnValue.getTargetHost(),
15-
returnValue.getLocalAddress(),
16-
new HttpHost(
17-
HttpProxyAgent.getAgentProxyHost(),
18-
HttpProxyAgent.getAgentProxyPort()
19-
),
20-
returnValue.isSecure()
24+
existingValue.getTargetHost(),
25+
existingValue.getLocalAddress(),
26+
new HttpHost(proxyAddress.getHostString(), proxyAddress.getPort()),
27+
existingValue.isSecure()
2128
);
2229
}
2330
}

0 commit comments

Comments
 (0)