GetWebConfiguration() public method

public GetWebConfiguration ( ) : Configuration
return Configuration
        public override void LoadPanels(MainForm mainForm, ServiceContainer serviceContainer, List <ModuleProvider> moduleProviders)
        {
            serviceContainer.RemoveService(typeof(IConfigurationService));
            serviceContainer.RemoveService(typeof(IControlPanel));
            var panel = new ApplicationPage(Application, mainForm);
            var scope = ManagementScope.Application;

            serviceContainer.AddService(typeof(IControlPanel), new ControlPanel());
            serviceContainer.AddService(typeof(IConfigurationService),
                                        new ConfigurationService(mainForm, Application.GetWebConfiguration(), scope, null, Application.Site,
                                                                 Application, null, null, this.Application.Location));
            foreach (var provider in moduleProviders)
            {
                if (!provider.SupportsScope(scope))
                {
                    continue;
                }

                var definition = provider.GetModuleDefinition(null);
                var type       = Type.GetType(definition.ClientModuleTypeName);
                if (type == null)
                {
                    continue;
                }

                if (!typeof(Module).IsAssignableFrom(type))
                {
                    continue;
                }

                var module = (Module)Activator.CreateInstance(type);
                module.Initialize(serviceContainer, null);
            }

            IModulePage page       = panel;
            var         mainModule = new MainModule();

            mainModule.Initialize(serviceContainer, null);
            page.Initialize(mainModule, null, null);
            mainForm.LoadPage(page);
        }
Example #2
0
        public async Task SaveAsync(Application application)
        {
            var variables = new SortedDictionary <string, List <string> >();

            foreach (var item in application.Extra)
            {
                variables.Add(item.Key, item.Value);
            }

            var vDir = application.VirtualDirectories[0];

            Configuration                  config = application.GetWebConfiguration();
            ConfigurationSection           defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            ConfigurationElementCollection filesCollection        = defaultDocumentSection.GetCollection("files");
            ConfigurationSection           httpLoggingSection     = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging", application.Location);
            ConfigurationSection           ipSecuritySection      = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/security/ipSecurity", application.Location);

            ConfigurationSection           requestFilteringSection  = config.GetSection("system.webServer/security/requestFiltering");
            ConfigurationElement           hiddenSegmentsElement    = requestFilteringSection.GetChildElement("hiddenSegments");
            ConfigurationElementCollection hiddenSegmentsCollection = hiddenSegmentsElement.GetCollection();

            ConfigurationSection           httpErrorsSection    = config.GetSection("system.webServer/httpErrors");
            ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

            var urlCompressionSection = config.GetSection("system.webServer/urlCompression");

            ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");

            ConfigurationSection           rewriteSection    = config.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rewriteCollection = rewriteSection.GetCollection();

            variables.Add("usehttps", new List <string> {
                (application.Site.Bindings[0].Protocol == "https").ToString()
            });
            variables.Add("addr", new List <string> {
                application.Site.Bindings[0].EndPoint.Address.ToString()
            });
            variables.Add("port", new List <string> {
                application.Site.Bindings[0].EndPoint.Port.ToString()
            });
            variables.Add("hosts", new List <string> {
                application.Site.Bindings[0].Host
            });
            variables.Add("root", new List <string> {
                string.Format("{0} {1}", vDir.Path, vDir.PhysicalPath)
            });
            variables.Add("nolog", new List <string> {
                httpLoggingSection["dontLog"].ToString()
            });
            variables.Add("keep_alive", new List <string> {
                httpProtocolSection["allowKeepAlive"].ToString()
            });

            var indexes = new StringBuilder();

            foreach (ConfigurationElement item in filesCollection)
            {
                indexes.AppendFormat("{0},", item.RawAttributes["value"]);
            }

            if (indexes.Length > 0)
            {
                indexes.Length--;
            }

            variables.Add("indexes", new List <string> {
                indexes.ToString()
            });

            var allows = new List <string>();
            var denys  = new List <string>();

            foreach (ConfigurationElement item in ipSecuritySection.GetCollection())
            {
                string element = string.IsNullOrEmpty((string)item["subnetMask"])
                    ? (string)item["ipAddress"]
                    : string.Format("{0}/{1}", item["ipAddress"], item["subnetMask"]);
                if ((bool)item["allowed"])
                {
                    allows.Add(element);
                }
                else
                {
                    denys.Add(element);
                }
            }

            variables.Add("allowfrom", allows);
            variables.Add("denyfrom", denys);

            var segments = new StringBuilder();

            foreach (ConfigurationElement item in hiddenSegmentsCollection)
            {
                segments.AppendFormat("{0},", item["segment"]);
            }

            if (segments.Length > 0)
            {
                segments.Length--;
            }

            variables.Add("denydirs", new List <string> {
                segments.ToString()
            });

            foreach (ConfigurationElement item in httpErrorsCollection)
            {
                if ((uint)item["statusCode"] == 404 && (int)item["subStatusCode"] == 0 &&
                    (string)item["prefixLanguageFilePath"] == @"%SystemDrive%\inetpub\custerr" &&
                    (long)item["responseMode"] == 1)
                {
                    variables.Add("nofile", new List <string> {
                        item["path"].ToString()
                    });
                }
            }

            variables.Add("usegzip", new List <string> {
                urlCompressionSection["doStaticCompression"].ToString()
            });

            var rules = new List <string>();

            foreach (ConfigurationElement item in rewriteCollection)
            {
                var action = item.GetChildElement("action");
                var match  = item.GetChildElement("match");
                if ((long)action["type"] == 2)
                {
                    rules.Add(string.Format("{0}{2} {1}", match["url"], action["url"], (bool)match["ignoreCase"] ? "/i" : string.Empty));
                }
            }

            variables.Add("rewrite", rules);

            if (string.IsNullOrEmpty(application.Server.HostName))
            {
                var rows = new List <string>();
                foreach (var item in variables)
                {
                    foreach (var line in item.Value)
                    {
                        rows.Add(string.Format("{0}={1}", item.Key, line));
                    }
                }

                var fileName = Path.Combine("siteconf", application.ToFileName());
                File.WriteAllLines(fileName, rows);
            }
            else
            {
                using (var client = GetClient())
                {
                    HttpResponseMessage response = await client.PutAsJsonAsync(string.Format("api/site/{0}", application.ToFileName()), variables);

                    if (response.IsSuccessStatusCode)
                    {
                    }
                }
            }
        }
Example #3
0
        public async Task LoadAsync(Application application, string file, string appName, SortedDictionary <string, List <string> > variables)
        {
            if (variables == null)
            {
                if (Credentials == null)
                {
                    var rows = File.ReadAllLines(file);
                    variables = new SortedDictionary <string, List <string> >();
                    foreach (var line in rows)
                    {
                        var index   = line.IndexOf('#');
                        var content = index == -1 ? line : line.Substring(0, index);
                        var parts   = content.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 2)
                        {
                            continue;
                        }

                        var key   = parts[0].Trim().ToLowerInvariant();
                        var value = parts[1].Trim();
                        if (variables.ContainsKey(key))
                        {
                            variables[key].Add(value);
                            continue;
                        }

                        variables.Add(key, new List <string> {
                            value
                        });
                    }
                }
                else
                {
                    using (var client = GetClient())
                    {
                        HttpResponseMessage response = await client.GetAsync(string.Format("api/app/get/{0}", appName));

                        if (response.IsSuccessStatusCode)
                        {
                            variables = (SortedDictionary <string, List <string> >) await response.Content.ReadAsAsync(typeof(SortedDictionary <string, List <string> >));
                        }
                    }
                }
            }

            variables.Load(new List <string> {
                "false"
            }, "usehttps");
            variables.Load(new List <string> {
                "*"
            }, "hosts", "host");
            variables.Load(new List <string> {
                IPAddress.Any.ToString()
            }, "addr", "address");
            variables.Load(new List <string> {
                "80"
            }, "port");
            var root = variables.Load(new List <string> {
                "/ /var/www/default"
            }, "root")[0];
            var split = root.IndexOf(' ');

            if (split == -1 || split == 0)
            {
                throw new ServerManagerException("invalid root mapping");
            }

            var virtualDirectory = new VirtualDirectory(null, application.VirtualDirectories);

            virtualDirectory.Path         = root.Substring(0, split);
            virtualDirectory.PhysicalPath = root.Substring(split + 1);
            application.VirtualDirectories.Add(virtualDirectory);
            var configuration   = application.GetWebConfiguration();
            var defaultDocument = configuration.GetSection("system.webServer/defaultDocument");

            defaultDocument["enabled"] = true;
            var collection = defaultDocument.GetCollection("files");

            collection.Clear();
            var names = variables.Load(new List <string> {
                Constants.DefaultDocumentList
            }, "indexes", "indexs")[0];
            var pageNames = names.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var name in pageNames)
            {
                var file1 = collection.CreateElement();
                file1.Attributes["value"].Value = name;
                collection.Add(file1);
            }


            ConfigurationSection httpLoggingSection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging", application.Location);
            var dontLog = Convert.ToBoolean(variables.Load(new List <string> {
                "false"
            }, "nolog")[0]);

            httpLoggingSection["dontLog"] = dontLog;

            var ipSecuritySection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/security/ipSecurity", application.Location);

            ipSecuritySection["enableReverseDns"] = false;
            ipSecuritySection["allowUnlisted"]    = true;

            ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

            ipSecurityCollection.Clear();
            var deny = variables.Load(new List <string>(), "denyfrom", "ip.deny");

            foreach (var denyEntry in deny)
            {
                var denyItems = denyEntry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in denyItems)
                {
                    ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                    if (item.Contains("/"))
                    {
                        var parts = item.Split('/');
                        addElement["ipAddress"]  = parts[0];
                        addElement["subnetMask"] = parts[1];
                    }
                    else
                    {
                        addElement["ipAddress"] = item;
                    }

                    addElement["allowed"] = false;
                    ipSecurityCollection.Add(addElement);
                }
            }

            var allow = variables.Load(new List <string>(), "allowfrom", "ip.allow");

            foreach (var allowEntry in allow)
            {
                var allowItems = allowEntry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in allowItems)
                {
                    ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                    if (item.Contains("/"))
                    {
                        var parts = item.Split('/');
                        addElement["ipAddress"]  = parts[0];
                        addElement["subnetMask"] = parts[1];
                    }
                    else
                    {
                        addElement["ipAddress"] = item;
                    }

                    addElement["allowed"] = true;
                    ipSecurityCollection.Add(addElement);
                }
            }

            ConfigurationSection           requestFilteringSection  = configuration.GetSection("system.webServer/security/requestFiltering");
            ConfigurationElement           hiddenSegmentsElement    = requestFilteringSection.ChildElements["hiddenSegments"];
            ConfigurationElementCollection hiddenSegmentsCollection = hiddenSegmentsElement.GetCollection();

            hiddenSegmentsCollection.Clear();
            var hidden = variables.Load(new List <string> {
                string.Empty
            }, "denydirs")[0];
            var hiddenItems = hidden.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in hiddenItems)
            {
                ConfigurationElement add = hiddenSegmentsCollection.CreateElement("add");
                add["segment"] = item;
                hiddenSegmentsCollection.Add(add);
            }

            ConfigurationSection           httpErrorsSection    = configuration.GetSection("system.webServer/httpErrors");
            ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

            httpErrorsCollection.Clear();
            Debug.Assert(variables != null, "variables != null");
            if (variables.ContainsKey("nofile"))
            {
                var error = variables["nofile"][0];
                ConfigurationElement errorElement = httpErrorsCollection.CreateElement("error");
                errorElement["statusCode"]             = 404;
                errorElement["subStatusCode"]          = 0;
                errorElement["prefixLanguageFilePath"] = @"%SystemDrive%\inetpub\custerr";
                errorElement["responseMode"]           = "ExecuteURL";
                errorElement["path"] = error;
                httpErrorsCollection.Add(errorElement);
                variables.Remove("nofile");
            }

            var urlCompressionSection = configuration.GetSection("system.webServer/urlCompression");

            urlCompressionSection["doStaticCompression"] = Convert.ToBoolean(variables.Load(new List <string> {
                "true"
            }, "usegzip")[0]);

            ConfigurationSection httpProtocolSection = configuration.GetSection("system.webServer/httpProtocol");

            httpProtocolSection["allowKeepAlive"] = Convert.ToBoolean(variables.Load(new List <string> {
                "true"
            }, "keep_alive")[0]);

            ConfigurationSection           rulesSection    = configuration.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

            rulesCollection.Clear();
            if (variables.ContainsKey("rewrite"))
            {
                var rules = variables["rewrite"];
                for (int i = 0; i < rules.Count; i++)
                {
                    var rule = rules[i];
                    ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
                    ruleElement["name"]           = @"rule" + i;
                    ruleElement["enabled"]        = true;
                    ruleElement["patternSyntax"]  = 0;
                    ruleElement["stopProcessing"] = false;

                    var parts = rule.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    ConfigurationElement matchElement = ruleElement.GetChildElement("match");
                    matchElement["ignoreCase"] = parts[0].EndsWith("/i", StringComparison.Ordinal);
                    matchElement["url"]        = parts[0].EndsWith("/i", StringComparison.Ordinal) ? parts[0].Substring(0, parts[0].Length - 2) : parts[0];

                    ConfigurationElement actionElement = ruleElement.GetChildElement("action");
                    actionElement["type"] = 2;
                    actionElement["url"]  = parts[1];
                    actionElement["appendQueryString"] = true;
                    rulesCollection.Add(ruleElement);
                }

                variables.Remove("rewrite");
            }

            application.Extra = variables;
        }
        public async Task SaveAsync(Application application)
        {
            var variables = new SortedDictionary<string, List<string>>();
            foreach (var item in application.Extra)
            {
                variables.Add(item.Key, item.Value);
            }

            var vDir = application.VirtualDirectories[0];

            Configuration config = application.GetWebConfiguration();
            ConfigurationSection defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            ConfigurationElementCollection filesCollection = defaultDocumentSection.GetCollection("files");
            ConfigurationSection httpLoggingSection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging", application.Location);
            ConfigurationSection ipSecuritySection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/security/ipSecurity", application.Location);

            ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
            ConfigurationElement hiddenSegmentsElement = requestFilteringSection.GetChildElement("hiddenSegments");
            ConfigurationElementCollection hiddenSegmentsCollection = hiddenSegmentsElement.GetCollection();

            ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
            ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

            var urlCompressionSection = config.GetSection("system.webServer/urlCompression");

            ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");

            ConfigurationSection rewriteSection = config.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rewriteCollection = rewriteSection.GetCollection();

            variables.Add("usehttps", new List<string> { (application.Site.Bindings[0].Protocol == "https").ToString() });
            variables.Add("addr", new List<string> { application.Site.Bindings[0].EndPoint.Address.ToString() });
            variables.Add("port", new List<string> { application.Site.Bindings[0].EndPoint.Port.ToString() });
            variables.Add("hosts", new List<string> { application.Site.Bindings[0].Host });
            variables.Add("root", new List<string> { string.Format("{0} {1}", vDir.Path, vDir.PhysicalPath) });
            variables.Add("nolog", new List<string> { httpLoggingSection["dontLog"].ToString() });
            variables.Add("keep_alive", new List<string> { httpProtocolSection["allowKeepAlive"].ToString() });

            var indexes = new StringBuilder();
            foreach (ConfigurationElement item in filesCollection)
            {
                indexes.AppendFormat("{0},", item.RawAttributes["value"]);
            }

            if (indexes.Length > 0)
            {
                indexes.Length--;
            }

            variables.Add("indexes", new List<string> { indexes.ToString() });

            var allows = new List<string>();
            var denys = new List<string>();
            foreach (ConfigurationElement item in ipSecuritySection.GetCollection())
            {
                string element = string.IsNullOrEmpty((string)item["subnetMask"])
                    ? (string)item["ipAddress"]
                    : string.Format("{0}/{1}", item["ipAddress"], item["subnetMask"]);
                if ((bool)item["allowed"])
                {
                    allows.Add(element);
                }
                else
                {
                    denys.Add(element);
                }
            }

            variables.Add("allowfrom", allows);
            variables.Add("denyfrom", denys);

            var segments = new StringBuilder();
            foreach (ConfigurationElement item in hiddenSegmentsCollection)
            {
                segments.AppendFormat("{0},", item["segment"]);
            }

            if (segments.Length > 0)
            {
                segments.Length--;
            }

            variables.Add("denydirs", new List<string> { segments.ToString() });

            foreach (ConfigurationElement item in httpErrorsCollection)
            {
                if ((uint)item["statusCode"] == 404 && (int)item["subStatusCode"] == 0
                    && (string)item["prefixLanguageFilePath"] == @"%SystemDrive%\inetpub\custerr"
                    && (long)item["responseMode"] == 1)
                {
                    variables.Add("nofile", new List<string> { item["path"].ToString() });
                }
            }

            variables.Add("usegzip", new List<string> { urlCompressionSection["doStaticCompression"].ToString() });

            var rules = new List<string>();
            foreach (ConfigurationElement item in rewriteCollection)
            {
                var action = item.GetChildElement("action");
                var match = item.GetChildElement("match");
                if ((long)action["type"] == 2)
                {
                    rules.Add(string.Format("{0}{2} {1}", match["url"], action["url"], (bool)match["ignoreCase"] ? "/i" : string.Empty));
                }
            }

            variables.Add("rewrite", rules);

            if (string.IsNullOrEmpty(application.Server.HostName))
            {
                var rows = new List<string>();
                foreach (var item in variables)
                {
                    foreach (var line in item.Value)
                    {
                        rows.Add(string.Format("{0}={1}", item.Key, line));
                    }
                }

                var fileName = Path.Combine("siteconf", application.ToFileName());
                File.WriteAllLines(fileName, rows);
            }
            else
            {
                using (var client = GetClient())
                {
                    HttpResponseMessage response = await client.PutAsJsonAsync(string.Format("api/site/{0}", application.ToFileName()), variables);
                    if (response.IsSuccessStatusCode)
                    {
                    }
                }
            }
        }
        public async Task LoadAsync(Application application, string file, string appName, SortedDictionary<string, List<string>> variables)
        {
            if (variables == null)
            {
                if (Credentials == null)
                {
                    var rows = File.ReadAllLines(file);
                    variables = new SortedDictionary<string, List<string>>();
                    foreach (var line in rows)
                    {
                        var index = line.IndexOf('#');
                        var content = index == -1 ? line : line.Substring(0, index);
                        var parts = content.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 2)
                        {
                            continue;
                        }

                        var key = parts[0].Trim().ToLowerInvariant();
                        var value = parts[1].Trim();
                        if (variables.ContainsKey(key))
                        {
                            variables[key].Add(value);
                            continue;
                        }

                        variables.Add(key, new List<string> { value });
                    }
                }
                else
                {
                    using (var client = GetClient())
                    {
                        HttpResponseMessage response = await client.GetAsync(string.Format("api/app/get/{0}", appName));
                        if (response.IsSuccessStatusCode)
                        {
                            variables = (SortedDictionary<string, List<string>>)await response.Content.ReadAsAsync(typeof(SortedDictionary<string, List<string>>));
                        }
                    }
                }
            }

            variables.Load(new List<string> { "false" }, "usehttps");
            variables.Load(new List<string> { "*" }, "hosts", "host");
            variables.Load(new List<string> { IPAddress.Any.ToString() }, "addr", "address");
            variables.Load(new List<string> { "80" }, "port");
            var root = variables.Load(new List<string> { "/ /var/www/default" }, "root")[0];
            var split = root.IndexOf(' ');
            if (split == -1 || split == 0)
            {
                throw new ServerManagerException("invalid root mapping");
            }

            var virtualDirectory = new VirtualDirectory(null, application.VirtualDirectories);
            virtualDirectory.Path = root.Substring(0, split);
            virtualDirectory.PhysicalPath = root.Substring(split + 1);
            application.VirtualDirectories.Add(virtualDirectory);
            var configuration = application.GetWebConfiguration();
            var defaultDocument = configuration.GetSection("system.webServer/defaultDocument");
            defaultDocument["enabled"] = true;
            var collection = defaultDocument.GetCollection("files");
            collection.Clear();
            var names = variables.Load(new List<string> { Constants.DefaultDocumentList }, "indexes", "indexs")[0];
            var pageNames = names.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var name in pageNames)
            {
                var file1 = collection.CreateElement();
                file1.Attributes["value"].Value = name;
                collection.Add(file1);
            }


            ConfigurationSection httpLoggingSection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging", application.Location);
            var dontLog = Convert.ToBoolean(variables.Load(new List<string> { "false" }, "nolog")[0]);
            httpLoggingSection["dontLog"] = dontLog;

            var ipSecuritySection = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/security/ipSecurity", application.Location);
            ipSecuritySection["enableReverseDns"] = false;
            ipSecuritySection["allowUnlisted"] = true;

            ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
            ipSecurityCollection.Clear();
            var deny = variables.Load(new List<string>(), "denyfrom", "ip.deny");
            foreach (var denyEntry in deny)
            {
                var denyItems = denyEntry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in denyItems)
                {
                    ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                    if (item.Contains("/"))
                    {
                        var parts = item.Split('/');
                        addElement["ipAddress"] = parts[0];
                        addElement["subnetMask"] = parts[1];
                    }
                    else
                    {
                        addElement["ipAddress"] = item;
                    }

                    addElement["allowed"] = false;
                    ipSecurityCollection.Add(addElement);
                }
            }

            var allow = variables.Load(new List<string>(), "allowfrom", "ip.allow");
            foreach (var allowEntry in allow)
            {
                var allowItems = allowEntry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in allowItems)
                {
                    ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                    if (item.Contains("/"))
                    {
                        var parts = item.Split('/');
                        addElement["ipAddress"] = parts[0];
                        addElement["subnetMask"] = parts[1];
                    }
                    else
                    {
                        addElement["ipAddress"] = item;
                    }

                    addElement["allowed"] = true;
                    ipSecurityCollection.Add(addElement);
                }
            }

            ConfigurationSection requestFilteringSection = configuration.GetSection("system.webServer/security/requestFiltering");
            ConfigurationElement hiddenSegmentsElement = requestFilteringSection.ChildElements["hiddenSegments"];
            ConfigurationElementCollection hiddenSegmentsCollection = hiddenSegmentsElement.GetCollection();
            hiddenSegmentsCollection.Clear();
            var hidden = variables.Load(new List<string> { string.Empty }, "denydirs")[0];
            var hiddenItems = hidden.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in hiddenItems)
            {
                ConfigurationElement add = hiddenSegmentsCollection.CreateElement("add");
                add["segment"] = item;
                hiddenSegmentsCollection.Add(add);
            }

            ConfigurationSection httpErrorsSection = configuration.GetSection("system.webServer/httpErrors");
            ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();
            httpErrorsCollection.Clear();
            Debug.Assert(variables != null, "variables != null");
            if (variables.ContainsKey("nofile"))
            {
                var error = variables["nofile"][0];
                ConfigurationElement errorElement = httpErrorsCollection.CreateElement("error");
                errorElement["statusCode"] = 404;
                errorElement["subStatusCode"] = 0;
                errorElement["prefixLanguageFilePath"] = @"%SystemDrive%\inetpub\custerr";
                errorElement["responseMode"] = "ExecuteURL";
                errorElement["path"] = error;
                httpErrorsCollection.Add(errorElement);
                variables.Remove("nofile");
            }

            var urlCompressionSection = configuration.GetSection("system.webServer/urlCompression");
            urlCompressionSection["doStaticCompression"] = Convert.ToBoolean(variables.Load(new List<string> { "true" }, "usegzip")[0]);

            ConfigurationSection httpProtocolSection = configuration.GetSection("system.webServer/httpProtocol");
            httpProtocolSection["allowKeepAlive"] = Convert.ToBoolean(variables.Load(new List<string> { "true" }, "keep_alive")[0]);

            ConfigurationSection rulesSection = configuration.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();
            rulesCollection.Clear();
            if (variables.ContainsKey("rewrite"))
            {
                var rules = variables["rewrite"];
                for (int i = 0; i < rules.Count; i++)
                {
                    var rule = rules[i];
                    ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
                    ruleElement["name"] = @"rule" + i;
                    ruleElement["enabled"] = true;
                    ruleElement["patternSyntax"] = 0;
                    ruleElement["stopProcessing"] = false;

                    var parts = rule.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    ConfigurationElement matchElement = ruleElement.GetChildElement("match");
                    matchElement["ignoreCase"] = parts[0].EndsWith("/i", StringComparison.Ordinal);
                    matchElement["url"] = parts[0].EndsWith("/i", StringComparison.Ordinal) ? parts[0].Substring(0, parts[0].Length - 2) : parts[0];

                    ConfigurationElement actionElement = ruleElement.GetChildElement("action");
                    actionElement["type"] = 2;
                    actionElement["url"] = parts[1];
                    actionElement["appendQueryString"] = true;
                    rulesCollection.Add(ruleElement);
                }

                variables.Remove("rewrite");
            }

            application.Extra = variables;
        }