Skip to content

Commit

Permalink
1. streamlined rules cache
Browse files Browse the repository at this point in the history
2. fixed bug in condition match tester and created unit test
3. added exception handling to initializer
4. added back in reinitialization of cache on a request
  • Loading branch information
iamandycohen committed Oct 23, 2015
1 parent 5862c71 commit 4992c4b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 55 deletions.
61 changes: 61 additions & 0 deletions Hi.UrlRewrite.Tests/RewriteHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Hi.UrlRewrite.Entities.Conditions;
using Hi.UrlRewrite.Entities.Rules;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hi.UrlRewrite.Processing;

Expand Down Expand Up @@ -44,5 +46,64 @@ public void ReplaceTokens()
// assert
Assert.AreEqual(replacementOutput, @"test var1=1&var2=2 test This is a test test text/html test off test");
}

[TestMethod]
public void ConditionMatchAllMustMatchButOneDoesNot()
{
var rule = new InboundRule
{
ConditionLogicalGrouping = LogicalGrouping.MatchAll,
Conditions = new List<Condition>
{
new Condition()
{
CheckIfInputString = CheckIfInputString.DoesNotMatchThePattern,
InputString = "OFF",
Pattern = "OFF"
},
new Condition()
{
CheckIfInputString = CheckIfInputString.MatchesThePattern,
InputString = "OFF",
Pattern = "OFF"
},
}
};

Match match = null;
var conditionMatchResult = RewriteHelper.TestConditionMatches(rule, null, out match);

Assert.IsFalse(conditionMatchResult.Matched);
}


[TestMethod]
public void ConditionMatchesAtLeastOne()
{
var rule = new InboundRule
{
ConditionLogicalGrouping = LogicalGrouping.MatchAny,
Conditions = new List<Condition>
{
new Condition()
{
CheckIfInputString = CheckIfInputString.DoesNotMatchThePattern,
InputString = "OFF",
Pattern = "OFF"
},
new Condition()
{
CheckIfInputString = CheckIfInputString.MatchesThePattern,
InputString = "OFF",
Pattern = "OFF"
},
}
};

Match match = null;
var conditionMatchResult = RewriteHelper.TestConditionMatches(rule, null, out match);

Assert.IsTrue(conditionMatchResult.Matched);
}
}
}
38 changes: 17 additions & 21 deletions Hi.UrlRewrite/Caching/RulesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,28 @@ public RulesCache(Database db) :

public List<InboundRule> GetInboundRules()
{
List<InboundRule> returnRules = null;
var rules = GetObject(inboundRulesKey) as IEnumerable<InboundRule>;
if (rules != null)
{
returnRules = rules.ToList();
}

return returnRules;
return GetRules<InboundRule>(inboundRulesKey);
}

public void SetInboundRules(IEnumerable<InboundRule> inboundRules)
{
long size;
SetRules(inboundRules, inboundRulesKey);
}

using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, inboundRules.ToList());
size = memoryStream.Length;
}
public List<OutboundRule> GetOutboundRules()
{
return GetRules<OutboundRule>(outboundRulesKey);
}

SetObject(inboundRulesKey, inboundRules, size);
public void SetOutboundRules(IEnumerable<OutboundRule> outboundRules)
{
SetRules(outboundRules, outboundRulesKey);
}

public List<OutboundRule> GetOutboundRules()
public List<T> GetRules<T>(string key) where T : IBaseRule
{
List<OutboundRule> returnRules = null;
var rules = GetObject(inboundRulesKey) as IEnumerable<OutboundRule>;
List<T> returnRules = null;
var rules = GetObject(key) as IEnumerable<T>;
if (rules != null)
{
returnRules = rules.ToList();
Expand All @@ -59,7 +53,7 @@ public List<OutboundRule> GetOutboundRules()
return returnRules;
}

public void SetOutboundRules(IEnumerable<OutboundRule> outboundRules)
public void SetRules<T>(IEnumerable<T> outboundRules, string key) where T : IBaseRule
{
long size;

Expand All @@ -70,8 +64,10 @@ public void SetOutboundRules(IEnumerable<OutboundRule> outboundRules)
size = memoryStream.Length;
}

SetObject(outboundRulesKey, outboundRules, size);
SetObject(key, outboundRules, size);
}



}
}
9 changes: 7 additions & 2 deletions Hi.UrlRewrite/Processing/InboundRewriteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ private List<InboundRule> GetInboundRules(Database db)
var cache = RulesCacheManager.GetCache(db);
var inboundRules = cache.GetInboundRules();

