Пример #1
0
        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);
        }