Added a default status code for redirects in mod_rewrite

This commit is contained in:
Mikael Mengistu 2016-11-03 13:22:10 -07:00 committed by GitHub
parent a52301f886
commit 222addf264
2 changed files with 28 additions and 4 deletions

View File

@ -3,7 +3,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches;
@ -12,7 +14,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
public class RuleBuilder
{
private IList<Condition> _conditions;
private IList<UrlAction> _actions = new List<UrlAction>();
internal IList<UrlAction> _actions = new List<UrlAction>();
private UrlMatch _match;
private CookieActionFactory _cookieActionFactory = new CookieActionFactory();
@ -200,12 +202,16 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
string statusCode;
if (flags.GetValue(FlagType.Redirect, out statusCode))
{
int res;
if (!int.TryParse(statusCode, out res))
int responseStatusCode;
if (string.IsNullOrEmpty(statusCode))
{
responseStatusCode = StatusCodes.Status302Found;
}
else if (!int.TryParse(statusCode, NumberStyles.None, CultureInfo.InvariantCulture, out responseStatusCode))
{
throw new FormatException(Resources.FormatError_InputParserInvalidInteger(statusCode, -1));
}
_actions.Add(new RedirectAction(res, pattern, queryStringAppend, queryStringDelete, escapeBackReference));
_actions.Add(new RedirectAction(responseStatusCode, pattern, queryStringAppend, queryStringDelete, escapeBackReference));
}
else
{

View File

@ -2,7 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite.Internal;
using Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite;
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
using Xunit;
namespace Microsoft.AspNetCore.Rewrite.Tests
@ -20,5 +24,19 @@ namespace Microsoft.AspNetCore.Rewrite.Tests
var ex = Assert.Throws<NotSupportedException>(() => builder.AddAction(null, flags));
Assert.Equal(Resources.Error_ChangeEnvironmentNotSupported, ex.Message);
}
[Fact]
public void AddAction_DefaultRedirectStatusCode()
{
var builder = new RuleBuilder();
var flags = new Flags();
var pattern = new Pattern(new List<PatternSegment>());
flags.SetFlag(FlagType.Redirect, string.Empty);
builder.AddAction(pattern, flags);
var redirectAction = (RedirectAction)builder._actions[0];
Assert.Equal(StatusCodes.Status302Found, redirectAction.StatusCode);
}
}
}