Beispiel #1
0
        private static void ReadRule(XmlNode node, IRewriterConfiguration config)
        {
            var parsed  = false;
            var parsers = config.ActionParserFactory.GetParsers(node.LocalName);

            if (parsers != null)
            {
                foreach (var parser in parsers)
                {
                    if (!parser.AllowsNestedActions && node.ChildNodes.Count > 0)
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, parser.Name), node);
                    }

                    if (!parser.AllowsAttributes && node.Attributes.Count > 0)
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoAttributes, parser.Name), node);
                    }

                    var rule = parser.Parse(node, config);
                    if (rule != null)
                    {
                        config.Rules.Add(rule);
                        parsed = true;
                        break;
                    }
                }
            }

            if (!parsed)
            {
                // No parsers recognised to handle this node.
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, node.LocalName), node);
            }
        }
Beispiel #2
0
        private static void ReadMapping(XmlNode node, IRewriterConfiguration config)
        {
            // Name attribute.
            var mappingName = node.GetRequiredAttribute(Constants.AttrName);

            // Mapper type not specified.  Load in the hash map.
            var map = new StringDictionary();

            foreach (XmlNode mapNode in node.ChildNodes)
            {
                if (mapNode.NodeType == XmlNodeType.Element)
                {
                    if (mapNode.LocalName == Constants.ElementMap)
                    {
                        var fromValue = mapNode.GetRequiredAttribute(Constants.AttrFrom, true);
                        var toValue   = mapNode.GetRequiredAttribute(Constants.AttrTo, true);

                        map.Add(fromValue, toValue);
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, mapNode.LocalName), node);
                    }
                }
            }

            IRewriteTransform mapping = new StaticMappingTransform(mappingName, map);

            config.TransformFactory.Add(mapping);
        }
Beispiel #3
0
        private static void ReadErrorHandler(XmlNode node, IRewriterConfiguration config)
        {
            var code = node.GetRequiredAttribute(Constants.AttrCode);

            XmlNode typeNode = node.Attributes[Constants.AttrType];
            XmlNode urlNode  = node.Attributes[Constants.AttrUrl];

            if (typeNode == null && urlNode == null)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.AttributeRequired, Constants.AttrUrl), node);
            }

            IRewriteErrorHandler handler;

            if (typeNode != null)
            {
                // <error-handler code="500" url="/oops.aspx" />
                handler = TypeHelper.Activate(typeNode.Value, null) as IRewriteErrorHandler;
                if (handler == null)
                {
                    throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidTypeSpecified, typeNode.Value, typeof(IRewriteErrorHandler)), node);
                }
            }
            else
            {
                handler = new DefaultErrorHandler(urlNode.Value);
            }

            if (!int.TryParse(code, out var statusCode))
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidHttpStatusCode, code), node);
            }

            config.ErrorHandlers.Add(statusCode, handler);
        }
Beispiel #4
0
        private const int MaxRestarts = 10;          // Controls the number of restarts so we don't get into an infinite loop

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="httpContext">The HTTP context facade.</param>
        /// <param name="configurationManager">The configuration manager facade.</param>
        /// <param name="configuration">The URL rewriter configuration.</param>
        public RewriterEngine(
            IHttpContext httpContext,
            IConfigurationManager configurationManager,
            IRewriterConfiguration configuration)
        {
            this._httpContext          = httpContext ?? throw new ArgumentNullException(nameof(httpContext));
            this._configurationManager = configurationManager ?? throw new ArgumentNullException(nameof(configurationManager));
            this._configuration        = configuration ?? throw new ArgumentNullException(nameof(configuration));
        }
Beispiel #5
0
 private static void ReadDefaultDocuments(XmlNode node, IRewriterConfiguration config)
 {
     node.ChildNodes.Cast <XmlNode>().ForEach(childNode =>
     {
         if (childNode.NodeType == XmlNodeType.Element && childNode.LocalName == Constants.ElementDocument)
         {
             config.DefaultDocuments.Add(childNode.InnerText);
         }
     });
 }
