Skip to content

Commit c004c43

Browse files
committed
Merge branch 'samirsyed-master'
2 parents 59af924 + b7cfac9 commit c004c43

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

docs/features/headerstransformation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Ocelot allows placeholders that can be used in header transformation.
8585
{BaseUrl} - This will use Ocelot's base url e.g. http://localhost:5000 as its value.
8686
{DownstreamBaseUrl} - This will use the downstream services base url e.g. http://localhost:5000 as its value. This only works for DownstreamHeaderTransform at the moment.
8787
{TraceId} - This will use the Butterfly APM Trace Id. This only works for DownstreamHeaderTransform at the moment.
88+
{UpstreamHost} - This will look for the incoming Host header.
8889

8990
Handling 302 Redirects
9091
^^^^^^^^^^^^^^^^^^^^^^

src/Ocelot/Infrastructure/Placeholders.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Ocelot.Infrastructure
77
using Ocelot.Responses;
88
using System;
99
using System.Collections.Generic;
10+
using System.Linq;
1011

1112
public class Placeholders : IPlaceholders
1213
{
@@ -25,12 +26,13 @@ public Placeholders(IBaseUrlFinder finder, IRequestScopedDataRepository repo, IH
2526
{
2627
{ "{BaseUrl}", GetBaseUrl() },
2728
{ "{TraceId}", GetTraceId() },
28-
{ "{RemoteIpAddress}", GetRemoteIpAddress() }
29+
{ "{RemoteIpAddress}", GetRemoteIpAddress() },
30+
{ "{UpstreamHost}", GetUpstreamHost() },
2931
};
3032

3133
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
3234
{
33-
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() }
35+
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() },
3436
};
3537
}
3638

@@ -130,5 +132,25 @@ private Func<Response<string>> GetBaseUrl()
130132
{
131133
return () => new OkResponse<string>(_finder.Find());
132134
}
135+
136+
private Func<Response<string>> GetUpstreamHost()
137+
{
138+
return () =>
139+
{
140+
try
141+
{
142+
if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue("Host", out var upstreamHost))
143+
{
144+
return new OkResponse<string>(upstreamHost.First());
145+
}
146+
147+
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
148+
}
149+
catch
150+
{
151+
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
152+
}
153+
};
154+
}
133155
}
134156
}

test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,33 @@ public void should_return_error_when_removed()
123123
result.IsError.ShouldBeTrue();
124124
result.Errors[0].Message.ShouldBe("Unable to remove placeholder: {Test}, placeholder does not exists");
125125
}
126+
127+
[Fact]
128+
public void should_return_upstreamHost()
129+
{
130+
var upstreamHost = "UpstreamHostA";
131+
var httpContext = new DefaultHttpContext();
132+
httpContext.Request.Headers.Add("Host", upstreamHost);
133+
_accessor.Setup(x => x.HttpContext).Returns(httpContext);
134+
var result = _placeholders.Get("{UpstreamHost}");
135+
result.Data.ShouldBe(upstreamHost);
136+
}
137+
138+
[Fact]
139+
public void should_return_error_when_finding_upstbecause_Host_not_set()
140+
{
141+
var httpContext = new DefaultHttpContext();
142+
_accessor.Setup(x => x.HttpContext).Returns(httpContext);
143+
var result = _placeholders.Get("{UpstreamHost}");
144+
result.IsError.ShouldBeTrue();
145+
}
146+
147+
[Fact]
148+
public void should_return_error_when_finding_upstream_host_because_exception_thrown()
149+
{
150+
_accessor.Setup(x => x.HttpContext).Throws(new Exception());
151+
var result = _placeholders.Get("{UpstreamHost}");
152+
result.IsError.ShouldBeTrue();
153+
}
126154
}
127155
}

0 commit comments

Comments
 (0)