Skip to content

Commit cfa1c86

Browse files
committed
Support IPv6 in routing procedure responses
This commit improves parsing in `BoltServerAddress` to support IPv6 in `getServers()` and `getRoutingTable()` procedure responses.
1 parent 45c2930 commit cfa1c86

File tree

9 files changed

+75
-41
lines changed

9 files changed

+75
-41
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class DriverFactory
5656
public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings routingSettings,
5757
RetrySettings retrySettings, Config config )
5858
{
59-
BoltServerAddress address = BoltServerAddress.from( uri );
59+
BoltServerAddress address = new BoltServerAddress( uri );
6060
RoutingSettings newRoutingSettings = routingSettings.withRoutingContext( new RoutingContext( uri ) );
6161
SecurityPlan securityPlan = createSecurityPlan( address, config );
6262
ConnectionPool connectionPool = createConnectionPool( authToken, securityPlan, config );

driver/src/main/java/org/neo4j/driver/internal/net/BoltServerAddress.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,23 @@ public class BoltServerAddress
3434
public static final int DEFAULT_PORT = 7687;
3535
public static final BoltServerAddress LOCAL_DEFAULT = new BoltServerAddress( "localhost", DEFAULT_PORT );
3636

37-
public static BoltServerAddress from( URI uri )
38-
{
39-
int port = uri.getPort();
40-
if ( port == -1 )
41-
{
42-
port = DEFAULT_PORT;
43-
}
44-
45-
if( uri.getHost() == null )
46-
{
47-
throw new IllegalArgumentException( "Invalid URI format `" + uri.toString() + "`");
48-
}
49-
50-
return new BoltServerAddress( uri.getHost(), port );
51-
}
52-
5337
private final String host;
5438
private final int port;
5539

56-
private SocketAddress socketAddress = null; // created lazily if required
40+
public BoltServerAddress( String address )
41+
{
42+
this( uriFrom( address ) );
43+
}
5744

58-
public BoltServerAddress( String host, int port )
45+
public BoltServerAddress( URI uri )
5946
{
60-
this.host = host;
61-
this.port = port;
47+
this( hostFrom( uri ), portFrom( uri ) );
6248
}
6349

64-
public BoltServerAddress( String host )
50+
public BoltServerAddress( String host, int port )
6551
{
66-
int colon = host.indexOf( ':' );
67-
if ( colon >= 0 )
68-
{
69-
this.port = Integer.parseInt( host.substring( colon + 1 ) );
70-
this.host = host.substring( 0, colon );
71-
}
72-
else
73-
{
74-
this.host = host;
75-
this.port = DEFAULT_PORT;
76-
}
52+
this.host = host;
53+
this.port = port;
7754
}
7855

7956
@Override
@@ -145,4 +122,27 @@ public int port()
145122
return port;
146123
}
147124

125+
private static String hostFrom( URI uri )
126+
{
127+
String host = uri.getHost();
128+
if ( host == null )
129+
{
130+
throw new IllegalArgumentException( "Invalid URI format `" + uri.toString() + "`" );
131+
}
132+
return host;
133+
}
134+
135+
private static int portFrom( URI uri )
136+
{
137+
int port = uri.getPort();
138+
return port == -1 ? DEFAULT_PORT : port;
139+
}
140+
141+
private static URI uriFrom( String address )
142+
{
143+
// URI can't parse addresses without scheme, prepend fake "bolt://" to reuse the parsing facility
144+
boolean hasScheme = address.contains( "://" );
145+
String addressWithScheme = hasScheme ? address : "bolt://" + address;
146+
return URI.create( addressWithScheme );
147+
}
148148
}

driver/src/test/java/org/neo4j/driver/internal/DirectDriverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void shouldAllowIPv6Address()
8383

8484
// Given
8585
URI uri = URI.create( "bolt://[::1]" );
86-
BoltServerAddress address = BoltServerAddress.from( uri );
86+
BoltServerAddress address = new BoltServerAddress( uri );
8787