Beispiel #6
0
 private static void ReadDefaultDocuments(XmlNode node, IRewriterConfiguration config)
 {
     foreach (XmlNode childNode in node.ChildNodes)
     {
         if (childNode.NodeType == XmlNodeType.Element && childNode.LocalName == Constants.ElementDocument)
         {
             config.DefaultDocuments.Add(childNode.InnerText);
         }
     }
 }
 private static void ReadDefaultDocuments(XmlNode node, IRewriterConfiguration config)
 {
     foreach (XmlNode childNode in node.ChildNodes)
     {
         if (childNode.NodeType == XmlNodeType.Element && childNode.LocalName == Constants.ElementDocument)
         {
             config.DefaultDocuments.Add(childNode.InnerText);
         }
     }
 }
        public void Constructor_WithNullConfiguration_Throws()
        {
            // Arrange
            XmlNode                emptySection         = CreateEmptyXmlNode();
            IHttpContext           httpContext          = null;
            IConfigurationManager  configurationManager = new MockConfigurationManager(emptySection);
            IRewriterConfiguration configuration        = null;

            // Act/Assert
            ExceptionAssert.Throws <ArgumentNullException>(() =>
                                                           new RewriterEngine(httpContext, configurationManager, configuration));
        }
Beispiel #9
0
        /// <summary>
        /// Reads configuration information from the given XML Node.
        /// </summary>
        /// <param name="config">The rewriter configuration object to populate.</param>
        /// <param name="section">The XML node to read configuration from.</param>
        /// <returns>The configuration information.</returns>
        public static void Read(IRewriterConfiguration config, XmlNode section)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                switch (node.LocalName)
                {
                case Constants.ElementErrorHandler:
                    ReadErrorHandler(node, config);
                    break;

                case Constants.ElementDefaultDocuments:
                    ReadDefaultDocuments(node, config);
                    break;

                case Constants.ElementRegister when node.Attributes[Constants.AttrParser] != null:
                    ReadRegisterParser(node, config);
                    break;

                case Constants.ElementRegister when node.Attributes[Constants.AttrTransform] != null:
                    ReadRegisterTransform(node, config);
                    break;

                case Constants.ElementRegister:
                {
                    if (node.Attributes[Constants.AttrLogger] != null)
                    {
                        ReadRegisterLogger(node, config);
                    }

                    break;
                }

                case Constants.ElementMapping:
                    ReadMapping(node, config);
                    break;

                default:
                    ReadRule(node, config);
                    break;
                }
            }
        }
        /// <summary>
        /// Parses conditions from the node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="conditions">Conditions list to add new conditions to.</param>
        /// <param name="negative">Whether the conditions should be negated.</param>
        /// <param name="config">Rewriter configuration</param>
        protected void ParseConditions(XmlNode node, IList<IRewriteCondition> conditions, bool negative, IRewriterConfiguration config)
        {
            if (config == null)
            {
                return;
            }
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (conditions == null)
            {
                throw new ArgumentNullException("conditions");
            }

            // Parse attribute-based conditions.
            foreach (IRewriteConditionParser parser in config.ConditionParserPipeline)
            {
                IRewriteCondition condition = parser.Parse(node);
                if (condition != null)
                {
                    if (negative)
                    {
                        condition = new NegativeCondition(condition);
                    }

                    conditions.Add(condition);
                }
            }

            // Now, process the nested <and> conditions.
            XmlNode childNode = node.FirstChild;
            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    if (childNode.LocalName == Constants.ElementAnd)
                    {
                        ParseConditions(childNode, conditions, negative, config);

                        XmlNode childNode2 = childNode.NextSibling;
                        node.RemoveChild(childNode);
                        childNode = childNode2;
                        continue;
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            string to = node.GetRequiredAttribute(Constants.AttrTo, true);
            bool permanent = node.GetBooleanAttribute(Constants.AttrPermanent) ?? true;

            RedirectAction action = new RedirectAction(to, permanent);
            ParseConditions(node, action.Conditions, false, config);
            return action;
        }
Beispiel #12
0
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var to        = node.GetRequiredAttribute(Constants.AttrTo, true);
            var permanent = node.GetBooleanAttribute(Constants.AttrPermanent) ?? true;

            var action = new RedirectAction(to, permanent);

            this.ParseConditions(node, action.Conditions, false, config);
            return(action);
        }
