Ejemplo n.º 1
0
        public void Initialize()
        {
            _request1 = new MockRequestInfo("/path1/path2?param=value", "http", "test.com", 80);
            _request2 = new MockRequestInfo("/path1/path2", "https", "secure.test.com", 443);
            _request3 = new MockRequestInfo("/path1/path2/", "http", "test.com", 80);

            _request1.NewPath[1]             = "changed1";
            _request1.NewParameters["param"] = new List <string> {
                "changed"
            };
            _request1.PathChanged();
            _request1.SetServerVariable("PATH_INFO", "/company/news/");
            _request1.SetHeader("HOST", "www.mysite.com");

            _request2.NewPath[2] = "changed2";
            _request2.PathChanged();
            _request2.SetServerVariable("SERVER_PORT", "443");
            _request2.SetHeader("USER_AGENT", "blah blah blah");

            _request3.NewUrlString           = "/changed1/changed2/";
            _request3.NewParameters["param"] = new List <string> {
                "added"
            };
            _request3.ParametersChanged();

            IFactory factory = new NinjectFactory();

            _valueGetter = factory.Create <IValueGetter>();
            _ruleResult  = factory.Create <IRuleResult>();
        }
Ejemplo n.º 2
0
        public override void PerformAction(
            IRequestInfo requestInfo,
            IRuleResult ruleResult,
            out bool stopProcessing,
            out bool endRequest)
        {
            switch (_scope)
            {
            case Scope.Header:
                foreach (var header in requestInfo.GetHeaderNames())
                {
                    if (!_scopeIndex.Contains(header.ToLower()))
                    {
                        requestInfo.SetHeader(header, null);
                    }
                }
                break;

            case Scope.Parameter:
                var parameters = new Dictionary <string, IList <string> >();
                foreach (var parameterName in _scopeIndex)
                {
                    IList <string> parameterValue;
                    if (requestInfo.NewParameters.TryGetValue(parameterName, out parameterValue))
                    {
                        if (parameterValue != null && parameterValue.Count > 0)
                        {
                            parameters[parameterName] = parameterValue;
                        }
                    }
                }
                requestInfo.NewParameters = parameters;
                break;

            case Scope.PathElement:
                // Note that _scopeIndexValue is sorted into ascending order and always includes 0
                var newPath = _scopeIndexValue
                              .Where(i => i >= 0 && i < requestInfo.NewPath.Count)
                              .Select(index => requestInfo.NewPath[index])
                              .ToList();
                if (newPath.Count < 2)
                {
                    newPath.Add(string.Empty);
                }
                requestInfo.NewPath = newPath;
                break;
            }

            stopProcessing = _stopProcessing;
            endRequest     = _endRequest;
        }
Ejemplo n.º 3
0
        public override void PerformAction(
            IRequestInfo requestInfo,
            IRuleResult ruleResult,
            out bool stopProcessing,
            out bool endRequest)
        {
            var value = _valueGetter.GetString(requestInfo, ruleResult);

            switch (_scope)
            {
            case Scope.Url:
                requestInfo.NewUrlString = value;
                break;

            case Scope.Path:
                requestInfo.NewPathString = value;
                break;

            case Scope.QueryString:
                requestInfo.NewParametersString = value;
                break;

            case Scope.Header:
                requestInfo.SetHeader(_scopeIndex, value);
                break;

            case Scope.Parameter:
                requestInfo.NewParameters[_scopeIndex] = new List <string> {
                    value
                };
                requestInfo.ParametersChanged();
                break;

            case Scope.PathElement:
                if (_scopeIndexValue == 0)
                {
                    requestInfo.NewPathString = value;
                }
                else if (_scopeIndexValue > 0)
                {
                    var count = requestInfo.NewPath.Count;
                    if (string.IsNullOrEmpty(requestInfo.NewPath[count - 1]))
                    {
                        count--;
                    }
                    if (_scopeIndexValue < count)
                    {
                        requestInfo.NewPath[_scopeIndexValue] = value;
                        requestInfo.PathChanged();
                    }
                }
                else
                {
                    var count = requestInfo.NewPath.Count;
                    if (string.IsNullOrEmpty(requestInfo.NewPath[count - 1]))
                    {
                        count--;
                    }
                    var index = count + _scopeIndexValue;
                    if (index > 0)
                    {
                        requestInfo.NewPath[index] = value;
                        requestInfo.PathChanged();
                    }
                }
                break;

            case Scope.ServerVariable:
                requestInfo.SetServerVariable(_scopeIndex, value);
                break;
            }

            stopProcessing = _stopProcessing;
            endRequest     = _endRequest;
        }
