private RangeItemHeaderValue(RangeItemHeaderValue source)
        {
            Debug.Assert(source != null);

            _from = source._from;
            _to = source._to;
        }
        private RangeItemHeaderValue(RangeItemHeaderValue source)
        {
            Contract.Requires(source != null);

            this.from = source.from;
            this.to = source.to;
        }
		public void Equals ()
		{
			var value = new RangeItemHeaderValue (5, null);
			Assert.AreEqual (value, new RangeItemHeaderValue (5, null), "#1");
			Assert.AreNotEqual (value, new RangeItemHeaderValue (6, null), "#2");
			Assert.AreNotEqual (value, new RangeItemHeaderValue (5, 10), "#3");
		}
        private RangeItemHeaderValue(RangeItemHeaderValue source)
        {
            Contract.Requires(source != null);

            _from = source._from;
            _to = source._to;
        }
Exemple #5
0
        public ByteRangeStream(Stream innerStream, RangeItemHeaderValue range)
            : base(innerStream)
        {
            if (range == null)
            {
                throw new ArgumentNullException("range");
            }
            if (!innerStream.CanSeek)
            {
                throw new ArgumentException(RS.Format(Resources.ByteRangeStreamNotSeekable, typeof(ByteRangeStream).Name), "innerStream");
            }
            if (innerStream.Length < 1)
            {
                throw new ArgumentOutOfRangeException("innerStream", innerStream.Length,
                    RS.Format(Resources.ByteRangeStreamEmpty, typeof(ByteRangeStream).Name));
            }
            if (range.From.HasValue && range.From.Value > innerStream.Length)
            {
                throw new ArgumentOutOfRangeException("range", range.From,
                    RS.Format(Resources.ByteRangeStreamInvalidFrom, innerStream.Length));
            }

            // Ranges are inclusive so 0-9 means the first 10 bytes
            long maxLength = innerStream.Length - 1;
            long upperbounds;
            if (range.To.HasValue)
            {
                if (range.From.HasValue)
                {
                    // e.g bytes=0-499 (the first 500 bytes offsets 0-499)
                    upperbounds = Math.Min(range.To.Value, maxLength);
                    _lowerbounds = range.From.Value;
                }
                else
                {
                    // e.g bytes=-500 (the final 500 bytes)
                    upperbounds = maxLength;
                    _lowerbounds = Math.Max(innerStream.Length - range.To.Value, 0);
                }
            }
            else
            {
                if (range.From.HasValue)
                {
                    // e.g bytes=500- (from byte offset 500 and up)
                    upperbounds = maxLength;
                    _lowerbounds = range.From.Value;
                }
                else
                {
                    // e.g. bytes=- (invalid so will never get here)
                    upperbounds = maxLength;
                    _lowerbounds = 0;
                }
            }

            _totalCount = upperbounds - _lowerbounds + 1;
            ContentRange = new ContentRangeHeaderValue(_lowerbounds, upperbounds, innerStream.Length);
        }
		public void Ctor ()
		{
			var v = new RangeItemHeaderValue (1, null);
			Assert.AreEqual ("1-", v.ToString (), "#1");

			v = new RangeItemHeaderValue (null, 1);
			Assert.AreEqual ("-1", v.ToString (), "#2");
		}
		public void Properties ()
		{
			var value = new RangeItemHeaderValue (3, 23);
			Assert.AreEqual (3, value.From, "#1");
			Assert.AreEqual (23, value.To, "#2");

			value = new RangeItemHeaderValue (5, null);
			Assert.AreEqual (5, value.From, "#3");
			Assert.IsNull (value.To, "#4");
		}
        public void Ctor_ThrowsIfCantSeekInnerStream()
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(false);
            RangeItemHeaderValue range = new RangeItemHeaderValue(0, 10);

            // Act/Assert
            Assert.ThrowsArgument(() => new ByteRangeStream(mockInnerStream.Object, range), "innerStream");
        }
        public void Ctor_ThrowsIfLowerRangeExceedsInnerStream()
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(5);
            RangeItemHeaderValue range = new RangeItemHeaderValue(10, 20);

            // Act/Assert
            Assert.ThrowsArgumentOutOfRange(() => new ByteRangeStream(mockInnerStream.Object, range), "range",
                "The 'From' value of the range must be less than or equal to 5.", false, 10);
        }
        public void ToString_UseDifferentRangeItems_AllSerializedCorrectly()
        {
            // Make sure ToString() doesn't add any separators.
            RangeItemHeaderValue rangeItem = new RangeItemHeaderValue(1000000000, 2000000000);
            Assert.Equal("1000000000-2000000000", rangeItem.ToString());

            rangeItem = new RangeItemHeaderValue(5, null);
            Assert.Equal("5-", rangeItem.ToString());

            rangeItem = new RangeItemHeaderValue(null, 10);
            Assert.Equal("-10", rangeItem.ToString());
        }
        public void Ctor_ThrowsIfInnerStreamLengthIsLessThanOne(int innerLength)
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(innerLength);
            RangeItemHeaderValue range = new RangeItemHeaderValue(null, 0);

            // Act/Assert
            Assert.ThrowsArgumentOutOfRange(() => new ByteRangeStream(mockInnerStream.Object, range), "innerStream",
                "The stream over which 'ByteRangeStream' provides a range view must have a length greater than or equal to 1.",
                false, innerLength);
        }
        public void GetHashCode_UseSameAndDifferentRangeItems_SameOrDifferentHashCodes()
        {
            RangeItemHeaderValue rangeItem1 = new RangeItemHeaderValue(1, 2);
            RangeItemHeaderValue rangeItem2 = new RangeItemHeaderValue(1, null);
            RangeItemHeaderValue rangeItem3 = new RangeItemHeaderValue(null, 2);
            RangeItemHeaderValue rangeItem4 = new RangeItemHeaderValue(2, 2);
            RangeItemHeaderValue rangeItem5 = new RangeItemHeaderValue(1, 2);

            Assert.NotEqual(rangeItem1.GetHashCode(), rangeItem2.GetHashCode());
            Assert.NotEqual(rangeItem1.GetHashCode(), rangeItem3.GetHashCode());
            Assert.NotEqual(rangeItem1.GetHashCode(), rangeItem4.GetHashCode());
            Assert.Equal(rangeItem1.GetHashCode(), rangeItem5.GetHashCode());
        }
        public void Ctor_SetsContentRange()
        {
            // Arrange
            ContentRangeHeaderValue expectedContentRange = new ContentRangeHeaderValue(5, 9, 20);
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(20);
            RangeItemHeaderValue range = new RangeItemHeaderValue(5, 9);

            // Act
            ByteRangeStream rangeStream = new ByteRangeStream(mockInnerStream.Object, range);

            // Assert
            Assert.Equal(expectedContentRange, rangeStream.ContentRange);
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            RangeItemHeaderValue rangeItem1 = new RangeItemHeaderValue(1, 2);
            RangeItemHeaderValue rangeItem2 = new RangeItemHeaderValue(1, null);
            RangeItemHeaderValue rangeItem3 = new RangeItemHeaderValue(null, 2);
            RangeItemHeaderValue rangeItem4 = new RangeItemHeaderValue(2, 2);
            RangeItemHeaderValue rangeItem5 = new RangeItemHeaderValue(1, 2);

            Assert.False(rangeItem1.Equals(rangeItem2), "1-2 vs. 1-.");
            Assert.False(rangeItem2.Equals(rangeItem1), "1- vs. 1-2.");
            Assert.False(rangeItem1.Equals(null), "1-2 vs. null.");
            Assert.False(rangeItem1.Equals(rangeItem3), "1-2 vs. -2.");
            Assert.False(rangeItem3.Equals(rangeItem1), "-2 vs. 1-2.");
            Assert.False(rangeItem1.Equals(rangeItem4), "1-2 vs. 2-2.");
            Assert.True(rangeItem1.Equals(rangeItem5), "1-2 vs. 1-2.");
        }
