Ocelot/src/Ocelot/Headers/HttpResponseHeaderReplacer.cs
Tom Pallister a15f75dda8
#298 initial hacking around better aggregation (#310)
* #298 initial hacking around better aggregation

* #298 bit more hacking around

* #298 abstraction over httpresponsemessage

* #298 tidying up

* #298 docs

* #298 missed this
2018-04-12 17:35:04 +01:00

56 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Ocelot.Configuration;
using Ocelot.Infrastructure;
using Ocelot.Infrastructure.Extensions;
using Ocelot.Middleware;
using Ocelot.Middleware.Multiplexer;
using Ocelot.Request.Middleware;
using Ocelot.Responses;
namespace Ocelot.Headers
{
public class HttpResponseHeaderReplacer : IHttpResponseHeaderReplacer
{
private readonly IPlaceholders _placeholders;
public HttpResponseHeaderReplacer(IPlaceholders placeholders)
{
_placeholders = placeholders;
}
public Response Replace(DownstreamResponse response, List<HeaderFindAndReplace> fAndRs, DownstreamRequest request)
{
foreach (var f in fAndRs)
{
var dict = response.Headers.ToDictionary(x => x.Key);
//if the response headers contain a matching find and replace
if(dict.TryGetValue(f.Key, out var values))
{
//check to see if it is a placeholder in the find...
var placeholderValue = _placeholders.Get(f.Find, request);
if(!placeholderValue.IsError)
{
//if it is we need to get the value of the placeholder
var replaced = values.Values.ToList()[f.Index].Replace(placeholderValue.Data, f.Replace.LastCharAsForwardSlash());
response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
response.Headers.Add(new Header(f.Key, new List<string> { replaced }));
}
else
{
var replaced = values.Values.ToList()[f.Index].Replace(f.Find, f.Replace);
response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
response.Headers.Add(new Header(f.Key, new List<string> { replaced }));
}
}
}
return new OkResponse();
}
}
}