8888
// When
8989
driver = GraphDatabase.driver( uri, neo4j.authToken() );
@@ -115,7 +115,7 @@ public void shouldRegisterSingleServer()
115115
{
116116
// Given
117117
URI uri = URI.create( "bolt://localhost:7687" );
118-
BoltServerAddress address = BoltServerAddress.from( uri );
118+
BoltServerAddress address = new BoltServerAddress( uri );
119119

120120
// When
121121
driver = GraphDatabase.driver( uri, neo4j.authToken() );

driver/src/test/java/org/neo4j/driver/internal/InternalBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.neo4j.driver.internal.summary.InternalServerInfo;
2727
import org.neo4j.driver.internal.summary.InternalSummaryCounters;
2828
import org.neo4j.driver.internal.summary.SummaryBuilder;
29-
import org.neo4j.driver.v1.summary.ResultSummary;
3029
import org.neo4j.driver.v1.Statement;
30+
import org.neo4j.driver.v1.summary.ResultSummary;
3131
import org.neo4j.driver.v1.summary.ServerInfo;
3232
import org.neo4j.driver.v1.summary.SummaryCounters;
3333

@@ -90,7 +90,7 @@ public void shouldObtainStatementAndServerInfoFromSummaryBuilder() throws Throwa
9090
{
9191
// Given
9292
SummaryBuilder builder = new SummaryBuilder( new Statement( "This is a test statement"), new
93-
InternalServerInfo( BoltServerAddress.from( URI.create( "http://neo4j.com" ) ),
93+
InternalServerInfo( new BoltServerAddress( URI.create( "http://neo4j.com" ) ),
9494
"super-awesome" ) );
9595

9696
// When

driver/src/test/java/org/neo4j/driver/internal/net/BoltServerAddressTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.SocketAddress;
2424

2525
import static org.hamcrest.CoreMatchers.equalTo;
26+
import static org.junit.Assert.assertEquals;
2627
import static org.junit.Assert.assertNotSame;
2728
import static org.junit.Assert.assertThat;
2829

@@ -50,4 +51,37 @@ public void shouldAlwaysResolveAddress()
5051

5152
assertNotSame( socketAddress1, socketAddress2 );
5253
}
54+
55+
@Test
56+
public void shouldParseIPv4Addresses()
57+
{
58+
BoltServerAddress address1 = new BoltServerAddress( "127.0.0.1:1234" );
59+
assertEquals( "127.0.0.1", address1.host() );
60+
assertEquals( 1234, address1.port() );
61+
62+
BoltServerAddress address2 = new BoltServerAddress( "8.8.8.8:8080" );
63+
assertEquals( "8.8.8.8", address2.host() );
64+
assertEquals( 8080, address2.port() );
65+
}
66+
67+
@Test
68+
public void shouldParseIPv6Addresses()
69+
{
70+
BoltServerAddress address1 = new BoltServerAddress( "[::1]:7688" );
71+
assertEquals( "[::1]", address1.host() );
72+
assertEquals( 7688, address1.port() );
73+
74+
BoltServerAddress address2 = new BoltServerAddress( "[1afc:0:a33:85a3::ff2f]:9001" );
75+
assertEquals( "[1afc:0:a33:85a3::ff2f]", address2.host() );
76+
assertEquals( 9001, address2.port() );
77+
}
78+
79+
@Test
80+
public void shouldParseBoltAddresses()
81+
{
82+
BoltServerAddress address = new BoltServerAddress( "bolt://host:6565" );
83+
84+
assertEquals( "host", address.host() );
85+
assertEquals( 6565, address.port() );
86+
}
5387
}

driver/src/test/java/org/neo4j/driver/internal/net/SocketConnectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void shouldReceiveServerInfoAfterInit() throws Throwable
6363
SocketClient socket = mock( SocketClient.class );
6464
SocketConnection conn = new SocketConnection( socket, SERVER_INFO, DEV_NULL_LOGGER );
6565

66-
when( socket.address() ).thenReturn( BoltServerAddress.from( URI.create( "http://neo4j.com:9000" ) ) );
66+
when( socket.address() ).thenReturn( new BoltServerAddress( URI.create( "http://neo4j.com:9000" ) ) );
6767

6868
// set up response messages
6969
ArrayList<Message> serverResponses = new ArrayList<>();

driver/src/test/java/org/neo4j/driver/v1/util/Neo4jRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Neo4jRunner
5454
private static final String DEFAULT_NEOCTRL_ARGS = "-e 3.2.0";
5555
public static final String NEOCTRL_ARGS = System.getProperty( "neoctrl.args", DEFAULT_NEOCTRL_ARGS );
5656
public static final URI DEFAULT_URI = URI.create( "bolt://localhost:7687" );
57-
public static final BoltServerAddress DEFAULT_ADDRESS = BoltServerAddress.from( DEFAULT_URI );
57+
public static final BoltServerAddress DEFAULT_ADDRESS = new BoltServerAddress( DEFAULT_URI );
5858

5959
public static final String USER = "neo4j";
6060
public static final String PASSWORD = "password";

driver/src/test/java/org/neo4j/driver/v1/util/cc/Cluster.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ private static BoltServerAddress newBoltServerAddress( URI uri )
380380
{
381381
try
382382
{
383-
return BoltServerAddress.from( uri ).resolve();
383+
return new BoltServerAddress( uri ).resolve();
384384
}
385385
catch ( UnknownHostException e )
386386
{

driver/src/test/java/org/neo4j/driver/v1/util/cc/ClusterMember.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static BoltServerAddress newBoltServerAddress( URI uri )
7575
{
7676
try
7777
{
78-
return BoltServerAddress.from( uri ).resolve();
78+
return new BoltServerAddress( uri ).resolve();
7979
}
8080
catch ( UnknownHostException e )
8181
{

0 commit comments

Comments
 (0)