Add missing properties for inbound/global/outbound rules.

This commit is contained in:
Jimmy Campbell 2017-07-24 10:44:53 -07:00
Родитель 1b8e03d2c0
Коммит 0972e3cb53
6 изменённых файлов: 153 добавлений и 31 удалений

Просмотреть файл

@ -6,6 +6,7 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
{
sealed class InboundRule : RuleElement {
public const string RedirectTypeAttribute = "redirectType";
public const string ResponseCacheDirectiveAttribute = "responseCacheDirective";
private InboundActionElement _action;

Просмотреть файл

@ -4,12 +4,40 @@
namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
{
using Microsoft.IIS.Administration.Core;
using System;
// Keep public for resolution of enums from 'dynamic' types in helper classes i.e. DynamicHelper
public enum LogicalGrouping {
MatchAll = 0,
MatchAny = 1,
}
static class LogicalGroupingHelper
{
public static string ToJsonModel(LogicalGrouping logicalGrouping)
{
switch (logicalGrouping) {
case LogicalGrouping.MatchAll:
return "match_all";
case LogicalGrouping.MatchAny:
return "match_any";
default:
throw new ArgumentException(nameof(logicalGrouping));
}
}
public static LogicalGrouping FromJsonModel(string model)
{
switch (model.ToLowerInvariant()) {
case "match_all":
return LogicalGrouping.MatchAll;
case "match_any":
return LogicalGrouping.MatchAny;
default:
throw new ApiArgumentException("condition_match_constraints");
}
}
}
}

Просмотреть файл

@ -174,15 +174,23 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
//
// action
if (fields.Exists("action")) {
obj.action = new {
type = ActionTypeHelper.ToJsonModel(rule.Action.Type),
url = rule.Action.Url,
append_query_string = rule.Action.AppendQueryString,
status_code = rule.Action.StatusCode,
sub_status_code = rule.Action.SubStatusCode,
description = rule.Action.StatusDescription,
reason = rule.Action.StatusReason
};
obj.action = new ExpandoObject();
dynamic action = obj.action;
action.type = ActionTypeHelper.ToJsonModel(rule.Action.Type);
action.url = rule.Action.Url;
action.append_query_string = rule.Action.AppendQueryString;
if (rule.Action.Type == ActionType.Redirect) {
action.redirect_type = Enum.GetName(typeof(RedirectType), rule.Action.RedirectType).ToLowerInvariant();
}
if (rule.Action.Type == ActionType.CustomResponse) {
action.status_code = rule.Action.StatusCode;
action.sub_status_code = rule.Action.SubStatusCode;
action.description = rule.Action.StatusDescription;
action.reason = rule.Action.StatusReason;
}
}
//
@ -195,6 +203,18 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
});
}
//
// condition_match_constraints
if (fields.Exists("condition_match_constraints")) {
obj.condition_match_constraints = LogicalGroupingHelper.ToJsonModel(rule.Conditions.LogicalGrouping);
}
//
// track_all_captures
if (fields.Exists("track_all_captures")) {
obj.track_all_captures = rule.Conditions.TrackAllCaptures;
}
//
// conditions
if (fields.Exists("conditions")) {
@ -381,6 +401,7 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
DynamicHelper.If<long>((object)action.sub_status_code, v => rule.Action.SubStatusCode = v);
DynamicHelper.If((object)action.description, v => rule.Action.StatusDescription = v);
DynamicHelper.If((object)action.reason, v => rule.Action.StatusReason = v);
DynamicHelper.If<RedirectType>((object)action.redirect_type, v => rule.Action.RedirectType = v);
}
//
@ -421,6 +442,9 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
}
}
DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
DynamicHelper.If<bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures = v);
//
// Conditions
if (model.conditions != null) {

Просмотреть файл

@ -158,15 +158,24 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
//
// action
if (fields.Exists("action")) {
obj.action = new {
type = ActionTypeHelper.ToJsonModel(rule.Action.Type),
url = rule.Action.Url,
append_query_string = rule.Action.AppendQueryString,
status_code = rule.Action.StatusCode,
sub_status_code = rule.Action.SubStatusCode,
description = rule.Action.StatusDescription,
reason = rule.Action.StatusReason
};
obj.action = new ExpandoObject();
dynamic action = obj.action;
action.type = ActionTypeHelper.ToJsonModel(rule.Action.Type);
action.url = rule.Action.Url;
action.append_query_string = rule.Action.AppendQueryString;
action.log_rewritten_url = rule.Action.LogRewrittenUrl;
if (rule.Action.Type == ActionType.Redirect) {
action.redirect_type = Enum.GetName(typeof(RedirectType), rule.Action.RedirectType).ToLowerInvariant();
}
if (rule.Action.Type == ActionType.CustomResponse) {
action.status_code = rule.Action.StatusCode;
action.sub_status_code = rule.Action.SubStatusCode;
action.description = rule.Action.StatusDescription;
action.reason = rule.Action.StatusReason;
}
}
//
@ -179,6 +188,18 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
});
}
//
// condition_match_constraints
if (fields.Exists("condition_match_constraints")) {
obj.condition_match_constraints = LogicalGroupingHelper.ToJsonModel(rule.Conditions.LogicalGrouping);
}
//
// track_all_captures
if (fields.Exists("track_all_captures")) {
obj.track_all_captures = rule.Conditions.TrackAllCaptures;
}
//
// conditions
if (fields.Exists("conditions")) {
@ -397,10 +418,12 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
DynamicHelper.If((object)action.type, v => rule.Action.Type = ActionTypeHelper.FromJsonModel(v));
DynamicHelper.If((object)action.url, v => rule.Action.Url = v);
DynamicHelper.If<bool>((object)action.append_query_string, v => rule.Action.AppendQueryString = v);
DynamicHelper.If<bool>((object)action.log_rewritten_url, v => rule.Action.LogRewrittenUrl = v);
DynamicHelper.If<long>((object)action.status_code, v => rule.Action.StatusCode = v);
DynamicHelper.If<long>((object)action.sub_status_code, v => rule.Action.SubStatusCode = v);
DynamicHelper.If((object)action.description, v => rule.Action.StatusDescription = v);
DynamicHelper.If((object)action.reason, v => rule.Action.StatusReason = v);
DynamicHelper.If<RedirectType>((object)action.redirect_type, v => rule.Action.RedirectType = v);
}
//
@ -443,6 +466,9 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
}
}
DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
DynamicHelper.If<bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures = v);
//
// Conditions
if (model.conditions != null) {

Просмотреть файл

@ -345,6 +345,18 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
obj.replace_server_variable = rule.Action.ReplaceServerVariable;
}
//
// condition_match_constraints
if (fields.Exists("condition_match_constraints")) {
obj.condition_match_constraints = LogicalGroupingHelper.ToJsonModel(rule.Conditions.LogicalGrouping);
}
//
// track_all_captures
if (fields.Exists("track_all_captures")) {
obj.track_all_captures = rule.Conditions.TrackAllCaptures;
}
//
// conditions
if (fields.Exists("conditions")) {
@ -771,6 +783,9 @@ namespace Microsoft.IIS.Administration.WebServer.UrlRewrite
rule.PreCondition = pc.Name;
}
DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
DynamicHelper.If<bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures = v);
//
// Conditions
if (model.conditions != null) {

Просмотреть файл

@ -63,8 +63,7 @@ namespace Microsoft.IIS.Administration.Tests
type = "rewrite",
url = "def.aspx?a={R:1}&c={R:2}",
append_query_string = true,
description = "A test rule",
reason = "Replace url"
log_rewritten_url = true
},
server_variables = new object[] {
new {
@ -73,6 +72,8 @@ namespace Microsoft.IIS.Administration.Tests
replace = true
}
},
condition_match_constraints = "match_any",
track_all_captures = false,
conditions = new object[] {
new {
input = "{REQUEST_FILENAME}",
@ -102,8 +103,8 @@ namespace Microsoft.IIS.Administration.Tests
type = "redirect",
url = "def.aspx",
append_query_string = false,
description = "A test rule2",
reason = "Replace url2"
log_rewritten_url = false,
redirect_type = "found"
},
server_variables = new object[] {
new {
@ -112,6 +113,8 @@ namespace Microsoft.IIS.Administration.Tests
replace = false
}
},
condition_match_constraints = "match_all",
track_all_captures = true,
conditions = new object[] {
new {
input = "{REQUEST_FILENAME}2",
@ -168,9 +171,8 @@ namespace Microsoft.IIS.Administration.Tests
action = new {
type = "rewrite",
url = "def.aspx?a={R:1}&c={R:2}",
append_query_string = true,
description = "A test rule",
reason = "Replace url"
append_query_string = true
// log_rewritten_url not present in schema for global rule
},
server_variables = new object[] {
new {
@ -179,6 +181,8 @@ namespace Microsoft.IIS.Administration.Tests
replace = true
}
},
condition_match_constraints = "match_any",
track_all_captures = true,
conditions = new object[] {
new {
input = "{REQUEST_FILENAME}",
@ -208,8 +212,8 @@ namespace Microsoft.IIS.Administration.Tests
type = "redirect",
url = "def.aspx",
append_query_string = false,
description = "A test rule2",
reason = "Replace url2"
redirect_type = "found"
// log_rewritten_url not present in schema for global rule
},
server_variables = new object[] {
new {
@ -218,6 +222,8 @@ namespace Microsoft.IIS.Administration.Tests
replace = false
}
},
condition_match_constraints = "match_all",
track_all_captures = false,
conditions = new object[] {
new {
input = "{REQUEST_FILENAME}2",
@ -544,6 +550,8 @@ namespace Microsoft.IIS.Administration.Tests
negate = true,
stop_processing = false,
rewrite_value = "test rewrite value",
condition_match_constraints = "match_any",
track_all_captures = true,
conditions = new object[] {
new {
input = "{URL}",
@ -575,6 +583,8 @@ namespace Microsoft.IIS.Administration.Tests
negate = false,
stop_processing = true,
rewrite_value = "test rewrite update",
condition_match_constraints = "match_all",
track_all_captures = false,
conditions = new object[] {
new {
input = "{CONTENT_TYPE}",
@ -728,6 +738,8 @@ namespace Microsoft.IIS.Administration.Tests
Assert.Equal(a.Value<bool>("ignore_case"), b.Value<bool>("ignore_case"));
Assert.Equal(a.Value<bool>("negate"), b.Value<bool>("negate"));
Assert.Equal(a.Value<bool>("stop_processing"), b.Value<bool>("stop_processing"));
Assert.Equal(a.Value<string>("condition_match_constraints"), b.Value<string>("condition_match_constraints"));
Assert.Equal(a.Value<bool>("track_all_captures"), b.Value<bool>("track_all_captures"));
JObject action = a.Value<JObject>("action");
JObject resultAction = b.Value<JObject>("action");
@ -735,8 +747,22 @@ namespace Microsoft.IIS.Administration.Tests
Assert.Equal(action.Value<string>("type"), resultAction.Value<string>("type"));
Assert.Equal(action.Value<bool>("append_query_string"), resultAction.Value<bool>("append_query_string"));
Assert.Equal(action.Value<string>("url"), resultAction.Value<string>("url"));
Assert.Equal(action.Value<string>("description"), resultAction.Value<string>("description"));
Assert.Equal(action.Value<string>("reason"), resultAction.Value<string>("reason"));
Assert.Equal(action.Value<bool>("log_rewritten_url"), resultAction.Value<bool>("log_rewritten_url"));
//
// redirect rule
if (action.Value<string>("type").Equals("redirect", StringComparison.OrdinalIgnoreCase)) {
Assert.Equal(action.Value<string>("redirect_type"), resultAction.Value<string>("redirect_type"));
}
//
// custom response rule
if (action.Value<string>("type").Equals("custom_response", StringComparison.OrdinalIgnoreCase)) {
Assert.Equal(action.Value<string>("description"), resultAction.Value<string>("description"));
Assert.Equal(action.Value<string>("reason"), resultAction.Value<string>("reason"));
Assert.Equal(action.Value<long>("status_code"), resultAction.Value<long>("status_code"));
Assert.Equal(action.Value<long>("sub_status_code"), resultAction.Value<long>("sub_status_code"));
}
JObject[] aServerVariables = a["server_variables"].ToObject<JObject[]>();
JObject[] bServerVariables = b["server_variables"].ToObject<JObject[]>();
@ -856,6 +882,8 @@ namespace Microsoft.IIS.Administration.Tests
Assert.Equal(a.Value<bool>("negate"), b.Value<bool>("negate"));
Assert.Equal(a.Value<bool>("stop_processing"), b.Value<bool>("stop_processing"));
Assert.Equal(a.Value<string>("rewrite_value"), b.Value<string>("rewrite_value"));
Assert.Equal(a.Value<string>("condition_match_constraints"), b.Value<string>("condition_match_constraints"));
Assert.Equal(a.Value<bool>("track_all_captures"), b.Value<bool>("track_all_captures"));
//
// Conditions