示例#1
0
        public static void UpdateRule(dynamic model, InboundRule rule, Site site, string path, string configPath = null)
        {
            InboundRulesSection           section = GetSection(site, path, configPath);
            AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath);

            SetRule(model, rule, section, serverVariablesSection);
        }
        public static void UpdateFeatureSettings(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            AllowedServerVariablesSection section = ServerVariablesHelper.GetSection(site, path, configPath);

            try {
                if (model.entries != null)
                {
                    IEnumerable <dynamic> variables = model.entries as IEnumerable <dynamic>;

                    if (variables == null)
                    {
                        throw new ApiArgumentException("entries", ForbiddenArgumentException.EXPECTED_ARRAY);
                    }

                    List <string> variableList = new List <string>();

                    // Validate all verbs provided
                    foreach (dynamic variable in variables)
                    {
                        string var = DynamicHelper.Value(variable);

                        if (string.IsNullOrEmpty(var))
                        {
                            throw new ApiArgumentException("entries.item");
                        }

                        variableList.Add(var);
                    }

                    // Clear configuration's collection
                    section.AllowedServerVariables.Clear();

                    // Move from temp list to the configuration's collection
                    variableList.ForEach(v => section.AllowedServerVariables.Add(v));
                }


                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => {
                        section.OverrideMode = v;
                    });
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
示例#3
0
        public object Get()
        {
            RewriteHelper.ResolveRewrite(Context, out Site site, out string path);

            if (path == null)
            {
                return(NotFound());
            }

            dynamic d = ServerVariablesHelper.ToJsonModel(site, path);

            return(LocationChanged(ServerVariablesHelper.GetLocation(d.id), d));
        }
示例#4
0
        public object Get(string id)
        {
            var serverVariablesId = new RewriteId(id);

            Site site = serverVariablesId.SiteId == null ? null : SiteHelper.GetSite(serverVariablesId.SiteId.Value);

            if (serverVariablesId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(null);
            }

            return(ServerVariablesHelper.ToJsonModel(site, serverVariablesId.Path));
        }
示例#5
0
        public void Delete(string id)
        {
            var serverVariablesId = new RewriteId(id);

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;

            Site site = (serverVariablesId.SiteId != null) ? SiteHelper.GetSite(serverVariablesId.SiteId.Value) : null;

            if (site != null)
            {
                var section = ServerVariablesHelper.GetSection(site, serverVariablesId.Path, ManagementUnit.ResolveConfigScope());
                section.RevertToParent();
                ManagementUnit.Current.Commit();
            }
        }
示例#6
0
        public static InboundRule CreateRule(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.pattern)))
            {
                throw new ApiArgumentException("pattern");
            }

            if (model.action == null)
            {
                throw new ApiArgumentException("action");
            }

            if (!(model.action is JObject))
            {
                throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT);
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.action.url)))
            {
                throw new ApiArgumentException("action.url");
            }

            InboundRulesSection           section = GetSection(site, path, configPath);
            AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath);

            var rule = (InboundRule)section.InboundRules.CreateElement();

            //
            // Defaults
            rule.PatternSyntax = PatternSyntax.ECMAScript;
            rule.Action.Type   = ActionType.Rewrite;

            SetRule(model, rule, section, serverVariablesSection);

            return(rule);
        }
示例#7
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteId serverVariablesId = new RewriteId(id);

            Site site = serverVariablesId.SiteId == null ? null : SiteHelper.GetSite(serverVariablesId.SiteId.Value);

            if (serverVariablesId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);

            ServerVariablesHelper.UpdateFeatureSettings(model, site, serverVariablesId.Path, configPath);

            ManagementUnit.Current.Commit();

            return(ServerVariablesHelper.ToJsonModel(site, serverVariablesId.Path));
        }
示例#8
0
        private void ConfigureServerVariables()
        {
            Environment.Host.RouteBuilder.MapWebApiRoute(Defines.ServerVariablesResource.Guid, $"{Defines.SERVER_VARIABLES_PATH}/{{id?}}", new { controller = "ServerVariables" });

            // Server Variables -> Self
            Environment.Hal.ProvideLink(Defines.ServerVariablesResource.Guid, "self", sv => new { href = ServerVariablesHelper.GetLocation(sv.id) });

            // Rewrite -> Server Variables
            Environment.Hal.ProvideLink(Defines.Resource.Guid, Defines.ServerVariablesResource.Name, rewrite => new { href = ServerVariablesHelper.GetLocation(rewrite.id) });
        }