Skip to content

Commit 296dbc1

Browse files
committed
Http Client made consistent with Default Proxy Configurations
1 parent 2692379 commit 296dbc1

File tree

9 files changed

+516
-4
lines changed

9 files changed

+516
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Addressing Issue [#4745](https://github.com/aws/aws-sdk-java-v2/issues/4745) , Netty and CRT clients' default proxy settings have been made consistent with the Apache client, now using environment and system property settings by default.\n To disable the use of environment variables and system properties by default, set useSystemPropertyValue(false) and useEnvironmentVariablesValues(false) in ProxyConfigurations."
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache;
17+
18+
import java.util.concurrent.ThreadLocalRandom;
19+
import org.apache.http.conn.HttpHostConnectException;
20+
import software.amazon.awssdk.http.SdkHttpClient;
21+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
22+
import software.amazon.awssdk.http.proxy.HttpClientDefaultPoxyConfigTest;
23+
import java.net.ConnectException;
24+
25+
public class ApacheClientProxyConfigurationTest extends HttpClientDefaultPoxyConfigTest {
26+
27+
@Override
28+
protected Class<? extends Exception> getProxyFailedExceptionType() {
29+
return HttpHostConnectException.class;
30+
31+
}
32+
33+
@Override
34+
protected Class<? extends Exception> getProxyFailedCauseExceptionType() {
35+
return ConnectException.class;
36+
}
37+
38+
@Override
39+
protected boolean isSyncClient() {
40+
return true;
41+
}
42+
43+
@Override
44+
protected SdkAsyncHttpClient createHttpClientWithDefaultProxy() {
45+
throw new IllegalArgumentException("Async client is not supported");
46+
}
47+
48+
@Override
49+
protected SdkHttpClient createSyncHttpClientWithDefaultProxy() {
50+
return ApacheHttpClient.builder().build();
51+
}
52+
53+
/**
54+
* Wrire code which inputs an integer args and returns one number between 0 to 65535 excluding number args
55+
*/
56+
private int getRandomPort(int currentPort) {
57+
int randomPort;
58+
do {
59+
randomPort = ThreadLocalRandom.current().nextInt(65535);
60+
} while (randomPort == currentPort);
61+
return randomPort;
62+
}
63+
64+
65+
}

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtClientBuilderBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public class AwsCrtClientBuilderBase<BuilderT> {
3030
private final AttributeMap.Builder standardOptions = AttributeMap.builder();
3131
private Long readBufferSize;
32-
private ProxyConfiguration proxyConfiguration;
32+
private ProxyConfiguration proxyConfiguration = ProxyConfiguration.builder().build();
3333
private ConnectionHealthConfiguration connectionHealthConfiguration;
3434
private TcpKeepAliveConfiguration tcpKeepAliveConfiguration;
3535
private Boolean postQuantumTlsEnabled;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.crt;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.ExecutionException;
20+
import software.amazon.awssdk.http.SdkHttpClient;
21+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
22+
import software.amazon.awssdk.http.proxy.HttpClientDefaultPoxyConfigTest;
23+
24+
public class AsyncCrtClientProxyConfigurationTest extends HttpClientDefaultPoxyConfigTest {
25+
26+
@Override
27+
protected Class<? extends Exception> getProxyFailedExceptionType() {
28+
return ExecutionException.class;
29+
}
30+
31+
@Override
32+
protected Class<? extends Exception> getProxyFailedCauseExceptionType() {
33+
return IOException.class;
34+
}
35+
36+
@Override
37+
protected boolean isSyncClient() {
38+
return false;
39+
}
40+
41+
@Override
42+
protected SdkAsyncHttpClient createHttpClientWithDefaultProxy() {
43+
return AwsCrtAsyncHttpClient.builder().build();
44+
}
45+
46+
@Override
47+
protected SdkHttpClient createSyncHttpClientWithDefaultProxy() {
48+
throw new IllegalArgumentException("Sync client is not supported");
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.crt;
17+
18+
19+
import java.io.IOException;
20+
import software.amazon.awssdk.crt.http.HttpException;
21+
import software.amazon.awssdk.http.SdkHttpClient;
22+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
23+
import software.amazon.awssdk.http.proxy.HttpClientDefaultPoxyConfigTest;
24+
25+
public class SyncCrtClientProxyConfigurationTest extends HttpClientDefaultPoxyConfigTest {
26+
27+
@Override
28+
protected Class<? extends Exception> getProxyFailedExceptionType() {
29+
return IOException.class;
30+
}
31+
32+
@Override
33+
protected Class<? extends Exception> getProxyFailedCauseExceptionType() {
34+
return HttpException.class;
35+
}
36+
37+
@Override
38+
protected boolean isSyncClient() {
39+
return true;
40+
}
41+
42+
@Override
43+
protected SdkAsyncHttpClient createHttpClientWithDefaultProxy() {
44+
throw new UnsupportedOperationException("Async client does not support proxy");
45+
}
46+
47+
@Override
48+
protected SdkHttpClient createSyncHttpClientWithDefaultProxy() {
49+
return AwsCrtHttpClient.create();
50+
}
51+
}

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/NettyNioAsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ private static final class DefaultBuilder implements Builder {
501501
private Integer maxHttp2Streams;
502502
private Http2Configuration http2Configuration;
503503
private SslProvider sslProvider;
504-
private ProxyConfiguration proxyConfiguration;
504+
private ProxyConfiguration proxyConfiguration = ProxyConfiguration.builder().build();
505505
private Boolean useNonBlockingDnsResolver;
506506

507507
private DefaultBuilder() {

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/AwaitCloseChannelPoolMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ private Bootstrap createBootstrap(URI poolKey) {
186186

187187

188188
private boolean shouldUseProxyForHost(URI remoteAddr) {
189-
if (proxyConfiguration == null) {
189+
if (proxyConfiguration == null || proxyConfiguration.host() == null) {
190190
return false;
191191
}
192192

193193

194194
return shouldProxyForHostCache.computeIfAbsent(remoteAddr, (uri) ->
195-
proxyConfiguration.nonProxyHosts().stream().noneMatch(h -> uri.getHost().matches(h))
195+
proxyConfiguration.nonProxyHosts().stream().noneMatch(h -> uri.getHost().matches(h))
196196
);
197197
}
198198

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import java.net.ConnectException;
17+
import java.util.concurrent.ExecutionException;
18+
import software.amazon.awssdk.http.SdkHttpClient;
19+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
20+
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
21+
import software.amazon.awssdk.http.proxy.HttpClientDefaultPoxyConfigTest;
22+
23+
public class NettyClientProxyConfigurationTest extends HttpClientDefaultPoxyConfigTest {
24+
@Override
25+
protected boolean isSyncClient() {
26+
return false;
27+
}
28+
29+
@Override
30+
protected SdkAsyncHttpClient createHttpClientWithDefaultProxy() {
31+
return NettyNioAsyncHttpClient.create();
32+
}
33+
34+
@Override
35+
protected SdkHttpClient createSyncHttpClientWithDefaultProxy() {
36+
throw new IllegalArgumentException("Sync client not supported");
37+
}
38+
39+
@Override
40+
protected Class<? extends Exception> getProxyFailedExceptionType() {
41+
return ExecutionException.class;
42+
}
43+
44+
@Override
45+
protected Class<? extends Exception> getProxyFailedCauseExceptionType() {
46+
return ConnectException.class;
47+
}
48+
}

0 commit comments

Comments
 (0)