public Task GetAsync() { var webRequest = (HttpWebRequest)WebRequest.Create(RemoteLocation); webRequest.AllowAutoRedirect = true; webRequest.Method = WebRequestMethods.Http.Get; webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return(Task.Factory.FromAsync(webRequest.BeginGetResponse, (Func <IAsyncResult, WebResponse>)webRequest.BetterEndGetResponse, this).ContinueWith(asyncResult => { // Logging.Logger.Message("In FromAsync Task::::::{0}", RemoteLocation); try { if (_isCanceled) { _failed(RemoteLocation); return; } var httpWebResponse = asyncResult.Result as HttpWebResponse; _lastStatus = httpWebResponse.StatusCode; if (httpWebResponse.StatusCode == HttpStatusCode.OK) { _lastModified = httpWebResponse.LastModified; _contentLength = httpWebResponse.ContentLength; ActualRemoteLocation = httpWebResponse.ResponseUri; if (_isCanceled) { _failed(RemoteLocation); return; } if (string.IsNullOrEmpty(_filename)) { _filename = httpWebResponse.ContentDispositionFilename(); if (string.IsNullOrEmpty(_filename)) { _filename = ActualRemoteLocation.LocalPath.Substring(ActualRemoteLocation.LocalPath.LastIndexOf('/') + 1); if (string.IsNullOrEmpty(_filename) || ServerSideExtensions.Contains(Path.GetExtension(_filename))) { ActualRemoteLocation.GetLeftPart(UriPartial.Path).MakeSafeFileName(); } } } try { if (Filename.FileIsLocalAndExists()) { var md5 = string.Empty; try { if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("x-ms-meta-MD5")) { // it's coming from azure, check the value of the md5 and compare against the file on disk ... better than date/size matching. md5 = httpWebResponse.Headers["x-ms-meta-MD5"].Trim(); } else if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("Content-MD5")) { md5 = httpWebResponse.Headers["Content-MD5"].Trim(); if (md5.EndsWith("=")) { md5 = Convert.FromBase64CharArray(md5.ToCharArray(), 0, md5.Length).ToUtf8String(); } } } catch { // something gone screwy? } if (!string.IsNullOrEmpty(md5)) { var localMD5 = string.Empty; using (var stream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { localMD5 = MD5.Create().ComputeHash(stream).ToHexString(); } if (string.Equals(md5, localMD5, StringComparison.CurrentCultureIgnoreCase)) { // it's the same file. We're not doin nothing. _completed(RemoteLocation); return; } // only do the size/date comparison if the server doesn't provide an MD5 } else if (_contentLength > 0 && _lastModified.CompareTo(File.GetCreationTime(Filename)) <= 0 && _contentLength == new FileInfo(Filename).Length) { // file is identical to the one on disk. // we're not going to reget it. :p _completed(RemoteLocation); return; } } // we should open the file here, so that it's ready when we start the async read cycle. if (_filestream != null) { _failed(RemoteLocation); throw new CoAppException("THIS VERY BAD AND UNEXPECTED. (Failed to close?)"); } _filestream = File.Open(Filename, FileMode.Create); if (_isCanceled) { _failed(RemoteLocation); return; } var tcs = new TaskCompletionSource <HttpWebResponse>(TaskCreationOptions.AttachedToParent); tcs.Iterate(AsyncReadImpl(tcs, httpWebResponse)); return; } catch { // failed to actually create the file, or some other catastrophic failure. _failed(RemoteLocation); return; } } // this is not good. _failed(RemoteLocation); } catch (AggregateException e) { _failed(RemoteLocation); // at this point, we've failed somehow if (_lastStatus == HttpStatusCode.NotImplemented) { // we never got started. Probably not found. } var ee = e.Flatten(); foreach (var ex in ee.InnerExceptions) { var wex = ex as WebException; if (wex != null) { // Console.WriteLine("Status:" + wex.Status); // Console.WriteLine("Response:" + wex.Response); // Console.WriteLine("Response:" + ((HttpWebResponse)wex.Response).StatusCode); } // Console.WriteLine(ex.GetType()); // Console.WriteLine(ex.Message); // Console.WriteLine(ex.StackTrace); } } catch { // Console.WriteLine(e.GetType()); // Console.WriteLine(e.Message); // Console.WriteLine(e.StackTrace); } }, TaskContinuationOptions.AttachedToParent)); }
private Task GetImplHttp(bool resumeExistingDownload = true) { var webRequest = (HttpWebRequest) WebRequest.Create(RemoteLocation); webRequest.AllowAutoRedirect = true; webRequest.Method = WebRequestMethods.Http.Get; if (IsCancelled) { return CancelledTask(); } return CoTask.Factory.FromAsync<WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse, this).ContinueWithParent( asyncResult => { try { var v = webRequest; if (IsCancelled) { Cancel(); } var httpWebResponse = asyncResult.Result as HttpWebResponse; LastStatus = httpWebResponse.StatusCode; if (httpWebResponse.StatusCode == HttpStatusCode.OK) { _lastModified = httpWebResponse.LastModified; _contentLength = httpWebResponse.ContentLength; ActualRemoteLocation = httpWebResponse.ResponseUri; if (IsCancelled) { Cancel(); } GenerateLocalFilename(); var filename = httpWebResponse.ContentDispositionFilename(); if (!string.IsNullOrEmpty(filename)) { GenerateLocalFilename(filename); } try { // we should open the file here, so that it's ready when we start the async read cycle. if( _filestream != null ) { throw new Exception("THIS VERY BAD AND UNEXPECTED."); } _filestream = File.Open(LocalFullPath, FileMode.Create); if (IsCancelled) { Cancel(); } var tcs = new TaskCompletionSource<HttpWebResponse>(TaskCreationOptions.AttachedToParent); ((Tasklet) tcs.Task).CancellationToken = Tasklet.CurrentCancellationToken; tcs.Iterate(AsyncReadImpl(tcs, httpWebResponse)); return; } catch { // failed to actually create the file, or some other catastrophic failure. Complete(); return; } } // this is not good. throw new Exception("Status Code other than OK"); } catch (WebException e) { try { LastStatus = ((HttpWebResponse) e.Response).StatusCode; } catch (Exception) { // if the fit hits the shan, just call it not found. LastStatus = HttpStatusCode.NotFound; } Complete(); } catch (AggregateException ae) { ae = ae.Flatten(); var e = ae.InnerExceptions[0] as WebException; if (e != null) { try { LastStatus = ((HttpWebResponse)e.Response).StatusCode; } catch (Exception) { // if the fit hits the shan, just call it not found. LastStatus = HttpStatusCode.NotFound; } } Complete(); return; } catch (Exception e) { Console.WriteLine("BAD ERROR: {0}\r\n{1}", e.Message, e.StackTrace); LastStatus = HttpStatusCode.NotFound; Complete(); return; } Console.WriteLine("Really? It gets here?"); Complete(); }); }
public Task GetAsync() { var webRequest = (HttpWebRequest)WebRequest.Create(RemoteLocation); webRequest.AllowAutoRedirect = true; webRequest.Method = WebRequestMethods.Http.Get; webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return Task.Factory.FromAsync(webRequest.BeginGetResponse, (Func<IAsyncResult, WebResponse>)webRequest.BetterEndGetResponse, this).ContinueWith(asyncResult => { // Logging.Logger.Message("In FromAsync Task::::::{0}", RemoteLocation); try { if (_isCanceled) { _failed(RemoteLocation); return; } var httpWebResponse = asyncResult.Result as HttpWebResponse; _lastStatus = httpWebResponse.StatusCode; if (httpWebResponse.StatusCode == HttpStatusCode.OK) { _lastModified = httpWebResponse.LastModified; _contentLength = httpWebResponse.ContentLength; ActualRemoteLocation = httpWebResponse.ResponseUri; if (_isCanceled) { _failed(RemoteLocation); return; } if (string.IsNullOrEmpty(_filename)) { _filename = httpWebResponse.ContentDispositionFilename(); if (string.IsNullOrEmpty(_filename)) { _filename = ActualRemoteLocation.LocalPath.Substring(ActualRemoteLocation.LocalPath.LastIndexOf('/') + 1); if (string.IsNullOrEmpty(_filename) || ServerSideExtensions.Contains(Path.GetExtension(_filename))) { ActualRemoteLocation.GetLeftPart(UriPartial.Path).MakeSafeFileName(); } } } try { if (Filename.FileIsLocalAndExists()) { var md5 = string.Empty; try { if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("x-ms-meta-MD5")) { // it's coming from azure, check the value of the md5 and compare against the file on disk ... better than date/size matching. md5 = httpWebResponse.Headers["x-ms-meta-MD5"].Trim(); } else if (httpWebResponse.Headers.AllKeys.ContainsIgnoreCase("Content-MD5")) { md5 = httpWebResponse.Headers["Content-MD5"].Trim(); if (md5.EndsWith("=")) { md5 = Convert.FromBase64CharArray(md5.ToCharArray(), 0, md5.Length).ToUtf8String(); } } } catch { // something gone screwy? } if (!string.IsNullOrEmpty(md5)) { var localMD5 = string.Empty; using (var stream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { localMD5 = MD5.Create().ComputeHash(stream).ToHexString(); } if (string.Equals(md5, localMD5, StringComparison.CurrentCultureIgnoreCase)) { // it's the same file. We're not doin nothing. _completed(RemoteLocation); return; } // only do the size/date comparison if the server doesn't provide an MD5 } else if (_contentLength > 0 && _lastModified.CompareTo(File.GetCreationTime(Filename)) <= 0 && _contentLength == new FileInfo(Filename).Length) { // file is identical to the one on disk. // we're not going to reget it. :p _completed(RemoteLocation); return; } } // we should open the file here, so that it's ready when we start the async read cycle. if (_filestream != null) { _failed(RemoteLocation); throw new CoAppException("THIS VERY BAD AND UNEXPECTED. (Failed to close?)"); } _filestream = File.Open(Filename, FileMode.Create); if (_isCanceled) { _failed(RemoteLocation); return; } var tcs = new TaskCompletionSource<HttpWebResponse>(TaskCreationOptions.AttachedToParent); tcs.Iterate(AsyncReadImpl(tcs, httpWebResponse)); return; } catch { // failed to actually create the file, or some other catastrophic failure. _failed(RemoteLocation); return; } } // this is not good. _failed(RemoteLocation); } catch (AggregateException e) { _failed(RemoteLocation); // at this point, we've failed somehow if (_lastStatus == HttpStatusCode.NotImplemented) { // we never got started. Probably not found. } var ee = e.Flatten(); foreach (var ex in ee.InnerExceptions) { var wex = ex as WebException; if (wex != null) { // Console.WriteLine("Status:" + wex.Status); // Console.WriteLine("Response:" + wex.Response); // Console.WriteLine("Response:" + ((HttpWebResponse)wex.Response).StatusCode); } // Console.WriteLine(ex.GetType()); // Console.WriteLine(ex.Message); // Console.WriteLine(ex.StackTrace); } } catch { // Console.WriteLine(e.GetType()); // Console.WriteLine(e.Message); // Console.WriteLine(e.StackTrace); } }, TaskContinuationOptions.AttachedToParent); }