Skip to content

Commit

Permalink
Add retries to CDN downloads (dotnet#7881)
Browse files Browse the repository at this point in the history
Add retries to CDN downloads
  • Loading branch information
ryanbrandenburg authored Feb 25, 2019
1 parent 3d17ac3 commit 03460d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/ProjectTemplates/test/CdnScriptTagTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -112,7 +114,7 @@ public async Task FallbackSrcContent_Matches_CDNContent(ScriptTag scriptTag)
.TrimStart('~')
.TrimStart('/');

var cdnContent = await _httpClient.GetStringAsync(scriptTag.Src);
var cdnContent = await GetStringFromCDN(scriptTag.Src);
var fallbackSrcContent = GetFileContentFromArchive(scriptTag, fallbackSrc);

Assert.Equal(RemoveLineEndings(cdnContent), RemoveLineEndings(fallbackSrcContent));
Expand Down Expand Up @@ -145,6 +147,27 @@ public override string ToString()
}
}

private async Task<string> GetStringFromCDN(string src)
{
var response = await GetFromCDN(src);
return await response.Content.ReadAsStringAsync();
}

private async Task<byte[]> GetByteArrayFromCDN(string src)
{
var response = await GetFromCDN(src);
return await response.Content.ReadAsByteArrayAsync();
}

private async Task<HttpResponseMessage> GetFromCDN(string src)
{
var logger = NullLogger.Instance;
return await RetryHelper.RetryRequest(async () => {
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(src));
return await _httpClient.SendAsync(request);
}, logger);
}

private Task<string> GetShaIntegrity(ScriptTag scriptTag)
{
return GetShaIntegrity(scriptTag.Integrity, scriptTag.Src);
Expand All @@ -158,7 +181,7 @@ private Task<string> GetShaIntegrity(LinkTag linkTag)
private async Task<string> GetShaIntegrity(string integrity, string src)
{
var prefix = integrity.Substring(0, 6);
using (var respStream = await _httpClient.GetStreamAsync(src))
var respStream = await GetByteArrayFromCDN(src);
using (HashAlgorithm alg = string.Equals(prefix, "sha256") ? (HashAlgorithm)SHA256.Create() : (HashAlgorithm)SHA384.Create())
{
var hash = alg.ComputeHash(respStream);
Expand Down
1 change: 1 addition & 0 deletions src/ProjectTemplates/test/ProjectTemplates.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepositoryRoot)src\Hosting\Server.IntegrationTesting\src\Microsoft.AspNetCore.Server.IntegrationTesting.csproj" />
<ProjectReference Include="../testassets/DotNetToolsInstaller/DotNetToolsInstaller.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="../Web.Client.ItemTemplates/Microsoft.DotNet.Web.Client.ItemTemplates.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="../Web.ItemTemplates/Microsoft.DotNet.Web.ItemTemplates.csproj" ReferenceOutputAssembly="false" />
Expand Down

0 comments on commit 03460d8

Please sign in to comment.