Exemple #15
0
 public static bool TryReadRangeItem(RangeItemHeaderValue range, long contentLength,
     out long start, out long end)
 {
     if (range.From != null)
     {
         start = range.From.Value;
         if (range.To != null)
             end = range.To.Value;
         else
             end = contentLength - 1;
     }
     else
     {
         end = contentLength - 1;
         if (range.To != null)
             start = contentLength - range.To.Value;
         else
             start = 0;
     }
     return (start < contentLength && end < contentLength);
 }
        internal static int GetRangeItemLength(string input, int startIndex, out RangeItemHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            // This parser parses number ranges: e.g. '1-2', '1-', '-2'.

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return 0;
            }

            // Caller must remove leading whitespaces. If not, we'll return 0.
            int current = startIndex;

            // Try parse the first value of a value pair.
            int fromStartIndex = current;
            int fromLength = HttpRuleParser.GetNumberLength(input, current, false);

            if (fromLength > HttpRuleParser.MaxInt64Digits)
            {
                return 0;
            }

            current = current + fromLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // Afer the first value, the '-' character must follow.
            if ((current == input.Length) || (input[current] != '-'))
            {
                // We need a '-' character otherwise this can't be a valid range.
                return 0;
            }

            current++; // skip the '-' character
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            int toStartIndex = current;
            int toLength = 0;

            // If we didn't reach the end of the string, try parse the second value of the range.
            if (current < input.Length)
            {
                toLength = HttpRuleParser.GetNumberLength(input, current, false);

                if (toLength > HttpRuleParser.MaxInt64Digits)
                {
                    return 0;
                }

                current = current + toLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }

            if ((fromLength == 0) && (toLength == 0))
            {
                return 0; // At least one value must be provided in order to be a valid range.
            }

            // Try convert first value to int64
            long from = 0;
            if ((fromLength > 0) && !HeaderUtilities.TryParseInt64(input.Substring(fromStartIndex, fromLength), out from))
            {
                return 0;
            }

            // Try convert second value to int64
            long to = 0;
            if ((toLength > 0) && !HeaderUtilities.TryParseInt64(input.Substring(toStartIndex, toLength), out to))
            {
                return 0;
            }

            // 'from' must not be greater than 'to'
            if ((fromLength > 0) && (toLength > 0) && (from > to))
            {
                return 0;
            }

            parsedValue = new RangeItemHeaderValue((fromLength == 0 ? (long?)null : (long?)from),
                (toLength == 0 ? (long?)null : (long?)to));
            return current - startIndex;
        }
 public void Ctor_ThrowsOnNullInnerStream()
 {
     RangeItemHeaderValue range = new RangeItemHeaderValue(0, 10);
     Assert.ThrowsArgumentNull(() => new ByteRangeStream(innerStream: null, range: range), "innerStream");
 }
        public void ReadByte_ReadsEffectiveLengthTimes(int from, int to, int innerLength, int effectiveLength)
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(innerLength);
            RangeItemHeaderValue range = new RangeItemHeaderValue(from, to);

            // Act
            ByteRangeStream rangeStream = new ByteRangeStream(mockInnerStream.Object, range);
            int counter = 0;
            while (rangeStream.ReadByte() != -1)
            {
                counter++;
            }

            // Assert
            Assert.Equal(effectiveLength, counter);
            mockInnerStream.Verify(s => s.ReadByte(), Times.Exactly(effectiveLength));
        }
        public void Ctor_SetsLength(int from, int to, int innerLength, int expectedLength)
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(innerLength);
            RangeItemHeaderValue range = new RangeItemHeaderValue(from, to);

            // Act
            ByteRangeStream rangeStream = new ByteRangeStream(mockInnerStream.Object, range);

            // Assert
            Assert.Equal(expectedLength, rangeStream.Length);
        }
