diff --git a/shared/Microsoft.AspNetCore.RangeHelper.Sources/RangeHelper.cs b/shared/Microsoft.AspNetCore.RangeHelper.Sources/RangeHelper.cs index 64f0e2a607..8378d301f3 100644 --- a/shared/Microsoft.AspNetCore.RangeHelper.Sources/RangeHelper.cs +++ b/shared/Microsoft.AspNetCore.RangeHelper.Sources/RangeHelper.cs @@ -6,6 +6,8 @@ using System.Diagnostics; using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Headers; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; @@ -16,21 +18,32 @@ namespace Microsoft.AspNetCore.Internal /// internal static class RangeHelper { + // Is temporary to avoid build break + public static (bool isRangeRequest, RangeItemHeaderValue range) ParseRange( + HttpContext context, + RequestHeaders requestHeaders, + long length) + { + return ParseRange(context, requestHeaders, length, NullLogger.Instance); + } + /// /// Returns the normalized form of the requested range if the Range Header in the is valid. /// /// The associated with the request. /// The associated with the given . /// The total length of the file representation requested. + /// The . /// A boolean value which represents if the contain a single valid /// range request. A which represents the normalized form of the /// range parsed from the or null if it cannot be normalized. /// If the Range header exists but cannot be parsed correctly, or if the provided length is 0, then the range request cannot be satisfied (status 416). /// This results in (true,null) return values. - public static (bool, RangeItemHeaderValue) ParseRange( + public static (bool isRangeRequest, RangeItemHeaderValue range) ParseRange( HttpContext context, RequestHeaders requestHeaders, - long length) + long length, + ILogger logger) { var rawRangeHeader = context.Request.Headers[HeaderNames.Range]; if (StringValues.IsNullOrEmpty(rawRangeHeader)) @@ -41,6 +54,8 @@ namespace Microsoft.AspNetCore.Internal // Perf: Check for a single entry before parsing it if (rawRangeHeader.Count > 1 || rawRangeHeader[0].IndexOf(',') >= 0) { + logger.LogWarning("Multiple ranges are not supported."); + // The spec allows for multiple ranges but we choose not to support them because the client may request // very strange ranges (e.g. each byte separately, overlapping ranges, etc.) that could negatively // impact the server. Ignore the header and serve the response normally. diff --git a/src/Microsoft.AspNetCore.StaticFiles/StaticFileContext.cs b/src/Microsoft.AspNetCore.StaticFiles/StaticFileContext.cs index e1e77acc39..b50b8fe433 100644 --- a/src/Microsoft.AspNetCore.StaticFiles/StaticFileContext.cs +++ b/src/Microsoft.AspNetCore.StaticFiles/StaticFileContext.cs @@ -257,7 +257,7 @@ namespace Microsoft.AspNetCore.StaticFiles return; } - (_isRangeRequest, _range) = RangeHelper.ParseRange(_context, _requestHeaders, _length); + (_isRangeRequest, _range) = RangeHelper.ParseRange(_context, _requestHeaders, _length, _logger); } public void ApplyResponseHeaders(int statusCode) diff --git a/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/Microsoft.AspNetCore.RangeHelper.Sources.Test.csproj b/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/Microsoft.AspNetCore.RangeHelper.Sources.Test.csproj index 760b63a097..e75e27a9bb 100644 --- a/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/Microsoft.AspNetCore.RangeHelper.Sources.Test.csproj +++ b/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/Microsoft.AspNetCore.RangeHelper.Sources.Test.csproj @@ -10,6 +10,7 @@ + diff --git a/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/RangeHelperTests.cs b/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/RangeHelperTests.cs index c1a4a3d282..a6e06810a5 100644 --- a/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/RangeHelperTests.cs +++ b/test/Microsoft.AspNetCore.RangeHelper.Sources.Test/RangeHelperTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Net.Http.Headers; using Xunit; @@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Internal httpContext.Request.Headers[HeaderNames.Range] = range; // Act - var (isRangeRequest, parsedRangeResult) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 10); + var (isRangeRequest, parsedRangeResult) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 10, NullLogger.Instance); // Assert Assert.False(isRangeRequest); @@ -82,7 +83,7 @@ namespace Microsoft.AspNetCore.Internal httpContext.Request.Headers[HeaderNames.Range] = range; // Act - var (isRangeRequest, parsedRangeResult) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 10); + var (isRangeRequest, parsedRangeResult) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 10, NullLogger.Instance); // Assert Assert.False(isRangeRequest); @@ -98,7 +99,7 @@ namespace Microsoft.AspNetCore.Internal httpContext.Request.Headers[HeaderNames.Range] = range.ToString(); // Act - var (isRangeRequest, parsedRange) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 4); + var (isRangeRequest, parsedRange) = RangeHelper.ParseRange(httpContext, httpContext.Request.GetTypedHeaders(), 4, NullLogger.Instance); // Assert Assert.True(isRangeRequest);