Added UpstreamHost placeholder to identify host from which request origination

This commit is contained in:
Samir Syed
2020-03-22 22:36:23 +05:30
parent 347ea7280c
commit f63afbf1b6
2 changed files with 36 additions and 1 deletions

View File

@ -7,6 +7,7 @@ namespace Ocelot.Infrastructure
using Ocelot.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
public class Placeholders : IPlaceholders
{
@ -25,7 +26,8 @@ namespace Ocelot.Infrastructure
{
{ "{BaseUrl}", GetBaseUrl() },
{ "{TraceId}", GetTraceId() },
{ "{RemoteIpAddress}", GetRemoteIpAddress() }
{ "{RemoteIpAddress}", GetRemoteIpAddress() },
{ "{UpstreamHost}", GetUpstreamHost() },
};
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
@ -130,5 +132,27 @@ namespace Ocelot.Infrastructure
{
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}"));
}
};
}
}
}