Skip to content

Commit

Permalink
NCBC-896: Support translation between Hostname and IPAddress
Browse files Browse the repository at this point in the history
Motivation
----------
The IPEndpointExtensions.GetServer(server) method, unlike the other
overloads, did not correctly handle the case where the passed in hostname
was not an IPAddress. This patch will correctly resolve the IPAddress of
the hostname.

Modifications
-------------
Updated IPEndpointExtensions.GetServer(server) so that if the value of
server is a hostname, the IPAddress will be resolved from it.

Results
-------
If a cluster is configured to use a hostname as opposed to a IPAddress,
the SDK will correctly resolve it.

Change-Id: I9de54a354c5f9e5952a0405696c92e596d01017d
Reviewed-on: http://review.couchbase.org/51237
Tested-by: Jeffry Morris <jeffrymorris@gmail.com>
Reviewed-by: Simon Baslé <simon@couchbase.com>
  • Loading branch information
jeffrymorris committed May 20, 2015
1 parent 354e20a commit 2e4faf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Src/Couchbase.Tests/Utils/IPEndpointExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,14 @@ public void When_NodeAdapter_And_UseSsl_Is_True_IPEndPoint_Uses_Port_11210()
var actual = IPEndPointExtensions.GetEndPoint(serverConfig.GetNodes()[0], clientConfig, serverConfig);
Assert.AreEqual(expected, actual.ToString());
}

[Test]
public void When_Hostname_Is_Provided_IPAdress_Is_Returned()
{
var hostname = "localhost:8091";
var expected = "127.0.0.1";
var actual = IPEndPointExtensions.GetEndPoint(hostname);
Assert.AreEqual(expected, actual.Address.ToString());
}
}
}
7 changes: 6 additions & 1 deletion Src/Couchbase/Utils/IPEndPointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public static IPEndPoint GetEndPoint(string server)
IPAddress ipAddress;
if (!IPAddress.TryParse(address[0], out ipAddress))
{
throw new ArgumentException("ipAddress");
var uri = new Uri(String.Format("http://{0}", address[0]));
ipAddress = uri.GetIpAddress();
if (ipAddress == null)
{
throw new ArgumentException("ipAddress");
}
}
int port;
if (!int.TryParse(address[1], out port))
Expand Down

0 comments on commit 2e4faf8

Please sign in to comment.