Skip to content

Commit c3d59e5

Browse files
authored
Track If Netty Builder Set Resolver and Properly Use Noop Resolver When Proxying (Azure#26611)
Track If Netty Builder Set Resolver and Properly Use Noop Resolver When Proxying
1 parent 162f5be commit c3d59e5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

sdk/core/azure-core-http-netty/src/main/java/com/azure/core/http/netty/NettyAsyncHttpClientBuilder.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ public NettyAsyncHttpClientBuilder(HttpClient nettyHttpClient) {
128128
*/
129129
public com.azure.core.http.HttpClient build() {
130130
HttpClient nettyHttpClient;
131+
132+
// Used to track if the builder set the DefaultAddressResolverGroup. If it did, when proxying it allows the
133+
// no-op address resolver to be set.
134+
boolean addressResolverWasSetByBuilder = false;
131135
if (this.baseHttpClient != null) {
132136
nettyHttpClient = baseHttpClient;
133137
} else if (this.connectionProvider != null) {
134138
nettyHttpClient = HttpClient.create(this.connectionProvider).resolver(DefaultAddressResolverGroup.INSTANCE);
139+
addressResolverWasSetByBuilder = true;
135140
} else {
136141
nettyHttpClient = HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
142+
addressResolverWasSetByBuilder = true;
137143
}
138144

139145
nettyHttpClient = nettyHttpClient
@@ -187,11 +193,6 @@ public com.azure.core.http.HttpClient build() {
187193
handler, proxyChallengeHolder));
188194
}
189195
});
190-
191-
AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
192-
if (resolver == null) {
193-
nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
194-
}
195196
} else {
196197
nettyHttpClient = nettyHttpClient.proxy(proxy ->
197198
proxy.type(toReactorNettyProxyType(buildProxyOptions.getType(), logger))
@@ -200,6 +201,11 @@ public com.azure.core.http.HttpClient build() {
200201
.password(ignored -> buildProxyOptions.getPassword())
201202
.nonProxyHosts(buildProxyOptions.getNonProxyHosts()));
202203
}
204+
205+
AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
206+
if (resolver == null || addressResolverWasSetByBuilder) {
207+
nettyHttpClient = nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
208+
}
203209
}
204210

205211
return new NettyAsyncHttpClient(nettyHttpClient, disableBufferCopy,

sdk/core/azure-core-http-netty/src/test/java/com/azure/core/http/netty/NettyAsyncHttpClientTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.github.tomakehurst.wiremock.WireMockServer;
2222
import com.github.tomakehurst.wiremock.http.Fault;
2323
import io.netty.handler.proxy.ProxyConnectException;
24+
import io.netty.resolver.DefaultAddressResolverGroup;
25+
import io.netty.resolver.NoopAddressResolverGroup;
2426
import org.junit.jupiter.api.AfterAll;
2527
import org.junit.jupiter.api.Assertions;
2628
import org.junit.jupiter.api.BeforeAll;
@@ -32,6 +34,7 @@
3234
import reactor.core.publisher.Flux;
3335
import reactor.core.publisher.Mono;
3436
import reactor.core.scheduler.Schedulers;
37+
import reactor.netty.resources.ConnectionProvider;
3538
import reactor.test.StepVerifier;
3639
import reactor.test.StepVerifierOptions;
3740

@@ -56,6 +59,7 @@
5659
import static org.junit.jupiter.api.Assertions.assertEquals;
5760
import static org.junit.jupiter.api.Assertions.assertFalse;
5861
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
62+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5963
import static org.junit.jupiter.api.Assertions.assertNotNull;
6064

6165
public class NettyAsyncHttpClientTests {
@@ -436,6 +440,41 @@ public void failedProxyAuthenticationReturnsCorrectError() {
436440
}
437441
}
438442

443+
@Test
444+
public void httpClientWithDefaultResolverUsesNoopResolverWithProxy() {
445+
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
446+
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder()
447+
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
448+
.build();
449+
450+
assertEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
451+
}
452+
}
453+
454+
@Test
455+
public void httpClientWithConnectionProviderUsesNoopResolverWithProxy() {
456+
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
457+
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder()
458+
.connectionProvider(ConnectionProvider.newConnection())
459+
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
460+
.build();
461+
462+
assertEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
463+
}
464+
}
465+
466+
@Test
467+
public void httpClientWithResolverUsesConfiguredResolverWithProxy() {
468+
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
469+
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder(
470+
reactor.netty.http.client.HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE))
471+
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
472+
.build();
473+
474+
assertNotEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
475+
}
476+
}
477+
439478
private static Stream<Arguments> requestHeaderSupplier() {
440479
return Stream.of(
441480
Arguments.of(null, NettyAsyncHttpClientResponseTransformer.NULL_REPLACEMENT),

0 commit comments

Comments
 (0)