public void EnsureLoaded(Assembly a) { if (assembliesProcessed.Contains(a.FullName)) return; Stopwatch sw = new Stopwatch(); sw.Start(); try { object[] attrs = a.GetCustomAttributes(typeof(Util.NativeDependenciesAttribute), true); if (attrs.Length == 0) return; var attr = attrs[0] as Util.NativeDependenciesAttribute; string shortName = a.GetName().Name; string resourceName = shortName + "." + attr.Value; var info = a.GetManifestResourceInfo(resourceName); if (info == null) { this.AcceptIssue(new Issues.Issue("Plugin error - failed to find embedded resource " + resourceName, Issues.IssueSeverity.Error)); return; } using (Stream s = a.GetManifestResourceStream(resourceName)){ var x = new System.Xml.XmlDocument(); x.Load(s); var n = new Node(x.DocumentElement, this); EnsureLoaded(n, shortName,sw); } } finally { assembliesProcessed.Add(a.FullName); } }
/// <summary> /// Redacts any connectionString attribute from the diaglostics page. /// </summary> /// <param name="resizer"></param> /// <returns></returns> public Configuration.Xml.Node RedactFrom(Node resizer) { if (resizer == null) return null; var nodes = resizer.queryUncached("plugins.add"); if (nodes == null) return resizer; foreach (Node n in nodes) { if (n.Attrs["connectionString"] != null) n.Attrs.Set("connectionString", "[redacted]"); } return resizer; }
/// <summary> /// Makes a recusive copy of the subtree, keeping no duplicate references to mutable types. /// </summary> /// <returns></returns> public Node deepCopy() { Node n = new Node(this.name); //copy attrs foreach (string key in this.Attrs.Keys) { n[key] = this[key]; } //copy children recursive if (this.children != null) foreach (Node c in this.Children) n.Children.Add(c.deepCopy()); //Copy text contents n.TextContents = this.TextContents; return n; }
public static Node FromXmlFragment(string xml, IssueSink sink) { NameTable nt = new NameTable(); XmlNamespaceManager nsmanager = new XmlNamespaceManager(nt); XmlParserContext context = new XmlParserContext(nt, nsmanager, "elem", XmlSpace.None, System.Text.Encoding.UTF8); XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element, context); Node n = new Node(new XmlDocument().ReadNode(reader) as XmlElement, sink); reader.Close(); return n; }
public CleanupStrategy(Node n) : base("DiskCache.CleanupStrategy") { SaveDefaults(); LoadFrom(n); }
/// <summary> /// Loads settings from the specified node. Attribute names and property names must match. /// </summary> /// <param name="n"></param> public void LoadFrom(Node n) { if (n == null) return; foreach (string s in properties) LoadProperty(n.Attrs, s); }
public Configuration.Xml.Node RedactFrom(Node resizer) { foreach (Node n in resizer.queryUncached("plugins.add")) { if (n.Attrs["accessKeyId"] != null) n.Attrs.Set("accessKeyId", "[redacted]"); if (n.Attrs["secretAccessKey"] != null) n.Attrs.Set("secretAccessKey", "[redacted]"); } return resizer; }
internal List<byte[]> LicensesFromXml(Node xml) { List<byte[]> licenses = new List<byte[]>(); var n = xml; foreach (var l in n.childrenByName("license")) { if (l.TextContents == null) continue; licenses.Add(Convert.FromBase64String(l.TextContents.Trim())); } return licenses; }
/// <summary> /// Parse the specified XML into a Node. The text should include 1 root element, <resizer> /// </summary> /// <param name="xml"></param> public ResizerSection(string xml) { n = Node.FromXmlFragment(xml,sink); }
/// <summary> /// Create a ResizerSection instance that wraps the specified node. The node should be a <resizer> element. /// </summary> /// <param name="root"></param> public ResizerSection(Node root) { n = root; }
public void replaceRootNode(Node n) { lock (nSync) { this.n = n; } }
public void EnsureLoaded(Node manifest, string assemblyName, Stopwatch sw = null) { if (TargetFolder == null) { this.AcceptIssue( new Issue("Applicaiton does not have IOPermission; Native dependencies for " + assemblyName + " will not be downloaded if missing")); return; } string platform = IntPtr.Size == 8 ? "64" : "32"; Queue<Dependency> q = new Queue<Dependency>(); try { foreach (Node c in manifest.childrenByName("file")) { string bitness = c.Attrs["bitness"];//Skip files with the wrong bitness if (bitness != null && !bitness.Equals(platform, StringComparison.OrdinalIgnoreCase)) continue; string name = c.Attrs["name"]; //Skip duplicate names if (string.IsNullOrEmpty(name)) this.AcceptIssue(new Issues.Issue("Missing attribute 'name' in native dependency manifest for " + assemblyName, Issues.IssueSeverity.Warning)); if (filesVerified.Contains(name)) continue; //What is the expected size? If none listed, any size will work. int fileBytes = 0; if (c.Attrs["fileBytes"] != null && !int.TryParse(c.Attrs["fileBytes"], System.Globalization.NumberStyles.Number, NumberFormatInfo.InvariantInfo, out fileBytes)) this.AcceptIssue(new Issues.Issue("Failed to parse fileBytes value " + c.Attrs["fileBytes"] + " in native dependency manifest for " + assemblyName, Issues.IssueSeverity.Warning)); //Download url? string url = c.Attrs["url"]; string destPath = Path.Combine(TargetFolder, name); long existingLength = 0; //Does it already exist? if (File.Exists(destPath)) { if (fileBytes < 1) { filesVerified.Add(name); continue; } else { existingLength = new FileInfo(destPath).Length; if (existingLength == fileBytes) { filesVerified.Add(name); continue; } } } var d = new Dependency() { Exists = existingLength > 0, Name = name, Url = url, DestPath = destPath, ExistingLength = existingLength, ExpectedLength = fileBytes, Client = new WebClient(), RequestingAssembly =assemblyName }; q.Enqueue(d); } sw.Stop(); if (sw.ElapsedMilliseconds > 100 && q.Count < 1) this.AcceptIssue(new Issues.Issue("Verifying native dependencies for " + assemblyName + " took " + sw.ElapsedMilliseconds + "ms.", Issues.IssueSeverity.Warning)); ServicePointManager.DefaultConnectionLimit = 1000; //Allow more than 2 simultaneous http requests. StringBuilder message = new StringBuilder(); if (q.Count > 0) { Stopwatch dsw = new Stopwatch(); dsw.Start(); using (var cd = new Countdown(q.Count)) { foreach (var current in q.ToArray()) { var d = current; ThreadPool.QueueUserWorkItem(x => { DownloadFile(d, message); cd.Signal(); }); } cd.Wait(); } dsw.Stop(); this.AcceptIssue(new Issues.Issue("Some native dependencies for " + assemblyName + " were missing, but were downloaded successfully. This delayed startup time by " + (sw.ElapsedMilliseconds + dsw.ElapsedMilliseconds).ToString() + "ms.", message.ToString(), Issues.IssueSeverity.Warning)); } } finally { foreach (Dependency d in q.ToArray()) { d.Client.Dispose(); } } }
public Configuration.Xml.Node RedactFrom(Node resizer) { if (resizer.queryFirst("remoteReader") != null) resizer.setAttr("remoteReader.signingKey", "[redacted]"); return resizer; }
private Node LicensesToXml(ICollection<KeyValuePair<string,byte[]>> licenses) { Node n = new Node("licenses"); foreach (var p in licenses){ var l = new Node("license"); var desc = new Node("description"); desc.TextContents = p.Key; l.Children.Add(desc); l.TextContents = Convert.ToBase64String(p.Value); n.Children.Add(l); } return n; }
/// <summary> /// Traverses the specified path, creating any missing elements along the way. Uses existing nodes if found. /// </summary> /// <param name="selector"></param> /// <returns></returns> public Node makeNodeTree(string selector) { Node n = this; Selector s = new Selector(selector); foreach (string part in s) { IList<Node> results = n.childrenByName(part); //Add it if doesn't exist if (results == null || results.Count == 0) { Node newNode = new Node(part); if (n.children == null) n.children = new List<Node>(); n.Children.Add(newNode); n = newNode; } else { n = results[0]; } } return n; }
/// <summary> /// Removes connection string attributes for security /// </summary> /// <param name="resizer"></param> /// <returns></returns> public Node RedactFrom(Node resizer) { foreach (var n in resizer.queryUncached("plugins.add")) { if (n.Attrs["connectionString"] != null) n.Attrs.Set("connectionString", "[redacted]"); } return resizer; }
protected Dictionary<string, IEnumerable<Layer>> ParseWatermarks(Node n, ref ImageLayer otherImageDefaults) { Dictionary<string, IEnumerable<Layer>> dict = new Dictionary<string, IEnumerable<Layer>>(StringComparer.OrdinalIgnoreCase); if (n == null || n.Children == null) return dict; foreach (Node c in n.Children) { //Verify the name is specified and is unique. string name = c.Attrs["name"]; if (c.Name.Equals("image", StringComparison.OrdinalIgnoreCase) || c.Name.Equals("text", StringComparison.OrdinalIgnoreCase) || c.Name.Equals("group", StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(name) || dict.ContainsKey(name)) { this.c.configurationSectionIssues.AcceptIssue(new Issue("WatermarkPlugin", "The name attribute for each watermark or watermark group must be specified, and must be unique.", "XML: " + c.ToString(), IssueSeverity.ConfigurationError)); continue; } } if (c.Name.Equals("otherimages", StringComparison.OrdinalIgnoreCase)) otherImageDefaults = new ImageLayer(c.Attrs, this.c); if (c.Name.Equals("image", StringComparison.OrdinalIgnoreCase)) dict.Add(name, new Layer[]{new ImageLayer(c.Attrs, this.c)}); if (c.Name.Equals("text", StringComparison.OrdinalIgnoreCase)) dict.Add(name, new Layer[] {new TextLayer(c.Attrs) }); if (c.Name.Equals("group", StringComparison.OrdinalIgnoreCase)) { List<Layer> layers = new List<Layer>(); if (c.Children != null) { foreach (Node layer in c.Children) { if (layer.Name.Equals("image", StringComparison.OrdinalIgnoreCase)) layers.Add(new ImageLayer(layer.Attrs, this.c)); if (layer.Name.Equals("text", StringComparison.OrdinalIgnoreCase)) layers.Add(new TextLayer(layer.Attrs)); } } dict.Add(name, layers); } } return dict; }
protected void ParseXml(Node n, Config conf) { if (n == null ) return; OnlyAllowPresets = NameValueCollectionExtensions.Get(n.Attrs, "onlyAllowPresets", OnlyAllowPresets); if (n.Children == null) return; foreach (Node c in n.Children) { string name = c.Attrs["name"]; if (c.Name.Equals("preset", StringComparison.OrdinalIgnoreCase)) { //Verify the name is specified and is unique. if (string.IsNullOrEmpty(name) || defaults.ContainsKey(name) || settings.ContainsKey(name)) { conf.configurationSectionIssues.AcceptIssue(new Issue("Presets", "The name attribute for each preset must be specified, and must be unique.", "XML: " + c.ToString(), IssueSeverity.ConfigurationError)); continue; } if (!string.IsNullOrEmpty(c.Attrs["defaults"])) defaults[name] = new ResizeSettings(c.Attrs["defaults"]); if (!string.IsNullOrEmpty(c.Attrs["settings"])) settings[name] = new ResizeSettings(c.Attrs["settings"]); } } return; }