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); } } }
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); }
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"); }
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); }
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); }
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); } }
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(); } } } }
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); }
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"); }
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); }
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(); } } } }