コード例 #1
0
        private static void GetSizeAndSource(DownloadOptions options)
        {
            var client   = new SimpleHttpGetByRangeClient(options.Uri);
            var response = client.Get(options.Uri, 0, 1);

            if (response != null)
            {
                if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location))
                {
                    if (response.Location != options.Uri.AbsoluteUri)
                    {
                        options.Uri = new Uri(response.Location);
                        Console.WriteLine("Detected Redirect: " + options.Uri);
                        GetSizeAndSource(options);
                    }
                    else
                    {
                        throw new ArgumentException("Supplied Url has no source");
                    }
                }
                else if (response.WasSuccessful && response.ContentRangeLength >= 0)
                {
                    options.FileSize = response.ContentRangeLength;
                }
                else
                {
                    throw new Exception("Response was not successful, status code: " + response.StatusCode);
                }
            }
        }
コード例 #2
0
        public void ReasonableTimeoutPreventsIOException()
        {
            var client   = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1000 * 10);
            var response = client.Get(0, 1024 * 2);//something large

            Assert.NotNull(response);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: basespace/TerminalVelocity
        private static void GetSizeAndSource(DownloadOptions options)
        {
            var client = new SimpleHttpGetByRangeClient(options.Uri);
            var response = client.Get(options.Uri, 0, 1);

            if (response != null)
            {
                if (response.IsStatusCodeRedirect && !String.IsNullOrWhiteSpace(response.Location))
                {
                    if (response.Location != options.Uri.AbsoluteUri)
                    {
                        options.Uri = new Uri(response.Location);
                        Console.WriteLine("Detected Redirect: " + options.Uri);
                        GetSizeAndSource(options);
                    }
                    else
                    {
                        throw new ArgumentException("Supplied Url has no source");
                    }
                }
                 else if (response.WasSuccessful && response.ContentRangeLength >= 0)
                {
                    options.FileSize = response.ContentRangeLength;
                }
                else
                {
                    throw new Exception("Response was not successful, status code: " + response.StatusCode);
                }

            }
        }
コード例 #4
0
        public void TimeoutQuitsAsExpected()
        {
            var client   = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1);
            var response = client.Get(0, 1024 * 1024 * 10);//something large

            Assert.Fail("Timeout didn't happen");
        }
コード例 #5
0
        public void CanWeCreateRequestCorrectlyCustomPort()
        {
            Uri uri      = new Uri(@"http://google.com:8080/foo/bar?baz=boo");
            var request  = SimpleHttpGetByRangeClient.BuildHttpRequest(uri, 0, 100);
            var expected = string.Format(SampleHttpRequest, uri.PathAndQuery, "google.com:8080", 0, 99);

            Assert.AreEqual(request, expected);
        }
コード例 #6
0
        public void CanWeCreateRequestCorrectlyDefaultPort()
        {
            Uri uri      = new Uri(@"http://google.com");
            var request  = SimpleHttpGetByRangeClient.BuildHttpRequest(uri, 0, 100);
            var expected = string.Format(SampleHttpRequest, uri.PathAndQuery, uri.Host, 0, 99);

            Assert.AreEqual(request, expected);
        }
コード例 #7
0
 public void ReadMultipleGetsInARow()
 {
     int reads = 5;
     var client = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1000 * 10);
     for (int i = 0; i < reads; i++)
     {
         var response = client.Get(i, 1024 * i + 1);//something large
         Assert.NotNull(response);
     }
 }
コード例 #8
0
        public void ReadMultipleGetsInARow()
        {
            int reads  = 5;
            var client = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1000 * 10);

            for (int i = 0; i < reads; i++)
            {
                var response = client.Get(i, 1024 * i + 1);//something large
                Assert.NotNull(response);
            }
        }
コード例 #9
0
        public void ChangeInTimeoutWorks()
        {
            SimpleHttpGetByRangeClient client = null;
            SimpleHttpResponse response;
            try
            {
                client = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1);
                 response = client.Get(0, 1024*10); //something large
            }
            catch (IOException ex)
            {

            }
            client.ConnectionTimeout = 1000*10;
             response = client.Get(0, 1024 * 10); //gave it enough time
            Assert.NotNull(response);
        }
コード例 #10
0
        public void ChangeInTimeoutWorks()
        {
            SimpleHttpGetByRangeClient client = null;
            SimpleHttpResponse         response;

            try
            {
                client   = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1);
                response = client.Get(0, 1024 * 10); //something large
            }
            catch (IOException ex)
            {
            }
            client.ConnectionTimeout = 1000 * 10;
            response = client.Get(0, 1024 * 10);  //gave it enough time
            Assert.NotNull(response);
        }
