Example #1
0
        /// <summary>
        /// Initialize the buffered range reader stream provided request URL.
        /// </summary>
        /// <param name="requestUri">The request URL.</param>
        /// <returns>The buffered range reader stream.</returns>
        public async Task <Stream> GetStreamAsync(Uri requestUri)
        {
            // Determine if the exists endpoint's length and whether it supports range requests.
            using (var request = new HttpRequestMessage(HttpMethod.Head, requestUri))
                using (var response = await _httpClient.SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new MiniZipException(string.Format(
                                                       Strings.UnsuccessfulHttpStatusCodeWhenGettingLength,
                                                       (int)response.StatusCode,
                                                       response.ReasonPhrase));
                    }

                    if (response.Content?.Headers?.ContentLength == null)
                    {
                        throw new MiniZipException(Strings.ContentLengthHeaderNotFound);
                    }

                    if (response.Headers.AcceptRanges == null ||
                        !response.Headers.AcceptRanges.Contains(HttpConstants.BytesUnit))
                    {
                        throw new MiniZipException(string.Format(
                                                       Strings.AcceptRangesBytesValueNotFoundFormat,
                                                       HttpConstants.BytesUnit));
                    }

                    var length = response.Content.Headers.ContentLength.Value;

                    var etagBehavior = ETagBehavior;
                    var etag         = response.Headers?.ETag;
                    if (etag != null && (etag.IsWeak || etagBehavior == ETagBehavior.Ignore))
                    {
                        etag = null;
                    }

                    if (etag == null && etagBehavior == ETagBehavior.Required)
                    {
                        throw new MiniZipException(string.Format(
                                                       Strings.MissingETagHeader,
                                                       nameof(MiniZip.ETagBehavior),
                                                       nameof(ETagBehavior.Required)));
                    }

                    var httpRangeReader    = new HttpRangeReader(_httpClient, requestUri, length, etag);
                    var bufferSizeProvider = new ZipBufferSizeProvider(FirstBufferSize, SecondBufferSize, BufferGrowthExponent);
                    var stream             = new BufferedRangeStream(httpRangeReader, length, bufferSizeProvider);

                    return(stream);
                }
        }
Example #2
0
            public void GivesExpectedSequence()
            {
                // Arrange
                var target = new ZipBufferSizeProvider(100, 5000, 25);

                // Act
                var values = Enumerable
                             .Range(0, 8)
                             .Select(x => target.GetNextBufferSize())
                             .ToList();

                // Assert
                Assert.Equal(
                    new[] { 100, 5000, 125000, 3125000, 78125000, 1953125000, int.MaxValue, int.MaxValue },
                    values);
            }
Example #3
0
        public async Task WithSelfUsingFileRangeReader(string path)
        {
            // Arrange
            using (var memoryStream = TestUtility.BufferTestData(path))
            {
                var fullPath           = Path.Combine(TestUtility.TestDataDirectory, path);
                var length             = new FileInfo(fullPath).Length;
                var fileRangeReader    = new FileRangeReader(fullPath);
                var bufferSizeProvider = new ZipBufferSizeProvider(firstBufferSize: 1, secondBufferSize: 1, exponent: 2);
                using (var bufferedRangeStream = new BufferedRangeStream(fileRangeReader, length, bufferSizeProvider))
                {
                    // Act
                    var a = await TestUtility.ReadWithMiniZipAsync(memoryStream);

                    var b = await TestUtility.ReadWithMiniZipAsync(bufferedRangeStream);

                    // Assert
                    TestUtility.VerifyJsonEquals(a.Data, b.Data);
                    Assert.Equal(a.Success, b.Success);
                    Assert.Equal(a.Exception?.Message, b.Exception?.Message);
                    Assert.Equal(a.Exception?.GetType(), b.Exception?.GetType());
                }
            }
        }