mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	UpstreamHttpMethod property (of class FileReRoute) changes from string to List<string>.
This commit is contained in:
		@@ -67,11 +67,13 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            _upstreamTemplatePattern = input;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
        public ReRouteBuilder WithUpstreamHttpMethod(string input)
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder WithUpstreamHttpMethod(List<string> input)
 | 
			
		||||
        {
 | 
			
		||||
            _upstreamHttpMethod = string.IsNullOrWhiteSpace(input) ? new List<HttpMethod>() : input.Split(',').Select(x => new HttpMethod(x.Trim())).ToList();
 | 
			
		||||
            _upstreamHttpMethod = (input.Count == 0) ? new List<HttpMethod>() : input.Select(x => new HttpMethod(x.Trim())).ToList();
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder WithIsAuthenticated(bool input)
 | 
			
		||||
        {
 | 
			
		||||
            _isAuthenticated = input;
 | 
			
		||||
@@ -144,7 +146,6 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
       
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder WithLoadBalancerKey(string loadBalancerKey)
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancerKey = loadBalancerKey;
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ namespace Ocelot.Configuration.File
 | 
			
		||||
    {
 | 
			
		||||
        public FileReRoute()
 | 
			
		||||
        {
 | 
			
		||||
            UpstreamHttpMethod = new List<string>();
 | 
			
		||||
            AddHeadersToRequest = new Dictionary<string, string>();
 | 
			
		||||
            AddClaimsToRequest = new Dictionary<string, string>();
 | 
			
		||||
            RouteClaimsRequirement = new Dictionary<string, string>();
 | 
			
		||||
@@ -18,7 +19,7 @@ namespace Ocelot.Configuration.File
 | 
			
		||||
 | 
			
		||||
        public string DownstreamPathTemplate { get; set; }
 | 
			
		||||
        public string UpstreamPathTemplate { get; set; }
 | 
			
		||||
        public string UpstreamHttpMethod { get; set; }
 | 
			
		||||
        public List<string> UpstreamHttpMethod { get; set; }
 | 
			
		||||
        public FileAuthenticationOptions AuthenticationOptions { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddHeadersToRequest { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddClaimsToRequest { get; set; }
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ namespace Ocelot.Configuration.Validator
 | 
			
		||||
    {
 | 
			
		||||
        public Response<ConfigurationValidationResult> IsValid(FileConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var result = CheckForDupliateReRoutes(configuration);
 | 
			
		||||
            var result = CheckForDuplicateReRoutes(configuration);
 | 
			
		||||
 | 
			
		||||
            if (result.IsError)
 | 
			
		||||
            {
 | 
			
		||||
@@ -91,25 +91,41 @@ namespace Ocelot.Configuration.Validator
 | 
			
		||||
            return new ConfigurationValidationResult(false, errors);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private ConfigurationValidationResult CheckForDupliateReRoutes(FileConfiguration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            var hasDupes = configuration.ReRoutes
 | 
			
		||||
                   .GroupBy(x => new { x.UpstreamPathTemplate, x.UpstreamHttpMethod }).Any(x => x.Skip(1).Any());
 | 
			
		||||
        private ConfigurationValidationResult CheckForDuplicateReRoutes(FileConfiguration configuration)
 | 
			
		||||
        {         
 | 
			
		||||
            var duplicatedUpstreamPathTemplates = new List<string>();
 | 
			
		||||
 | 
			
		||||
            if (!hasDupes)
 | 
			
		||||
            var distinctUpstreamPathTemplates = configuration.ReRoutes.Select(x => x.UpstreamPathTemplate).Distinct();
 | 
			
		||||
            
 | 
			
		||||
            foreach (string upstreamPathTemplate in distinctUpstreamPathTemplates)
 | 
			
		||||
            {
 | 
			
		||||
                var reRoutesWithUpstreamPathTemplate = configuration.ReRoutes.Where(x => x.UpstreamPathTemplate == upstreamPathTemplate);
 | 
			
		||||
 | 
			
		||||
                var hasEmptyListToAllowAllHttpVerbs = reRoutesWithUpstreamPathTemplate.Where(x => x.UpstreamHttpMethod.Count() == 0).Any();
 | 
			
		||||
                var hasDuplicateEmptyListToAllowAllHttpVerbs = reRoutesWithUpstreamPathTemplate.Where(x => x.UpstreamHttpMethod.Count() == 0).Count() > 1;
 | 
			
		||||
                var hasSpecificHttpVerbs = reRoutesWithUpstreamPathTemplate.Where(x => x.UpstreamHttpMethod.Count() > 0).Any();
 | 
			
		||||
                var hasDuplicateSpecificHttpVerbs = reRoutesWithUpstreamPathTemplate.SelectMany(x => x.UpstreamHttpMethod).GroupBy(x => x.ToLower()).SelectMany(x => x.Skip(1)).Any();
 | 
			
		||||
 | 
			
		||||
                if (hasDuplicateEmptyListToAllowAllHttpVerbs || hasDuplicateSpecificHttpVerbs || (hasEmptyListToAllowAllHttpVerbs && hasSpecificHttpVerbs))
 | 
			
		||||
                {
 | 
			
		||||
                    duplicatedUpstreamPathTemplates.Add(upstreamPathTemplate);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (duplicatedUpstreamPathTemplates.Count() == 0)
 | 
			
		||||
            {
 | 
			
		||||
                return new ConfigurationValidationResult(false);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                var errors = duplicatedUpstreamPathTemplates
 | 
			
		||||
                    .Select(d => new DownstreamPathTemplateAlreadyUsedError(string.Format("Duplicate DownstreamPath: {0}", d)))
 | 
			
		||||
                    .Cast<Error>()
 | 
			
		||||
                    .ToList();
 | 
			
		||||
 | 
			
		||||
            var dupes = configuration.ReRoutes.GroupBy(x => new { x.UpstreamPathTemplate, x.UpstreamHttpMethod })
 | 
			
		||||
                               .Where(x => x.Skip(1).Any());
 | 
			
		||||
                return new ConfigurationValidationResult(true, errors);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var errors = dupes
 | 
			
		||||
                .Select(d => new DownstreamPathTemplateAlreadyUsedError(string.Format("Duplicate DownstreamPath: {0}", d.Key.UpstreamPathTemplate)))
 | 
			
		||||
                .Cast<Error>()
 | 
			
		||||
                .ToList();
 | 
			
		||||
 | 
			
		||||
            return new ConfigurationValidationResult(true, errors);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user