diff --git a/src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs b/src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs index 48c07d7e..e7794764 100644 --- a/src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs +++ b/src/NuGetUtility/LicenseValidator/LicenseDownloadException.cs @@ -7,10 +7,10 @@ namespace NuGetUtility.LicenseValidator { public class LicenseDownloadException : Exception { - public LicenseDownloadException(Exception inner, string context, PackageIdentity packageInfo) + public LicenseDownloadException(Exception inner, string context, Uri url, PackageIdentity packageInfo) : base( - $"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}).\nContext: {context}", + $"Failed to download license for package {packageInfo.Id} ({packageInfo.Version}) from url: {url}.\nContext: {context}", inner) { } } diff --git a/src/NuGetUtility/LicenseValidator/LicenseValidator.cs b/src/NuGetUtility/LicenseValidator/LicenseValidator.cs index db924b5e..a2c943dd 100644 --- a/src/NuGetUtility/LicenseValidator/LicenseValidator.cs +++ b/src/NuGetUtility/LicenseValidator/LicenseValidator.cs @@ -68,7 +68,7 @@ private bool IsIgnoredPackage(PackageIdentity identity) return Array.Exists(_ignoredPackages, ignored => identity.Id.Like(ignored)); } - private void AddOrUpdateLicense( + private static void AddOrUpdateLicense( ConcurrentDictionary result, IPackageMetadata info, LicenseInformationOrigin origin, @@ -90,7 +90,7 @@ private void AddOrUpdateLicense( (key, oldValue) => UpdateResult(oldValue, newValue)); } - private void AddOrUpdateLicense( + private static void AddOrUpdateLicense( ConcurrentDictionary result, IPackageMetadata info, LicenseInformationOrigin origin, @@ -110,7 +110,7 @@ private void AddOrUpdateLicense( (key, oldValue) => UpdateResult(oldValue, newValue)); } - private LicenseValidationResult UpdateResult(LicenseValidationResult oldValue, + private static LicenseValidationResult UpdateResult(LicenseValidationResult oldValue, LicenseValidationResult newValue) { oldValue.ValidationErrors.AddRange(newValue.ValidationErrors); @@ -244,7 +244,7 @@ await _fileDownloader.DownloadFile(licenseUrl, } catch (Exception e) { - throw new LicenseDownloadException(e, context, identity); + throw new LicenseDownloadException(e, context, licenseUrl, identity); } } @@ -259,17 +259,17 @@ private bool IsLicenseValid(string licenseId) return _allowedLicenses.Any(allowedLicense => allowedLicense.Equals(licenseId)); } - private string GetLicenseNotAllowedMessage(string license) + private static string GetLicenseNotAllowedMessage(string license) { return $"License \"{license}\" not found in list of supported licenses"; } - private Uri GetLicenseUrl(string spdxIdentifier) + private static Uri GetLicenseUrl(string spdxIdentifier) { return new Uri($"https://licenses.nuget.org/({spdxIdentifier})"); } - private LicenseInformationOrigin ToLicenseOrigin(LicenseType type) => type switch + private static LicenseInformationOrigin ToLicenseOrigin(LicenseType type) => type switch { LicenseType.Overwrite => LicenseInformationOrigin.Overwrite, LicenseType.Expression => LicenseInformationOrigin.Expression, diff --git a/src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs b/src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs index a926c0b7..ac59ec1f 100644 --- a/src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs +++ b/src/NuGetUtility/LicenseValidator/UrlToLicenseMapping.cs @@ -38,7 +38,6 @@ public static class UrlToLicenseMapping new KeyValuePair(new Uri("http://go.microsoft.com/fwlink/?linkid=833178"), Mit), new KeyValuePair(new Uri("http://www.gnu.org/licenses/old-licenses/gpl-2.0.html"), Gpl20), new KeyValuePair(new Uri("https://raw.githubusercontent.com/AArnott/Validation/8377954d86/LICENSE.txt"), MsPl), - new KeyValuePair(new Uri(" http://opensource.org/licenses/mit-license.php"), Mit), new KeyValuePair(new Uri("https://raw.githubusercontent.com/bchavez/Bogus/master/LICENSE"), MitAndBsd3Clause), new KeyValuePair(new Uri("https://github.com/Microsoft/dotnet/blob/master/LICENSE"), Mit) } diff --git a/src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs b/src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs index 1a8bdc0b..2c148009 100644 --- a/src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs +++ b/src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs @@ -30,7 +30,7 @@ public async Task DownloadFile(Uri url, string fileNameStem, CancellationToken t { return; } - await Task.Delay((int)Math.Pow(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS, i + 1), token); + await Task.Delay(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS * ((int)Math.Pow(2, i)), token); } } finally @@ -39,38 +39,20 @@ public async Task DownloadFile(Uri url, string fileNameStem, CancellationToken t } } -#if NETFRAMEWORK - private async Task TryDownload(string fileNameStem, Uri url, CancellationToken _) +#pragma warning disable S1172 + private async Task TryDownload(string fileNameStem, Uri url, CancellationToken token) +#pragma warning restore S1172 { var request = new HttpRequestMessage(HttpMethod.Get, url); +#if NETFRAMEWORK HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); // System.Net.HttpStatusCode.TooManyRequests does not exist in .net472 if (response.StatusCode == (System.Net.HttpStatusCode)429) - { - return false; - } - response.EnsureSuccessStatusCode(); - - string extension = "html"; - if (response.Content.Headers.ContentType.MediaType == "text/plain") - { - extension = "txt"; - } - string fileName = fileNameStem + "." + extension; - using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName)); - using Stream downloadStream = await response.Content.ReadAsStreamAsync(); - - await downloadStream.CopyToAsync(file); - return true; - } #else - private async Task TryDownload(string fileNameStem, Uri url, CancellationToken token) - { - var request = new HttpRequestMessage(HttpMethod.Get, url); - HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token); if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests) +#endif { return false; } @@ -81,13 +63,20 @@ private async Task TryDownload(string fileNameStem, Uri url, CancellationT { extension = "txt"; } - string fileName = fileNameStem + "." + extension; + string fileName = $"{fileNameStem}.{extension}"; +#if NETFRAMEWORK + using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName)); +#else await using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName)); +#endif using Stream downloadStream = await response.Content.ReadAsStreamAsync(); +#if NETFRAMEWORK + await downloadStream.CopyToAsync(file); +#else await downloadStream.CopyToAsync(file, token); +#endif return true; } -#endif } } diff --git a/tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs b/tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs index 4275731b..f905ca60 100644 --- a/tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs +++ b/tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs @@ -928,7 +928,7 @@ public void ValidatingLicensesWithUrlInformation_Should_ThrowLicenseDownloadInfo Assert.ThrowsAsync(() => _uut.Validate(CreateInput(package, _context), _token.Token)); Assert.IsInstanceOf(exception!.InnerException); Assert.AreEqual( - $"Failed to download license for package {packageId} ({packageVersion}).\nContext: {_context}", + $"Failed to download license for package {packageId} ({packageVersion}) from url: {urlMatch.Key}.\nContext: {_context}", exception.Message); } diff --git a/tests/NuGetUtility.Test/NuGetUtility.Test.csproj b/tests/NuGetUtility.Test/NuGetUtility.Test.csproj index edb039b5..8993d432 100644 --- a/tests/NuGetUtility.Test/NuGetUtility.Test.csproj +++ b/tests/NuGetUtility.Test/NuGetUtility.Test.csproj @@ -22,8 +22,9 @@ + - + diff --git a/tests/targets/ProjectWithTransitiveNuget/ProjectWithTransitiveNuget.csproj b/tests/targets/ProjectWithTransitiveNuget/ProjectWithTransitiveNuget.csproj index 1a50e697..bf01dc37 100644 --- a/tests/targets/ProjectWithTransitiveNuget/ProjectWithTransitiveNuget.csproj +++ b/tests/targets/ProjectWithTransitiveNuget/ProjectWithTransitiveNuget.csproj @@ -9,7 +9,7 @@ - +