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

Track If Netty Builder Set Resolver and Properly Use Noop Resolver When Proxying #26611

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 @@ -128,12 +128,18 @@ public NettyAsyncHttpClientBuilder(HttpClient nettyHttpClient) {
*/
public com.azure.core.http.HttpClient build() {
HttpClient nettyHttpClient;

// Used to track if the builder set the DefaultAddressResolverGroup. If it did, when proxying it allows the
// no-op address resolver to be set.
boolean addressResolverWasSetByBuilder = false;
if (this.baseHttpClient != null) {
nettyHttpClient = baseHttpClient;
} else if (this.connectionProvider != null) {
nettyHttpClient = HttpClient.create(this.connectionProvider).resolver(DefaultAddressResolverGroup.INSTANCE);
addressResolverWasSetByBuilder = true;
} else {
nettyHttpClient = HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
addressResolverWasSetByBuilder = true;
}

nettyHttpClient = nettyHttpClient
Expand Down Expand Up @@ -187,11 +193,6 @@ public com.azure.core.http.HttpClient build() {
handler, proxyChallengeHolder));
}
});

AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
if (resolver == null) {
nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
}
} else {
nettyHttpClient = nettyHttpClient.proxy(proxy ->
proxy.type(toReactorNettyProxyType(buildProxyOptions.getType(), logger))
Expand All @@ -200,6 +201,11 @@ public com.azure.core.http.HttpClient build() {
.password(ignored -> buildProxyOptions.getPassword())
.nonProxyHosts(buildProxyOptions.getNonProxyHosts()));
}

AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
if (resolver == null || addressResolverWasSetByBuilder) {
nettyHttpClient = nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
}
}

return new NettyAsyncHttpClient(nettyHttpClient, disableBufferCopy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.http.Fault;
import io.netty.handler.proxy.ProxyConnectException;
import io.netty.resolver.DefaultAddressResolverGroup;
import io.netty.resolver.NoopAddressResolverGroup;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -32,6 +34,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.netty.resources.ConnectionProvider;
import reactor.test.StepVerifier;
import reactor.test.StepVerifierOptions;

Expand All @@ -56,6 +59,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class NettyAsyncHttpClientTests {
Expand Down Expand Up @@ -436,6 +440,41 @@ public void failedProxyAuthenticationReturnsCorrectError() {
}
}

@Test
public void httpClientWithDefaultResolverUsesNoopResolverWithProxy() {
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
.build();

assertEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
}
}

@Test
public void httpClientWithConnectionProviderUsesNoopResolverWithProxy() {
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder()
.connectionProvider(ConnectionProvider.newConnection())
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
.build();

assertEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
}
}

@Test
public void httpClientWithResolverUsesConfiguredResolverWithProxy() {
try (MockProxyServer mockProxyServer = new MockProxyServer()) {
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientBuilder(
reactor.netty.http.client.HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE))
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, mockProxyServer.socketAddress()))
.build();

assertNotEquals(NoopAddressResolverGroup.INSTANCE, httpClient.nettyClient.configuration().resolver());
}
}

private static Stream<Arguments> requestHeaderSupplier() {
return Stream.of(
Arguments.of(null, NettyAsyncHttpClientResponseTransformer.NULL_REPLACEMENT),
Expand Down