From c4580f8201b6a0f3940b5435507a9fa52c96085f Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 20 Oct 2016 21:21:48 -0700 Subject: [PATCH 1/3] Fixed exception messages for server variable parsing and fixed some discrepancies between the two server variable parsing methods --- .../ApacheModRewrite/ServerVariables.cs | 24 +++++++++---------- .../Internal/IISUrlRewrite/InputParser.cs | 2 +- .../Internal/IISUrlRewrite/ServerVariables.cs | 10 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs index eb3e73f8..6c936332 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs @@ -16,12 +16,12 @@ public static class ServerVariables /// /// Translates mod_rewrite server variables strings to an enum of different server variables. /// - /// The server variable string. + /// The server variable string. /// The Parser context /// The appropriate enum if the server variable exists, else ServerVariable.None - public static PatternSegment FindServerVariable(string variable, ParserContext context) + public static PatternSegment FindServerVariable(string serverVariable, ParserContext context) { - switch (variable) + switch (serverVariable) { case "HTTP_ACCEPT": return new HeaderSegment(HeaderNames.Accept); @@ -84,21 +84,21 @@ public static PatternSegment FindServerVariable(string variable, ParserContext c case "SERVER_SOFTWARE": throw new NotSupportedException("Rules using the SERVER_SOFTWARE server variable are not supported"); case "TIME_YEAR": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_MON": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_DAY": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_HOUR": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_MIN": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_SEC": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME_WDAY": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "TIME": - return new DateTimeSegment(variable); + return new DateTimeSegment(serverVariable); case "API_VERSION": throw new NotSupportedException(); case "HTTPS": @@ -116,7 +116,7 @@ public static PatternSegment FindServerVariable(string variable, ParserContext c case "THE_REQUEST": throw new NotSupportedException("Rules using the THE_REQUEST server variable are not supported"); default: - throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(variable, context.Index)); + throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(serverVariable, context.Index)); } } } diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/InputParser.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/InputParser.cs index 39268037..8a944972 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/InputParser.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/InputParser.cs @@ -75,7 +75,7 @@ private static void ParseParameter(ParserContext context, IList { // This is just a server variable, so we do a lookup and verify the server variable exists. parameter = context.Capture(); - results.Add(ServerVariables.FindServerVariable(parameter)); + results.Add(ServerVariables.FindServerVariable(parameter, context)); return; } else if (context.Current == Colon) diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/ServerVariables.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/ServerVariables.cs index 35112b78..a9c92a9a 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/ServerVariables.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/ServerVariables.cs @@ -9,15 +9,15 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite { public static class ServerVariables { - public static PatternSegment FindServerVariable(string serverVariable) + public static PatternSegment FindServerVariable(string serverVariable, ParserContext context) { switch (serverVariable) { // TODO Add all server variables here. 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": - 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": return new HeaderSegment(HeaderNames.ContentLength); case "CONTENT_TYPE": @@ -41,7 +41,7 @@ public static PatternSegment FindServerVariable(string serverVariable) case "LOCAL_ADDR": return new LocalAddressSegment(); 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": return new QueryStringSegment(); case "REMOTE_ADDR": @@ -55,7 +55,7 @@ public static PatternSegment FindServerVariable(string serverVariable) case "REQUEST_URI": return new UrlSegment(); default: - throw new FormatException("Unrecognized server variable"); + throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(serverVariable, context.Index)); } } } From 1cb14f74b79adf21f02b5ec338f7dd34fc82a6ad Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Thu, 20 Oct 2016 21:59:52 -0700 Subject: [PATCH 2/3] Update test --- .../IISUrlRewrite/ServerVariableTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.Rewrite.Tests/IISUrlRewrite/ServerVariableTests.cs b/test/Microsoft.AspNetCore.Rewrite.Tests/IISUrlRewrite/ServerVariableTests.cs index 3235ab7d..dd35c76c 100644 --- a/test/Microsoft.AspNetCore.Rewrite.Tests/IISUrlRewrite/ServerVariableTests.cs +++ b/test/Microsoft.AspNetCore.Rewrite.Tests/IISUrlRewrite/ServerVariableTests.cs @@ -27,7 +27,8 @@ public class ServerVariableTests public void CheckServerVariableParsingAndApplication(string variable, string expected) { // 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()); // Assert Assert.Equal(expected, lookup); From b8c1bde3e2e168bba5e5f55ca1ab500fd39dd163 Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Mon, 24 Oct 2016 10:49:51 -0700 Subject: [PATCH 3/3] Added another exception message --- .../Internal/ApacheModRewrite/ServerVariables.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs index 6c936332..2b44331a 100644 --- a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/ServerVariables.cs @@ -100,7 +100,7 @@ public static PatternSegment FindServerVariable(string serverVariable, ParserCon case "TIME": return new DateTimeSegment(serverVariable); case "API_VERSION": - throw new NotSupportedException(); + throw new NotSupportedException("Rules using the API_VERSION server variable are not supported"); case "HTTPS": return new IsHttpsModSegment(); case "HTTP2":