Skip to content

Commit

Permalink
Add parsing of zero port for unknown Uri scheme (#40986)
Browse files Browse the repository at this point in the history
Fixes #37865
  • Loading branch information
CarnaViire authored Aug 18, 2020
1 parent a949686 commit 154e971
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Private.Uri/src/System/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ private unsafe void CreateUriInfo(Flags cF)
}
}
}
if (notEmpty && info.Offset.PortValue != port)
if (notEmpty && _syntax.DefaultPort != port)
{
info.Offset.PortValue = (ushort)port;
cF |= Flags.NotDefaultPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void GetComponents_UnknownScheme_ComponentsUnaffected()
testUri = new Uri("abc://127.00.000.001:01234");
Assert.Equal("127.00.000.001:1234", testUri.Authority);

// Known limitation: port 0 is ignored.
// Port 0 is parsed correctly.
testUri = new Uri("cbd://127.00.1.2:0000");
Assert.Equal("127.00.1.2", testUri.Authority);
Assert.Equal("127.00.1.2:0", testUri.Authority);
testUri = new Uri("cbd://127.00.1.2:0");
Assert.Equal("127.00.1.2", testUri.Authority);
Assert.Equal("127.00.1.2:0", testUri.Authority);

testUri = new Uri("eb://[0000::01:123.32.123.23]/dir");
Assert.Equal("[::1:7b20:7b17]", testUri.Authority);
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,23 @@ public static void FilePathHandlesNonAscii(string uriString, string toString, st
Assert.Equal(absoluteUri, uri2.AbsoluteUri);
Assert.Equal(localPath, uri2.LocalPath);
}

public static IEnumerable<object[]> ZeroPortIsParsedForBothKnownAndUnknownSchemes_TestData()
{
yield return new object[] { "http://example.com:0", 0, false };
yield return new object[] { "http://example.com", 80, true };
yield return new object[] { "rtsp://example.com:0", 0, false };
yield return new object[] { "rtsp://example.com", -1, true };
}

[Theory]
[MemberData(nameof(ZeroPortIsParsedForBothKnownAndUnknownSchemes_TestData))]
public static void ZeroPortIsParsedForBothKnownAndUnknownSchemes(string uriString, int port, bool isDefaultPort)
{
Uri.TryCreate(uriString, UriKind.Absolute, out var uri);
Assert.Equal(port, uri.Port);
Assert.Equal(isDefaultPort, uri.IsDefaultPort);
Assert.Equal(uriString + "/", uri.ToString());
}
}
}

0 comments on commit 154e971

Please sign in to comment.