Ejemplo n.º 4
0
        public override void PerformAction(
            IRequestInfo requestInfo,
            IRuleResult ruleResult,
            out bool stopProcessing,
            out bool endRequest)
        {
            var value = _valueGetter.GetString(requestInfo, ruleResult);

            switch (_scope)
            {
            case Scope.Url:
                requestInfo.NewUrlString = requestInfo.NewUrlString + value;
                break;

            case Scope.Path:
                requestInfo.NewPathString = requestInfo.NewPathString
                                            + (requestInfo.NewPathString.EndsWith("/") ? "" : "/") + value;
                break;

            case Scope.QueryString:
                if (!string.IsNullOrWhiteSpace(value))
                {
                    requestInfo.NewParametersString = requestInfo.NewParametersString
                                                      + (requestInfo.NewParametersString.Length > 0 ? "&" : "") + value;
                }
                break;

            case Scope.Header:
                requestInfo.SetHeader(_scopeIndex, requestInfo.GetHeader(_scopeIndex) + value);
                break;

            case Scope.Parameter:
                requestInfo.NewParameters[_scopeIndex].Add(value);
                requestInfo.ParametersChanged();
                break;

            case Scope.PathElement:
            {
                var count = requestInfo.NewPath.Count;
                if (string.IsNullOrEmpty(requestInfo.NewPath[count - 1]))
                {
                    count--;
                }
                if (_scopeIndexValue > 0)
                {
                    if (_scopeIndexValue < count)
                    {
                        requestInfo.NewPath[_scopeIndexValue] = requestInfo.NewPath[_scopeIndexValue] + value;
                        requestInfo.PathChanged();
                    }
                }
                else
                {
                    var index = count + _scopeIndexValue;
                    if (index > 0)
                    {
                        requestInfo.NewPath[index] = requestInfo.NewPath[index] + value;
                        requestInfo.PathChanged();
                    }
                }
                requestInfo.PathChanged();
                break;
            }

            case Scope.ServerVariable:
                requestInfo.SetServerVariable(_scopeIndex, requestInfo.GetServerVariable(_scopeIndex) + value);
                break;
            }

            stopProcessing = _stopProcessing;
            endRequest     = _endRequest;
        }
Ejemplo n.º 5
0
        public override void PerformAction(
            IRequestInfo requestInfo,
            IRuleResult ruleResult,
            out bool stopProcessing,
            out bool endRequest)
        {
            switch (_scope)
            {
            case Scope.Url:
                requestInfo.NewUrlString = "/";
                break;

            case Scope.Path:
                requestInfo.NewPathString = "/";
                break;

            case Scope.QueryString:
                requestInfo.NewParametersString = string.Empty;
                break;

            case Scope.Header:
                requestInfo.SetHeader(_scopeIndex, null);
                break;

            case Scope.Parameter:
                requestInfo.NewParameters.Remove(_scopeIndex);
                requestInfo.ParametersChanged();
                break;

            case Scope.PathElement:
            {
                if (_scopeIndexValue == 0 || requestInfo.NewPathString == "/")
                {
                    requestInfo.NewPathString = "/";
                }
                else
                {
                    var count = requestInfo.NewPath.Count;
                    if (string.IsNullOrEmpty(requestInfo.NewPath[count - 1]))
                    {
                        count--;
                    }
                    var indexToRemove = _scopeIndexValue < 0
                            ? count + _scopeIndexValue
                            : _scopeIndexValue;
                    if (indexToRemove > 0 && indexToRemove < count)
                    {
                        requestInfo.NewPath.RemoveAt(indexToRemove);
                        requestInfo.PathChanged();
                    }
                }
                break;
            }

            case Scope.ServerVariable:
                requestInfo.SetServerVariable(_scopeIndex, null);
                break;
            }

            stopProcessing = _stopProcessing;
            endRequest     = _endRequest;
        }