Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed May 13, 2024
1 parent 11696f9 commit 7d420fa
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"HotReloadStaticAssets": true,
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static RazorComponentsEndpointConventionBuilder AddInteractiveWebAssembly
ComponentEndpointConventionBuilderHelper.AddRenderMode(builder, new WebAssemblyRenderModeWithOptions(options));

var endpointBuilder = ComponentEndpointConventionBuilderHelper.GetEndpointRouteBuilder(builder);
var environment = endpointBuilder.ServiceProvider.GetRequiredService<IHostEnvironment>();

// If the static assets data source for the given manifest name is already added, then just wire-up the Blazor WebAssembly conventions.
// MapStaticWebAssetEndpoints is idempotent and will not add the data source if it already exists.
Expand All @@ -64,7 +63,9 @@ public static RazorComponentsEndpointConventionBuilder AddInteractiveWebAssembly

return builder;
}
else if (environment.IsDevelopment())

var environment = endpointBuilder.ServiceProvider.GetRequiredService<IHostEnvironment>();
if (environment.IsDevelopment())
{
var logger = endpointBuilder.ServiceProvider.GetRequiredService<ILogger<WebAssemblyComponentsEndpointOptions>>();
if (options.StaticAssetsManifestPath is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.AspNetCore.Routing.Matching;

internal class ContentEncodingNegotiationMatcherPolicy : NegotiationMatcherPolicy<ContentEncodingMetadata>
internal sealed class ContentEncodingNegotiationMatcherPolicy : NegotiationMatcherPolicy<ContentEncodingMetadata>
{
internal static string HeaderName => "Accept-Encoding";

Expand All @@ -28,7 +28,7 @@ private protected override bool IsDefaultMetadataValue(ReadOnlySpan<char> candid

private protected override NegotiationPolicyJumpTable CreateTable(int exitDestination, (string negotiationValue, double quality, int destination)[] destinations, int noNegotiationHeaderDestination) => new ContentEncodingPolicyJumpTable(exitDestination, noNegotiationHeaderDestination, new ContentEncodingDestinationsLookUp(destinations));

internal class ContentEncodingPolicyJumpTable(int anyContentEncodingDestination, int noContentEncodingDestination, ContentEncodingDestinationsLookUp destinations) : NegotiationPolicyJumpTable("Accept-Encoding", anyContentEncodingDestination, noContentEncodingDestination)
internal sealed class ContentEncodingPolicyJumpTable(int anyContentEncodingDestination, int noContentEncodingDestination, ContentEncodingDestinationsLookUp destinations) : NegotiationPolicyJumpTable("Accept-Encoding", anyContentEncodingDestination, noContentEncodingDestination)
{
private readonly ContentEncodingDestinationsLookUp _destinations = destinations;

Expand All @@ -37,7 +37,7 @@ internal class ContentEncodingPolicyJumpTable(int anyContentEncodingDestination,
protected override double GetQuality(string? value) => _destinations.GetValueQuality(value);
}

internal class ContentEncodingDestinationsLookUp
internal sealed class ContentEncodingDestinationsLookUp
{
private readonly int _brotliDestination = -1;
private readonly double _brotliQuality;
Expand Down Expand Up @@ -76,6 +76,9 @@ public ContentEncodingDestinationsLookUp((string contentEncoding, double quality

public int GetDestination(string? negotiationValue)
{
// Specialcase the lookup based on the length of the negotiation value
// to reduce the number of required comparisons needed to find a match.
// The match will be validated after this selection.
var (matchedEncoding, destination) = negotiationValue?.Length switch
{
2 => ("br", _brotliDestination),
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Routing/src/Matching/NegotiationMatcherPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ private void EvaluateCandidate(
}

// Explainer:
// This is responsible for building the branches in the DFA that will be used to match the
// based on the Accept-Encoding header of the request.
// This is responsible for building the branches in the DFA that will be used to match a
// concrete endpoint based on the Accept-Encoding header of the request.
// To give you an idea lets explain this through a sample.
// Say we have the following endpoints:
// 1 - Resource.css - [ Accept-Encoding: gzip ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static long GetContentLength(this StaticAssetDescriptor descriptor)
{
foreach (var header in descriptor.ResponseHeaders)
{
if (header.Name == "Content-Length")
if (string.Equals(header.Name, "Content-Length", StringComparison.OrdinalIgnoreCase))
{
return long.Parse(header.Value, CultureInfo.InvariantCulture);
}
Expand All @@ -26,7 +26,7 @@ internal static DateTimeOffset GetLastModified(this StaticAssetDescriptor descri
{
foreach (var header in descriptor.ResponseHeaders)
{
if (header.Name == "Last-Modified")
if (string.Equals(header.Name, "Last-Modified", StringComparison.OrdinalIgnoreCase))
{
return DateTimeOffset.Parse(header.Value, CultureInfo.InvariantCulture);
}
Expand All @@ -39,7 +39,7 @@ internal static EntityTagHeaderValue GetWeakETag(this StaticAssetDescriptor desc
{
foreach (var header in descriptor.ResponseHeaders)
{
if (header.Name == "ETag")
if (string.Equals(header.Name, "ETag", StringComparison.OrdinalIgnoreCase))
{
var eTag = EntityTagHeaderValue.Parse(header.Value);
if (eTag.IsWeak)
Expand All @@ -56,7 +56,7 @@ internal static bool HasContentEncoding(this StaticAssetDescriptor descriptor)
{
foreach (var selector in descriptor.Selectors)
{
if (selector.Name == "Content-Encoding")
if (string.Equals(selector.Name, "Content-Encoding", StringComparison.OrdinalIgnoreCase))
{
return true;
}
Expand All @@ -69,7 +69,7 @@ internal static bool HasETag(this StaticAssetDescriptor descriptor, string tag)
{
foreach (var header in descriptor.ResponseHeaders)
{
if (header.Name == "ETag")
if (string.Equals(header.Name, "ETag", StringComparison.OrdinalIgnoreCase))
{
var eTag = EntityTagHeaderValue.Parse(header.Value);
if (!eTag.IsWeak && eTag.Tag == tag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Microsoft.AspNetCore.Builder;
// Handles changes during development to support common scenarios where for example, a developer changes a file in the wwwroot folder.
internal sealed partial class StaticAssetDevelopmentRuntimeHandler(List<StaticAssetDescriptor> descriptors)
{
private const string ReloadStaticAssetsAtRuntimeKey = "ReloadStaticAssetsAtRuntime";

public void AttachRuntimePatching(EndpointBuilder builder)
{
var original = builder.RequestDelegate!;
Expand Down Expand Up @@ -125,7 +127,7 @@ public Task SendFileAsync(string path, long offset, long? count, CancellationTok
{
// Clear all the ETag headers, as they'll be replaced with the new ones.
_context.Response.Headers.ETag = "";
// Compute the new ETag, if this is a compressed asset, HotReloadStaticAsset will update it.
// Compute the new ETag, if this is a compressed asset, RuntimeStaticAssetResponseBodyFeature will update it.
_context.Response.Headers.ETag = GetETag(fileInfo);
_context.Response.Headers.ContentLength = fileInfo.Length;
_context.Response.Headers.LastModified = fileInfo.LastModified.ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -157,7 +159,7 @@ private static StaticAssetDescriptor FindOriginalAsset(string tag, List<StaticAs
internal static bool IsEnabled(IServiceProvider serviceProvider, IWebHostEnvironment environment)
{
var config = serviceProvider.GetRequiredService<IConfiguration>();
var explicitlyConfigured = bool.TryParse(config["HotReloadStaticAssets"], out var hotReload);
var explicitlyConfigured = bool.TryParse(config[ReloadStaticAssetsAtRuntimeKey], out var hotReload);
return (!explicitlyConfigured && environment.IsDevelopment()) || (explicitlyConfigured && hotReload);
}

Expand All @@ -170,7 +172,7 @@ internal static void EnableSupport(
var config = endpoints.ServiceProvider.GetRequiredService<IConfiguration>();
var hotReloadHandler = new StaticAssetDevelopmentRuntimeHandler(descriptors);
builder.Add(hotReloadHandler.AttachRuntimePatching);
var disableFallback = bool.TryParse(config["DisableStaticAssetFallback"], out var disableFallbackValue) && disableFallbackValue;
var disableFallback = bool.TryParse(config["DisableStaticAssetNotFoundRuntimeFallback"], out var disableFallbackValue) && disableFallbackValue;

if (!disableFallback)
{
Expand Down
2 changes: 1 addition & 1 deletion src/StaticAssets/src/EndpointProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.StaticAssets;

// Represents a property of an endpoint.
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
internal class EndpointProperty(string name, string value)
internal sealed class EndpointProperty(string name, string value)
{
public string Name { get; } = name;
public string Value { get; } = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
namespace Microsoft.AspNetCore.StaticAssets.Infrastructure;

/// <summary>
/// This type is not recommended for use outside of ASP.NET Core.
/// For internal framework use only.
/// </summary>
public static class StaticAssetsEndpointDataSourceHelper
{
/// <summary>
/// This method is not recommended for use outside of ASP.NET Core.
/// For internal framework use only.
/// </summary>
/// <param name="dataSource"></param>
/// <param name="staticAssetsManifestPath"></param>

public static bool IsStaticAssetsDataSource(EndpointDataSource dataSource, string? staticAssetsManifestPath = null)
{
if (dataSource is StaticAssetsEndpointDataSource staticAssetsDataSource)
Expand Down
4 changes: 2 additions & 2 deletions src/StaticAssets/src/ResponseHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace Microsoft.AspNetCore.StaticAssets;

// Represents a response header for a static resource.
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
internal class ResponseHeader(string name, string value)
internal sealed class ResponseHeader(string name, string value)
{
public string Name { get; } = name;
public string Value { get; } = value;

private string GetDebuggerDisplay() => $"Name: {Name} Value:{Value}";
private string GetDebuggerDisplay() => $"Name: {Name} Value: {Value}";
}
4 changes: 2 additions & 2 deletions src/StaticAssets/src/StaticAssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.StaticAssets;

// Represents a static resource.
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
internal class StaticAssetDescriptor(
internal sealed class StaticAssetDescriptor(
string route,
string assetFile,
StaticAssetSelector[] selectors,
Expand All @@ -22,6 +22,6 @@ internal class StaticAssetDescriptor(

private string GetDebuggerDisplay()
{
return $"Route: {Route} Path:{AssetFile}";
return $"Route: {Route} Path: {AssetFile}";
}
}
4 changes: 2 additions & 2 deletions src/StaticAssets/src/StaticAssetSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Microsoft.AspNetCore.StaticAssets;

// Represents a selector for a static resource.
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
internal class StaticAssetSelector(string name, string value, string quality)
internal sealed class StaticAssetSelector(string name, string value, string quality)
{
public string Name { get; } = name;
public string Value { get; } = value;
public string Quality { get; } = quality;

private string GetDebuggerDisplay() => $"Name: {Name} Value:{Value} Quality:{Quality}";
private string GetDebuggerDisplay() => $"Name: {Name} Value: {Value} Quality: {Quality}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Builder;
public static class StaticAssetsEndpointRouteBuilderExtensions
{
/// <summary>
/// Maps static files produced during the build as endpoints.
/// Maps static files produced during the build as endpoints..
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="staticAssetsManifestPath">The path to the static assets manifest file.</param>
Expand Down Expand Up @@ -83,5 +83,4 @@ internal static StaticAssetsEndpointConventionBuilder MapStaticAssets(this IEndp

return result;
}

}
6 changes: 2 additions & 4 deletions src/StaticAssets/src/StaticAssetsInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,8 @@ public async Task Invoke(HttpContext context)
await SendRangeAsync(requestContext, range);
return;
}
else
{
context.Response.ContentLength = _length;
}

context.Response.ContentLength = _length;

await SendAsync(requestContext);
_logger.FileServed(Route, PhysicalPath);
Expand Down
6 changes: 3 additions & 3 deletions src/StaticAssets/test/StaticAssetsIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task CanServeNewFilesAddedAfterBuildDuringDevelopment()
WebRootPath = webRoot
});

builder.WebHost.UseSetting("HotReloadStaticAssets", "true");
builder.WebHost.UseSetting(StaticAssetDevelopmentRuntimeHandler.ReloadStaticAssetsAtRuntimeKey, "true");
builder.WebHost.ConfigureServices(services =>
{
services.AddRouting();
Expand Down Expand Up @@ -145,7 +145,7 @@ public async Task CanModifyAssetsOnTheFlyInDevelopment()
EnvironmentName = "Development",
WebRootPath = webRoot
});
builder.WebHost.UseSetting("HotReloadStaticAssets", "true");
builder.WebHost.UseSetting(StaticAssetDevelopmentRuntimeHandler.ReloadStaticAssetsAtRuntimeKey, "true");
builder.WebHost.ConfigureServices(services =>
{
services.AddRouting();
Expand Down Expand Up @@ -198,7 +198,7 @@ public async Task CanModifyAssetsWithCompressedVersionsOnTheFlyInDevelopment()
EnvironmentName = "Development",
WebRootPath = webRoot
});
builder.WebHost.UseSetting("HotReloadStaticAssets", "true");
builder.WebHost.UseSetting(StaticAssetDevelopmentRuntimeHandler.ReloadStaticAssetsAtRuntimeKey, "true");
builder.WebHost.ConfigureServices(services =>
{
services.AddRouting();
Expand Down

0 comments on commit 7d420fa

Please sign in to comment.