Fix invalid parsing of not unicode Uri (#59252)
authorMaximys <mixim33@yandex.ru>
Tue, 21 Sep 2021 10:10:21 +0000 (13:10 +0300)
committerGitHub <noreply@github.com>
Tue, 21 Sep 2021 10:10:21 +0000 (03:10 -0700)
* Fix parsing of not unicode Uri (#56482).

* Changing test for reproducing problem with Uri creation by Unicode (#59252).

src/libraries/System.Private.Uri/src/System/Uri.cs
src/libraries/System.Private.Uri/tests/FunctionalTests/UriTests.cs

index 7d1b4d7..b0bda36 100644 (file)
@@ -3922,6 +3922,12 @@ namespace System
 
             Debug.Assert((_flags & Flags.HasUserInfo) == 0 && (_flags & Flags.HostTypeMask) == 0);
 
+            // need to build new Iri'zed string
+            if (hostNotUnicodeNormalized)
+            {
+                newHost = _originalUnicodeString.Substring(0, startInput);
+            }
+
             //Special case is an empty authority
             if (idx == length || ((ch = pString[idx]) == '/' || (ch == '\\' && StaticIsFile(syntax)) || ch == '#' || ch == '?'))
             {
@@ -3944,12 +3950,6 @@ namespace System
                 return idx;
             }
 
-            // need to build new Iri'zed string
-            if (hostNotUnicodeNormalized)
-            {
-                newHost = _originalUnicodeString.Substring(0, startInput);
-            }
-
             string? userInfoString = null;
             // Attempt to parse user info first
 
index 2b2222f..2656fe9 100644 (file)
@@ -778,6 +778,13 @@ namespace System.PrivateUri.Tests
             Assert.False(timedOut);
         }
 
+        [Fact]
+        public static void UriWithUnicodeAndEmptyAuthority_ParsedCorrectly()
+        {
+            const string UriString = "custom:///\u00FC";
+            Assert.Equal(UriString, new Uri(UriString, UriKind.Absolute).ToString());
+        }
+
         public static IEnumerable<object[]> FilePathHandlesNonAscii_TestData()
         {
             if (PlatformDetection.IsNotWindows)