Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Fixed exception messages for server variable parsing #150

Merged
merged 3 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public static class ServerVariables
/// <summary>
/// Translates mod_rewrite server variables strings to an enum of different server variables.
/// </summary>
/// <param name="variable">The server variable string.</param>
/// <param name="serverVariable">The server variable string.</param>
/// <param name="context">The Parser context</param>
/// <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":
return new HeaderSegment(HeaderNames.Accept);
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a message for this one

case "HTTPS":
Expand All @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static void ParseParameter(ParserContext context, IList<PatternSegment>
{
// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has become inconsistent on strings vs resources. We should add an issue to clean this up. (You don't need to add to this PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I logged #153

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down