This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Add RangeHelper #185
Merged
Merged
Add RangeHelper #185
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
{ | ||
"Default": { | ||
"rules": [ | ||
"DefaultCompositeRule" | ||
"adx-nonshipping": { | ||
"rules": [], | ||
"packages": { | ||
"Microsoft.AspNetCore.RangeHelper.Sources": {} | ||
} | ||
}, | ||
"Default": { | ||
"rules": [ | ||
"DefaultCompositeRule" | ||
] | ||
} | ||
} |
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
168 changes: 168 additions & 0 deletions
168
shared/Microsoft.AspNetCore.RangeHelper.Sources/RangeHelper.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 |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Headers; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNetCore.Internal | ||
{ | ||
/// <summary> | ||
/// Provides a parser for the Range Header in an <see cref="HttpContext.Request"/>. | ||
/// </summary> | ||
internal static class RangeHelper | ||
{ | ||
/// <summary> | ||
/// Returns the requested range if the Range Header in the <see cref="HttpContext.Request"/> is valid. | ||
/// </summary> | ||
/// <param name="context">The <see cref="HttpContext"/> associated with the request.</param> | ||
/// <param name="requestHeaders">The <see cref="RequestHeaders"/> associated with the given <paramref name="context"/>.</param> | ||
/// <param name="lastModified">The <see cref="DateTimeOffset"/> representation of the last modified date of the file.</param> | ||
/// <param name="etag">The <see cref="EntityTagHeaderValue"/> provided in the <see cref="HttpContext.Request"/>.</param> | ||
/// <returns>A collection of <see cref="RangeItemHeaderValue"/> containing the ranges parsed from the <paramref name="requestHeaders"/>.</returns> | ||
public static ICollection<RangeItemHeaderValue> ParseRange(HttpContext context, RequestHeaders requestHeaders, DateTimeOffset? lastModified = null, EntityTagHeaderValue etag = null) | ||
{ | ||
var rawRangeHeader = context.Request.Headers[HeaderNames.Range]; | ||
if (StringValues.IsNullOrEmpty(rawRangeHeader)) | ||
{ | ||
return null; | ||
} | ||
|
||
// Perf: Check for a single entry before parsing it | ||
if (rawRangeHeader.Count > 1 || rawRangeHeader[0].IndexOf(',') >= 0) | ||
{ | ||
// 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. | ||
return null; | ||
} | ||
|
||
var rangeHeader = requestHeaders.Range; | ||
if (rangeHeader == null) | ||
{ | ||
// Invalid | ||
return null; | ||
} | ||
|
||
// Already verified above | ||
Debug.Assert(rangeHeader.Ranges.Count == 1); | ||
|
||
// 14.27 If-Range | ||
var ifRangeHeader = requestHeaders.IfRange; | ||
if (ifRangeHeader != null) | ||
{ | ||
// If the validator given in the If-Range header field matches the | ||
// current validator for the selected representation of the target | ||
// resource, then the server SHOULD process the Range header field as | ||
// requested. If the validator does not match, the server MUST ignore | ||
// the Range header field. | ||
bool ignoreRangeHeader = false; | ||
if (ifRangeHeader.LastModified.HasValue) | ||
{ | ||
if (lastModified.HasValue && lastModified > ifRangeHeader.LastModified) | ||
{ | ||
ignoreRangeHeader = true; | ||
} | ||
} | ||
else if (etag != null && ifRangeHeader.EntityTag != null && !ifRangeHeader.EntityTag.Compare(etag, useStrongComparison: true)) | ||
{ | ||
ignoreRangeHeader = true; | ||
} | ||
|
||
if (ignoreRangeHeader) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
return rangeHeader.Ranges; | ||
} | ||
|
||
/// <summary> | ||
/// A helper method to normalize a collection of <see cref="RangeItemHeaderValue"/>s. | ||
/// </summary> | ||
/// <param name="ranges">A collection of <see cref="RangeItemHeaderValue"/> to normalize.</param> | ||
/// <param name="length">The length of the provided <see cref="RangeItemHeaderValue"/>.</param> | ||
/// <returns>A normalized list of <see cref="RangeItemHeaderValue"/>s.</returns> | ||
// 14.35.1 Byte Ranges - If a syntactically valid byte-range-set includes at least one byte-range-spec whose | ||
// first-byte-pos is less than the current length of the entity-body, or at least one suffix-byte-range-spec | ||
// with a non-zero suffix-length, then the byte-range-set is satisfiable. | ||
// Adjusts ranges to be absolute and within bounds. | ||
public static IList<RangeItemHeaderValue> NormalizeRanges(ICollection<RangeItemHeaderValue> ranges, long length) | ||
{ | ||
if (ranges == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (ranges.Count == 0) | ||
{ | ||
return Array.Empty<RangeItemHeaderValue>(); | ||
} | ||
|
||
if (length == 0) | ||
{ | ||
return Array.Empty<RangeItemHeaderValue>(); | ||
} | ||
|
||
var normalizedRanges = new List<RangeItemHeaderValue>(ranges.Count); | ||
foreach (var range in ranges) | ||
{ | ||
var normalizedRange = NormalizeRange(range, length); | ||
|
||
if (normalizedRange != null) | ||
{ | ||
normalizedRanges.Add(normalizedRange); | ||
} | ||
} | ||
|
||
return normalizedRanges; | ||
} | ||
|
||
/// <summary> | ||
/// A helper method to normalize a <see cref="RangeItemHeaderValue"/>. | ||
/// </summary> | ||
/// <param name="range">The <see cref="RangeItemHeaderValue"/> to normalize.</param> | ||
/// <param name="length">The length of the provided <see cref="RangeItemHeaderValue"/>.</param> | ||
/// <returns>A normalized <see cref="RangeItemHeaderValue"/>.</returns> | ||
public static RangeItemHeaderValue NormalizeRange(RangeItemHeaderValue range, long length) | ||
{ | ||
var start = range.From; | ||
var end = range.To; | ||
|
||
// X-[Y] | ||
if (start.HasValue) | ||
{ | ||
if (start.Value >= length) | ||
{ | ||
// Not satisfiable, skip/discard. | ||
return null; | ||
} | ||
if (!end.HasValue || end.Value >= length) | ||
{ | ||
end = length - 1; | ||
} | ||
} | ||
else | ||
{ | ||
// suffix range "-X" e.g. the last X bytes, resolve | ||
if (end.Value == 0) | ||
{ | ||
// Not satisfiable, skip/discard. | ||
return null; | ||
} | ||
|
||
var bytes = Math.Min(end.Value, length); | ||
start = length - bytes; | ||
end = start + bytes - 1; | ||
} | ||
|
||
var normalizedRange = new RangeItemHeaderValue(start, end); | ||
return normalizedRange; | ||
} | ||
} | ||
} |
59 changes: 0 additions & 59 deletions
59
src/Microsoft.AspNetCore.StaticFiles/Infrastructure/RangeHelpers.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shared source does not have a logger. Should I add it? @Tratcher
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because then you'll have to add LogMultipleFileRanges (which you should now remove).