mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 18:32:51 +08:00
working state
This commit is contained in:
parent
4f2d94ceba
commit
4ca1dc8846
@ -0,0 +1,7 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
|
||||||
|
{
|
||||||
|
public interface IUrlPathToUrlPathTemplateMatcher
|
||||||
|
{
|
||||||
|
bool Match(string urlPath, string urlPathTemplate);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
|
||||||
|
{
|
||||||
|
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
|
||||||
|
{
|
||||||
|
public bool Match(string urlPath, string urlPathTemplate)
|
||||||
|
{
|
||||||
|
urlPath = urlPath.ToLower();
|
||||||
|
|
||||||
|
urlPathTemplate = urlPathTemplate.ToLower();
|
||||||
|
|
||||||
|
int counterForUrl = 0;
|
||||||
|
|
||||||
|
for (int counterForTemplate = 0; counterForTemplate < urlPathTemplate.Length; counterForTemplate++)
|
||||||
|
{
|
||||||
|
if (CharactersDontMatch(urlPathTemplate[counterForTemplate], urlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,urlPath.Length))
|
||||||
|
{
|
||||||
|
if (IsPlaceholder(urlPathTemplate[counterForTemplate]))
|
||||||
|
{
|
||||||
|
counterForTemplate = GetNextCounterPosition(urlPathTemplate, counterForTemplate, '}');
|
||||||
|
counterForUrl = GetNextCounterPosition(urlPath, counterForUrl, '/');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counterForUrl++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetNextCounterPosition(string urlTemplate, int counterForTemplate, char delimiter)
|
||||||
|
{
|
||||||
|
var closingPlaceHolderPositionOnTemplate = urlTemplate.IndexOf(delimiter, counterForTemplate);
|
||||||
|
return closingPlaceHolderPositionOnTemplate + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CharactersDontMatch(char characterOne, char characterTwo)
|
||||||
|
{
|
||||||
|
return characterOne != characterTwo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ContinueScanningUrl(int counterForUrl, int urlLength)
|
||||||
|
{
|
||||||
|
return counterForUrl < urlLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsPlaceholder(char character)
|
||||||
|
{
|
||||||
|
return character == '{';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,6 +32,7 @@ namespace Ocelot
|
|||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||||
{
|
{
|
||||||
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||||
|
|
||||||
loggerFactory.AddDebug();
|
loggerFactory.AddDebug();
|
||||||
|
|
||||||
app.Run(async context =>
|
app.Run(async context =>
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
using System;
|
using System;
|
||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
using Ocelot.Library.Infrastructure.Router.UpstreamRouter;
|
using Ocelot.Library.Infrastructure.Router.UpstreamRouter;
|
||||||
|
using Ocelot.Library.Infrastructure.Router.UrlPathMatcher;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests
|
namespace Ocelot.UnitTests
|
||||||
{
|
{
|
||||||
public class UrlMapperTests
|
public class UrlPathToUrlPathTemplateMatcherTests
|
||||||
{
|
{
|
||||||
private UrlToUrlTemplateMatcher _urlMapper;
|
private IUrlPathToUrlPathTemplateMatcher _urlMapper;
|
||||||
|
|
||||||
public UrlMapperTests()
|
public UrlPathToUrlPathTemplateMatcherTests()
|
||||||
{
|
{
|
||||||
_urlMapper = new UrlToUrlTemplateMatcher();
|
_urlMapper = new UrlPathToUrlPathTemplateMatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -78,56 +79,4 @@ namespace Ocelot.UnitTests
|
|||||||
result.ShouldBeTrue();
|
result.ShouldBeTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UrlToUrlTemplateMatcher
|
|
||||||
{
|
|
||||||
public bool Match(string url, string urlTemplate)
|
|
||||||
{
|
|
||||||
url = url.ToLower();
|
|
||||||
|
|
||||||
urlTemplate = urlTemplate.ToLower();
|
|
||||||
|
|
||||||
int counterForUrl = 0;
|
|
||||||
|
|
||||||
for (int counterForTemplate = 0; counterForTemplate < urlTemplate.Length; counterForTemplate++)
|
|
||||||
{
|
|
||||||
if (CharactersDontMatch(urlTemplate[counterForTemplate], url[counterForUrl]) && ContinueScanningUrl(counterForUrl,url.Length))
|
|
||||||
{
|
|
||||||
if (IsPlaceholder(urlTemplate[counterForTemplate]))
|
|
||||||
{
|
|
||||||
counterForTemplate = GetNextCounterPosition(urlTemplate, counterForTemplate, '}');
|
|
||||||
counterForUrl = GetNextCounterPosition(url, counterForUrl, '/');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
counterForUrl++;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int GetNextCounterPosition(string urlTemplate, int counterForTemplate, char delimiter)
|
|
||||||
{
|
|
||||||
var closingPlaceHolderPositionOnTemplate = urlTemplate.IndexOf(delimiter, counterForTemplate);
|
|
||||||
return closingPlaceHolderPositionOnTemplate + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool CharactersDontMatch(char characterOne, char characterTwo)
|
|
||||||
{
|
|
||||||
return characterOne != characterTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool ContinueScanningUrl(int counterForUrl, int urlLength)
|
|
||||||
{
|
|
||||||
return counterForUrl < urlLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsPlaceholder(char character)
|
|
||||||
{
|
|
||||||
return character == '{';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user