private bool PatchSingleFile(DownloadQueueItem item, Hashtable dependencyTable)
 {
     if (item._hashCollection == null)
     {
         return false;
     }
     string location = null;
     using (HashCollection.HashEnumerator enumerator = item._hashCollection.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             string compositString = enumerator.Current.CompositString;
             if (dependencyTable.Contains(compositString))
             {
                 location = (string) dependencyTable[compositString];
                 goto Label_0062;
             }
         }
     }
 Label_0062:
     if (location == null)
     {
         return false;
     }
     if (this._fCancelPending)
     {
         return false;
     }
     if (!FileHashVerified(item._hashCollection, location))
     {
         Logger.AddInternalState("Hash verify failed for " + location + ", not using it for file patching.");
         return false;
     }
     FileStream patchSourceStream = null;
     FileStream patchTargetStream = null;
     try
     {
         patchSourceStream = GetPatchSourceStream(location);
         if (patchSourceStream == null)
         {
             return false;
         }
         Directory.CreateDirectory(Path.GetDirectoryName(item._targetPath));
         patchTargetStream = GetPatchTargetStream(item._targetPath);
         if (patchTargetStream == null)
         {
             return false;
         }
         this._eventArgs._fileSourceUri = item._sourceUri;
         this._eventArgs.FileLocalPath = item._targetPath;
         this._eventArgs.Cookie = null;
         this._eventArgs._fileResponseUri = null;
         this.CheckForSizeLimit((ulong) patchSourceStream.Length, true);
         this._accumulatedBytesTotal += patchSourceStream.Length;
         this.SetBytesTotal();
         this.OnModified();
         int count = 0;
         int tickCount = Environment.TickCount;
         patchTargetStream.SetLength(patchSourceStream.Length);
         patchTargetStream.Position = 0L;
         do
         {
             if (this._fCancelPending)
             {
                 return false;
             }
             count = patchSourceStream.Read(this._buffer, 0, this._buffer.Length);
             if (count > 0)
             {
                 patchTargetStream.Write(this._buffer, 0, count);
             }
             this._eventArgs._bytesCompleted += count;
             this._eventArgs._progress = (int) ((this._eventArgs._bytesCompleted * 100L) / this._eventArgs._bytesTotal);
             this.OnModifiedWithThrottle(ref tickCount);
         }
         while (count > 0);
     }
     finally
     {
         if (patchSourceStream != null)
         {
             patchSourceStream.Close();
         }
         if (patchTargetStream != null)
         {
             patchTargetStream.Close();
         }
     }
     this._eventArgs.Cookie = item._cookie;
     this._eventArgs._filesCompleted++;
     this.OnModified();
     DownloadResult result = new DownloadResult {
         ResponseUri = null
     };
     this._downloadResults.Add(result);
     Logger.AddInternalState(item._targetPath + " is patched from store.");
     return true;
 }
 protected void DownloadSingleFile(FileDownloader.DownloadQueueItem next)
 {
     Logger.AddMethodCall("DownloadSingleFile called");
     Logger.AddInternalState("DownloadQueueItem : " + ((next != null) ? next.ToString() : "null"));
     WebRequest request = WebRequest.Create(next._sourceUri);
     request.Credentials = CredentialCache.DefaultCredentials;
     RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
     request.CachePolicy = policy;
     HttpWebRequest httpreq = request as HttpWebRequest;
     if (httpreq != null)
     {
         httpreq.UnsafeAuthenticatedConnectionSharing = true;
         httpreq.AutomaticDecompression = DecompressionMethods.GZip;
         httpreq.CookieContainer = GetUriCookieContainer(httpreq.RequestUri);
         IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
         if (defaultWebProxy != null)
         {
             defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
         }
         Logger.AddInternalState("HttpWebRequest=" + Logger.Serialize(httpreq));
     }
     if (!base._fCancelPending)
     {
         WebResponse response = null;
         try
         {
             response = request.GetResponse();
             UriHelper.ValidateSupportedScheme(response.ResponseUri);
             if (!base._fCancelPending)
             {
                 base._eventArgs._fileSourceUri = next._sourceUri;
                 base._eventArgs._fileResponseUri = response.ResponseUri;
                 base._eventArgs.FileLocalPath = next._targetPath;
                 base._eventArgs.Cookie = null;
                 if (response.ContentLength > 0L)
                 {
                     base.CheckForSizeLimit((ulong) response.ContentLength, false);
                     base._accumulatedBytesTotal += response.ContentLength;
                 }
                 base.SetBytesTotal();
                 base.OnModified();
                 Stream responseStream = null;
                 Stream outputFileStream = null;
                 int tickCount = Environment.TickCount;
                 try
                 {
                     responseStream = response.GetResponseStream();
                     Directory.CreateDirectory(Path.GetDirectoryName(next._targetPath));
                     outputFileStream = GetOutputFileStream(next._targetPath);
                     if (outputFileStream != null)
                     {
                         int num3;
                         long num2 = 0L;
                         if (response.ContentLength > 0L)
                         {
                             outputFileStream.SetLength(response.ContentLength);
                         }
                         do
                         {
                             if (base._fCancelPending)
                             {
                                 return;
                             }
                             num3 = responseStream.Read(base._buffer, 0, base._buffer.Length);
                             if (num3 > 0)
                             {
                                 outputFileStream.Write(base._buffer, 0, num3);
                             }
                             base._eventArgs._bytesCompleted += num3;
                             if (response.ContentLength <= 0L)
                             {
                                 base._accumulatedBytesTotal += num3;
                                 base.SetBytesTotal();
                             }
                             num2 += num3;
                             if ((next._maxFileSize != -1) && (num2 > next._maxFileSize))
                             {
                                 throw new InvalidDeploymentException(ExceptionTypes.FileSizeValidation, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FileBeingDownloadedTooLarge"), new object[] { next._sourceUri.ToString(), next._maxFileSize }));
                             }
                             base.CheckForSizeLimit((ulong) num3, true);
                             if (base._eventArgs._bytesTotal > 0L)
                             {
                                 base._eventArgs._progress = (int) ((base._eventArgs._bytesCompleted * 100L) / base._eventArgs._bytesTotal);
                             }
                             base.OnModifiedWithThrottle(ref tickCount);
                         }
                         while (num3 > 0);
                         if (response.ContentLength != num2)
                         {
                             outputFileStream.SetLength(num2);
                         }
                     }
                 }
                 finally
                 {
                     if (responseStream != null)
                     {
                         responseStream.Close();
                     }
                     if (outputFileStream != null)
                     {
                         outputFileStream.Close();
                     }
                 }
                 base._eventArgs.Cookie = next._cookie;
                 base._eventArgs._filesCompleted++;
                 Logger.AddInternalState("HttpWebResponse=" + Logger.Serialize(response));
                 base.OnModified();
                 DownloadResult result = new DownloadResult {
                     ResponseUri = response.ResponseUri
                 };
                 result.ServerInformation.Server = response.Headers["Server"];
                 result.ServerInformation.PoweredBy = response.Headers["X-Powered-By"];
                 result.ServerInformation.AspNetVersion = response.Headers["X-AspNet-Version"];
                 base._downloadResults.Add(result);
             }
         }
         catch (InvalidOperationException exception)
         {
             throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception);
         }
         catch (IOException exception2)
         {
             throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception2);
         }
         catch (UnauthorizedAccessException exception3)
         {
             throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception3);
         }
         finally
         {
             if (response != null)
             {
                 response.Close();
             }
         }
     }
 }
        protected void DownloadSingleFile(FileDownloader.DownloadQueueItem next)
        {
            Logger.AddMethodCall("DownloadSingleFile called");
            Logger.AddInternalState("DownloadQueueItem : " + ((next != null) ? next.ToString() : "null"));
            WebRequest request = WebRequest.Create(next._sourceUri);

            request.Credentials = CredentialCache.DefaultCredentials;
            RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.BypassCache);

            request.CachePolicy = policy;
            HttpWebRequest httpreq = request as HttpWebRequest;

            if (httpreq != null)
            {
                httpreq.UnsafeAuthenticatedConnectionSharing = true;
                httpreq.AutomaticDecompression = DecompressionMethods.GZip;
                httpreq.CookieContainer        = GetUriCookieContainer(httpreq.RequestUri);
                IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
                if (defaultWebProxy != null)
                {
                    defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
                }
                Logger.AddInternalState("HttpWebRequest=" + Logger.Serialize(httpreq));
            }
            if (!base._fCancelPending)
            {
                WebResponse response = null;
                try
                {
                    response = request.GetResponse();
                    UriHelper.ValidateSupportedScheme(response.ResponseUri);
                    if (!base._fCancelPending)
                    {
                        base._eventArgs._fileSourceUri   = next._sourceUri;
                        base._eventArgs._fileResponseUri = response.ResponseUri;
                        base._eventArgs.FileLocalPath    = next._targetPath;
                        base._eventArgs.Cookie           = null;
                        if (response.ContentLength > 0L)
                        {
                            base.CheckForSizeLimit((ulong)response.ContentLength, false);
                            base._accumulatedBytesTotal += response.ContentLength;
                        }
                        base.SetBytesTotal();
                        base.OnModified();
                        Stream responseStream   = null;
                        Stream outputFileStream = null;
                        int    tickCount        = Environment.TickCount;
                        try
                        {
                            responseStream = response.GetResponseStream();
                            Directory.CreateDirectory(Path.GetDirectoryName(next._targetPath));
                            outputFileStream = GetOutputFileStream(next._targetPath);
                            if (outputFileStream != null)
                            {
                                int  num3;
                                long num2 = 0L;
                                if (response.ContentLength > 0L)
                                {
                                    outputFileStream.SetLength(response.ContentLength);
                                }
                                do
                                {
                                    if (base._fCancelPending)
                                    {
                                        return;
                                    }
                                    num3 = responseStream.Read(base._buffer, 0, base._buffer.Length);
                                    if (num3 > 0)
                                    {
                                        outputFileStream.Write(base._buffer, 0, num3);
                                    }
                                    base._eventArgs._bytesCompleted += num3;
                                    if (response.ContentLength <= 0L)
                                    {
                                        base._accumulatedBytesTotal += num3;
                                        base.SetBytesTotal();
                                    }
                                    num2 += num3;
                                    if ((next._maxFileSize != -1) && (num2 > next._maxFileSize))
                                    {
                                        throw new InvalidDeploymentException(ExceptionTypes.FileSizeValidation, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FileBeingDownloadedTooLarge"), new object[] { next._sourceUri.ToString(), next._maxFileSize }));
                                    }
                                    base.CheckForSizeLimit((ulong)num3, true);
                                    if (base._eventArgs._bytesTotal > 0L)
                                    {
                                        base._eventArgs._progress = (int)((base._eventArgs._bytesCompleted * 100L) / base._eventArgs._bytesTotal);
                                    }
                                    base.OnModifiedWithThrottle(ref tickCount);
                                }while (num3 > 0);
                                if (response.ContentLength != num2)
                                {
                                    outputFileStream.SetLength(num2);
                                }
                            }
                        }
                        finally
                        {
                            if (responseStream != null)
                            {
                                responseStream.Close();
                            }
                            if (outputFileStream != null)
                            {
                                outputFileStream.Close();
                            }
                        }
                        base._eventArgs.Cookie = next._cookie;
                        base._eventArgs._filesCompleted++;
                        Logger.AddInternalState("HttpWebResponse=" + Logger.Serialize(response));
                        base.OnModified();
                        DownloadResult result = new DownloadResult {
                            ResponseUri = response.ResponseUri
                        };
                        result.ServerInformation.Server        = response.Headers["Server"];
                        result.ServerInformation.PoweredBy     = response.Headers["X-Powered-By"];
                        result.ServerInformation.AspNetVersion = response.Headers["X-AspNet-Version"];
                        base._downloadResults.Add(result);
                    }
                }
                catch (InvalidOperationException exception)
                {
                    throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception);
                }
                catch (IOException exception2)
                {
                    throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception2);
                }
                catch (UnauthorizedAccessException exception3)
                {
                    throw new DeploymentDownloadException(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FailedWhileDownloading"), new object[] { next._sourceUri }), exception3);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }
        }
        private bool PatchSingleFile(DownloadQueueItem item, Hashtable dependencyTable)
        {
            if (item._hashCollection == null)
            {
                return(false);
            }
            string location = null;

            using (HashCollection.HashEnumerator enumerator = item._hashCollection.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    string compositString = enumerator.Current.CompositString;
                    if (dependencyTable.Contains(compositString))
                    {
                        location = (string)dependencyTable[compositString];
                        goto Label_0062;
                    }
                }
            }
Label_0062:
            if (location == null)
            {
                return(false);
            }
            if (this._fCancelPending)
            {
                return(false);
            }
            if (!FileHashVerified(item._hashCollection, location))
            {
                Logger.AddInternalState("Hash verify failed for " + location + ", not using it for file patching.");
                return(false);
            }
            FileStream patchSourceStream = null;
            FileStream patchTargetStream = null;

            try
            {
                patchSourceStream = GetPatchSourceStream(location);
                if (patchSourceStream == null)
                {
                    return(false);
                }
                Directory.CreateDirectory(Path.GetDirectoryName(item._targetPath));
                patchTargetStream = GetPatchTargetStream(item._targetPath);
                if (patchTargetStream == null)
                {
                    return(false);
                }
                this._eventArgs._fileSourceUri   = item._sourceUri;
                this._eventArgs.FileLocalPath    = item._targetPath;
                this._eventArgs.Cookie           = null;
                this._eventArgs._fileResponseUri = null;
                this.CheckForSizeLimit((ulong)patchSourceStream.Length, true);
                this._accumulatedBytesTotal += patchSourceStream.Length;
                this.SetBytesTotal();
                this.OnModified();
                int count     = 0;
                int tickCount = Environment.TickCount;
                patchTargetStream.SetLength(patchSourceStream.Length);
                patchTargetStream.Position = 0L;
                do
                {
                    if (this._fCancelPending)
                    {
                        return(false);
                    }
                    count = patchSourceStream.Read(this._buffer, 0, this._buffer.Length);
                    if (count > 0)
                    {
                        patchTargetStream.Write(this._buffer, 0, count);
                    }
                    this._eventArgs._bytesCompleted += count;
                    this._eventArgs._progress        = (int)((this._eventArgs._bytesCompleted * 100L) / this._eventArgs._bytesTotal);
                    this.OnModifiedWithThrottle(ref tickCount);
                }while (count > 0);
            }
            finally
            {
                if (patchSourceStream != null)
                {
                    patchSourceStream.Close();
                }
                if (patchTargetStream != null)
                {
                    patchTargetStream.Close();
                }
            }
            this._eventArgs.Cookie = item._cookie;
            this._eventArgs._filesCompleted++;
            this.OnModified();
            DownloadResult result = new DownloadResult {
                ResponseUri = null
            };

            this._downloadResults.Add(result);
            Logger.AddInternalState(item._targetPath + " is patched from store.");
            return(true);
        }