diff --git a/Genuine Channels/Sources/GenuineUtility.cs b/Genuine Channels/Sources/GenuineUtility.cs index 9ee19eb..f1e65c4 100755 --- a/Genuine Channels/Sources/GenuineUtility.cs +++ b/Genuine Channels/Sources/GenuineUtility.cs @@ -144,15 +144,15 @@ public static string SplitToHostAndPort(string url, out int port) { port = 0; - if (string.IsNullOrEmpty(url)) - return null; - - Uri parsed; - if (Uri.TryCreate(url, UriKind.Absolute, out parsed)) - { - port = parsed.IsDefaultPort ? 0 : parsed.Port; - return parsed.DnsSafeHost.Replace("%25", "%").TrimStart('[').TrimEnd(']'); - } + if (string.IsNullOrEmpty(url)) + return null; + + Uri parsed; + if (Uri.TryCreate(url, UriKind.Absolute, out parsed)) + { + port = parsed.IsDefaultPort ? 0 : parsed.Port; + return parsed.DnsSafeHost.Replace("%25", "%").TrimStart('[').TrimEnd(']'); + } Match match = _UrlToHost.Match(url); if (!match.Success) diff --git a/Genuine Channels/Tests/UnitTests/GenuineUtilityTests.cs b/Genuine Channels/Tests/UnitTests/GenuineUtilityTests.cs index f8c41a3..20bc7f8 100644 --- a/Genuine Channels/Tests/UnitTests/GenuineUtilityTests.cs +++ b/Genuine Channels/Tests/UnitTests/GenuineUtilityTests.cs @@ -70,32 +70,32 @@ public void SplitToHostAndPort_IPv6() RunSplitToHostAndPortWithInputDictionary(testDict); } - [TestMethod,Description("Tests with Uris build using the .NET UriBuilder")] - public void SplitToHostAndPort_IPv6_UriSyntax() - { - // as described in RFC 6874 (https://tools.ietf.org/html/rfc6874): - // Used within a URI, a literal IPv6 address is always embedded between "[" and "]" - // Zone IDs have to be URL encoded ("%" ==> "%25"). - var testDict = new Dictionary> - { - { "gtcp://[::1]:1233", new Tuple("::1", 1233)}, - { "gtcp://[fec0:0:0:ffff::1]:1234", new Tuple("fec0:0:0:ffff::1", 1234)}, - { "gtcp://[fec0:0:0:ffff::1%251]:1235", new Tuple("fec0:0:0:ffff::1%1", 1235)}, - { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]:1236", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 1236)}, - { "gtcp://[::1]", new Tuple("::1", 0)}, - { "gtcp://[fec0:0:0:ffff::1%251]", new Tuple("fec0:0:0:ffff::1%1", 0)}, - { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 0)}, - { "gtcp://[::1]:1233/subdomain", new Tuple("::1", 1233)}, - { "gtcp://[fec0:0:0:ffff::1]:1234/a/b/c", new Tuple("fec0:0:0:ffff::1", 1234)}, - { "gtcp://[fec0:0:0:ffff::1%251]:1235/index.rem", new Tuple("fec0:0:0:ffff::1%1", 1235)}, - { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]:1236/service", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 1236)}, - { "gtcp://[::1]/myservices", new Tuple("::1", 0)}, - { "gtcp://[fec0:0:0:ffff::1%251]/a/b/c", new Tuple("fec0:0:0:ffff::1%1", 0)}, - { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]/index.rem", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 0)} - }; + [TestMethod, Description("Tests with Uris build using the .NET UriBuilder")] + public void SplitToHostAndPort_IPv6_UriSyntax() + { + // as described in RFC 6874 (https://tools.ietf.org/html/rfc6874): + // Used within a URI, a literal IPv6 address is always embedded between "[" and "]" + // Zone IDs have to be URL encoded ("%" ==> "%25"). + var testDict = new Dictionary> + { + { "gtcp://[::1]:1233", new Tuple("::1", 1233)}, + { "gtcp://[fec0:0:0:ffff::1]:1234", new Tuple("fec0:0:0:ffff::1", 1234)}, + { "gtcp://[fec0:0:0:ffff::1%251]:1235", new Tuple("fec0:0:0:ffff::1%1", 1235)}, + { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]:1236", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 1236)}, + { "gtcp://[::1]", new Tuple("::1", 0)}, + { "gtcp://[fec0:0:0:ffff::1%251]", new Tuple("fec0:0:0:ffff::1%1", 0)}, + { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 0)}, + { "gtcp://[::1]:1233/subdomain", new Tuple("::1", 1233)}, + { "gtcp://[fec0:0:0:ffff::1]:1234/a/b/c", new Tuple("fec0:0:0:ffff::1", 1234)}, + { "gtcp://[fec0:0:0:ffff::1%251]:1235/index.rem", new Tuple("fec0:0:0:ffff::1%1", 1235)}, + { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]:1236/service", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 1236)}, + { "gtcp://[::1]/myservices", new Tuple("::1", 0)}, + { "gtcp://[fec0:0:0:ffff::1%251]/a/b/c", new Tuple("fec0:0:0:ffff::1%1", 0)}, + { "gtcp://[fe80::1d2b:147c:c69a:26ef%2530]/index.rem", new Tuple("fe80::1d2b:147c:c69a:26ef%30", 0)} + }; - RunSplitToHostAndPortWithInputDictionary(testDict); - } + RunSplitToHostAndPortWithInputDictionary(testDict); + } private static void RunSplitToHostAndPortWithInputDictionary(Dictionary> testDict) {