forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert Razor.Runtime.Manual.cs and regenerate ref assemblies
- Loading branch information
1 parent
b92f423
commit f2dd6d4
Showing
9 changed files
with
216 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace RepoTasks | ||
{ | ||
public class DownloadFile : Microsoft.Build.Utilities.Task | ||
{ | ||
[Required] | ||
public string Uri { get; set; } | ||
|
||
/// <summary> | ||
/// If this field is set and the task fail to download the file from `Uri`, with a NotFound | ||
/// status, it will try to download the file from `PrivateUri`. | ||
/// </summary> | ||
public string PrivateUri { get; set; } | ||
|
||
/// <summary> | ||
/// Suffix for the private URI in base64 form (for SAS compatibility) | ||
/// </summary> | ||
public string PrivateUriSuffix { get; set; } | ||
|
||
public int MaxRetries { get; set; } = 5; | ||
|
||
[Required] | ||
public string DestinationPath { get; set; } | ||
|
||
public bool Overwrite { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
return ExecuteAsync().GetAwaiter().GetResult(); | ||
} | ||
|
||
private async System.Threading.Tasks.Task<bool> ExecuteAsync() | ||
{ | ||
string destinationDir = Path.GetDirectoryName(DestinationPath); | ||
if (!Directory.Exists(destinationDir)) | ||
{ | ||
Directory.CreateDirectory(destinationDir); | ||
} | ||
|
||
if (File.Exists(DestinationPath) && !Overwrite) | ||
{ | ||
return true; | ||
} | ||
|
||
const string FileUriProtocol = "file://"; | ||
|
||
if (Uri.StartsWith(FileUriProtocol, StringComparison.Ordinal)) | ||
{ | ||
var filePath = Uri.Substring(FileUriProtocol.Length); | ||
Log.LogMessage($"Copying '{filePath}' to '{DestinationPath}'"); | ||
File.Copy(filePath, DestinationPath); | ||
return true; | ||
} | ||
|
||
List<string> errorMessages = new List<string>(); | ||
bool? downloadStatus = await DownloadWithRetriesAsync(Uri, DestinationPath, errorMessages); | ||
|
||
if (downloadStatus == false && !string.IsNullOrEmpty(PrivateUri)) | ||
{ | ||
string uriSuffix = ""; | ||
if (!string.IsNullOrEmpty(PrivateUriSuffix)) | ||
{ | ||
var uriSuffixBytes = System.Convert.FromBase64String(PrivateUriSuffix); | ||
uriSuffix = System.Text.Encoding.UTF8.GetString(uriSuffixBytes); | ||
} | ||
downloadStatus = await DownloadWithRetriesAsync($"{PrivateUri}{uriSuffix}", DestinationPath, errorMessages); | ||
} | ||
|
||
if (downloadStatus != true) | ||
{ | ||
foreach (var error in errorMessages) | ||
{ | ||
Log.LogError(error); | ||
} | ||
} | ||
|
||
return downloadStatus == true; | ||
} | ||
|
||
/// <summary> | ||
/// Attempt to download file from `source` with retries when response error is different of FileNotFound and Success. | ||
/// </summary> | ||
/// <param name="source">URL to the file to be downloaded.</param> | ||
/// <param name="target">Local path where to put the downloaded file.</param> | ||
/// <returns>true: Download Succeeded. false: Download failed with 404. null: Download failed but is retriable.</returns> | ||
private async Task<bool?> DownloadWithRetriesAsync(string source, string target, List<string> errorMessages) | ||
{ | ||
Random rng = new Random(); | ||
|
||
Log.LogMessage(MessageImportance.High, $"Attempting download '{source}' to '{target}'"); | ||
|
||
using (var httpClient = new HttpClient()) | ||
{ | ||
for (int retryNumber = 0; retryNumber < MaxRetries; retryNumber++) | ||
{ | ||
try | ||
{ | ||
var httpResponse = await httpClient.GetAsync(source); | ||
|
||
Log.LogMessage(MessageImportance.High, $"{source} -> {httpResponse.StatusCode}"); | ||
|
||
// The Azure Storage REST API returns '400 - Bad Request' in some cases | ||
// where the resource is not found on the storage. | ||
// https://docs.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes | ||
if (httpResponse.StatusCode == HttpStatusCode.NotFound || | ||
httpResponse.ReasonPhrase.IndexOf("The requested URI does not represent any resource on the server.", StringComparison.OrdinalIgnoreCase) == 0) | ||
{ | ||
errorMessages.Add($"Problems downloading file from '{source}'. Does the resource exist on the storage? {httpResponse.StatusCode} : {httpResponse.ReasonPhrase}"); | ||
return false; | ||
} | ||
|
||
httpResponse.EnsureSuccessStatusCode(); | ||
|
||
using (var outStream = File.Create(target)) | ||
{ | ||
await httpResponse.Content.CopyToAsync(outStream); | ||
} | ||
|
||
Log.LogMessage(MessageImportance.High, $"returning true {source} -> {httpResponse.StatusCode}"); | ||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.LogMessage(MessageImportance.High, $"returning error in {source} "); | ||
errorMessages.Add($"Problems downloading file from '{source}'. {e.Message} {e.StackTrace}"); | ||
File.Delete(target); | ||
} | ||
|
||
await System.Threading.Tasks.Task.Delay(rng.Next(1000, 10000)); | ||
} | ||
} | ||
|
||
Log.LogMessage(MessageImportance.High, $"giving up {source} "); | ||
errorMessages.Add($"Giving up downloading the file from '{source}' after {MaxRetries} retries."); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 20 additions & 9 deletions
29
src/Razor/Razor.Runtime/ref/Microsoft.AspNetCore.Razor.Runtime.Manual.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers | ||
{ | ||
public partial class TagHelperExecutionContext | ||
{ | ||
internal TagHelperExecutionContext(string tagName, Microsoft.AspNetCore.Razor.TagHelpers.TagMode tagMode) { } | ||
[System.Diagnostics.DebuggerStepThroughAttribute] | ||
internal System.Threading.Tasks.Task<Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContent> GetChildContentAsync(bool useCachedResult, System.Text.Encodings.Web.HtmlEncoder encoder) { throw null; } | ||
} | ||
} | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
[assembly: TypeForwardedTo(typeof(DefaultTagHelperContent))] | ||
[assembly: TypeForwardedTo(typeof(HtmlAttributeNameAttribute))] | ||
[assembly: TypeForwardedTo(typeof(HtmlAttributeNotBoundAttribute))] | ||
[assembly: TypeForwardedTo(typeof(HtmlTargetElementAttribute))] | ||
[assembly: TypeForwardedTo(typeof(ITagHelper))] | ||
[assembly: TypeForwardedTo(typeof(ITagHelperComponent))] | ||
[assembly: TypeForwardedTo(typeof(NullHtmlEncoder))] | ||
[assembly: TypeForwardedTo(typeof(OutputElementHintAttribute))] | ||
[assembly: TypeForwardedTo(typeof(ReadOnlyTagHelperAttributeList))] | ||
[assembly: TypeForwardedTo(typeof(RestrictChildrenAttribute))] | ||
[assembly: TypeForwardedTo(typeof(TagHelper))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperAttribute))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperAttributeList))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperComponent))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperContent))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperContext))] | ||
[assembly: TypeForwardedTo(typeof(TagHelperOutput))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters