mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-07-31 08:35:58 +08:00
regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment
This commit is contained in:
@ -1,14 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public class Configuration
|
||||
{
|
||||
public Configuration()
|
||||
{
|
||||
ReRoutes = new List<ReRoute>();
|
||||
}
|
||||
|
||||
public List<ReRoute> ReRoutes { get; set; }
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public class DownstreamTemplateAlreadyUsedError : Error
|
||||
{
|
||||
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public interface IConfigurationValidator
|
||||
{
|
||||
Response<ConfigurationValidationResult> IsValid(Configuration configuration);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public interface IOcelotConfiguration
|
||||
{
|
||||
List<ReRoute> ReRoutes { get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ocelot.Library.Infrastructure.Configuration.Yaml;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
{
|
||||
public class OcelotConfiguration : IOcelotConfiguration
|
||||
{
|
||||
private readonly IOptions<YamlConfiguration> _options;
|
||||
private readonly List<ReRoute> _reRoutes;
|
||||
private const string RegExMatchEverything = ".*";
|
||||
private const string RegExMatchEndString = "$";
|
||||
|
||||
public OcelotConfiguration(IOptions<YamlConfiguration> options)
|
||||
{
|
||||
_options = options;
|
||||
_reRoutes = new List<ReRoute>();
|
||||
SetReRoutes();
|
||||
}
|
||||
|
||||
private void SetReRoutes()
|
||||
{
|
||||
foreach(var reRoute in _options.Value.ReRoutes)
|
||||
{
|
||||
var upstreamTemplate = reRoute.UpstreamTemplate;
|
||||
var placeholders = new List<string>();
|
||||
|
||||
for (int i = 0; i < upstreamTemplate.Length; i++)
|
||||
{
|
||||
if (IsPlaceHolder(upstreamTemplate, i))
|
||||
{
|
||||
var postitionOfPlaceHolderClosingBracket = upstreamTemplate.IndexOf('}', i);
|
||||
var difference = postitionOfPlaceHolderClosingBracket - i + 1;
|
||||
var variableName = upstreamTemplate.Substring(i, difference);
|
||||
placeholders.Add(variableName);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var placeholder in placeholders)
|
||||
{
|
||||
upstreamTemplate = upstreamTemplate.Replace(placeholder, RegExMatchEverything);
|
||||
}
|
||||
|
||||
upstreamTemplate = $"{upstreamTemplate}{RegExMatchEndString}";
|
||||
|
||||
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate));
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsPlaceHolder(string upstreamTemplate, int i)
|
||||
{
|
||||
return upstreamTemplate[i] == '{';
|
||||
}
|
||||
|
||||
public List<ReRoute> ReRoutes => _reRoutes;
|
||||
}
|
||||
}
|
@ -2,8 +2,17 @@
|
||||
{
|
||||
public class ReRoute
|
||||
{
|
||||
public string DownstreamTemplate { get; set; }
|
||||
public string UpstreamTemplate { get; set; }
|
||||
public string UpstreamHttpMethod { get; set; }
|
||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern)
|
||||
{
|
||||
DownstreamTemplate = downstreamTemplate;
|
||||
UpstreamTemplate = upstreamTemplate;
|
||||
UpstreamHttpMethod = upstreamHttpMethod;
|
||||
UpstreamTemplatePattern = upstreamTemplatePattern;
|
||||
}
|
||||
|
||||
public string DownstreamTemplate { get; private set; }
|
||||
public string UpstreamTemplate { get; private set; }
|
||||
public string UpstreamTemplatePattern { get; private set; }
|
||||
public string UpstreamHttpMethod { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public class ConfigurationValidationResult
|
||||
{
|
@ -2,11 +2,11 @@
|
||||
using System.Linq;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public class ConfigurationValidator : IConfigurationValidator
|
||||
{
|
||||
public Response<ConfigurationValidationResult> IsValid(Configuration configuration)
|
||||
public Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration)
|
||||
{
|
||||
var duplicateUpstreamTemplates = configuration.ReRoutes
|
||||
.Select(r => r.DownstreamTemplate)
|
@ -0,0 +1,11 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public class DownstreamTemplateAlreadyUsedError : Error
|
||||
{
|
||||
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public interface IConfigurationValidator
|
||||
{
|
||||
Response<ConfigurationValidationResult> IsValid(YamlConfiguration configuration);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public class YamlConfiguration
|
||||
{
|
||||
public YamlConfiguration()
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>();
|
||||
}
|
||||
|
||||
public List<YamlReRoute> ReRoutes { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
{
|
||||
public class YamlReRoute
|
||||
{
|
||||
public string DownstreamTemplate { get; set; }
|
||||
public string UpstreamTemplate { get; set; }
|
||||
public string UpstreamHttpMethod { get; set; }
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ocelot.Library.Infrastructure.Configuration;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
|
||||
@ -9,24 +10,29 @@ namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
||||
{
|
||||
public class DownstreamRouteFinder : IDownstreamRouteFinder
|
||||
{
|
||||
private readonly IOptions<Configuration.Configuration> _configuration;
|
||||
private readonly IOcelotConfiguration _configuration;
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||
private readonly ITemplateVariableNameAndValueFinder _templateVariableNameAndValueFinder;
|
||||
|
||||
public DownstreamRouteFinder(IOptions<Configuration.Configuration> configuration, IUrlPathToUrlTemplateMatcher urlMatcher)
|
||||
public DownstreamRouteFinder(IOcelotConfiguration configuration, IUrlPathToUrlTemplateMatcher urlMatcher, ITemplateVariableNameAndValueFinder templateVariableNameAndValueFinder)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_urlMatcher = urlMatcher;
|
||||
_templateVariableNameAndValueFinder = templateVariableNameAndValueFinder;
|
||||
}
|
||||
|
||||
public Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
|
||||
{
|
||||
foreach (var template in _configuration.Value.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)))
|
||||
foreach (var template in _configuration.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplate);
|
||||
var urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplatePattern);
|
||||
|
||||
if (urlMatch.Match)
|
||||
if (urlMatch.Data.Match)
|
||||
{
|
||||
return new OkResponse<DownstreamRoute>(new DownstreamRoute(urlMatch.TemplateVariableNameAndValues, template.DownstreamTemplate));
|
||||
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
|
||||
template.UpstreamTemplate);
|
||||
|
||||
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template.DownstreamTemplate));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,13 @@ using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.RequestBuilder
|
||||
{
|
||||
public class HttpRequestBuilder : IRequestBuilder
|
||||
{
|
||||
public async Task<Request> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
|
||||
public async Task<Response<Request>> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
|
||||
IRequestCookieCollection cookies, string queryString, string contentType)
|
||||
{
|
||||
var method = new HttpMethod(httpMethod);
|
||||
@ -64,9 +65,8 @@ namespace Ocelot.Library.Infrastructure.RequestBuilder
|
||||
cookieContainer.Add(uri, new Cookie(cookie.Key, cookie.Value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new Request(httpRequestMessage, cookieContainer);
|
||||
|
||||
return new OkResponse<Request>(new Request(httpRequestMessage, cookieContainer));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.RequestBuilder
|
||||
{
|
||||
public interface IRequestBuilder
|
||||
{
|
||||
Task<Request> Build(string httpMethod,
|
||||
Task<Response<Request>> Build(string httpMethod,
|
||||
string downstreamUrl,
|
||||
Stream content,
|
||||
IHeaderDictionary headers,
|
||||
|
@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
{
|
||||
public interface ITemplateVariableNameAndValueFinder
|
||||
{
|
||||
Response<List<TemplateVariableNameAndValue>> Find(string upstreamUrlPath, string upstreamUrlPathTemplate);
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
{
|
||||
public interface IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
UrlMatch Match(string upstreamUrlPath, string upstreamUrlPathTemplate);
|
||||
Response<UrlMatch> Match(string upstreamUrlPath, string upstreamUrlPathTemplate);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcher : IUrlPathToUrlTemplateMatcher
|
||||
{
|
||||
public Response<UrlMatch> Match(string upstreamUrlPath, string upstreamUrlPathTemplate)
|
||||
{
|
||||
var regex = new Regex(upstreamUrlPathTemplate);
|
||||
|
||||
return regex.IsMatch(upstreamUrlPath)
|
||||
? new OkResponse<UrlMatch>(new UrlMatch(true))
|
||||
: new OkResponse<UrlMatch>(new UrlMatch(false));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
{
|
||||
public class UrlPathToUrlTemplateMatcher : IUrlPathToUrlTemplateMatcher
|
||||
public class TemplateVariableNameAndValueFinder : ITemplateVariableNameAndValueFinder
|
||||
{
|
||||
public UrlMatch Match(string upstreamUrlPath, string upstreamUrlPathTemplate)
|
||||
public Response<List<TemplateVariableNameAndValue>> Find(string upstreamUrlPath, string upstreamUrlPathTemplate)
|
||||
{
|
||||
if (upstreamUrlPath.Length > upstreamUrlPathTemplate.Length)
|
||||
{
|
||||
return new UrlMatch(false, new List<TemplateVariableNameAndValue>(), string.Empty);
|
||||
}
|
||||
|
||||
var urlPathTemplateCopy = upstreamUrlPathTemplate;
|
||||
|
||||
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
|
||||
|
||||
int counterForUrl = 0;
|
||||
|
||||
|
||||
for (int counterForTemplate = 0; counterForTemplate < upstreamUrlPathTemplate.Length; counterForTemplate++)
|
||||
{
|
||||
if (CharactersDontMatch(upstreamUrlPathTemplate[counterForTemplate], upstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,upstreamUrlPath.Length))
|
||||
@ -37,15 +30,14 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
counterForUrl = GetNextCounterPosition(upstreamUrlPath, counterForUrl, '/');
|
||||
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new UrlMatch(false, templateKeysAndValues, string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
return new OkResponse<List<TemplateVariableNameAndValue>>(templateKeysAndValues);
|
||||
}
|
||||
counterForUrl++;
|
||||
}
|
||||
return new UrlMatch(true, templateKeysAndValues, urlPathTemplateCopy);
|
||||
|
||||
return new OkResponse<List<TemplateVariableNameAndValue>>(templateKeysAndValues);
|
||||
}
|
||||
|
||||
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl)
|
@ -1,17 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlMatcher
|
||||
{
|
||||
public class UrlMatch
|
||||
{
|
||||
public UrlMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlTemplate)
|
||||
public UrlMatch(bool match)
|
||||
{
|
||||
Match = match;
|
||||
TemplateVariableNameAndValues = templateVariableNameAndValues;
|
||||
DownstreamUrlTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
public bool Match {get;private set;}
|
||||
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
|
||||
public string DownstreamUrlTemplate {get;private set;}
|
||||
}
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
using System.Text;
|
||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
|
||||
{
|
||||
public class DownstreamUrlTemplateVariableReplacer : IDownstreamUrlTemplateVariableReplacer
|
||||
{
|
||||
public string ReplaceTemplateVariables(DownstreamRoute downstreamRoute)
|
||||
public Response<string> ReplaceTemplateVariables(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
var upstreamUrl = new StringBuilder();
|
||||
|
||||
@ -16,7 +17,7 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
|
||||
upstreamUrl.Replace(templateVarAndValue.TemplateVariableName, templateVarAndValue.TemplateVariableValue);
|
||||
}
|
||||
|
||||
return upstreamUrl.ToString();
|
||||
return new OkResponse<string>(upstreamUrl.ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
|
||||
{
|
||||
public interface IDownstreamUrlTemplateVariableReplacer
|
||||
{
|
||||
string ReplaceTemplateVariables(DownstreamRoute downstreamRoute);
|
||||
Response<string> ReplaceTemplateVariables(DownstreamRoute downstreamRoute);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user