Exemplo n.º 1
0
        public ConfigurationSection GetSection(string sectionPath, string locationPath)
        {
            Initialize();
            if (!_dontThrow)
            {
                _server.VerifyLocation(locationPath);
            }

            return(RootSectionGroup.FindSection(sectionPath, locationPath, this));
        }
Exemplo n.º 2
0
        private void LoadDocument(string file, string location)
        {
            using (var stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                _document = XDocument.Load(stream, LoadOptions.SetLineInfo);
            }

            if (Root == null)
            {
                return;
            }

            var      nodes = Root.Nodes();
            XElement first = null;

            foreach (var node in nodes)
            {
                if (node is XComment)
                {
                    continue;
                }

                if (node is XElement element)
                {
                    first = first ?? element;
                    var tag = element.Name.LocalName;
                    if (tag == "configSections")
                    {
                        if (first != element)
                        {
                            // IMPORTANT: double spaces before It to match IIS.
                            throw new COMException($"Line number: {(element as IXmlLineInfo).LineNumber}\r\nError: Only one <configSections> element allowed.  It must be the first child element of the root <configuration> element   \r\n");
                        }

                        RootSectionGroup.ParseSectionDefinitions(element, _sectionSchemas);
                        if (Parent == null)
                        {
                            // machine.config
                            RootSectionGroup.AddSpecial();
                        }

                        // TODO: can we use _sectionSchemas.
                        RootSectionGroup.GetAllDefinitions(DefinitionCache);
                        continue;
                    }

                    if (tag == "location")
                    {
                        // IMPORTANT: allow null path due to root web.config.
                        var path  = element.Attribute("path")?.Value;
                        var mode  = element.Attribute("overrideMode").LoadString("Inherit");
                        var found = Locations.FirstOrDefault(item => item.Path == path);
                        if (found == null)
                        {
                            found = new Location(path, mode, element, true);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                        continue;
                    }

                    if (location == null)
                    {
                        ParseSections(element, null, null);
                    }
                    else
                    {
                        var found = Locations.FirstOrDefault(item => item.Path == location);
                        if (found == null)
                        {
                            found = new Location(location, "Inherit", element);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void LoadDocument(string file, string location)
        {
            _document = XDocument.Load(file, LoadOptions.SetLineInfo);
            if (Root == null)
            {
                return;
            }

            var nodes = Root.Nodes();

            foreach (var node in nodes)
            {
                if (node is XComment)
                {
                    continue;
                }

                var element = node as XElement;
                if (element != null)
                {
                    var tag = element.Name.LocalName;
                    if (tag == "configSections")
                    {
                        RootSectionGroup.ParseSectionDefinitions(element, _sectionSchemas);
                        continue;
                    }

                    if (tag == "location")
                    {
                        // IMPORTANT: allow null path due to root web.config.
                        var path  = element.Attribute("path")?.Value;
                        var mode  = element.Attribute("overrideMode").LoadString("Inherit");
                        var found = Locations.FirstOrDefault(item => item.Path == path);
                        if (found == null)
                        {
                            found = new Location(path, mode, element, true);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                        continue;
                    }

                    if (location == null)
                    {
                        ParseSections(element, null, null);
                    }
                    else
                    {
                        var found = Locations.FirstOrDefault(item => item.Path == location);
                        if (found == null)
                        {
                            found = new Location(location, "Inherit", element);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private bool ParseSections(XElement element, ConfigurationElement parent, Location location)
        {
            var result = false;
            var path   = element.GetPath();
            ConfigurationElement node = null;
            var schema = FindSchema(path);
            var sec    = FindSectionSchema(path);
            var name   = element.Name.LocalName;

            if (schema != null)
            {
                if (sec != null)
                {
                    var section = new ConfigurationSection(path, sec.Root, location?.Path, this, element);
                    RootSectionGroup.Add(section, location);
                    node = section;
                }
                else
                {
                    node = schema.CollectionSchema == null
                               ? new ConfigurationElement(null, name, schema, parent, element, this)
                               : new ConfigurationElementCollection(
                        name,
                        schema,
                        parent,
                        element,
                        this);
                }

                parent?.AddChild(node);
                result = true;
            }
            else
            {
                var found = RootSectionGroup.GetSectionDefinition(path);
                if (found != null && found.Ignore && path != "system.webServer")
                {
                    return(true);
                }
            }

            foreach (var item in element.Nodes())
            {
                var child = item as XElement;
                if (child == null)
                {
                    continue;
                }

                var childAdded = ParseSections(child, node, location);
                result = result || childAdded;
            }

            if (!result && !_dontThrow && name != "location")
            {
                throw new COMException(
                          string.Format(
                              "Line number: {0}\r\nError: Unrecognized element '{1}'\r\n",
                              (element as IXmlLineInfo).LineNumber,
                              name));
            }

            return(result);
        }
Exemplo n.º 5
0
        private bool ParseSections(XElement element, ConfigurationElement parent, Location location)
        {
            var result = false;
            var path   = element.GetPath();
            ConfigurationElement node = null;
            var schema = FindSchema(path);
            var sec    = FindSectionSchema(path);
            var name   = element.Name.LocalName;

            if (schema != null)
            {
                if (sec != null)
                {
                    var section = new ConfigurationSection(path, sec.Root, location?.Path, this, element);
                    RootSectionGroup.Add(section, location);
                    node = section;
                }
                else
                {
                    node = schema.CollectionSchema == null
                               ? new ConfigurationElement(null, name, schema, parent, element, this)
                               : new ConfigurationElementCollection(
                        name,
                        schema,
                        parent,
                        element,
                        this);
                }

                parent?.AddChild(node);
                result = true;
            }
            else
            {
                var found = RootSectionGroup.GetSectionDefinition(path);
                if (found != null && found.Ignore)
                {
                    if (path == "runtime")
                    {
                        // examples: <runtime> in machine.config.
                        return(true);
                    }
                    else
                    {
                        if (!element.HasElements)
                        {
                            // like empty tag in web.config.
                            return(true);
                        }
                    }
                }
                else
                {
                    // TODO: improve performance.
                    var foundChild = RootSectionGroup.GetChildSectionDefinition(path);
                    if (foundChild != null && !element.HasElements)
                    {
                        return(true);
                    }
                }
            }

            foreach (var item in element.Nodes())
            {
                var child = item as XElement;
                if (child == null)
                {
                    continue;
                }

                var childAdded = ParseSections(child, node, location);
                result = result || childAdded;
            }

            if (!result && !_dontThrow && name != "location")
            {
                string link = null;
                string oob  = null;
                if (path.StartsWith("system.webServer/aspNetCore/"))
                {
                    oob  = "ASP.NET Core Module (system.webServer/aspNetCore/)";
                    link = "https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x#install-the-net-core-windows-server-hosting-bundle";
                }
                else if (path.StartsWith("system.webServer/rewrite/"))
                {
                    oob  = "URL Rewrite Module (system.webServer/rewrite/)";
                    link = "https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module#where-to-get-the-url-rewrite-module";
                }
                else if (path.StartsWith("system.webServer/webFarms/"))
                {
                    oob  = "Application Request Routing Module (system.webServer/webFarms/)";
                    link = "https://docs.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/define-and-configure-an-application-request-routing-server-farm#prerequisites";
                }

                var exception = new COMException(
                    string.Format(
                        "Line number: {0}\r\nError: Unrecognized element '{1}'\r\n",
                        (element as IXmlLineInfo).LineNumber,
                        name));
                if (oob != null)
                {
                    exception.Data.Add("oob", oob);
                    exception.Data.Add("link", link);
                }

                throw exception;
            }

            return(result);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initialize data.
 /// </summary>
 public void InitData()
 {
     RootSectionGroup.InitData(this);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Synchronize data.
 /// </summary>
 public void SyncData()
 {
     RootSectionGroup.SyncData(this);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Generate code.
 /// </summary>
 /// <returns>The generated code compile unit.</returns>
 public CodeCompileUnit Generate()
 {
     RootSectionGroup.Generate(this);
     return(_codeCompileUnit);
 }