Added url replacer

This commit is contained in:
Tom Gardham-Pallister
2016-07-21 21:14:27 +01:00
parent dbff2b9530
commit d3b9fddcfd
8 changed files with 250 additions and 38 deletions

View File

@ -2,6 +2,6 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public interface IUrlPathToUrlPathTemplateMatcher
{
UrlPathMatch Match(string urlPath, string urlPathTemplate);
UrlPathMatch Match(string downstreamUrlPath, string downStreamUrlPathTemplate);
}
}

View File

@ -4,15 +4,14 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathMatch
{
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string urlPathTemplate)
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlPathTemplate)
{
Match = match;
TemplateVariableNameAndValues = templateVariableNameAndValues;
UrlPathTemplate = urlPathTemplate;
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
}
public bool Match {get;private set;}
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
public string UrlPathTemplate {get;private set;}
public string DownstreamUrlPathTemplate {get;private set;}
}
}

View File

@ -5,35 +5,31 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
{
public UrlPathMatch Match(string urlPath, string urlPathTemplate)
public UrlPathMatch Match(string downstreamUrlPath, string downstreamUrlPathTemplate)
{
var urlPathTemplateCopy = urlPathTemplate;
var urlPathTemplateCopy = downstreamUrlPathTemplate;
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
urlPath = urlPath.ToLower();
urlPathTemplate = urlPathTemplate.ToLower();
int counterForUrl = 0;
for (int counterForTemplate = 0; counterForTemplate < urlPathTemplate.Length; counterForTemplate++)
for (int counterForTemplate = 0; counterForTemplate < downstreamUrlPathTemplate.Length; counterForTemplate++)
{
if (CharactersDontMatch(urlPathTemplate[counterForTemplate], urlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,urlPath.Length))
if (CharactersDontMatch(downstreamUrlPathTemplate[counterForTemplate], downstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,downstreamUrlPath.Length))
{
if (IsPlaceholder(urlPathTemplate[counterForTemplate]))
if (IsPlaceholder(downstreamUrlPathTemplate[counterForTemplate]))
{
var variableName = GetPlaceholderVariableName(urlPathTemplate, counterForTemplate);
var variableName = GetPlaceholderVariableName(downstreamUrlPathTemplate, counterForTemplate);
var variableValue = GetPlaceholderVariableValue(urlPath, counterForUrl);
var variableValue = GetPlaceholderVariableValue(downstreamUrlPath, counterForUrl);
var templateVariableNameAndValue = new TemplateVariableNameAndValue(variableName, variableValue);
templateKeysAndValues.Add(templateVariableNameAndValue);
counterForTemplate = GetNextCounterPosition(urlPathTemplate, counterForTemplate, '}');
counterForTemplate = GetNextCounterPosition(downstreamUrlPathTemplate, counterForTemplate, '}');
counterForUrl = GetNextCounterPosition(urlPath, counterForUrl, '/');
counterForUrl = GetNextCounterPosition(downstreamUrlPath, counterForUrl, '/');
continue;
}

View File

@ -0,0 +1,10 @@
using Ocelot.Library.Infrastructure.UrlPathMatcher;
namespace Ocelot.Library.Infrastructure.UrlPathReplacer
{
public interface IUpstreamUrlPathTemplateVariableReplacer
{
string ReplaceTemplateVariable(string upstreamPathTemplate, UrlPathMatch urlPathMatch);
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Text;
using Ocelot.Library.Infrastructure.UrlPathMatcher;
namespace Ocelot.Library.Infrastructure.UrlPathReplacer
{
public class UpstreamUrlPathTemplateVariableReplacer : IUpstreamUrlPathTemplateVariableReplacer
{
public string ReplaceTemplateVariable(string upstreamPathTemplate, UrlPathMatch urlPathMatch)
{
var upstreamUrl = new StringBuilder();
upstreamUrl.Append(upstreamPathTemplate);
foreach (var templateVarAndValue in urlPathMatch.TemplateVariableNameAndValues)
{
upstreamUrl.Replace(templateVarAndValue.TemplateVariableName, templateVarAndValue.TemplateVariableValue);
}
return upstreamUrl.ToString();
}
}
}