Beispiel #13
0
        private static void ReadRegisterLogger(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            var type = node.GetRequiredAttribute(Constants.AttrLogger);

            // Logger type specified.  Create an instance and add it
            // as the mapper handler for this map.
            if (TypeHelper.Activate(type, null) is IRewriteLogger logger)
            {
                config.Logger = logger;
            }
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string propertyName = node.GetRequiredAttribute(Constants.AttrProperty);
            string appSettingKey = node.GetRequiredAttribute(Constants.AttrKey);

            return new SetAppSettingPropertyAction(propertyName, appSettingKey);
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string propertyName  = node.GetRequiredAttribute(Constants.AttrProperty);
            string appSettingKey = node.GetRequiredAttribute(Constants.AttrKey);

            return(new SetAppSettingPropertyAction(propertyName, appSettingKey));
        }
        /// <summary>
        /// Reads configuration information from the given XML Node.
        /// </summary>
        /// <param name="config">The rewriter configuration object to populate.</param>
        /// <param name="section">The XML node to read configuration from.</param>
        /// <returns>The configuration information.</returns>
        public static void Read(IRewriterConfiguration config, XmlNode section)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    if (node.LocalName == Constants.ElementErrorHandler)
                    {
                        ReadErrorHandler(node, config);
                    }
                    else if (node.LocalName == Constants.ElementDefaultDocuments)
                    {
                        ReadDefaultDocuments(node, config);
                    }
                    else if (node.LocalName == Constants.ElementRegister)
                    {
                        if (node.Attributes[Constants.AttrParser] != null)
                        {
                            ReadRegisterParser(node, config);
                        }
                        else if (node.Attributes[Constants.AttrTransform] != null)
                        {
                            ReadRegisterTransform(node, config);
                        }
                        else if (node.Attributes[Constants.AttrLogger] != null)
                        {
                            ReadRegisterLogger(node, config);
                        }
                    }
                    else if (node.LocalName == Constants.ElementMapping)
                    {
                        ReadMapping(node, config);
                    }
                    else
                    {
                        ReadRule(node, config);
                    }
                }
            }
        }
        /// <summary>
        /// Reads configuration information from the given XML Node.
        /// </summary>
        /// <param name="config">The rewriter configuration object to populate.</param>
        /// <param name="section">The XML node to read configuration from.</param>
        /// <returns>The configuration information.</returns>
        public static void Read(IRewriterConfiguration config, XmlNode section)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    if (node.LocalName == Constants.ElementErrorHandler)
                    {
                        ReadErrorHandler(node, config);
                    }
                    else if (node.LocalName == Constants.ElementDefaultDocuments)
                    {
                        ReadDefaultDocuments(node, config);
                    }
                    else if (node.LocalName == Constants.ElementRegister)
                    {
                        if (node.Attributes[Constants.AttrParser] != null)
                        {
                            ReadRegisterParser(node, config);
                        }
                        else if (node.Attributes[Constants.AttrTransform] != null)
                        {
                            ReadRegisterTransform(node, config);
                        }
                        else if (node.Attributes[Constants.AttrLogger] != null)
                        {
                            ReadRegisterLogger(node, config);
                        }
                    }
                    else if (node.LocalName == Constants.ElementMapping)
                    {
                        ReadMapping(node, config);
                    }
                    else
                    {
                        ReadRule(node, config);
                    }
                }
            }
        }
Beispiel #18
0
        private static void ReadRegisterTransform(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            var type = node.GetRequiredAttribute(Constants.AttrTransform);

            // Transform type specified.
            // Create an instance and add it as the mapper handler for this map.
            if (!(TypeHelper.Activate(type, null) is IRewriteTransform transform))
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidTypeSpecified, type, typeof(IRewriteTransform)), node);
            }

            config.TransformFactory.Add(transform);
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            int? statusCode = node.GetIntegerAttribute(Constants.AttrStatus);
            if (!statusCode.HasValue)
            {
                return null;
            }

            return new SetStatusAction((HttpStatusCode)statusCode.Value);
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            int?statusCode = node.GetIntegerAttribute(Constants.AttrStatus);

            if (!statusCode.HasValue)
            {
                return(null);
            }

            return(new SetStatusAction((HttpStatusCode)statusCode.Value));
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string to = node.GetRequiredAttribute(Constants.AttrTo, true);

            RewriteProcessing processing = ParseProcessing(node);

            RewriteAction action = new RewriteAction(to, processing);
            ParseConditions(node, action.Conditions, false, config);

            return action;
        }
