Skip to content

Commit 27c587d

Browse files
committed
Ensure Akka HTTP(S) interception correctly handles plain HTTP
If test URL is changed to plain HTTP the tests fail, but that's expected, as Wiremock only supports HTTPS and Akka & Spring don't support HTTP-over-HTTPS proxying correctly. HTTP Toolkit supports both on the same port just fine though, so that's enough for now. Might be worth investigating this further in future, but it's quite tricky, not worth it until it causes a real world issue.
1 parent 1f24bbb commit 27c587d

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/main/java/tech/httptoolkit/javaagent/advice/akka/OverrideHttpSettingsAdvice.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class OverrideHttpSettingsAdvice {
2020
// For 10.2+:
2121
? ConnectionContext.httpsClient(HttpProxyAgent.getInterceptedSslContext())
2222
// For everything before then:
23-
: akka.http.javadsl.ConnectionContext.https(HttpProxyAgent.getInterceptedSslContext());
23+
: ConnectionContext.https(HttpProxyAgent.getInterceptedSslContext());
2424

2525
@Advice.OnMethodEnter
2626
public static void beforeOutgoingConnection(
@@ -30,12 +30,14 @@ public static void beforeOutgoingConnection(
3030
// Change all new outgoing connections to use the proxy:
3131
clientSettings = clientSettings.withTransport(
3232
ClientTransport.httpsProxy(new InetSocketAddress(
33-
HttpProxyAgent.getAgentProxyHost(),
34-
HttpProxyAgent.getAgentProxyPort()
33+
HttpProxyAgent.getAgentProxyHost(),
34+
HttpProxyAgent.getAgentProxyPort()
3535
))
3636
);
3737

3838
// Change all new outgoing connections to trust our certificate:
39-
connectionContext = interceptedConnectionContext;
39+
if (connectionContext.isSecure()) {
40+
connectionContext = OverrideHttpSettingsAdvice.interceptedConnectionContext;
41+
}
4042
}
4143
}

src/main/java/tech/httptoolkit/javaagent/advice/akka/ResetPoolSetupAdvice.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static void afterConstructor(
2929
settings = settings.withTransport(interceptedProxyTransport);
3030

3131
// Change all new outgoing connections to trust our certificate:
32-
connContext = OverrideHttpSettingsAdvice.interceptedConnectionContext;
32+
if (connContext.isSecure()) {
33+
connContext = OverrideHttpSettingsAdvice.interceptedConnectionContext;
34+
}
3335
}
3436
}

test-app/src/main/java/tech/httptoolkit/testapp/cases/AkkaHostClientCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import akka.NotUsed;
44
import akka.actor.ActorSystem;
55
import akka.actor.ExtendedActorSystem;
6+
import akka.http.javadsl.ConnectHttp;
67
import akka.http.javadsl.HostConnectionPool;
78
import akka.http.javadsl.Http;
89
import akka.http.javadsl.model.HttpRequest;
910
import akka.http.javadsl.model.HttpResponse;
11+
import akka.http.javadsl.model.Uri;
1012
import akka.japi.Pair;
1113
import akka.stream.javadsl.Flow;
1214
import akka.stream.javadsl.Sink;
@@ -28,9 +30,9 @@ public class AkkaHostClientCase extends ClientCase<
2830

2931
@Override
3032
public Flow<Pair<HttpRequest, NotUsed>, Pair<Try<HttpResponse>, NotUsed>, HostConnectionPool> newClient(String url) throws Exception {
31-
URI uri = new URI(url);
33+
Uri uri = Uri.create(url);
3234
return new Http((ExtendedActorSystem) system)
33-
.cachedHostConnectionPool(uri.getHost());
35+
.cachedHostConnectionPoolHttps(ConnectHttp.toHost(uri)); // HTTPS required here, or HTTP is *always* used
3436
}
3537

3638
@Override

0 commit comments

Comments
 (0)