示例#1
0
        public static HttpRange GetResponseRangeBy(HttpRange requestRange, long fileLength)
        {
            if (requestRange.Begin > fileLength)
            {
                throw new ArgumentOutOfRangeException("requestRange", requestRange.Begin, "The begin of range is too large");
            }

            var end = requestRange.End >= fileLength ? fileLength - 1 : requestRange.End;
            return new HttpRange { Begin = requestRange.Begin, End = end };
        }
示例#2
0
        public static bool TryParse(string header, out HttpRange range)
        {
            try
            {
                if (header.StartsWith(BytesEquals))
                {
                    header = header.Remove(0, BytesEquals.Length);
                }
                else
                {
                    throw new ArgumentException("The header should start with bytes=", "header");
                }

                var numbers = header.Split('-');
                if (numbers.Length != 2)
                {
                    throw new ArgumentException("There are more than 2 numbers in range header", "header");
                }

                var begin = long.Parse(numbers[0]);
                var end = string.IsNullOrEmpty(numbers[1]) ? long.MaxValue : long.Parse(numbers[1]);

                if (begin > end)
                {
                    throw new ArgumentException("The range is invalid.", "header");
                }

                range = new HttpRange
                {
                    Begin = begin,
                    End = end
                };
            }
            catch
            {
                range = Empty;
                return false;
            }

            return true;
        }
示例#3
0
 public static bool IsEmpty(HttpRange range)
 {
     return range.Begin == 0L && range.End == 0L;
 }