private static Script genDownload(ScriptContext context, List<string> filteredArgs) { Script script; script = createEmptyScript(context,"xsharper //download"); script.Id = "download"; filteredArgs.AddRange(context.GetStringArray(xs.download)); script.Usage.Options = UsageOptions.IfHelp | UsageOptions.IfNoArguments | UsageOptions.UsageLine | UsageOptions.AutoSuffix; script.Parameters.Add(new CommandLineParameter("uri", CommandLineValueCount.Single, null, null) { Required = true, Value = "Source URL" }); script.Parameters.Add(new CommandLineParameter("file", CommandLineValueCount.Single, ".",null) { Value = "Destination file or directory" }); script.Parameters.Add(new CommandLineParameter(null, "cache", CommandLineValueCount.Single, Utils.LowercaseFirstLetter(RequestCacheLevel.Default.ToString()), null) { Description = "cache-level", Value = "Cache level, one of "+Utils.AllEnumValuesToString(typeof(RequestCacheLevel)," / ") }); script.Parameters.Add(new CommandLineParameter("passive", "activeFtp", CommandLineValueCount.None, true, false) { Value = "Use active FTP" }); script.Parameters.Add(new CommandLineParameter("userAgent","userAgent", CommandLineValueCount.Single, null,null) { Value = "User agent string (http://)" }); script.Parameters.Add(new CommandLineParameter("post", "post", CommandLineValueCount.Single, null, null) { Value = "HTTP Post string (evaluated as XSharper expression)"}); script.Parameters.Add(new CommandLineParameter("postContentType", "postContentType", CommandLineValueCount.Single, null, null) { Value = "HTTP Post content type (default is application/x-www-form-urlencoded)" }); script.Parameters.Add(new CommandLineParameter("timeout", "timeout", CommandLineValueCount.Single, null, null) { Value = "Timeout" }); script.Parameters.Add(new CommandLineParameter("ignoreCertificateErrors", "ignoreCertificateErrors", CommandLineValueCount.None, true, false) { Value = "Ignore SSL certificate errors " }); script.Add(new Set("oldReceived", "0")); script.Add(new SetAttr("d","cacheLevel","${cache}")); script.Add(new SetAttr("d", "passiveFtp", "${passive}")); script.Add(new Set("fn", "${=XS.Download.UrlToLocalFilename($uri,$file)}",TransformRules.Expand)); script.Add(new Print { Value = "Downloading ${=XS.Utils.SecureUri($uri)} => ${fn} ... "}); If ifs1 = new If() { IsTrue = "${ignoreCertificateErrors}"}; ifs1.Add(new Code("System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };") { Dynamic = true }); script.Add(ifs1); Download d = new Download { Id = "d", From="${uri}", To = "${file}", UserAgent = "${userAgent|=null}", Post = "${=.Expand(${post|=null})}", PostContentType = "${postContentType|=null}", Timeout = "${timeout|=null}", Transform = TransformRules.Expand }; If ifs = new If() { Condition = "${= $.bytesReceived/1024/100 #GT# $oldReceived/1024/100}" }; ifs.Add(new Print(".") { OutTo="^info" , NewLine = false}); d.Add(ifs); d.Add(new Set("oldReceived","${= $.bytesReceived}",TransformRules.Expand)); script.Add(d); script.Add(new Print { Value = "Completed. ${oldReceived} bytes downloaded." }); return script; }
void downloadPackagesInfo() { ScriptContext context = Context; var edition =Context.TransformStr(Edition, Transform); Uri piUri; if (Regex.IsMatch(edition, "^\\w+$")) { var ub = new UriBuilder(new Uri(Context.TransformStr(Repository, Transform))); ub.Path = ub.Path.TrimEnd('/') + "/" + edition + ".pinfo"; piUri = ub.Uri; } else piUri=new Uri(edition); // Download the file Context.Info.WriteLine("Downloading available packages from " + Utils.SecureUri(piUri)); byte[] pinfo = null; context.ExecuteWithVars(() => { Download d = new Download(); d.From = piUri.ToString(); d.OutTo = "var"; d.Binary = true; d.CacheLevel = CacheLevel; context.Execute(d); pinfo = (byte[]) Context["var"]; return null; }, new Vars(), null); XmlDocument x = new XmlDocument(); x.PreserveWhitespace = true; x.Load(new MemoryStream(pinfo)); if (ValidateSignature) { SignedXml verifier = new SignedXml(x); var el = x.GetElementsByTagName("Signature"); bool valid = false; if (el != null && el.Count != 0 && el[0] != null && el[0] is XmlElement) { verifier.LoadXml((XmlElement) el[0]); valid = Context.VerifyXmlSignature(verifier); } else VerboseMessage("PI. Validity of information about available packages cannot be verified, signature not found in {0}.", Utils.SecureUri(piUri)); if (!valid) throw new ScriptRuntimeException("Information about available packages is corrupted in " + Utils.SecureUri(piUri)); } // Parsing try { Dictionary<string, PackageInfo> pinfo1 = new Dictionary<string, PackageInfo>(StringComparer.OrdinalIgnoreCase); var n = x.SelectNodes("//package"); if (n != null) foreach (XmlNode node in n) { PackageInfo pi = new PackageInfo(); var id = node.Attributes["name"]; if (id == null) id = node.Attributes["id"]; if (id == null) throw new ScriptRuntimeException("name attribute is missing in package information"); var v = node.Attributes["version"]; if (v == null) throw new ScriptRuntimeException("version attribute is missing in package information [" + id.Value + "]"); pi.Version = new Version(v.Value); var hash = (node.Attributes["sha1"]); if (hash != null) pi.Hash = Utils.ToBytes(hash.Value); var urlNode = node.Attributes["location"]; if (urlNode==null) urlNode=node.Attributes["url"]; if (urlNode != null && !string.IsNullOrEmpty(urlNode.Value)) { string url = urlNode.InnerText; Uri outUri = null; if (!Uri.TryCreate(url, UriKind.Absolute, out outUri)) Uri.TryCreate(piUri, url, out outUri); if (outUri != null) pi.DownloadUri = outUri; else throw new ScriptRuntimeException("Invalid download URL " + Utils.SecureUri(url) + " in package information [" + id.Value + "]"); } pinfo1[id.Value] = pi; } knownPackages = pinfo1; } catch (Exception e) { throw new ScriptRuntimeException("An error occured while processing " + Utils.SecureUri(piUri) + " .", e); } VerboseMessage("PI. Information about {0} available packages is loaded.", knownPackages.Count); }
void downloadPackagesInfo() { ScriptContext context = Context; var edition = Context.TransformStr(Edition, Transform); Uri piUri; if (Regex.IsMatch(edition, "^\\w+$")) { var ub = new UriBuilder(new Uri(Context.TransformStr(Repository, Transform))); ub.Path = ub.Path.TrimEnd('/') + "/" + edition + ".pinfo"; piUri = ub.Uri; } else { piUri = new Uri(edition); } // Download the file Context.Info.WriteLine("Downloading available packages from " + Utils.SecureUri(piUri)); byte[] pinfo = null; context.ExecuteWithVars(() => { Download d = new Download(); d.From = piUri.ToString(); d.OutTo = "var"; d.Binary = true; d.CacheLevel = CacheLevel; context.Execute(d); pinfo = (byte[])Context["var"]; return(null); }, new Vars(), null); XmlDocument x = new XmlDocument(); x.PreserveWhitespace = true; x.Load(new MemoryStream(pinfo)); if (ValidateSignature) { SignedXml verifier = new SignedXml(x); var el = x.GetElementsByTagName("Signature"); bool valid = false; if (el != null && el.Count != 0 && el[0] != null && el[0] is XmlElement) { verifier.LoadXml((XmlElement)el[0]); valid = Context.VerifyXmlSignature(verifier); } else { VerboseMessage("PI. Validity of information about available packages cannot be verified, signature not found in {0}.", Utils.SecureUri(piUri)); } if (!valid) { throw new ScriptRuntimeException("Information about available packages is corrupted in " + Utils.SecureUri(piUri)); } } // Parsing try { Dictionary <string, PackageInfo> pinfo1 = new Dictionary <string, PackageInfo>(StringComparer.OrdinalIgnoreCase); var n = x.SelectNodes("//package"); if (n != null) { foreach (XmlNode node in n) { PackageInfo pi = new PackageInfo(); var id = node.Attributes["name"]; if (id == null) { id = node.Attributes["id"]; } if (id == null) { throw new ScriptRuntimeException("name attribute is missing in package information"); } var v = node.Attributes["version"]; if (v == null) { throw new ScriptRuntimeException("version attribute is missing in package information [" + id.Value + "]"); } pi.Version = new Version(v.Value); var hash = (node.Attributes["sha1"]); if (hash != null) { pi.Hash = Utils.ToBytes(hash.Value); } var urlNode = node.Attributes["location"]; if (urlNode == null) { urlNode = node.Attributes["url"]; } if (urlNode != null && !string.IsNullOrEmpty(urlNode.Value)) { string url = urlNode.InnerText; Uri outUri = null; if (!Uri.TryCreate(url, UriKind.Absolute, out outUri)) { Uri.TryCreate(piUri, url, out outUri); } if (outUri != null) { pi.DownloadUri = outUri; } else { throw new ScriptRuntimeException("Invalid download URL " + Utils.SecureUri(url) + " in package information [" + id.Value + "]"); } } pinfo1[id.Value] = pi; } } knownPackages = pinfo1; } catch (Exception e) { throw new ScriptRuntimeException("An error occured while processing " + Utils.SecureUri(piUri) + " .", e); } VerboseMessage("PI. Information about {0} available packages is loaded.", knownPackages.Count); }
private object downloadSingleFile(IStringFilter nf, Uri single, FileInfo toFile) { var from = new UriFileInfo(single); var ff = from.Name; if (string.IsNullOrEmpty(ff)) ff = toFile.Name; if (nf != null && (!nf.IsMatch(ff))) { VerboseMessage("{0} did not pass filter", single); return null; } var to = new FileOrDirectoryInfo(toFile); bool skip = false; object ret = ProcessPrepare(from, to, delegate { if (toFile.Directory != null && !toFile.Directory.Exists) { bool created; object r = createDir(new DirectoryInfo(Path.GetTempPath()), toFile.Directory, out created); if (!created || r != null) return r; } bool overwrite = (Overwrite == OverwriteMode.Always); if (Overwrite == OverwriteMode.IfNewer) { if (toFile.Exists && toFile.LastWriteTimeUtc >= from.LastWriteTimeUtc) { VerboseMessage("Ignoring never file {0} ", toFile.FullName); return null; } overwrite = true; } skip = (toFile.Exists && !overwrite); return null; }); if (ret != null) return ret; if (skip && Overwrite != OverwriteMode.Confirm) { VerboseMessage("Ignoring existing file {0} ", toFile.FullName); return null; } ret = ProcessComplete(from, to, skip, delegate(bool skip1) { if (!skip1) { // Continue with copy if (toFile.Directory != null && !toFile.Directory.Exists) toFile.Directory.Create(); VerboseMessage("Downloading {0} => {1}", from.FullName, toFile.FullName); Download dn = new Download { From = single.OriginalString, To = toFile.FullName, Transform = TransformRules.None }; return Context.Execute(dn); } return null; }); return ret; }
/// Download package public void Download(Updater updater) { var context = ScriptContextScope.Current; DirectoryInfo download = updater.GetDownloadDirectory(); download.Create(); DirectoryInfo dsum = updater.GetWorkingDirectory(Name); if (updater.Cleanup) Cleanup(updater); dsum.Create(); PackageInfo pi = updater.GetPackage(Name); if (pi.DownloadUri == null) { context.Info.WriteLine("[{0}] No download location provided...", Name); return; } var edition = context.TransformStr(updater.Edition, Transform); string fileName = edition + "." +Name + "." + pi.Version + ".zip"; _packageDownloadName = Path.Combine(download.FullName, fileName); // If there is hash, we don't necessarily have to download if (pi.Hash==null || !isValidHash(_packageDownloadName, pi.Hash)) { context.Info.WriteLine("[{0}] Downloading package from {1}...", Name, Utils.SecureUri(pi.DownloadUri)); Download dn = new Download { From = pi.DownloadUri.ToString(), To = _packageDownloadName, CacheLevel = updater.CacheLevel, Transform = TransformRules.None }; context.InitializeAndExecute(dn); // Validate hash if (pi.Hash!=null && !isValidHash(_packageDownloadName, pi.Hash)) throw new ScriptRuntimeException("Hash of data downloaded from " + Utils.SecureUri(pi.DownloadUri) + " is invalid"); } // Extract the file context.Info.WriteLine("[{0}] Extracting {1}...", Name, _packageDownloadName); Unzip ex = new Unzip { To = dsum.FullName, From = _packageDownloadName, Clean = false, ZipTime = updater.ZipTime, Password = updater.Password, Transform = TransformRules.None }; context.InitializeAndExecute(ex); }