From 40fb71fc2b098bcb3953eb1f057b42bc29655932 Mon Sep 17 00:00:00 2001 From: AnakinRaW Date: Sat, 10 Aug 2024 13:31:28 +0200 Subject: [PATCH 1/3] support ros for IsChildOf --- .../src/Extensions/PathExtensions.cs | 29 +++++++++++++++---- .../test/PathExtensionsTest.IsChildOf.cs | 29 ++++++++++++++++++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/CommonUtilities.FileSystem/src/Extensions/PathExtensions.cs b/src/CommonUtilities.FileSystem/src/Extensions/PathExtensions.cs index 4462f33..e9e4cb1 100644 --- a/src/CommonUtilities.FileSystem/src/Extensions/PathExtensions.cs +++ b/src/CommonUtilities.FileSystem/src/Extensions/PathExtensions.cs @@ -299,7 +299,7 @@ public static bool AreEqual(this IPath _, string pathA, string pathB) internal static bool PathsEqual(string path1, string path2) { - return PathsEqual(path1, path2, Math.Max(path1.Length, path2.Length)); + return PathsEqual(path1.AsSpan(), path2.AsSpan(), Math.Max(path1.Length, path2.Length)); } private static void CombinePathsUnchecked(ref ValueStringBuilder sb, string relativePath) @@ -318,7 +318,7 @@ private static void CombinePathsUnchecked(ref ValueStringBuilder sb, string rela /// /// True if the two paths are the same. (but only up to the specified length) /// - private static bool PathsEqual(string path1, string path2, int length) + private static bool PathsEqual(ReadOnlySpan path1, ReadOnlySpan path2, int length) { if (path1.Length < length || path2.Length < length) return false; @@ -353,11 +353,28 @@ public static bool IsChildOf(this IPath _, string basePath, string candidate) { var fullBase = _.GetFullPath(basePath); var fullCandidate = _.GetFullPath(candidate); + return _.IsChildOf(fullBase.AsSpan(), fullCandidate.AsSpan()); + } - return fullBase.Length > 0 - && fullCandidate.Length > fullBase.Length - && PathsEqual(fullCandidate, fullBase, fullBase.Length) - && (IsAnyDirectorySeparator(fullBase[fullBase.Length - 1]) || IsAnyDirectorySeparator(fullCandidate[fullBase.Length])); + /// + /// Determines whether a specified candidate path is a real child path to a specified base path. + /// + /// + /// The base path- + /// The relative path candidate. + /// if is a child path to ; otherwise, . + /// or are not fully qualified. + public static bool IsChildOf(this IPath _, ReadOnlySpan basePath, ReadOnlySpan candidate) + { + if (!_.IsPathFullyQualified(basePath)) + throw new ArgumentException("candidate must be fully qualified", nameof(basePath)); + if (!_.IsPathFullyQualified(candidate)) + throw new ArgumentException("candidate must be fully qualified", nameof(candidate)); + + return basePath.Length > 0 + && candidate.Length > basePath.Length + && PathsEqual(candidate, basePath, basePath.Length) + && (IsAnyDirectorySeparator(basePath[basePath.Length - 1]) || IsAnyDirectorySeparator(candidate[basePath.Length])); } internal static bool IsValidDriveChar(char value) diff --git a/src/CommonUtilities.FileSystem/test/PathExtensionsTest.IsChildOf.cs b/src/CommonUtilities.FileSystem/test/PathExtensionsTest.IsChildOf.cs index b0928ff..017805d 100644 --- a/src/CommonUtilities.FileSystem/test/PathExtensionsTest.IsChildOf.cs +++ b/src/CommonUtilities.FileSystem/test/PathExtensionsTest.IsChildOf.cs @@ -1,4 +1,5 @@ -using System.IO.Abstractions; +using System; +using System.IO.Abstractions; using AnakinRaW.CommonUtilities.Testing; using Testably.Abstractions.Testing; using Xunit; @@ -35,6 +36,10 @@ public void TestIsChild_Windows(string basePath, string candidate, bool expected _fileSystem.Initialize().WithSubdirectory("C:\\current"); _fileSystem.Directory.SetCurrentDirectory("C:\\current"); Assert.Equal(expected, _fileSystem.Path.IsChildOf(basePath, candidate)); + Assert.Equal( + expected, + _fileSystem.Path.IsChildOf(_fileSystem.Path.GetFullPath(basePath).AsSpan(), + _fileSystem.Path.GetFullPath(candidate).AsSpan())); } [PlatformSpecificTheory(TestPlatformIdentifier.Linux)] @@ -49,5 +54,27 @@ public void TestIsChild_Linux(string basePath, string candidate, bool expected) _fileSystem.Initialize().WithSubdirectory("/current"); _fileSystem.Directory.SetCurrentDirectory("/current"); Assert.Equal(expected, _fileSystem.Path.IsChildOf(basePath, candidate)); + Assert.Equal( + expected, + _fileSystem.Path.IsChildOf(_fileSystem.Path.GetFullPath(basePath).AsSpan(), + _fileSystem.Path.GetFullPath(candidate).AsSpan())); + } + + [PlatformSpecificTheory(TestPlatformIdentifier.Linux)] + [InlineData("test", "/")] + [InlineData("/", "test")] + [InlineData("test", "test")] + public void TestIsChild_NoFullyQualifiedPathsForROS_Linux(string basePath, string candidate) + { + Assert.Throws(() => _fileSystem.Path.IsChildOf(basePath.AsSpan(), candidate.AsSpan())); + } + + [PlatformSpecificTheory(TestPlatformIdentifier.Windows)] + [InlineData("test", "test")] + [InlineData("C:/test", "test")] + [InlineData("test", "C:/test")] + public void TestIsChild_NoFullyQualifiedPathsForROS_Windows(string basePath, string candidate) + { + Assert.Throws(() => _fileSystem.Path.IsChildOf(basePath.AsSpan(), candidate.AsSpan())); } } \ No newline at end of file From 6048844136466177678e21dbb7358a51599e66a0 Mon Sep 17 00:00:00 2001 From: AnakinRaW Date: Sat, 10 Aug 2024 13:31:32 +0200 Subject: [PATCH 2/3] udpate deps --- Directory.Build.props | 2 +- .../test/CommonUtilities.DownloadManager.Test.csproj | 8 ++++---- .../src/Commonutilities.FileSystem.csproj | 2 +- .../test/CommonUtilities.FileSystem.Test.csproj | 8 ++++---- .../test/CommonUtilities.Registry.Test.csproj | 4 ++-- .../test/CommonUtilities.SimplePipeline.Test.csproj | 6 +++--- .../CommonUtilities.TestingUtilities.csproj | 4 ++-- src/CommonUtilities/src/CommonUtilities.csproj | 2 +- src/CommonUtilities/test/CommonUtilities.Test.csproj | 8 ++++---- 9 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index ba95efb..2087a1c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -25,7 +25,7 @@ all - 3.6.139 + 3.6.141 diff --git a/src/CommonUtilities.DownloadManager/test/CommonUtilities.DownloadManager.Test.csproj b/src/CommonUtilities.DownloadManager/test/CommonUtilities.DownloadManager.Test.csproj index e884ab0..e34447d 100644 --- a/src/CommonUtilities.DownloadManager/test/CommonUtilities.DownloadManager.Test.csproj +++ b/src/CommonUtilities.DownloadManager/test/CommonUtilities.DownloadManager.Test.csproj @@ -21,10 +21,10 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/CommonUtilities.FileSystem/src/Commonutilities.FileSystem.csproj b/src/CommonUtilities.FileSystem/src/Commonutilities.FileSystem.csproj index 0d1d42f..2a8ed1c 100644 --- a/src/CommonUtilities.FileSystem/src/Commonutilities.FileSystem.csproj +++ b/src/CommonUtilities.FileSystem/src/Commonutilities.FileSystem.csproj @@ -26,7 +26,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all diff --git a/src/CommonUtilities.FileSystem/test/CommonUtilities.FileSystem.Test.csproj b/src/CommonUtilities.FileSystem/test/CommonUtilities.FileSystem.Test.csproj index 55a109a..c66e8e8 100644 --- a/src/CommonUtilities.FileSystem/test/CommonUtilities.FileSystem.Test.csproj +++ b/src/CommonUtilities.FileSystem/test/CommonUtilities.FileSystem.Test.csproj @@ -28,10 +28,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/CommonUtilities.Registry/test/CommonUtilities.Registry.Test.csproj b/src/CommonUtilities.Registry/test/CommonUtilities.Registry.Test.csproj index b3e7e32..d6a6df4 100644 --- a/src/CommonUtilities.Registry/test/CommonUtilities.Registry.Test.csproj +++ b/src/CommonUtilities.Registry/test/CommonUtilities.Registry.Test.csproj @@ -18,8 +18,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/CommonUtilities.SimplePipeline/test/CommonUtilities.SimplePipeline.Test.csproj b/src/CommonUtilities.SimplePipeline/test/CommonUtilities.SimplePipeline.Test.csproj index a6f2cf3..e5b2da1 100644 --- a/src/CommonUtilities.SimplePipeline/test/CommonUtilities.SimplePipeline.Test.csproj +++ b/src/CommonUtilities.SimplePipeline/test/CommonUtilities.SimplePipeline.Test.csproj @@ -21,10 +21,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/CommonUtilities.TestingUtilities/CommonUtilities.TestingUtilities.csproj b/src/CommonUtilities.TestingUtilities/CommonUtilities.TestingUtilities.csproj index 1933daa..cfc3e8f 100644 --- a/src/CommonUtilities.TestingUtilities/CommonUtilities.TestingUtilities.csproj +++ b/src/CommonUtilities.TestingUtilities/CommonUtilities.TestingUtilities.csproj @@ -22,8 +22,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/CommonUtilities/src/CommonUtilities.csproj b/src/CommonUtilities/src/CommonUtilities.csproj index aef71bb..d59e034 100644 --- a/src/CommonUtilities/src/CommonUtilities.csproj +++ b/src/CommonUtilities/src/CommonUtilities.csproj @@ -28,7 +28,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/CommonUtilities/test/CommonUtilities.Test.csproj b/src/CommonUtilities/test/CommonUtilities.Test.csproj index 1e07088..9c610c4 100644 --- a/src/CommonUtilities/test/CommonUtilities.Test.csproj +++ b/src/CommonUtilities/test/CommonUtilities.Test.csproj @@ -22,10 +22,10 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all From aca5c03336565b9f5f6f9812a9042f99d26da566 Mon Sep 17 00:00:00 2001 From: AnakinRaW Date: Sat, 10 Aug 2024 15:57:42 +0200 Subject: [PATCH 3/3] support github downloads --- .../src/Providers/HttpClientDownloader.cs | 9 +++------ .../src/Providers/WebClientDownloader.cs | 12 +++++++++++- .../test/DownloadManagerIntegrationTest.cs | 12 ++++++------ .../test/Providers/HttpClientDownloadTest.cs | 2 +- .../test/Providers/WebClientDownloadTest.cs | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/CommonUtilities.DownloadManager/src/Providers/HttpClientDownloader.cs b/src/CommonUtilities.DownloadManager/src/Providers/HttpClientDownloader.cs index 8f853ce..62be124 100644 --- a/src/CommonUtilities.DownloadManager/src/Providers/HttpClientDownloader.cs +++ b/src/CommonUtilities.DownloadManager/src/Providers/HttpClientDownloader.cs @@ -45,11 +45,8 @@ protected override async Task DownloadAsyncCore(Uri uri, Stream #else using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); #endif - var contentLengthData = response.Content.Headers.ContentLength; - if (contentLengthData is null or 0L) - throw new IOException("Error: Response stream length is 0."); - - var contentLength = contentLengthData.Value; + var contentLengthData = response.Content.Headers.ContentLength ?? 0; + var contentLength = contentLengthData; var requestRegistration = cancellationToken.Register(webRequest.Dispose); try @@ -98,7 +95,7 @@ private static HttpRequestMessage CreateRequest(Uri uri) { var request = new HttpRequestMessage(HttpMethod.Get, uri); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); - request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("defalte")); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); return request; } diff --git a/src/CommonUtilities.DownloadManager/src/Providers/WebClientDownloader.cs b/src/CommonUtilities.DownloadManager/src/Providers/WebClientDownloader.cs index 5fc31d5..268528a 100644 --- a/src/CommonUtilities.DownloadManager/src/Providers/WebClientDownloader.cs +++ b/src/CommonUtilities.DownloadManager/src/Providers/WebClientDownloader.cs @@ -42,6 +42,9 @@ protected override async Task DownloadAsyncCore(Uri uri, Stream { try { +#if NET || NETSTANDARD2_1 + await +#endif using var responseStream = webResponse.GetResponseStream(); var contentLength = webResponse.Headers["Content-Length"]; if (string.IsNullOrEmpty(contentLength)) @@ -50,7 +53,7 @@ protected override async Task DownloadAsyncCore(Uri uri, Stream if (totalStreamLength.Equals(0L)) throw new IOException("Error: Response stream length is 0."); - var requestRegistration = cancellationToken.Register(() => webRequest!.Abort()); + var requestRegistration = cancellationToken.Register(webRequest.Abort); try { summary.DownloadedSize = await StreamUtilities.CopyStreamWithProgressAsync(responseStream, @@ -60,7 +63,11 @@ protected override async Task DownloadAsyncCore(Uri uri, Stream } finally { +#if NET || NETSTANDARD2_1 + await requestRegistration.DisposeAsync(); +#else requestRegistration.Dispose(); +#endif } } catch (WebException ex) @@ -110,6 +117,9 @@ private static HttpWebRequest CreateRequest(Uri uri) var success = false; try { +#if NET || NETSTANDARD2_1 + await +#endif using (cancellationToken.Register(webRequest.Abort)) httpWebResponse = (HttpWebResponse)await webRequest.GetResponseAsync().ConfigureAwait(false); diff --git a/src/CommonUtilities.DownloadManager/test/DownloadManagerIntegrationTest.cs b/src/CommonUtilities.DownloadManager/test/DownloadManagerIntegrationTest.cs index d66920f..6fa4b96 100644 --- a/src/CommonUtilities.DownloadManager/test/DownloadManagerIntegrationTest.cs +++ b/src/CommonUtilities.DownloadManager/test/DownloadManagerIntegrationTest.cs @@ -58,11 +58,11 @@ public async Task Test_DownloadAsync_DownloadWithHttpClient() var progressTriggered = false; - var summary = await manager.DownloadAsync(new Uri("http://speedtest.ftp.otenet.gr/files/test10Mb.db"), file, ProgressMethod, + var summary = await manager.DownloadAsync(new Uri("https://raw.githubusercontent.com/BitDoctor/speed-test-file/master/5mb.txt"), file, ProgressMethod, null, CancellationToken.None); - Assert.Equal(10 * 1024 * 1024, summary.DownloadedSize); - Assert.Equal(10 * 1024 * 1024, file.Length); + Assert.Equal(5 * 1024 * 1024, summary.DownloadedSize); + Assert.Equal(5 * 1024 * 1024, file.Length); Assert.True(progressTriggered); void ProgressMethod(ProgressUpdateStatus status) @@ -193,11 +193,11 @@ public async Task Test_DownloadAsync_DownloadWithWebClient() var progressTriggered = false; - var summary = await manager.DownloadAsync(new Uri("http://speedtest.ftp.otenet.gr/files/test10Mb.db"), file, ProgressMethod, + var summary = await manager.DownloadAsync(new Uri("https://raw.githubusercontent.com/BitDoctor/speed-test-file/master/5mb.txt"), file, ProgressMethod, null, CancellationToken.None); - Assert.Equal(10 * 1024 * 1024, summary.DownloadedSize); - Assert.Equal(10 * 1024 * 1024, file.Length); + Assert.Equal(5 * 1024 * 1024, summary.DownloadedSize); + Assert.Equal(5 * 1024 * 1024, file.Length); Assert.True(progressTriggered); void ProgressMethod(ProgressUpdateStatus status) diff --git a/src/CommonUtilities.DownloadManager/test/Providers/HttpClientDownloadTest.cs b/src/CommonUtilities.DownloadManager/test/Providers/HttpClientDownloadTest.cs index 5c1bfb7..cf4ede6 100644 --- a/src/CommonUtilities.DownloadManager/test/Providers/HttpClientDownloadTest.cs +++ b/src/CommonUtilities.DownloadManager/test/Providers/HttpClientDownloadTest.cs @@ -41,7 +41,7 @@ public async Task Test_DownloadAsync_Download() { var outStream = new MemoryStream(); var result = await _provider.DownloadAsync( - new Uri("http://speedtest.ftp.otenet.gr/files/test100k.db"), + new Uri("https://raw.githubusercontent.com/AnakinRaW/CommonUtilities/2ab2e6a26872974422459b0605b26222c9e126ca/README.md"), outStream, null, CancellationToken.None); Assert.True(result.DownloadedSize > 0); Assert.Equal(result.DownloadedSize, outStream.Length); diff --git a/src/CommonUtilities.DownloadManager/test/Providers/WebClientDownloadTest.cs b/src/CommonUtilities.DownloadManager/test/Providers/WebClientDownloadTest.cs index c99de5e..0efd393 100644 --- a/src/CommonUtilities.DownloadManager/test/Providers/WebClientDownloadTest.cs +++ b/src/CommonUtilities.DownloadManager/test/Providers/WebClientDownloadTest.cs @@ -37,7 +37,7 @@ public async Task Test_DownloadAsync_Download() { var outStream = new MemoryStream(); var result = await _provider.DownloadAsync( - new Uri("http://speedtest.ftp.otenet.gr/files/test100k.db"), + new Uri("https://raw.githubusercontent.com/AnakinRaW/CommonUtilities/2ab2e6a26872974422459b0605b26222c9e126ca/README.md"), outStream, null, CancellationToken.None); Assert.True(result.DownloadedSize > 0); Assert.Equal(result.DownloadedSize, outStream.Length);