Added a default status code for redirects in mod_rewrite
This commit is contained in:
parent
a52301f886
commit
222addf264
|
|
@ -3,7 +3,9 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
|
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
|
||||||
using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches;
|
using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches;
|
||||||
|
|
||||||
|
|
@ -12,7 +14,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
|
||||||
public class RuleBuilder
|
public class RuleBuilder
|
||||||
{
|
{
|
||||||
private IList<Condition> _conditions;
|
private IList<Condition> _conditions;
|
||||||
private IList<UrlAction> _actions = new List<UrlAction>();
|
internal IList<UrlAction> _actions = new List<UrlAction>();
|
||||||
private UrlMatch _match;
|
private UrlMatch _match;
|
||||||
private CookieActionFactory _cookieActionFactory = new CookieActionFactory();
|
private CookieActionFactory _cookieActionFactory = new CookieActionFactory();
|
||||||
|
|
||||||
|
|
@ -200,12 +202,16 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
|
||||||
string statusCode;
|
string statusCode;
|
||||||
if (flags.GetValue(FlagType.Redirect, out statusCode))
|
if (flags.GetValue(FlagType.Redirect, out statusCode))
|
||||||
{
|
{
|
||||||
int res;
|
int responseStatusCode;
|
||||||
if (!int.TryParse(statusCode, out res))
|
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));
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,11 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
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.ApacheModRewrite;
|
||||||
|
using Microsoft.AspNetCore.Rewrite.Internal.UrlActions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Rewrite.Tests
|
namespace Microsoft.AspNetCore.Rewrite.Tests
|
||||||
|
|
@ -20,5 +24,19 @@ namespace Microsoft.AspNetCore.Rewrite.Tests
|
||||||
var ex = Assert.Throws<NotSupportedException>(() => builder.AddAction(null, flags));
|
var ex = Assert.Throws<NotSupportedException>(() => builder.AddAction(null, flags));
|
||||||
Assert.Equal(Resources.Error_ChangeEnvironmentNotSupported, ex.Message);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue