mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	Add DownstreamHttpMethodCreatorMiddleware
This commit is contained in:
		@@ -41,6 +41,7 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
        private List<AddHeader> _addHeadersToUpstream;
 | 
					        private List<AddHeader> _addHeadersToUpstream;
 | 
				
			||||||
        private bool _dangerousAcceptAnyServerCertificateValidator;
 | 
					        private bool _dangerousAcceptAnyServerCertificateValidator;
 | 
				
			||||||
        private SecurityOptions _securityOptions;
 | 
					        private SecurityOptions _securityOptions;
 | 
				
			||||||
 | 
					        private string _downstreamHttpMethod;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public DownstreamReRouteBuilder()
 | 
					        public DownstreamReRouteBuilder()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -56,6 +57,12 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public DownstreamReRouteBuilder WithDownStreamHttpMethod(string method)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _downstreamHttpMethod = method;
 | 
				
			||||||
 | 
					            return this;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public DownstreamReRouteBuilder WithLoadBalancerOptions(LoadBalancerOptions loadBalancerOptions)
 | 
					        public DownstreamReRouteBuilder WithLoadBalancerOptions(LoadBalancerOptions loadBalancerOptions)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _loadBalancerOptions = loadBalancerOptions;
 | 
					            _loadBalancerOptions = loadBalancerOptions;
 | 
				
			||||||
@@ -282,7 +289,8 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
                _addHeadersToDownstream,
 | 
					                _addHeadersToDownstream,
 | 
				
			||||||
                _addHeadersToUpstream,
 | 
					                _addHeadersToUpstream,
 | 
				
			||||||
                _dangerousAcceptAnyServerCertificateValidator,
 | 
					                _dangerousAcceptAnyServerCertificateValidator,
 | 
				
			||||||
                _securityOptions);
 | 
					                _securityOptions,
 | 
				
			||||||
 | 
					                _downstreamHttpMethod);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -138,6 +138,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
                .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
 | 
					                .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
 | 
				
			||||||
                .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
 | 
					                .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
 | 
				
			||||||
                .WithSecurityOptions(securityOptions)
 | 
					                .WithSecurityOptions(securityOptions)
 | 
				
			||||||
 | 
					                .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod)
 | 
				
			||||||
                .Build();
 | 
					                .Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return reRoute;
 | 
					            return reRoute;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,7 +38,8 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
            List<AddHeader> addHeadersToDownstream,
 | 
					            List<AddHeader> addHeadersToDownstream,
 | 
				
			||||||
            List<AddHeader> addHeadersToUpstream,
 | 
					            List<AddHeader> addHeadersToUpstream,
 | 
				
			||||||
            bool dangerousAcceptAnyServerCertificateValidator,
 | 
					            bool dangerousAcceptAnyServerCertificateValidator,
 | 
				
			||||||
            SecurityOptions securityOptions)
 | 
					            SecurityOptions securityOptions,
 | 
				
			||||||
 | 
					            string downstreamHttpMethod)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
 | 
					            DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
 | 
				
			||||||
            AddHeadersToDownstream = addHeadersToDownstream;
 | 
					            AddHeadersToDownstream = addHeadersToDownstream;
 | 
				
			||||||
@@ -72,6 +73,7 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
            LoadBalancerKey = loadBalancerKey;
 | 
					            LoadBalancerKey = loadBalancerKey;
 | 
				
			||||||
            AddHeadersToUpstream = addHeadersToUpstream;
 | 
					            AddHeadersToUpstream = addHeadersToUpstream;
 | 
				
			||||||
            SecurityOptions = securityOptions;
 | 
					            SecurityOptions = securityOptions;
 | 
				
			||||||
 | 
					            DownstreamHttpMethod = downstreamHttpMethod;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public string Key { get; }
 | 
					        public string Key { get; }
 | 
				
			||||||
@@ -106,5 +108,6 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
        public List<AddHeader> AddHeadersToUpstream { get; }
 | 
					        public List<AddHeader> AddHeadersToUpstream { get; }
 | 
				
			||||||
        public bool DangerousAcceptAnyServerCertificateValidator { get; }
 | 
					        public bool DangerousAcceptAnyServerCertificateValidator { get; }
 | 
				
			||||||
        public SecurityOptions SecurityOptions { get; }
 | 
					        public SecurityOptions SecurityOptions { get; }
 | 
				
			||||||
 | 
					        public string DownstreamHttpMethod { get; }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,7 @@ namespace Ocelot.Configuration.File
 | 
				
			|||||||
        public string DownstreamPathTemplate { get; set; }
 | 
					        public string DownstreamPathTemplate { get; set; }
 | 
				
			||||||
        public string UpstreamPathTemplate { get; set; }
 | 
					        public string UpstreamPathTemplate { get; set; }
 | 
				
			||||||
        public List<string> UpstreamHttpMethod { get; set; }
 | 
					        public List<string> UpstreamHttpMethod { get; set; }
 | 
				
			||||||
 | 
					        public string DownstreamHttpMethod { get; set; }
 | 
				
			||||||
        public Dictionary<string, string> AddHeadersToRequest { get; set; }
 | 
					        public Dictionary<string, string> AddHeadersToRequest { get; set; }
 | 
				
			||||||
        public Dictionary<string, string> UpstreamHeaderTransform { get; set; }
 | 
					        public Dictionary<string, string> UpstreamHeaderTransform { get; set; }
 | 
				
			||||||
        public Dictionary<string, string> DownstreamHeaderTransform { get; set; }
 | 
					        public Dictionary<string, string> DownstreamHeaderTransform { get; set; }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					using Ocelot.Logging;
 | 
				
			||||||
 | 
					using Ocelot.Middleware;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.DownstreamUrlCreator.Middleware
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class DownstreamHttpMethodCreatorMiddleware : OcelotMiddleware
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private readonly OcelotRequestDelegate _next;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public DownstreamHttpMethodCreatorMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory)
 | 
				
			||||||
 | 
					            : base(loggerFactory.CreateLogger<DownstreamHttpMethodCreatorMiddleware>())
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _next = next;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public async Task Invoke(DownstreamContext context)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (context.DownstreamReRoute.DownstreamHttpMethod != null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            await _next.Invoke(context);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public HttpRequestHeaders Headers { get; }
 | 
					        public HttpRequestHeaders Headers { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public string Method { get; }
 | 
					        public string Method { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public string OriginalString { get; }
 | 
					        public string OriginalString { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -52,6 +52,8 @@ namespace Ocelot.Request.Middleware
 | 
				
			|||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _request.RequestUri = uriBuilder.Uri;
 | 
					            _request.RequestUri = uriBuilder.Uri;
 | 
				
			||||||
 | 
					            _request.Method = new HttpMethod(Method);
 | 
				
			||||||
 | 
					            _request.Content = Content;
 | 
				
			||||||
            return _request;
 | 
					            return _request;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
using Microsoft.AspNetCore.Builder;
 | 
					using Microsoft.AspNetCore.Builder;
 | 
				
			||||||
 | 
					using Ocelot.DownstreamUrlCreator.Middleware;
 | 
				
			||||||
using Ocelot.Middleware.Pipeline;
 | 
					using Ocelot.Middleware.Pipeline;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Ocelot.Request.Middleware
 | 
					namespace Ocelot.Request.Middleware
 | 
				
			||||||
@@ -7,7 +8,8 @@ namespace Ocelot.Request.Middleware
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder)
 | 
					        public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>();
 | 
					            return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>()
 | 
				
			||||||
 | 
					                .UseMiddleware<DownstreamHttpMethodCreatorMiddleware>();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user