public void InitializeRanges(HttpRequestMultipleRangeInfo rangeInfo) { IsRangeRequest = rangeInfo.IsRangeApplied; if (rangeInfo.IsRangeApplied) { RangeBegin = rangeInfo.Begin; RangeEnd = rangeInfo.End; } }
/// <summary> /// Validate /// </summary> public bool ValidateHeaderRange(HttpRequest request, HttpRequestMultipleRangeInfo rangeInfo, long contentLength) { var validRanges = true; // Check each found Range request for consistency for (int index = 0; index <= rangeInfo.Begin.Length; index++) { // Check if the requested range values are valid, // return false if they are not. // // Note: // Do not clean invalid values up by fitting them into // valid parameters using Math.Min and Math.Max, because // some download clients (like Go!Zilla) might send invalid // (e.g. too large) range requests to determine the file limits! // Begin and end must not exceed the file size if ((rangeInfo.Begin[index] > (contentLength - 1)) || (rangeInfo.End[index] > (contentLength - 1))) { validRanges = false; } // Begin and end cannot be < 0 if ((rangeInfo.Begin[index] < 0) || (rangeInfo.End[index] < 0)) { validRanges = false; } // End must be larger or equal to begin value if (rangeInfo.End[index] < rangeInfo.Begin[index]) { // The requested Range is invalid... validRanges = false; } } return(validRanges); }
/// <summary> /// Parses user request for range specifed parameters /// </summary> public static HttpRequestMultipleRangeInfo ParseHttpRequestHeaderMultipleRange( HttpRequest request, long contentLength) { //bool validRanges; string sSource; int iLoop; string[] sRanges; var rangeInfo = new HttpRequestMultipleRangeInfo(); // Parses the Range header from the Request (if there is one) // returns true, if the Range header was valid, or if there was no // Range header at all (meaning that the whole // file was requested) // returns false, if the Range header asked for unsatisfieable // ranges // Retrieve Range Header value from Request (Empty if none is indicated) sSource = RetrieveHeader(request, HTTP_HEADER_RANGE, String.Empty); if (string.IsNullOrEmpty(sSource)) { // No Range was requested, return the entire file range... rangeInfo.Begin = new long[1]; rangeInfo.End = new long[1]; rangeInfo.Begin[0] = 0; rangeInfo.End[0] = contentLength - 1; // no Range request rangeInfo.IsRangeApplied = false; } else { // A Range was requested... // return true for the bRange parameter, telling the caller // that the Request is indeed a Range request... rangeInfo.IsRangeApplied = true; // Remove "bytes=" from the beginning, and split the remaining // string by comma characters sRanges = sSource.Replace("bytes=", "").Split(",".ToCharArray()); rangeInfo.Begin = new long[sRanges.GetUpperBound(0) + 1]; rangeInfo.End = new long[sRanges.GetUpperBound(0) + 1]; // Check each found Range request for consistency for (iLoop = sRanges.GetLowerBound(0); iLoop <= sRanges.GetUpperBound(0); iLoop++) { // Split this range request by the dash character, // sRange(0) contains the requested begin-value, // sRange(1) contains the requested end-value... string[] sRange = sRanges[iLoop].Split("-".ToCharArray()); // Determine the end of the requested range if (sRange[1].Equals(String.Empty)) { // No end was specified, take the entire range rangeInfo.End[iLoop] = contentLength - 1; } else { // An end was specified... rangeInfo.End[iLoop] = long.Parse(sRange[1]); } // Determine the begin of the requested range if (sRange[0].Equals(String.Empty)) { // No begin was specified, which means that // the end value indicated to return the last n // bytes of the file: // Calculate the begin rangeInfo.Begin[iLoop] = contentLength - 1 - rangeInfo.End[iLoop]; // ... to the end of the file... rangeInfo.End[iLoop] = contentLength - 1; } else { // A normal begin value was indicated... rangeInfo.Begin[iLoop] = long.Parse(sRange[0]); } } } return(rangeInfo); }