Skip to content

Commit 780ed5f

Browse files
authored
Merge pull request #2256 from jglick/EndpointToRegion
Make EndpointToRegion.guessRegionOrRegionNameForEndpoint tolerate the format 127.0.0.1:1234
2 parents 30c2048 + 78f8755 commit 780ed5f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

aws-java-sdk-core/src/main/java/com/amazonaws/regions/EndpointToRegion.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.amazonaws.annotation.SdkProtectedApi;
1818
import com.amazonaws.util.AwsHostNameUtils;
1919
import java.net.URI;
20+
import java.net.URISyntaxException;
2021

2122
/**
2223
* Utilities to attempt to convert from a hostname/endpoint to an AWS region.
@@ -68,7 +69,12 @@ private static RegionOrRegionName guessRegionOrRegionNameForEndpoint(String endp
6869
return new RegionOrRegionName();
6970
}
7071

71-
String host = URI.create(endpoint).getHost();
72+
String host = null;
73+
try {
74+
host = new URI(endpoint).getHost();
75+
} catch (URISyntaxException x) {
76+
// proceed
77+
}
7278
if (host == null) {
7379
host = URI.create("http://" + endpoint).getHost();
7480
}

aws-java-sdk-core/src/test/java/com/amazonaws/regions/EndpointToRegionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ public void guessRegionForHostname_basedOnFollowingServiceName() {
111111
verifyRegionAndPartitionForHostname("us-gov-west-1", "aws-us-gov", "iam.us-gov-west-1.banana.com", "iam");
112112
}
113113

114+
@Test
115+
public void guessRegionForHostname_ipAddress() {
116+
assertNull(guessRegionNameForEndpoint("http://localhost"));
117+
assertNull(guessRegionNameForEndpoint("http://localhost:1234"));
118+
assertNull(guessRegionNameForEndpoint("http://127.0.0.1"));
119+
assertNull(guessRegionNameForEndpoint("http://127.0.0.1:1234"));
120+
assertNull(guessRegionNameForEndpoint("localhost"));
121+
assertNull(guessRegionNameForEndpoint("localhost:1234"));
122+
assertNull(guessRegionNameForEndpoint("127.0.0.1"));
123+
assertNull(guessRegionNameForEndpoint("127.0.0.1:1234"));
124+
}
125+
114126
/**
115127
* We migrated from AwsHostNameUtils.parseRegion to EndpointToRegion.guessRegionNameForHostname, because EndpointToRegion
116128
* can consider the contents of endpoints.json.

0 commit comments

Comments
 (0)