mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:55:28 +08:00 
			
		
		
		
	Added UpstreamHost placeholder to identify host from which request origination
This commit is contained in:
		@@ -7,6 +7,7 @@ namespace Ocelot.Infrastructure
 | 
				
			|||||||
    using Ocelot.Responses;
 | 
					    using Ocelot.Responses;
 | 
				
			||||||
    using System;
 | 
					    using System;
 | 
				
			||||||
    using System.Collections.Generic;
 | 
					    using System.Collections.Generic;
 | 
				
			||||||
 | 
					    using System.Linq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public class Placeholders : IPlaceholders
 | 
					    public class Placeholders : IPlaceholders
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -25,7 +26,8 @@ namespace Ocelot.Infrastructure
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                { "{BaseUrl}", GetBaseUrl() },
 | 
					                { "{BaseUrl}", GetBaseUrl() },
 | 
				
			||||||
                { "{TraceId}", GetTraceId() },
 | 
					                { "{TraceId}", GetTraceId() },
 | 
				
			||||||
                { "{RemoteIpAddress}", GetRemoteIpAddress() }
 | 
					                { "{RemoteIpAddress}", GetRemoteIpAddress() },
 | 
				
			||||||
 | 
					                { "{UpstreamHost}", GetUpstreamHost() },
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
 | 
					            _requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
 | 
				
			||||||
@@ -130,5 +132,27 @@ namespace Ocelot.Infrastructure
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            return () => new OkResponse<string>(_finder.Find());
 | 
					            return () => new OkResponse<string>(_finder.Find());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private Func<Response<string>> GetUpstreamHost()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return () =>
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                try
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue("Host", out var upstreamHost))
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        return new OkResponse<string>(upstreamHost.First());
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                catch
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -123,5 +123,16 @@ namespace Ocelot.UnitTests.Infrastructure
 | 
				
			|||||||
            result.IsError.ShouldBeTrue();
 | 
					            result.IsError.ShouldBeTrue();
 | 
				
			||||||
            result.Errors[0].Message.ShouldBe("Unable to remove placeholder: {Test}, placeholder does not exists");
 | 
					            result.Errors[0].Message.ShouldBe("Unable to remove placeholder: {Test}, placeholder does not exists");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_return_upstreamHost()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var upstreamHost = "UpstreamHostA";
 | 
				
			||||||
 | 
					            var httpContext = new DefaultHttpContext();
 | 
				
			||||||
 | 
					            httpContext.Request.Headers.Add("Host", upstreamHost);
 | 
				
			||||||
 | 
					            _accessor.Setup(x => x.HttpContext).Returns(httpContext);
 | 
				
			||||||
 | 
					            var result = _placeholders.Get("{UpstreamHost}");
 | 
				
			||||||
 | 
					            result.Data.ShouldBe(upstreamHost);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user