Exemple #20
0
        internal static int GetRangeItemLength(string input, int startIndex, out RangeItemHeaderValue parsedValue)
        {
            Debug.Assert(startIndex >= 0);

            // This parser parses number ranges: e.g. '1-2', '1-', '-2'.

            parsedValue = null;

            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Caller must remove leading whitespace. If not, we'll return 0.
            int current = startIndex;

            // Try parse the first value of a value pair.
            int fromStartIndex = current;
            int fromLength     = HttpRuleParser.GetNumberLength(input, current, false);

            if (fromLength > HttpRuleParser.MaxInt64Digits)
            {
                return(0);
            }

            current = current + fromLength;
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            // After the first value, the '-' character must follow.
            if ((current == input.Length) || (input[current] != '-'))
            {
                // We need a '-' character otherwise this can't be a valid range.
                return(0);
            }

            current++; // skip the '-' character
            current = current + HttpRuleParser.GetWhitespaceLength(input, current);

            int toStartIndex = current;
            int toLength     = 0;

            // If we didn't reach the end of the string, try parse the second value of the range.
            if (current < input.Length)
            {
                toLength = HttpRuleParser.GetNumberLength(input, current, false);

                if (toLength > HttpRuleParser.MaxInt64Digits)
                {
                    return(0);
                }

                current = current + toLength;
                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
            }

            if ((fromLength == 0) && (toLength == 0))
            {
                return(0); // At least one value must be provided in order to be a valid range.
            }

            // Try convert first value to int64
            long from = 0;

            if ((fromLength > 0) && !HeaderUtilities.TryParseInt64(input, fromStartIndex, fromLength, out from))
            {
                return(0);
            }

            // Try convert second value to int64
            long to = 0;

            if ((toLength > 0) && !HeaderUtilities.TryParseInt64(input, toStartIndex, toLength, out to))
            {
                return(0);
            }

            // 'from' must not be greater than 'to'
            if ((fromLength > 0) && (toLength > 0) && (from > to))
            {
                return(0);
            }

            parsedValue = new RangeItemHeaderValue((fromLength == 0 ? (long?)null : (long?)from),
                                                   (toLength == 0 ? (long?)null : (long?)to));
            return(current - startIndex);
        }
 public void Ctor_ValidFormat_SuccessfullyCreated()
 {
     RangeItemHeaderValue rangeItem = new RangeItemHeaderValue(1, 2);
     Assert.Equal(1, rangeItem.From);
     Assert.Equal(2, rangeItem.To);
 }
        public void Clone_Call_CloneFieldsMatchSourceFields()
        {
            RangeItemHeaderValue source = new RangeItemHeaderValue(1, 2);
            RangeItemHeaderValue clone = (RangeItemHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.From, clone.From);
            Assert.Equal(source.To, clone.To);

            source = new RangeItemHeaderValue(1, null);
            clone = (RangeItemHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.From, clone.From);
            Assert.Equal(source.To, clone.To);

            source = new RangeItemHeaderValue(null, 2);
            clone = (RangeItemHeaderValue)((ICloneable)source).Clone();
            Assert.Equal(source.From, clone.From);
            Assert.Equal(source.To, clone.To);
        }
        public void Read_ReadsEffectiveLengthBytes(int from, int to, int innerLength, int effectiveLength)
        {
            // Arrange
            Mock<Stream> mockInnerStream = new Mock<Stream>();
            mockInnerStream.Setup(s => s.CanSeek).Returns(true);
            mockInnerStream.Setup(s => s.Length).Returns(innerLength);
            RangeItemHeaderValue range = new RangeItemHeaderValue(from, to);
            byte[] data = new byte[25];
            int offset = 5;

            // Act
            ByteRangeStream rangeStream = new ByteRangeStream(mockInnerStream.Object, range);
            rangeStream.Read(data, offset, data.Length);

            // Assert
            mockInnerStream.Verify(s => s.Read(data, offset, effectiveLength), Times.Once());
        }
		public ByteRangeStream(Stream innerStream, RangeItemHeaderValue range)
			: base(innerStream)
		{
			// Ranges are inclusive so 0-9 means the first 10 bytes
			long maxLength = innerStream.Length - 1;
			long upperbounds;
			if (range.To.HasValue)
			{
				if (range.From.HasValue)
				{
					// e.g bytes=0-499 (the first 500 bytes offsets 0-499)
					upperbounds = Math.Min(range.To.Value, maxLength);
					_lowerbounds = range.From.Value;
				}
				else
				{
					// e.g bytes=-500 (the final 500 bytes)
					upperbounds = maxLength;
					_lowerbounds = Math.Max(innerStream.Length - range.To.Value, 0);
				}
			}
			else
			{
				if (range.From.HasValue)
				{
					// e.g bytes=500- (from byte offset 500 and up)
					upperbounds = maxLength;
					_lowerbounds = range.From.Value;
				}
				else
				{
					// e.g. bytes=- (invalid so will never get here)
					upperbounds = maxLength;
					_lowerbounds = 0;
				}
			}

			_totalCount = upperbounds - _lowerbounds + 1;
			ContentRange = new ContentRangeHeaderValue(_lowerbounds, upperbounds, innerStream.Length);
		}
Exemple #25
0
 internal static int GetRangeItemLength(string input, int startIndex, out RangeItemHeaderValue parsedValue)
 {
 }