Beispiel #22
0
        private static void ReadRegisterParser(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            var type = node.GetRequiredAttribute(Constants.AttrParser);

            var parser = TypeHelper.Activate(type, null);

            if (parser is IRewriteActionParser actionParser)
            {
                config.ActionParserFactory.Add(actionParser);
            }

            if (parser is IRewriteConditionParser conditionParser)
            {
                config.ConditionParserPipeline.Add(conditionParser);
            }
        }
Beispiel #23
0
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var to = node.GetRequiredAttribute(Constants.AttrTo, true);

            var processing = ParseProcessing(node);

            var action = new RewriteAction(to, processing);

            ParseConditions(node, action.Conditions, false, config);

            return(action);
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string cookieName = node.GetOptionalAttribute(Constants.AttrCookie);
            if (String.IsNullOrEmpty(cookieName))
            {
                return null;
            }

            string cookieValue = node.GetRequiredAttribute(Constants.AttrValue, true);

            return new SetCookieAction(cookieName, cookieValue);
        }
 /// <summary>
 ///     Constructor.
 /// </summary>
 /// <param name="httpContext">The HTTP context facade.</param>
 /// <param param name="configurationManager">The configuration manager facade.</param>
 /// <param name="configuration">The URL rewriter configuration.</param>
 public RewriterEngine(
     IHttpContext httpContext,
     IConfigurationManager configurationManager,
     IRewriterConfiguration configuration)
 {
     if (httpContext == null)
     {
         throw new ArgumentNullException("httpContext");
     }
     if (configurationManager == null)
     {
         throw new ArgumentNullException("configurationManager");
     }
     if (configuration == null)
     {
         throw new ArgumentNullException("configuration");
     }
     _httpContext = httpContext;
     _configurationManager = configurationManager;
     _configuration = configuration;
 }
Beispiel #26
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="httpContext">The HTTP context facade.</param>
 /// <param param name="configurationManager">The configuration manager facade.</param>
 /// <param name="configuration">The URL rewriter configuration.</param>
 public RewriterEngine(
     IHttpContext httpContext,
     IConfigurationManager configurationManager,
     IRewriterConfiguration configuration)
 {
     if (httpContext == null)
     {
         throw new ArgumentNullException("httpContext");
     }
     if (configurationManager == null)
     {
         throw new ArgumentNullException("configurationManager");
     }
     if (configuration == null)
     {
         throw new ArgumentNullException("configuration");
     }
     _httpContext          = httpContext;
     _configurationManager = configurationManager;
     _configuration        = configuration;
 }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string headerName = node.GetOptionalAttribute(Constants.AttrHeader);
            if (headerName == null)
            {
                return null;
            }

            string headerValue = node.GetRequiredAttribute(Constants.AttrValue, true);

            return new AddHeaderAction(headerName, headerValue);
        }
        /// <summary>
        /// Parses the action.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            ConditionalAction rule = new ConditionalAction();

            // Process the conditions on the element.
            bool negative = (node.LocalName == Constants.ElementUnless);
            ParseConditions(node, rule.Conditions, negative, config);

            // Next, process the actions on the element.
            ReadActions(node, rule.Actions, config);

            return rule;
        }
Beispiel #29
0
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var headerName = node.GetOptionalAttribute(Constants.AttrHeader);

            if (headerName == null)
            {
                return(null);
            }

            var headerValue = node.GetRequiredAttribute(Constants.AttrValue, true);

            return(new AddHeaderAction(headerName, headerValue));
        }
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            string cookieName = node.GetOptionalAttribute(Constants.AttrCookie);

            if (String.IsNullOrEmpty(cookieName))
            {
                return(null);
            }

            string cookieValue = node.GetRequiredAttribute(Constants.AttrValue, true);

            return(new SetCookieAction(cookieName, cookieValue));
        }
Beispiel #31
0
        /// <summary>
        /// Parses the action.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var rule = new ConditionalAction();

            // Process the conditions on the element.
            var negative = (node.LocalName == Constants.ElementUnless);

            ParseConditions(node, rule.Conditions, negative, config);

            // Next, process the actions on the element.
            ReadActions(node, rule.Actions, config);

            return(rule);
        }
