public void Dump(string outFile, Monitor m, string msg) { string tempFile = HttpGetter.GetTempFilePath(outFile); Stream outs = null; try { if (File.Exists(tempFile)) { outs = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 1024 * 1024 * 5); outs.Seek(0, SeekOrigin.End); } else { outs = Utils.OpenWrite(tempFile); } if (outs.Position > 0) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rm.url); request = HttpGetter.SetRequestDefaults(request); SetRange(request, outs.Position); response = (HttpWebResponse)request.GetResponse(); if (m.ShouldStop()) { return; } } catch (System.Net.WebException ex) { if (response != null) { response.Close(); } response = null; if (ex.Message.IndexOf("416") > 0) { if (outs != null) { outs.Close(); outs = null; } Utils.DeleteFile(outFile); Utils.DeleteFile(tempFile); outs = Utils.OpenWrite(tempFile); } else { throw ex; } } } if (response == null) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rm.url); request = HttpGetter.SetRequestDefaults(request); response = (HttpWebResponse)request.GetResponse(); if (m.ShouldStop()) { return; } } if (response.StatusCode != HttpStatusCode.PartialContent) { outs.Seek(0, SeekOrigin.Begin); outs.SetLength(0); } else { RawLog.Default.Log("resume"); } stream = response.GetResponseStream(); long cl = response.ContentLength; long fileTotalLength = -1; if (cl >= 0) { fileTotalLength = outs.Position + cl; } // verify size if we can if (!Config.Default.AppEncOn && (fileTotalLength >= 0) && rm.IsSizeValid()) { if (rm.size != fileTotalLength) { if (outs != null) { outs.Close(); outs = null; } Utils.DeleteFile(tempFile); throw new Exception(Str.Def.Get(Str.FailedLength) + rm.name); } } long len = rm.size; if ((cl >= 0) && (len != cl)) { len = cl; // update gui if (Config.Default.ReportFileSize) { msg += " " + Utils.SizeStr(len); m.Log(msg); } // update rm if (!rm.IsSizeValid()) { rm.size = fileTotalLength; } } if (m.ShouldStop()) { return; } Utils.WriteStream(stream, outs, m, len); outs.Close(); outs = null; Dispose(); if (m.ShouldStop()) { return; } Encoder.Encode(tempFile, Config.Default.AppEnc, false); // check length m.Log(Str.Def.Get(Str.VerifyingFile)); if (!rm.ValidateSize(tempFile)) { Utils.DeleteFile(tempFile); throw new Exception(Str.Def.Get(Str.FailedLength) + rm.name); } // check crc if (rm.ShouldCheckCrc) { CheckCrc(tempFile, m); } Utils.DeleteFile(outFile); File.Move(tempFile, outFile); } finally { if (outs != null) { outs.Close(); outs = null; } Dispose(); } }
// false mean nothing new private bool GetRemoteVersion(Monitor m, ref long lastTimeTicks) { Stream s = null; HttpWebResponse response = null; remoteFiles.Clear(); try { string url = Config.Default.AppUrl; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request = HttpGetter.SetRequestDefaults(request); response = (HttpWebResponse)request.GetResponse(); if (response == null) { throw new Exception(); } } catch { url = Config.Default.AppUrl2; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request = HttpGetter.SetRequestDefaults(request); response = (HttpWebResponse)request.GetResponse(); } if (response == null) { throw new Exception(); } // date try { DateTime lastTime = new DateTime(lastTimeTicks); DateTime remoteFilesDate = response.LastModified; if (Config.Default.checkRemoteFileDate) { if (DateTime.Compare(lastTime, remoteFilesDate) == 0) { return(false); } } lastTimeTicks = remoteFilesDate.Ticks; } catch (Exception xx) { Utils.OnError(xx); } s = response.GetResponseStream(); if ((m != null) && m.ShouldStop()) { return(false); } if (s == null) { throw new Exception(Str.Def.Get(Str.RemoteError)); } remoteFiles = RemoteFile.Load(s, ref this.remoteFileAsocs); } finally { if (s != null) { try { s.Close(); } catch { } } s = null; if (response != null) { try { response.Close(); } catch { } } response = null; } return(true); }