if (inboundRules == null)
if (inboundRules != null) return inboundRules;

Log.Info(this, db, "Initializing Inbound Rules.");

using (new SecurityDisabler())
{
Log.Info(this, db, "Url Rewrite cache has not been initialized yet.");
var rulesEngine = new RulesEngine(db);
inboundRules = rulesEngine.GetCachedInboundRules();
}

return inboundRules;
Expand Down
15 changes: 11 additions & 4 deletions Hi.UrlRewrite/Processing/InboundRuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ public void Process(PipelineArgs args)
{
Log.Info(this, "Initializing URL Rewrite.");

using (new SecurityDisabler())
try
{
foreach (var db in Factory.GetDatabases().Where(e => e.HasContentItem))
using (new SecurityDisabler())
{
var rulesEngine = new RulesEngine(db);
rulesEngine.GetCachedInboundRules();
foreach (var db in Factory.GetDatabases().Where(e => e.HasContentItem))
{
var rulesEngine = new RulesEngine(db);
rulesEngine.GetCachedInboundRules();
}
}
}
catch (Exception ex)
{
Hi.UrlRewrite.Log.Error(this, ex, "Exception during initialization.");
}
}
}
}
65 changes: 37 additions & 28 deletions Hi.UrlRewrite/Processing/RewriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,46 @@ public static string ReplaceTokens(Uri uri, string input)

public static string ReplaceTokens(Replacements replacements, string input)
{
var tokenRegex = new Regex(@"{(\w+)}");
var tokenMatches = tokenRegex.Matches(input);
string output = input;

foreach (Match tokenMatch in tokenMatches)
if (replacements != null)
{
var wholeToken = tokenMatch.Groups[0].Value;
var token = tokenMatch.Groups[1].Value;
string tokenReplacement = null;
var tokenRegex = new Regex(@"{(\w+)}");
var tokenMatches = tokenRegex.Matches(input);

const string RESPONSE = "RESPONSE_";
const string REQUEST = "REQUEST_";

if (replacements.ResponseHeaders != null && token.StartsWith(RESPONSE))
foreach (Match tokenMatch in tokenMatches)
{
var tokenKey = token.Remove(0, RESPONSE.Length);
tokenKey = tokenKey.Replace("_", "-");
var wholeToken = tokenMatch.Groups[0].Value;
var token = tokenMatch.Groups[1].Value;
string tokenReplacement = null;

tokenReplacement = replacements.ResponseHeaders[tokenKey];
}
else if (replacements.RequestHeaders != null && token.StartsWith(REQUEST))
{
var tokenKey = token.Remove(0, REQUEST.Length);
tokenKey = tokenKey.Replace("_", "-");
const string RESPONSE = "RESPONSE_";
const string REQUEST = "REQUEST_";

tokenReplacement = replacements.RequestHeaders[tokenKey];
}
else if (replacements.RequestServerVariables != null)
{
tokenReplacement = replacements.RequestServerVariables[token];
}
if (replacements.ResponseHeaders != null && token.StartsWith(RESPONSE))
{
var tokenKey = token.Remove(0, RESPONSE.Length);
tokenKey = tokenKey.Replace("_", "-");

if (!string.IsNullOrEmpty(tokenReplacement))
{
output = output.Replace(wholeToken, tokenReplacement);
}
tokenReplacement = replacements.ResponseHeaders[tokenKey];
}
else if (replacements.RequestHeaders != null && token.StartsWith(REQUEST))
{
var tokenKey = token.Remove(0, REQUEST.Length);
tokenKey = tokenKey.Replace("_", "-");

tokenReplacement = replacements.RequestHeaders[tokenKey];
}
else if (replacements.RequestServerVariables != null)
{
tokenReplacement = replacements.RequestServerVariables[token];
}

if (!string.IsNullOrEmpty(tokenReplacement))
{
output = output.Replace(wholeToken, tokenReplacement);
}
}
}

return output;
Expand Down Expand Up @@ -155,6 +159,11 @@ public static ConditionMatchResult TestConditionMatches(IConditionsProperties ru
conditionMatchResult.MatchedConditions.Add(new MatchedCondition(condition, conditionInput));
conditionMatchResult.Matched = true;
}
else
{
conditionMatchResult.Matched = false;
break;
}
}
}
else
Expand Down

0 comments on commit 4992c4b

Please sign in to comment.