Example #1
0
 public void ApplyRule(RewriteContext context) => _onApplyRule(context);
Example #2
0
        public virtual void ApplyRule(RewriteContext context)
        {
            var request  = context.HttpContext.Request;
            var path     = request.Path;
            var pathBase = request.PathBase;

            Match initMatchResults;

            if (!path.HasValue)
            {
                initMatchResults = InitialMatch.Match(string.Empty);
            }
            else
            {
                initMatchResults = InitialMatch.Match(path.ToString().Substring(1));
            }


            if (initMatchResults.Success)
            {
                var newPath  = initMatchResults.Result(Replacement);
                var response = context.HttpContext.Response;

                response.StatusCode = StatusCode;
                context.Result      = RuleResult.EndResponse;

                string encodedPath;

                if (string.IsNullOrEmpty(newPath))
                {
                    encodedPath = pathBase.HasValue ? pathBase.Value : "/";
                }
                else
                {
                    var host        = default(HostString);
                    var schemeSplit = newPath.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal);
                    if (schemeSplit >= 0)
                    {
                        schemeSplit += Uri.SchemeDelimiter.Length;
                        var pathSplit = newPath.IndexOf('/', schemeSplit);

                        if (pathSplit == -1)
                        {
                            host    = new HostString(newPath.Substring(schemeSplit));
                            newPath = "/";
                        }
                        else
                        {
                            host    = new HostString(newPath.Substring(schemeSplit, pathSplit - schemeSplit));
                            newPath = newPath.Substring(pathSplit);
                        }
                    }

                    if (newPath[0] != '/')
                    {
                        newPath = '/' + newPath;
                    }

                    var resolvedQuery = request.QueryString;
                    var resolvedPath  = newPath;
                    var querySplit    = newPath.IndexOf('?');
                    if (querySplit >= 0)
                    {
                        resolvedQuery = request.QueryString.Add(QueryString.FromUriComponent(newPath.Substring(querySplit)));
                        resolvedPath  = newPath.Substring(0, querySplit);
                    }

                    encodedPath = host.HasValue
                        ? UriHelper.BuildAbsolute(request.Scheme, host, pathBase, resolvedPath, resolvedQuery, default)
                        : UriHelper.BuildRelative(pathBase, resolvedPath, resolvedQuery, default);
                }

                // not using the HttpContext.Response.redirect here because status codes may be 301, 302, 307, 308
                response.Headers.Location = encodedPath;

                context.Logger.RedirectedRequest(newPath);
            }
        }
Example #3
0
        public virtual void ApplyRule(RewriteContext context)
        {
            var   path = context.HttpContext.Request.Path;
            Match initMatchResults;

            if (path == PathString.Empty)
            {
                initMatchResults = InitialMatch.Match(path.ToString());
            }
            else
            {
                initMatchResults = InitialMatch.Match(path.ToString().Substring(1));
            }

            if (initMatchResults.Success)
            {
                var result  = initMatchResults.Result(Replacement);
                var request = context.HttpContext.Request;

                if (StopProcessing)
                {
                    context.Result = RuleResult.SkipRemainingRules;
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = "/";
                }

                if (result.IndexOf("://", StringComparison.Ordinal) >= 0)
                {
                    string         scheme;
                    HostString     host;
                    PathString     pathString;
                    QueryString    query;
                    FragmentString fragment;
                    UriHelper.FromAbsolute(result, out scheme, out host, out pathString, out query, out fragment);

                    request.Scheme      = scheme;
                    request.Host        = host;
                    request.Path        = pathString;
                    request.QueryString = query.Add(request.QueryString);
                }
                else
                {
                    var split = result.IndexOf('?');
                    if (split >= 0)
                    {
                        var newPath = result.Substring(0, split);
                        if (newPath[0] == '/')
                        {
                            request.Path = PathString.FromUriComponent(newPath);
                        }
                        else
                        {
                            request.Path = PathString.FromUriComponent('/' + newPath);
                        }
                        request.QueryString = request.QueryString.Add(
                            QueryString.FromUriComponent(
                                result.Substring(split)));
                    }
                    else
                    {
                        if (result[0] == '/')
                        {
                            request.Path = PathString.FromUriComponent(result);
                        }
                        else
                        {
                            request.Path = PathString.FromUriComponent('/' + result);
                        }
                    }
                }

                context.Logger.RewrittenRequest(result);
            }
        }