コード例 #11
0
        public void SimpleGetClientCanDownloadTwentyMegFileSynchronously()
        {
            var timer  = new Stopwatch();
            var uri    = new Uri(Constants.TWENTY_MEG_FILE);
            var client = new SimpleHttpGetByRangeClient(uri);
            var path   = SafePath("sites_vcf.gz");

            timer.Start();

            using (FileStream output = File.Create(path))
            {
                const int chunksize = 1024 * 1000 * 2;
                var       response  = client.Get(uri, 0, chunksize);
                output.Write(response.Content, 0, (int)response.ContentLength);

                long currentFileSize = (int)response.ContentLength;
                long fileSize        = response.ContentRangeLength.Value;
                while (currentFileSize < fileSize)
                {
                    SimpleHttpResponse loopResponse;
                    long left = fileSize - currentFileSize;
                    Debug.WriteLine("chunk start {0} length {1} ", currentFileSize, left < chunksize ? left : chunksize);
                    loopResponse = client.Get(new Uri(Constants.TWENTY_MEG_FILE), currentFileSize, left < chunksize ? left : chunksize);
                    output.Write(loopResponse.Content, 0, (int)loopResponse.ContentLength);
                    currentFileSize += loopResponse.ContentLength;
                }
            }

            timer.Stop();
            Debug.WriteLine("total {0}ms or {1}secs", timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000);

            using (Stream fs = File.OpenRead(path))
            {
                using (Stream gzipStream = new GZipInputStream(fs))
                {
                    using (var reader = new StreamReader(gzipStream))
                    {
                        reader.Read();
                    }
                }
            }
        }
コード例 #12
0
        public void SimpleGetClientGetsFirst100Bytes()
        {
            var timer = new Stopwatch();

            timer.Start();
            var uri      = new Uri(Constants.ONE_GIG_FILE);
            var client   = new SimpleHttpGetByRangeClient(uri);
            var response = client.Get(uri, 0, 100);

            timer.Stop();
            Debug.WriteLine("total {0}ms or {1}secs", timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000);
            Assert.NotNull(response);
            Assert.True(response.ContentLength == 100);
            Assert.True(response.ContentRangeLength == 1297662912);
            Assert.True(response.ContentRangeStart == 0);
            Assert.True(response.ContentRangeStop == 99);
            Assert.True(response.StatusCode == 206);
            Assert.NotNull(response.Content);
            Assert.True(response.ContentLength == response.Content.Length);
        }
コード例 #13
0
 public void TimeoutQuitsAsExpected()
 {
     var client =  new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1);
     var response = client.Get(0, 1024*1024*10);//something large
     Assert.Fail("Timeout didn't happen");
 }
コード例 #14
0
 public void ReasonableTimeoutPreventsIOException()
 {
     var client = new SimpleHttpGetByRangeClient(new Uri(Constants.ONE_GIG_FILE_S_SL), timeout: 1000 * 10);
     var response = client.Get(0, 1024 * 2);//something large
     Assert.NotNull(response);
 }
コード例 #15
0
 public void SimpleGetClientGetsFirst100Bytes()
 {
     var timer = new Stopwatch();
     timer.Start();
     var uri = new Uri(Constants.ONE_GIG_FILE);
     var client = new SimpleHttpGetByRangeClient(uri);
     var response =client.Get(uri, 0, 100);
     timer.Stop();
     Debug.WriteLine("total {0}ms or {1}secs", timer.ElapsedMilliseconds, timer.ElapsedMilliseconds/1000);
     Assert.NotNull(response);
     Assert.True(response.ContentLength == 100);
     Assert.True(response.ContentRangeLength == 1297662912);
     Assert.True(response.ContentRangeStart == 0);
     Assert.True(response.ContentRangeStop == 99);
     Assert.True(response.StatusCode == 206);
     Assert.NotNull(response.Content);
     Assert.True(response.ContentLength == response.Content.Length);
 }
コード例 #16
0
        public void SimpleGetClientCanDownloadTwentyMegFileSynchronously()
        {
            var timer = new Stopwatch();
            var uri = new Uri(Constants.TWENTY_MEG_FILE);
            var client = new SimpleHttpGetByRangeClient(uri);
            var path = SafePath("sites_vcf.gz");
            timer.Start();

            using (FileStream output = File.Create(path))
            {
                const int chunksize = 1024*1000*2;
                var response = client.Get(uri, 0, chunksize);
                output.Write(response.Content, 0, (int)response.ContentLength);

                long currentFileSize = (int)response.ContentLength;
                long fileSize = response.ContentRangeLength.Value;
                while (currentFileSize < fileSize)
                {
                    SimpleHttpResponse loopResponse;
                    long left = fileSize - currentFileSize;
                    Debug.WriteLine("chunk start {0} length {1} ", currentFileSize, left < chunksize ? left : chunksize);
                    loopResponse = client.Get(new Uri(Constants.TWENTY_MEG_FILE), currentFileSize, left < chunksize ? left : chunksize);
                    output.Write(loopResponse.Content, 0, (int)loopResponse.ContentLength);
                    currentFileSize += loopResponse.ContentLength;
                }

            }

            timer.Stop();
            Debug.WriteLine("total {0}ms or {1}secs", timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000);

            using (Stream fs = File.OpenRead(path))
            {
                using (Stream gzipStream =  new GZipInputStream(fs) )
                {
                    using (var reader = new StreamReader(gzipStream))
                    {
                        reader.Read();
                    }
                }
            }
        }