Skip to content

Commit 0b4a4c1

Browse files
authored
Improve support for IPv6 addresses (#167)
Add support for IPv6 addresses defined without square brackets. JAVA-2220
1 parent c841b55 commit 0b4a4c1

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

driver-core/src/main/com/mongodb/ServerAddress.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ public ServerAddress(final String host, final int port) {
109109
}
110110
portToUse = Integer.parseInt(host.substring(portIdx + 2));
111111
}
112-
hostToUse = host.substring(0, idx + 1);
112+
hostToUse = host.substring(1, idx);
113113
} else {
114114
int idx = hostToUse.indexOf(":");
115-
if (idx > 0) {
115+
int lastIdx = hostToUse.lastIndexOf(":");
116+
if (idx == lastIdx && idx > 0) {
116117
if (port != defaultPort()) {
117118
throw new IllegalArgumentException("can't specify port in construct and via host");
118119
}
@@ -124,7 +125,6 @@ public ServerAddress(final String host, final int port) {
124125
hostToUse = hostToUse.substring(0, idx).trim();
125126
}
126127
}
127-
128128
this.host = hostToUse.toLowerCase();
129129
this.port = portToUse;
130130
}

driver-core/src/test/unit/com/mongodb/ServerAddressSpecification.groovy

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class ServerAddressSpecification extends Specification {
2424
@Unroll
2525
def 'constructors should parse hostname and port correctly'() {
2626
expect:
27-
address.getHost() == host;
28-
address.getPort() == port;
27+
address.getHost() == host
28+
address.getPort() == port
29+
address == new ServerAddress(host, port)
2930

3031
where:
3132
address | host | port
@@ -37,10 +38,11 @@ class ServerAddressSpecification extends Specification {
3738
new ServerAddress('SOMEWHERE') | 'somewhere' | ServerAddress.defaultPort()
3839
new ServerAddress('somewhere:1000') | 'somewhere' | 1000
3940
new ServerAddress('somewhere', 1000) | 'somewhere' | 1000
40-
new ServerAddress('[2010:836b:4179::836b:4179]') | '[2010:836b:4179::836b:4179]' | ServerAddress.defaultPort()
41-
new ServerAddress('[2010:836B:4179::836B:4179]') | '[2010:836b:4179::836b:4179]' | ServerAddress.defaultPort()
42-
new ServerAddress('[2010:836B:4179::836B:4179]:1000') | '[2010:836b:4179::836b:4179]' | 1000
43-
new ServerAddress('[2010:836B:4179::836B:4179]', 1000) | '[2010:836b:4179::836b:4179]' | 1000
41+
new ServerAddress('[2010:836B:4179::836B:4179]') | '2010:836b:4179::836b:4179' | ServerAddress.defaultPort()
42+
new ServerAddress('[2010:836B:4179::836B:4179]:1000') | '2010:836b:4179::836b:4179' | 1000
43+
new ServerAddress('[2010:836B:4179::836B:4179]', 1000) | '2010:836b:4179::836b:4179' | 1000
44+
new ServerAddress('2010:836B:4179::836B:4179') | '2010:836b:4179::836b:4179' | ServerAddress.defaultPort()
45+
new ServerAddress('2010:836B:4179::836B:4179', 1000) | '2010:836b:4179::836b:4179' | 1000
4446
}
4547

4648
def 'ipv4 host with a port specified should throw when a port is also specified as an argument'() {
@@ -92,4 +94,4 @@ class ServerAddressSpecification extends Specification {
9294
then:
9395
thrown(MongoException);
9496
}
95-
}
97+
}

driver-core/src/test/unit/com/mongodb/connection/JMXConnectionPoolListenerSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
120120
ObjectName objectName = new ObjectName(beanName)
121121

122122
then:
123-
objectName.toString() == "org.mongodb.driver:type=ConnectionPool,clusterId=${serverId.clusterId.value},host=\"[::1]\",port=27017"
123+
objectName.toString() == "org.mongodb.driver:type=ConnectionPool,clusterId=${serverId.clusterId.value},host=\"::1\",port=27017"
124124
}
125125

126126
def 'should include the description in the object name if set'() {

0 commit comments

Comments
 (0)