Fixed exception messages for server variable parsing

This commit is contained in:
Mikael Mengistu 2016-10-24 11:34:33 -07:00 committed by GitHub
parent 061f22be61
commit 88aff41b78
4 changed files with 21 additions and 20 deletions

View File

@ -16,12 +16,12 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
/// <summary> /// <summary>
/// Translates mod_rewrite server variables strings to an enum of different server variables. /// Translates mod_rewrite server variables strings to an enum of different server variables.
/// </summary> /// </summary>
/// <param name="variable">The server variable string.</param> /// <param name="serverVariable">The server variable string.</param>
/// <param name="context">The Parser context</param> /// <param name="context">The Parser context</param>
/// <returns>The appropriate enum if the server variable exists, else ServerVariable.None</returns> /// <returns>The appropriate enum if the server variable exists, else ServerVariable.None</returns>
public static PatternSegment FindServerVariable(string variable, ParserContext context) public static PatternSegment FindServerVariable(string serverVariable, ParserContext context)
{ {
switch (variable) switch (serverVariable)
{ {
case "HTTP_ACCEPT": case "HTTP_ACCEPT":
return new HeaderSegment(HeaderNames.Accept); return new HeaderSegment(HeaderNames.Accept);
@ -84,23 +84,23 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
case "SERVER_SOFTWARE": case "SERVER_SOFTWARE":
throw new NotSupportedException("Rules using the SERVER_SOFTWARE server variable are not supported"); throw new NotSupportedException("Rules using the SERVER_SOFTWARE server variable are not supported");
case "TIME_YEAR": case "TIME_YEAR":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_MON": case "TIME_MON":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_DAY": case "TIME_DAY":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_HOUR": case "TIME_HOUR":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_MIN": case "TIME_MIN":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_SEC": case "TIME_SEC":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME_WDAY": case "TIME_WDAY":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "TIME": case "TIME":
return new DateTimeSegment(variable); return new DateTimeSegment(serverVariable);
case "API_VERSION": case "API_VERSION":
throw new NotSupportedException(); throw new NotSupportedException("Rules using the API_VERSION server variable are not supported");
case "HTTPS": case "HTTPS":
return new IsHttpsModSegment(); return new IsHttpsModSegment();
case "HTTP2": case "HTTP2":
@ -116,7 +116,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
case "THE_REQUEST": case "THE_REQUEST":
throw new NotSupportedException("Rules using the THE_REQUEST server variable are not supported"); throw new NotSupportedException("Rules using the THE_REQUEST server variable are not supported");
default: default:
throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(variable, context.Index)); throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(serverVariable, context.Index));
} }
} }
} }

View File

@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
{ {
// This is just a server variable, so we do a lookup and verify the server variable exists. // This is just a server variable, so we do a lookup and verify the server variable exists.
parameter = context.Capture(); parameter = context.Capture();
results.Add(ServerVariables.FindServerVariable(parameter)); results.Add(ServerVariables.FindServerVariable(parameter, context));
return; return;
} }
else if (context.Current == Colon) else if (context.Current == Colon)

View File

@ -9,15 +9,15 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
{ {
public static class ServerVariables public static class ServerVariables
{ {
public static PatternSegment FindServerVariable(string serverVariable) public static PatternSegment FindServerVariable(string serverVariable, ParserContext context)
{ {
switch (serverVariable) switch (serverVariable)
{ {
// TODO Add all server variables here. // TODO Add all server variables here.
case "ALL_RAW": case "ALL_RAW":
throw new NotSupportedException("Rules using the AUTH_TYPE server variable are not supported"); throw new NotSupportedException("Rules using the ALL_RAW server variable are not supported");
case "APP_POOL_ID": case "APP_POOL_ID":
throw new NotSupportedException("Rules using the AUTH_TYPE server variable are not supported"); throw new NotSupportedException("Rules using the APP_POOL_ID server variable are not supported");
case "CONTENT_LENGTH": case "CONTENT_LENGTH":
return new HeaderSegment(HeaderNames.ContentLength); return new HeaderSegment(HeaderNames.ContentLength);
case "CONTENT_TYPE": case "CONTENT_TYPE":
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
case "LOCAL_ADDR": case "LOCAL_ADDR":
return new LocalAddressSegment(); return new LocalAddressSegment();
case "HTTP_PROXY_CONNECTION": case "HTTP_PROXY_CONNECTION":
throw new NotSupportedException("Rules using the AUTH_TYPE server variable are not supported"); throw new NotSupportedException("Rules using the HTTP_PROXY_CONNECTION server variable are not supported");
case "QUERY_STRING": case "QUERY_STRING":
return new QueryStringSegment(); return new QueryStringSegment();
case "REMOTE_ADDR": case "REMOTE_ADDR":
@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
case "REQUEST_URI": case "REQUEST_URI":
return new UrlSegment(); return new UrlSegment();
default: default:
throw new FormatException("Unrecognized server variable"); throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(serverVariable, context.Index));
} }
} }
} }

View File

@ -27,7 +27,8 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.UrlRewrite
public void CheckServerVariableParsingAndApplication(string variable, string expected) public void CheckServerVariableParsingAndApplication(string variable, string expected)
{ {
// Arrange and Act // Arrange and Act
var serverVar = ServerVariables.FindServerVariable(variable); var testParserContext = new ParserContext("test");
var serverVar = ServerVariables.FindServerVariable(variable, testParserContext);
var lookup = serverVar.Evaluate(CreateTestHttpContext(), CreateTestRuleMatch(), CreateTestCondMatch()); var lookup = serverVar.Evaluate(CreateTestHttpContext(), CreateTestRuleMatch(), CreateTestCondMatch());
// Assert // Assert
Assert.Equal(expected, lookup); Assert.Equal(expected, lookup);