public virtual void ApplyRule(RewriteContext context) { // 1. Figure out which section of the string to match for the initial rule. var initMatchRes = InitialMatch.Evaluate(context.HttpContext.Request.Path, context); if (!initMatchRes.Success) { context.Logger?.ModRewriteNotMatchedRule(); return; } BackReferenceCollection condBackReferences = null; if (Conditions != null) { var condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchRes.BackReferences); if (!condResult.Success) { context.Logger?.ModRewriteNotMatchedRule(); return; } } // At this point, we know our rule passed, first apply pre conditions, // which can modify things like the cookie or env, and then apply the action context.Logger?.ModRewriteMatchedRule(); foreach (var action in Actions) { action.ApplyAction(context, initMatchRes?.BackReferences, condBackReferences); } }
public virtual void ApplyRule(RewriteContext context) { var path = context.HttpContext.Request.Path; var pathBase = context.HttpContext.Request.PathBase; Match initMatchResults; if (path == PathString.Empty) { initMatchResults = InitialMatch.Match(path.ToString()); } 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; if (string.IsNullOrEmpty(newPath)) { response.Headers[HeaderNames.Location] = pathBase.HasValue ? pathBase.Value : "/"; return; } if (newPath.IndexOf("://", StringComparison.Ordinal) == -1 && newPath[0] != '/') { newPath = '/' + newPath; } var split = newPath.IndexOf('?'); if (split >= 0) { var query = context.HttpContext.Request.QueryString.Add( QueryString.FromUriComponent( newPath.Substring(split))); // not using the HttpContext.Response.redirect here because status codes may be 301, 302, 307, 308 response.Headers[HeaderNames.Location] = pathBase + newPath.Substring(0, split) + query; } else { response.Headers[HeaderNames.Location] = pathBase + newPath + context.HttpContext.Request.QueryString; } context.Logger?.RedirectedSummary(newPath); } }
public virtual void ApplyRule(RewriteContext context) { var pathWithQuery = context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; var initMatchResults = InitialMatch.Match(pathWithQuery == PathString.Empty ? pathWithQuery : pathWithQuery.Substring(1)); if (!initMatchResults.Success) { return; } 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) { UriHelper.FromAbsolute(result, out var scheme, out var host, out var pathString, out var query, out FragmentString _); 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); request.Path = newPath[0] == '/' ? PathString.FromUriComponent(newPath) : PathString.FromUriComponent('/' + newPath); request.QueryString = request.QueryString.Add(QueryString.FromUriComponent(result.Substring(split))); } else { request.Path = result[0] == '/' ? PathString.FromUriComponent(result) : PathString.FromUriComponent('/' + result); } } }
public virtual void ApplyRule(RewriteContext context) { // Due to the path string always having a leading slash, // remove it from the path before regex comparison var path = context.HttpContext.Request.Path; MatchResults initMatchResults; if (path == PathString.Empty) { initMatchResults = InitialMatch.Evaluate(path.ToString(), context); } else { initMatchResults = InitialMatch.Evaluate(path.ToString().Substring(1), context); } if (!initMatchResults.Success) { context.Logger?.UrlRewriteNotMatchedRule(Name); return; } MatchResults condResult = null; if (Conditions != null) { condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchResults.BackReferences); if (!condResult.Success) { context.Logger?.UrlRewriteNotMatchedRule(Name); return; } } context.Logger?.UrlRewriteMatchedRule(Name); // at this point we know the rule passed, evaluate the replacement. Action.ApplyAction(context, initMatchResults?.BackReferences, condResult?.BackReferences); }
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(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0) { string scheme; HostString host; PathString pathString; QueryString query; UriHelper.FromAbsolute(result, out scheme, out host, out pathString, out query, out _); 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); } }
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); } }