Beispiel #32
0
        /// <summary>
        /// Parses the node.
        /// </summary>
        /// <param name="node">The node to parse.</param>
        /// <param name="config">The rewriter configuration.</param>
        /// <returns>The parsed action, or null if no action parsed.</returns>
        public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var propertyName = node.GetOptionalAttribute(Constants.AttrProperty);

            if (propertyName.IsNotSet())
            {
                return(null);
            }

            var propertyValue = node.GetRequiredAttribute(Constants.AttrValue, true);

            return(new SetPropertyAction(propertyName, propertyValue));
        }
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return new GoneAction();
 }
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return new MethodNotAllowedAction();
 }
        private static void ReadErrorHandler(XmlNode node, IRewriterConfiguration config)
        {
            string code = node.GetRequiredAttribute(Constants.AttrCode);

            XmlNode typeNode = node.Attributes[Constants.AttrType];
            XmlNode urlNode = node.Attributes[Constants.AttrUrl];
            if (typeNode == null && urlNode == null)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.AttributeRequired, Constants.AttrUrl), node);
            }

            IRewriteErrorHandler handler = null;
            if (typeNode != null)
            {
                // <error-handler code="500" url="/oops.aspx" />
                handler = TypeHelper.Activate(typeNode.Value, null) as IRewriteErrorHandler;
                if (handler == null)
                {
                    throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidTypeSpecified, typeNode.Value, typeof(IRewriteErrorHandler)), node);
                }
            }
            else
            {
                handler = new DefaultErrorHandler(urlNode.Value);
            }

            int statusCode;
            if (!Int32.TryParse(code, out statusCode))
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidHttpStatusCode, code), node);
            }

            config.ErrorHandlers.Add(statusCode, handler);
        }
Beispiel #36
0
        /// <summary>
        /// Parses conditions from the node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="conditions">Conditions list to add new conditions to.</param>
        /// <param name="negative">Whether the conditions should be negated.</param>
        /// <param name="config">Rewriter configuration</param>
        protected void ParseConditions(XmlNode node, IList <IRewriteCondition> conditions, bool negative, IRewriterConfiguration config)
        {
            if (config == null)
            {
                return;
            }

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (conditions == null)
            {
                throw new ArgumentNullException("conditions");
            }

            // Parse attribute-based conditions.
            foreach (var parser in config.ConditionParserPipeline)
            {
                var condition = parser.Parse(node);
                if (condition != null)
                {
                    if (negative)
                    {
                        condition = new NegativeCondition(condition);
                    }

                    conditions.Add(condition);
                }
            }

            // Now, process the nested <and> conditions.
            var childNode = node.FirstChild;

            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    if (childNode.LocalName == Constants.ElementAnd)
                    {
                        this.ParseConditions(childNode, conditions, negative, config);

                        var childNode2 = childNode.NextSibling;
                        node.RemoveChild(childNode);
                        childNode = childNode2;
                        continue;
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
        private static void ReadActions(XmlNode node, ICollection <IRewriteAction> actions, IRewriterConfiguration config)
        {
            var childNode = node.FirstChild;

            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    var parsers = config.ActionParserFactory.GetParsers(childNode.LocalName);
                    if (parsers != null)
                    {
                        var parsed = false;
                        foreach (var parser in parsers)
                        {
                            var action = parser.Parse(childNode, config);

                            if (action == null)
                            {
                                continue;
                            }

                            parsed = true;
                            actions.Add(action);
                        }

                        if (!parsed)
                        {
                            throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, node.FirstChild.Name), node);
                        }
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
Beispiel #38
0
 /// <summary>
 /// Parses the action.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, null if no action parsed.</returns>
 public abstract IRewriteAction Parse(XmlNode node, IRewriterConfiguration config);
        private static void ReadRegisterTransform(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            string type = node.GetRequiredAttribute(Constants.AttrTransform);

            // Transform type specified.
            // Create an instance and add it as the mapper handler for this map.
            IRewriteTransform transform = TypeHelper.Activate(type, null) as IRewriteTransform;
            if (transform == null)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.InvalidTypeSpecified, type, typeof(IRewriteTransform)), node);
            }

            config.TransformFactory.Add(transform);
        }
        private static void ReadActions(XmlNode node, IList<IRewriteAction> actions, IRewriterConfiguration config)
        {
            XmlNode childNode = node.FirstChild;
            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    IList<IRewriteActionParser> parsers = config.ActionParserFactory.GetParsers(childNode.LocalName);
                    if (parsers != null)
                    {
                        bool parsed = false;
                        foreach (IRewriteActionParser parser in parsers)
                        {
                            IRewriteAction action = parser.Parse(childNode, config);
                            if (action != null)
                            {
                                parsed = true;
                                actions.Add(action);
                            }
                        }

                        if (!parsed)
                        {
                            throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, node.FirstChild.Name), node);
                        }
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
        private static void ReadRegisterLogger(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            string type = node.GetRequiredAttribute(Constants.AttrLogger);

            // Logger type specified.  Create an instance and add it
            // as the mapper handler for this map.
            IRewriteLogger logger = TypeHelper.Activate(type, null) as IRewriteLogger;
            if (logger != null)
            {
                config.Logger = logger;
            }
        }
        private static void ReadRegisterParser(XmlNode node, IRewriterConfiguration config)
        {
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, Constants.ElementRegister), node);
            }

            string type = node.GetRequiredAttribute(Constants.AttrParser);

            object parser = TypeHelper.Activate(type, null);
            IRewriteActionParser actionParser = parser as IRewriteActionParser;
            if (actionParser != null)
            {
                config.ActionParserFactory.Add(actionParser);
            }

            IRewriteConditionParser conditionParser = parser as IRewriteConditionParser;
            if (conditionParser != null)
            {
                config.ConditionParserPipeline.Add(conditionParser);
            }
        }
        private static void ReadMapping(XmlNode node, IRewriterConfiguration config)
        {
            // Name attribute.
            string mappingName = node.GetRequiredAttribute(Constants.AttrName);

            // Mapper type not specified.  Load in the hash map.
            StringDictionary map = new StringDictionary();
            foreach (XmlNode mapNode in node.ChildNodes)
            {
                if (mapNode.NodeType == XmlNodeType.Element)
                {
                    if (mapNode.LocalName == Constants.ElementMap)
                    {
                        string fromValue = mapNode.GetRequiredAttribute(Constants.AttrFrom, true);
                        string toValue = mapNode.GetRequiredAttribute(Constants.AttrTo, true);

                        map.Add(fromValue, toValue);
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, mapNode.LocalName), node);
                    }
                }
            }

            IRewriteTransform mapping = new StaticMappingTransform(mappingName, map);

            config.TransformFactory.Add(mapping);
        }
