|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.AspNetCore.Http.Headers; |
| 9 | +using Microsoft.Extensions.Primitives; |
| 10 | +using Microsoft.Net.Http.Headers; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.Internal |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Provides a parser for the Range Header in an <see cref="HttpContext.Request"/>. |
| 16 | + /// </summary> |
| 17 | + internal static class RangeHelper |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Returns the requested range if the Range Header in the <see cref="HttpContext.Request"/> is valid. |
| 21 | + /// </summary> |
| 22 | + /// <param name="context">The <see cref="HttpContext"/> associated with the request.</param> |
| 23 | + /// <param name="requestHeaders">The <see cref="RequestHeaders"/> associated with the given <paramref name="context"/>.</param> |
| 24 | + /// <param name="lastModified">The <see cref="DateTimeOffset"/> representation of the last modified date of the file.</param> |
| 25 | + /// <param name="etag">The <see cref="EntityTagHeaderValue"/> provided in the <see cref="HttpContext.Request"/>.</param> |
| 26 | + /// <returns>A collection of <see cref="RangeItemHeaderValue"/> containing the ranges parsed from the <paramref name="requestHeaders"/>.</returns> |
| 27 | + public static ICollection<RangeItemHeaderValue> ParseRange(HttpContext context, RequestHeaders requestHeaders, DateTimeOffset? lastModified = null, EntityTagHeaderValue etag = null) |
| 28 | + { |
| 29 | + var rawRangeHeader = context.Request.Headers[HeaderNames.Range]; |
| 30 | + if (StringValues.IsNullOrEmpty(rawRangeHeader)) |
| 31 | + { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + // Perf: Check for a single entry before parsing it |
| 36 | + if (rawRangeHeader.Count > 1 || rawRangeHeader[0].IndexOf(',') >= 0) |
| 37 | + { |
| 38 | + // The spec allows for multiple ranges but we choose not to support them because the client may request |
| 39 | + // very strange ranges (e.g. each byte separately, overlapping ranges, etc.) that could negatively |
| 40 | + // impact the server. Ignore the header and serve the response normally. |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + var rangeHeader = requestHeaders.Range; |
| 45 | + if (rangeHeader == null) |
| 46 | + { |
| 47 | + // Invalid |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + // Already verified above |
| 52 | + Debug.Assert(rangeHeader.Ranges.Count == 1); |
| 53 | + |
| 54 | + // 14.27 If-Range |
| 55 | + var ifRangeHeader = requestHeaders.IfRange; |
| 56 | + if (ifRangeHeader != null) |
| 57 | + { |
| 58 | + // If the validator given in the If-Range header field matches the |
| 59 | + // current validator for the selected representation of the target |
| 60 | + // resource, then the server SHOULD process the Range header field as |
| 61 | + // requested. If the validator does not match, the server MUST ignore |
| 62 | + // the Range header field. |
| 63 | + bool ignoreRangeHeader = false; |
| 64 | + if (ifRangeHeader.LastModified.HasValue) |
| 65 | + { |
| 66 | + if (lastModified.HasValue && lastModified > ifRangeHeader.LastModified) |
| 67 | + { |
| 68 | + ignoreRangeHeader = true; |
| 69 | + } |
| 70 | + } |
| 71 | + else if (etag != null && ifRangeHeader.EntityTag != null && !ifRangeHeader.EntityTag.Compare(etag, useStrongComparison: true)) |
| 72 | + { |
| 73 | + ignoreRangeHeader = true; |
| 74 | + } |
| 75 | + |
| 76 | + if (ignoreRangeHeader) |
| 77 | + { |
| 78 | + return null; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return rangeHeader.Ranges; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// A helper method to normalize a collection of <see cref="RangeItemHeaderValue"/>s. |
| 87 | + /// </summary> |
| 88 | + /// <param name="ranges">A collection of <see cref="RangeItemHeaderValue"/> to normalize.</param> |
| 89 | + /// <param name="length">The length of the provided <see cref="RangeItemHeaderValue"/>.</param> |
| 90 | + /// <returns>A normalized list of <see cref="RangeItemHeaderValue"/>s.</returns> |
| 91 | + // 14.35.1 Byte Ranges - If a syntactically valid byte-range-set includes at least one byte-range-spec whose |
| 92 | + // first-byte-pos is less than the current length of the entity-body, or at least one suffix-byte-range-spec |
| 93 | + // with a non-zero suffix-length, then the byte-range-set is satisfiable. |
| 94 | + // Adjusts ranges to be absolute and within bounds. |
| 95 | + public static IList<RangeItemHeaderValue> NormalizeRanges(ICollection<RangeItemHeaderValue> ranges, long length) |
| 96 | + { |
| 97 | + if (ranges == null) |
| 98 | + { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + if (ranges.Count == 0) |
| 103 | + { |
| 104 | + return Array.Empty<RangeItemHeaderValue>(); |
| 105 | + } |
| 106 | + |
| 107 | + if (length == 0) |
| 108 | + { |
| 109 | + return Array.Empty<RangeItemHeaderValue>(); |
| 110 | + } |
| 111 | + |
| 112 | + var normalizedRanges = new List<RangeItemHeaderValue>(ranges.Count); |
| 113 | + foreach (var range in ranges) |
| 114 | + { |
| 115 | + var normalizedRange = NormalizeRange(range, length); |
| 116 | + |
| 117 | + if (normalizedRange != null) |
| 118 | + { |
| 119 | + normalizedRanges.Add(normalizedRange); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return normalizedRanges; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// A helper method to normalize a <see cref="RangeItemHeaderValue"/>. |
| 128 | + /// </summary> |
| 129 | + /// <param name="range">The <see cref="RangeItemHeaderValue"/> to normalize.</param> |
| 130 | + /// <param name="length">The length of the provided <see cref="RangeItemHeaderValue"/>.</param> |
| 131 | + /// <returns>A normalized <see cref="RangeItemHeaderValue"/>.</returns> |
| 132 | + public static RangeItemHeaderValue NormalizeRange(RangeItemHeaderValue range, long length) |
| 133 | + { |
| 134 | + var start = range.From; |
| 135 | + var end = range.To; |
| 136 | + |
| 137 | + // X-[Y] |
| 138 | + if (start.HasValue) |
| 139 | + { |
| 140 | + if (start.Value >= length) |
| 141 | + { |
| 142 | + // Not satisfiable, skip/discard. |
| 143 | + return null; |
| 144 | + } |
| 145 | + if (!end.HasValue || end.Value >= length) |
| 146 | + { |
| 147 | + end = length - 1; |
| 148 | + } |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + // suffix range "-X" e.g. the last X bytes, resolve |
| 153 | + if (end.Value == 0) |
| 154 | + { |
| 155 | + // Not satisfiable, skip/discard. |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + var bytes = Math.Min(end.Value, length); |
| 160 | + start = length - bytes; |
| 161 | + end = start + bytes - 1; |
| 162 | + } |
| 163 | + |
| 164 | + var normalizedRange = new RangeItemHeaderValue(start, end); |
| 165 | + return normalizedRange; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments