public void InitializeRanges(WebResponseRangeInfo rangeInfo) { IsRangeRequest = rangeInfo.IsRangeApplied; if (rangeInfo.IsRangeApplied) { RangeBegin = new long[] { rangeInfo.Begin }; RangeEnd = new long[] { rangeInfo.End }; } }
/// <summary> /// Validate /// </summary> public bool ValidateHeaderRange(WebResponse webResponse, WebResponseRangeInfo rangeInfo) { // range is not valid if (rangeInfo.RangeLength > rangeInfo.DataLength) { return(false); } // range is not valid if (rangeInfo.Begin > rangeInfo.End) { return(false); } // range can not be larger than total data size if (rangeInfo.End > rangeInfo.DataLength) { return(false); } return(true); }
/// <summary> /// Parses server response indicating returned bytes /// </summary> public static WebResponseRangeInfo ParseWebResponseHeaderRange(WebResponse webResponse) { var rangeInfo = new WebResponseRangeInfo(); // Initial values rangeInfo.Begin = 0; rangeInfo.End = webResponse.ContentLength; rangeInfo.RangeLength = rangeInfo.End; rangeInfo.IsRangeApplied = false; //var dataLength = webResponse.ContentLength; if (webResponse is HttpWebResponse) { // only partial content ollowed range responses if ((webResponse as HttpWebResponse).StatusCode != HttpStatusCode.PartialContent) { rangeInfo.IsRangeApplied = false; return(rangeInfo); } string contentRange = RetrieveHeader(webResponse.Headers[HTTP_HEADER_CONTENT_RANGE], String.Empty); string contentLength = RetrieveHeader(webResponse.Headers[HTTP_HEADER_CONTENT_LENGTH], String.Empty); if (contentRange.Length == 0 || contentLength.Length == 0) { rangeInfo.IsRangeApplied = false; return(rangeInfo); } else { // the header whould be like this //HTTP/1.1 206 OK //Content-Range: bytes 500001-54216827/54216828 //Content-Length: 53716827 //Content-Type: application/x-zip-compressed //Last-Modified: Fri, 24 Jul 2009 21:45:22 GMT //ETag: "mTkR3PTZIofQXrAY3ZkNzA==" //Accept-Ranges: bytes //Binary content of DancingHampsters.zip from byte 500,001 onwards... rangeInfo.IsRangeApplied = true; string[] rangeParts = contentRange.Replace("bytes ", "").Split(new char[] { '/' }); if (rangeParts.Length == 2) { string[] dataRange = rangeParts[0].Split(new char[] { '-' }); rangeInfo.Begin = Convert.ToInt32(dataRange[0]); rangeInfo.End = Convert.ToInt64(dataRange[1]); // and the content length rangeInfo.DataLength = Convert.ToInt64(rangeParts[1]); // the range length is the content-length rangeInfo.RangeLength = Convert.ToInt64(contentLength); return(rangeInfo); } rangeInfo.IsRangeApplied = false; // invalid header! return(rangeInfo); } } rangeInfo.IsRangeApplied = false; return(rangeInfo); }