diff --git a/src/Csharpc/corelib/Net/Uri.cs b/src/Csharpc/corelib/Net/Uri.cs index 509eae37..7914e3d0 100644 --- a/src/Csharpc/corelib/Net/Uri.cs +++ b/src/Csharpc/corelib/Net/Uri.cs @@ -146,7 +146,10 @@ private static bool IsWindowsRootedPath(string s) => private static void CheckRelative(string s) { // '#' starts a fragment: .NET keeps it as it is, but calls such a reference not well formed. - if (s.Length != 0 && (s.Trim().Length == 0 || !IsPlain(s.Replace('#', '/'), 0, true))) throw Unsupported("this relative URI"); + // Spaces and backslashes too: .NET keeps them as they are in OriginalString and ToString() of a relative reference + // (a path such as ".../eM Client.app/Contents/..." or Windows' "dir with space\p.xml", which XmlResolver then makes + // absolute with Path.GetFullPath). Resolving such a reference against a base stays unsupported. + if (s.Length != 0 && (s.Trim().Length == 0 || !IsPlain(s.Replace('#', '/').Replace(' ', '/').Replace('\\', '/'), 0, true))) throw Unsupported("this relative URI"); } /// @@ -183,8 +186,12 @@ private static bool TryParseFile(string s, out string path, out string localPath } #endif string slashed = drive ? rest.Replace('\\', '/') : rest; - if (!IsPlain(slashed, 0, false) || HasDotSegment(slashed) || slashed.Contains("//")) throw Unsupported("file paths .NET escapes or rewrites"); - path = drive ? "/" + slashed : slashed; + // Spaces in an implicit path (".../eM Client.app/..."): .NET escapes them to %20 in the URI's path and AbsoluteUri, + // and keeps them in LocalPath and ToString(). No other character that .NET escapes is modelled ('%' stays refused, + // so ToString's unescaping of %20 is exact). + string checkedPath = explicitScheme ? slashed : slashed.Replace(' ', '/'); + if (!IsPlain(checkedPath, 0, false) || HasDotSegment(slashed) || slashed.Contains("//")) throw Unsupported("file paths .NET escapes or rewrites"); + path = (drive ? "/" + slashed : slashed).Replace(" ", "%20"); localPath = drive ? slashed.Replace('/', '\\') : slashed; return true; } @@ -486,7 +493,7 @@ public string GetLeftPart(UriPartial part) private static int DefaultPort(string scheme) => scheme switch { "http" => 80, "https" => 443, "ftp" => 21, "ws" => 80, "wss" => 443, _ => -1 }; - public override string ToString() => _relative ? _original : AbsoluteUri; + public override string ToString() => _relative ? _original : _file ? AbsoluteUri.Replace("%20", " ") : AbsoluteUri; // .NET compares the scheme, host, port, path and query (the scheme and host case-insensitively, // which the lowercased fields already are) and ignores the fragment; a string is parsed first. diff --git a/src/Csharpc/corelib/Net/UriRelative.cs b/src/Csharpc/corelib/Net/UriRelative.cs index 422aaa01..5033d208 100644 --- a/src/Csharpc/corelib/Net/UriRelative.cs +++ b/src/Csharpc/corelib/Net/UriRelative.cs @@ -276,9 +276,9 @@ public static int Compare(Uri uri1, Uri uri2, UriComponents partsToCompare, UriF // ---- well-formedness, formatting, host names ---- - // For this subset a reference is well formed unless it is an implicit file path or a relative reference - // with a fragment. - public bool IsWellFormedOriginalString() => _relative ? _original.IndexOf('#') < 0 : !_implicitFile; + // For this subset a reference is well formed unless it is an implicit file path or a relative reference with a + // fragment, a space or a backslash (characters .NET would have to escape; Uri.cs keeps them only in relative references). + public bool IsWellFormedOriginalString() => _relative ? _original.AsSpan().IndexOfAny('#', ' ', '\\') < 0 : !_implicitFile; public static bool IsWellFormedUriString(string uriString, UriKind uriKind) { diff --git a/tests/samples/1344_uri_paths_with_spaces.cs b/tests/samples/1344_uri_paths_with_spaces.cs new file mode 100644 index 00000000..7097a3fc --- /dev/null +++ b/tests/samples/1344_uri_paths_with_spaces.cs @@ -0,0 +1,50 @@ +// Paths with spaces as URIs: a relative reference keeps them as they are (OriginalString, ToString); an implicit file +// path escapes them to %20 in AbsoluteUri/AbsolutePath and keeps them in LocalPath and ToString(). XmlReader.Create(path) +// goes through both (XmlUrlResolver: a relative Uri, then Path.GetFullPath). +using System; +using System.IO; +using System.Xml; + +static class Program +{ + static void Main() + { + var relative = new Uri("dir with space/p.xml", UriKind.RelativeOrAbsolute); + Console.WriteLine($"{relative.IsAbsoluteUri} [{relative.OriginalString}] [{relative}]"); + Console.WriteLine(Uri.TryCreate("a b/c", UriKind.Relative, out var tried) ? $"relative [{tried}]" : "not relative"); + // Windows' Path.Combine gives backslashes: kept as they are too. Spaces, backslashes and '#' make a relative + // reference not well formed (characters .NET would have to escape). + var backslashed = new Uri("dir with space\\p.xml", UriKind.RelativeOrAbsolute); + Console.WriteLine($"{backslashed.IsAbsoluteUri} [{backslashed.OriginalString}] [{backslashed}]"); + foreach (var text in new[] { "a b", "a#b", "a/b", "a\\b" }) + Console.WriteLine($"{text}: {Uri.IsWellFormedUriString(text, UriKind.Relative)} {Uri.IsWellFormedUriString(text, UriKind.RelativeOrAbsolute)} {new Uri(text, UriKind.Relative).IsWellFormedOriginalString()}"); + + var root = Path.Combine(Path.GetTempPath(), "csharpc uri sample " + Environment.ProcessId); + var dir = Path.Combine(root, "dir with space"); + Directory.CreateDirectory(dir); + try + { + var file = Path.Combine(dir, "p.xml"); + File.WriteAllText(file, "hi"); + + var absolute = new Uri(file); + Console.WriteLine(absolute.IsAbsoluteUri + " " + absolute.IsFile + " " + absolute.Scheme); + Console.WriteLine(absolute.AbsoluteUri.EndsWith("/dir%20with%20space/p.xml", StringComparison.Ordinal)); + Console.WriteLine(absolute.AbsolutePath.EndsWith("/dir%20with%20space/p.xml", StringComparison.Ordinal)); + Console.WriteLine(absolute.LocalPath == file); + Console.WriteLine(absolute.ToString().EndsWith("/dir with space/p.xml", StringComparison.Ordinal)); + Console.WriteLine(absolute.Segments[^2] + absolute.Segments[^1]); + + Directory.SetCurrentDirectory(root); + using var reader = XmlReader.Create(Path.Combine("dir with space", "p.xml")); + reader.MoveToContent(); + Console.WriteLine($"{reader.Name} {reader.GetAttribute("a")} {reader.ReadInnerXml()}"); + Console.WriteLine(reader.BaseURI.EndsWith("/dir with space/p.xml", StringComparison.Ordinal)); + } + finally + { + Directory.SetCurrentDirectory(Path.GetTempPath()); + Directory.Delete(root, true); + } + } +} diff --git a/tests/samples/1344_uri_paths_with_spaces.expected b/tests/samples/1344_uri_paths_with_spaces.expected new file mode 100644 index 00000000..9781fd1a --- /dev/null +++ b/tests/samples/1344_uri_paths_with_spaces.expected @@ -0,0 +1,15 @@ +False [dir with space/p.xml] [dir with space/p.xml] +relative [a b/c] +False [dir with space\p.xml] [dir with space\p.xml] +a b: False False False +a#b: False False False +a/b: True True True +a\b: False False False +True True file +True +True +True +True +dir%20with%20space/p.xml +root 1 hi +True