Beispiel #44
0
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return(new NotImplementedAction());
 }
Beispiel #45
0
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return(new ForbiddenAction());
 }
Beispiel #46
0
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return(new MethodNotAllowedAction());
 }
        private static void ReadRule(XmlNode node, IRewriterConfiguration config)
        {
            bool parsed = false;
            IList<IRewriteActionParser> parsers = config.ActionParserFactory.GetParsers(node.LocalName);
            if (parsers != null)
            {
                foreach (IRewriteActionParser parser in parsers)
                {
                    if (!parser.AllowsNestedActions && node.ChildNodes.Count > 0)
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoElements, parser.Name), node);
                    }

                    if (!parser.AllowsAttributes && node.Attributes.Count > 0)
                    {
                        throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNoAttributes, parser.Name), node);
                    }

                    IRewriteAction rule = parser.Parse(node, config);
                    if (rule != null)
                    {
                        config.Rules.Add(rule);
                        parsed = true;
                        break;
                    }
                }
            }

            if (!parsed)
            {
                // No parsers recognised to handle this node.
                throw new ConfigurationErrorsException(MessageProvider.FormatString(Message.ElementNotAllowed, node.LocalName), node);
            }
        }
 /// <summary>
 /// Parses the node.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, or null if no action parsed.</returns>
 public override IRewriteAction Parse(XmlNode node, IRewriterConfiguration config)
 {
     return new NotImplementedAction();
 }
 /// <summary>
 /// Parses the action.
 /// </summary>
 /// <param name="node">The node to parse.</param>
 /// <param name="config">The rewriter configuration.</param>
 /// <returns>The parsed action, null if no action parsed.</returns>
 public abstract IRewriteAction Parse(XmlNode node, IRewriterConfiguration config);