Fixed url rewrite enum parsing.
This commit is contained in:
parent
f0b44ac7b5
commit
c0425c6253
|
|
@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
public enum PatternSyntax
|
||||
{
|
||||
ECMAScript,
|
||||
WildCard,
|
||||
Wildcard,
|
||||
ExactMatch
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,29 +5,30 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
{
|
||||
public static class RewriteTags
|
||||
{
|
||||
public const string Rewrite = "rewrite";
|
||||
public const string GlobalRules = "globalRules";
|
||||
public const string Rules = "rules";
|
||||
public const string Rule = "rule";
|
||||
public const string Action = "action";
|
||||
public const string Name = "name";
|
||||
public const string Enabled = "enabled";
|
||||
public const string PatternSyntax = "patternSyntax";
|
||||
public const string StopProcessing = "stopProcessing";
|
||||
public const string Match = "match";
|
||||
public const string Conditions = "conditions";
|
||||
public const string IgnoreCase = "ignoreCase";
|
||||
public const string Negate = "negate";
|
||||
public const string Url = "url";
|
||||
public const string MatchType = "matchType";
|
||||
public const string Add = "add";
|
||||
public const string TrackingAllCaptures = "trackingAllCaptures";
|
||||
public const string MatchPattern = "matchPattern";
|
||||
public const string Input = "input";
|
||||
public const string Pattern = "pattern";
|
||||
public const string Type = "type";
|
||||
public const string AppendQueryString = "appendQueryString";
|
||||
public const string Conditions = "conditions";
|
||||
public const string Enabled = "enabled";
|
||||
public const string GlobalRules = "globalRules";
|
||||
public const string IgnoreCase = "ignoreCase";
|
||||
public const string Input = "input";
|
||||
public const string LogicalGrouping = "logicalGrouping";
|
||||
public const string LogRewrittenUrl = "logRewrittenUrl";
|
||||
public const string Match = "match";
|
||||
public const string MatchPattern = "matchPattern";
|
||||
public const string MatchType = "matchType";
|
||||
public const string Name = "name";
|
||||
public const string Negate = "negate";
|
||||
public const string Pattern = "pattern";
|
||||
public const string PatternSyntax = "patternSyntax";
|
||||
public const string Rewrite = "rewrite";
|
||||
public const string RedirectType = "redirectType";
|
||||
public const string Rule = "rule";
|
||||
public const string Rules = "rules";
|
||||
public const string StopProcessing = "stopProcessing";
|
||||
public const string TrackingAllCaptures = "trackingAllCaptures";
|
||||
public const string Type = "type";
|
||||
public const string Url = "url";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,10 +77,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
|
||||
PatternSyntax patternSyntax;
|
||||
if (!Enum.TryParse(rule.Attribute(RewriteTags.PatternSyntax)?.Value, out patternSyntax))
|
||||
if (rule.Attribute(RewriteTags.PatternSyntax) == null)
|
||||
{
|
||||
patternSyntax = PatternSyntax.ECMAScript;
|
||||
}
|
||||
else if (!Enum.TryParse(rule.Attribute(RewriteTags.PatternSyntax).Value, ignoreCase: true, result: out patternSyntax))
|
||||
{
|
||||
ThrowParameterFormatException(rule, $"The {RewriteTags.PatternSyntax} parameter '{rule.Attribute(RewriteTags.PatternSyntax).Value}' was not recognized");
|
||||
}
|
||||
|
||||
bool stopProcessing;
|
||||
if (!bool.TryParse(rule.Attribute(RewriteTags.StopProcessing)?.Value, out stopProcessing))
|
||||
|
|
@ -116,7 +120,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
bool ignoreCase;
|
||||
if (!bool.TryParse(match.Attribute(RewriteTags.IgnoreCase)?.Value, out ignoreCase))
|
||||
{
|
||||
ignoreCase = true; // default
|
||||
ignoreCase = true;
|
||||
}
|
||||
|
||||
bool negate;
|
||||
|
|
@ -135,10 +139,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
|
||||
LogicalGrouping grouping;
|
||||
if (!Enum.TryParse(conditions.Attribute(RewriteTags.MatchType)?.Value, out grouping))
|
||||
if (conditions.Attribute(RewriteTags.LogicalGrouping) == null)
|
||||
{
|
||||
grouping = LogicalGrouping.MatchAll;
|
||||
}
|
||||
else if (!Enum.TryParse(conditions.Attribute(RewriteTags.LogicalGrouping).Value, ignoreCase: true, result: out grouping))
|
||||
{
|
||||
ThrowParameterFormatException(conditions, $"The {RewriteTags.LogicalGrouping} parameter '{conditions.Attribute(RewriteTags.LogicalGrouping).Value}' was not recognized");
|
||||
}
|
||||
|
||||
bool trackingAllCaptures;
|
||||
if (!bool.TryParse(conditions.Attribute(RewriteTags.TrackingAllCaptures)?.Value, out trackingAllCaptures))
|
||||
|
|
@ -169,10 +177,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
|
||||
MatchType matchType;
|
||||
if (!Enum.TryParse(condition.Attribute(RewriteTags.MatchType)?.Value, out matchType))
|
||||
if (condition.Attribute(RewriteTags.MatchType) == null)
|
||||
{
|
||||
matchType = MatchType.Pattern;
|
||||
}
|
||||
else if (!Enum.TryParse(condition.Attribute(RewriteTags.MatchType).Value, ignoreCase: true, result: out matchType))
|
||||
{
|
||||
ThrowParameterFormatException(condition, $"The {RewriteTags.MatchType} parameter '{condition.Attribute(RewriteTags.MatchType).Value}' was not recognized");
|
||||
}
|
||||
|
||||
var parsedInputString = condition.Attribute(RewriteTags.Input)?.Value;
|
||||
if (parsedInputString == null)
|
||||
|
|
@ -195,10 +207,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
private void ParseUrlAction(XElement urlAction, UrlRewriteRuleBuilder builder, bool stopProcessing)
|
||||
{
|
||||
ActionType actionType;
|
||||
if (!Enum.TryParse(urlAction.Attribute(RewriteTags.Type)?.Value, out actionType))
|
||||
if (urlAction.Attribute(RewriteTags.Type) == null)
|
||||
{
|
||||
actionType = ActionType.None;
|
||||
}
|
||||
else if (!Enum.TryParse(urlAction.Attribute(RewriteTags.Type).Value, ignoreCase: true, result: out actionType))
|
||||
{
|
||||
ThrowParameterFormatException(urlAction, $"The {RewriteTags.Type} parameter '{urlAction.Attribute(RewriteTags.Type).Value}' was not recognized");
|
||||
}
|
||||
|
||||
bool appendQuery;
|
||||
if (!bool.TryParse(urlAction.Attribute(RewriteTags.AppendQueryString)?.Value, out appendQuery))
|
||||
|
|
@ -207,10 +223,14 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
|
||||
RedirectType redirectType;
|
||||
if (!Enum.TryParse(urlAction.Attribute(RewriteTags.RedirectType)?.Value, out redirectType))
|
||||
if (urlAction.Attribute(RewriteTags.RedirectType) == null)
|
||||
{
|
||||
redirectType = RedirectType.Permanent;
|
||||
}
|
||||
else if (!Enum.TryParse(urlAction.Attribute(RewriteTags.RedirectType).Value, ignoreCase: true, result: out redirectType))
|
||||
{
|
||||
ThrowParameterFormatException(urlAction, $"The {RewriteTags.RedirectType} parameter '{urlAction.Attribute(RewriteTags.RedirectType).Value}' was not recognized");
|
||||
}
|
||||
|
||||
string url = string.Empty;
|
||||
if (urlAction.Attribute(RewriteTags.Url) != null)
|
||||
|
|
@ -235,16 +255,26 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
|
||||
private static void ThrowUrlFormatException(XElement element, string message)
|
||||
{
|
||||
var line = ((IXmlLineInfo)element).LineNumber;
|
||||
var col = ((IXmlLineInfo)element).LinePosition;
|
||||
var lineInfo = (IXmlLineInfo)element;
|
||||
var line = lineInfo.LineNumber;
|
||||
var col = lineInfo.LinePosition;
|
||||
throw new FormatException(Resources.FormatError_UrlRewriteParseError(message, line, col));
|
||||
}
|
||||
|
||||
private static void ThrowUrlFormatException(XElement element, string message, Exception ex)
|
||||
{
|
||||
var line = ((IXmlLineInfo)element).LineNumber;
|
||||
var col = ((IXmlLineInfo)element).LinePosition;
|
||||
var lineInfo = (IXmlLineInfo)element;
|
||||
var line = lineInfo.LineNumber;
|
||||
var col = lineInfo.LinePosition;
|
||||
throw new FormatException(Resources.FormatError_UrlRewriteParseError(message, line, col), ex);
|
||||
}
|
||||
|
||||
private static void ThrowParameterFormatException(XElement element, string message)
|
||||
{
|
||||
var lineInfo = (IXmlLineInfo)element;
|
||||
var line = lineInfo.LineNumber;
|
||||
var col = lineInfo.LinePosition;
|
||||
throw new FormatException(Resources.FormatError_UrlRewriteParseError(message, line, col));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
break;
|
||||
}
|
||||
case PatternSyntax.WildCard:
|
||||
case PatternSyntax.Wildcard:
|
||||
throw new NotSupportedException("Wildcard syntax is not supported");
|
||||
case PatternSyntax.ExactMatch:
|
||||
_initialMatch = new ExactMatch(ignoreCase, input, negate);
|
||||
|
|
@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
|
||||
public void AddUrlCondition(Pattern input, string pattern, PatternSyntax patternSyntax, MatchType matchType, bool ignoreCase, bool negate)
|
||||
{
|
||||
// If there are no conditions specified,
|
||||
// If there are no conditions specified
|
||||
if (_conditions == null)
|
||||
{
|
||||
AddUrlConditions(LogicalGrouping.MatchAll, trackingAllCaptures: false);
|
||||
|
|
@ -129,7 +129,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
|
|||
}
|
||||
break;
|
||||
}
|
||||
case PatternSyntax.WildCard:
|
||||
case PatternSyntax.Wildcard:
|
||||
throw new NotSupportedException("Wildcard syntax is not supported");
|
||||
case PatternSyntax.ExactMatch:
|
||||
if (pattern == null)
|
||||
|
|
|
|||
|
|
@ -101,6 +101,62 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.UrlRewrite
|
|||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'Url attribute cannot contain an empty string'. Line number '5': '14'.")]
|
||||
[InlineData(
|
||||
@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Remove trailing slash"">
|
||||
<match url = ""(.*)/$"" />
|
||||
<action type=""Redirect"" redirectType=""foo"" url =""{R:1}"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'The redirectType parameter 'foo' was not recognized'. Line number '5': '14'.")]
|
||||
[InlineData(
|
||||
@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Remove trailing slash"">
|
||||
<match url = ""(.*)/$"" />
|
||||
<action type=""foo"" url =""{R:1}"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'The type parameter 'foo' was not recognized'. Line number '5': '14'.")]
|
||||
[InlineData(
|
||||
@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Remove trailing slash"">
|
||||
<match url = ""(.*)/$"" />
|
||||
<conditions logicalGrouping=""foo"">
|
||||
<add input=""{REQUEST_FILENAME}"" matchType=""isFile"" negate=""true""/>
|
||||
</conditions>
|
||||
<action type=""Redirect"" url =""{R:1}"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'The logicalGrouping parameter 'foo' was not recognized'. Line number '5': '14'.")]
|
||||
[InlineData(
|
||||
@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Remove trailing slash"" patternSyntax=""foo"">
|
||||
<match url = ""(.*)/$"" />
|
||||
<action type=""Redirect"" url =""{R:1}"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'The patternSyntax parameter 'foo' was not recognized'. Line number '3': '10'.")]
|
||||
[InlineData(
|
||||
@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Remove trailing slash"">
|
||||
<match url = ""(.*)/$"" />
|
||||
<conditions>
|
||||
<add input=""{REQUEST_FILENAME}"" matchType=""foo"" negate=""true""/>
|
||||
</conditions>
|
||||
<action type=""Redirect"" url =""{R:1}"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>",
|
||||
"Could not parse the UrlRewrite file. Message: 'The matchType parameter 'foo' was not recognized'. Line number '6': '18'.")]
|
||||
public void ThrowFormatExceptionWithCorrectMessage(string input, string expected)
|
||||
{
|
||||
// Arrange, Act, Assert
|
||||
|
|
|
|||
|
|
@ -336,5 +336,35 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.UrlRewrite
|
|||
|
||||
Assert.Equal(response.Headers.Location.OriginalString, "/foo");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("IsFile")]
|
||||
[InlineData("isfile")]
|
||||
[InlineData("IsDirectory")]
|
||||
[InlineData("isdirectory")]
|
||||
public async Task VerifyIsFileAndIsDirectoryParsing(string matchType)
|
||||
{
|
||||
var options = new RewriteOptions().AddIISUrlRewrite(new StringReader($@"<rewrite>
|
||||
<rules>
|
||||
<rule name=""Test"">
|
||||
<match url=""(.*[^/])$"" />
|
||||
<conditions>
|
||||
<add input=""{{REQUEST_FILENAME}}"" matchType=""{matchType}"" negate=""true""/>
|
||||
</conditions>
|
||||
<action type=""Redirect"" url=""{{R:1}}/"" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>"));
|
||||
var builder = new WebHostBuilder()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseRewriter(options);
|
||||
});
|
||||
var server = new TestServer(builder);
|
||||
|
||||
var response = await server.CreateClient().GetAsync("hey/hello");
|
||||
|
||||
Assert.Equal("/hey/hello/", response.Headers.Location.OriginalString); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue