mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 19:58:14 +08:00
Feature/fix admin api caching wrong re routes (#421)
* #383 added failing test for this issue * #383 identified issue was with cached load balancer for a given upstream path template based on the key we use, have modified this to include more data, I guess this might be an issue again for other things so I will have a think about it * #383 fixed failing tests after key change * Seems to be an issue with coveralls new package not being on nuget...try same version as their nuget package * bash the old manual tests json back in
This commit is contained in:
@ -252,7 +252,7 @@ namespace Ocelot.Configuration.Creator
|
||||
return $"{nameof(CookieStickySessions)}:{fileReRoute.LoadBalancerOptions.Key}";
|
||||
}
|
||||
|
||||
return $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}";
|
||||
return $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}|{string.Join(",", fileReRoute.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Configuration.Setter
|
||||
{
|
||||
public interface IFileConfigurationSetter
|
||||
{
|
||||
Task<Response> Set(FileConfiguration config);
|
||||
}
|
||||
}
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Configuration.Setter
|
||||
{
|
||||
public interface IFileConfigurationSetter
|
||||
{
|
||||
Task<Response> Set(FileConfiguration config);
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,55 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +1,48 @@
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public class HttpHeadersTransformationMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IHttpContextRequestHeaderReplacer _preReplacer;
|
||||
private readonly IHttpResponseHeaderReplacer _postReplacer;
|
||||
private readonly IAddHeadersToResponse _addHeadersToResponse;
|
||||
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
||||
|
||||
public HttpHeadersTransformationMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IHttpContextRequestHeaderReplacer preReplacer,
|
||||
IHttpResponseHeaderReplacer postReplacer,
|
||||
IAddHeadersToResponse addHeadersToResponse,
|
||||
IAddHeadersToRequest addHeadersToRequest)
|
||||
:base(loggerFactory.CreateLogger<HttpHeadersTransformationMiddleware>())
|
||||
{
|
||||
_addHeadersToResponse = addHeadersToResponse;
|
||||
_addHeadersToRequest = addHeadersToRequest;
|
||||
_next = next;
|
||||
_postReplacer = postReplacer;
|
||||
_preReplacer = preReplacer;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
var preFAndRs = context.DownstreamReRoute.UpstreamHeadersFindAndReplace;
|
||||
|
||||
//todo - this should be on httprequestmessage not httpcontext?
|
||||
_preReplacer.Replace(context.HttpContext, preFAndRs);
|
||||
|
||||
_addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.AddHeadersToUpstream, context.HttpContext);
|
||||
|
||||
await _next.Invoke(context);
|
||||
|
||||
var postFAndRs = context.DownstreamReRoute.DownstreamHeadersFindAndReplace;
|
||||
|
||||
_postReplacer.Replace(context.DownstreamResponse, postFAndRs, context.DownstreamRequest);
|
||||
|
||||
_addHeadersToResponse.Add(context.DownstreamReRoute.AddHeadersToDownstream, context.DownstreamResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public class HttpHeadersTransformationMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IHttpContextRequestHeaderReplacer _preReplacer;
|
||||
private readonly IHttpResponseHeaderReplacer _postReplacer;
|
||||
private readonly IAddHeadersToResponse _addHeadersToResponse;
|
||||
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
||||
|
||||
public HttpHeadersTransformationMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IHttpContextRequestHeaderReplacer preReplacer,
|
||||
IHttpResponseHeaderReplacer postReplacer,
|
||||
IAddHeadersToResponse addHeadersToResponse,
|
||||
IAddHeadersToRequest addHeadersToRequest)
|
||||
:base(loggerFactory.CreateLogger<HttpHeadersTransformationMiddleware>())
|
||||
{
|
||||
_addHeadersToResponse = addHeadersToResponse;
|
||||
_addHeadersToRequest = addHeadersToRequest;
|
||||
_next = next;
|
||||
_postReplacer = postReplacer;
|
||||
_preReplacer = preReplacer;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
var preFAndRs = context.DownstreamReRoute.UpstreamHeadersFindAndReplace;
|
||||
|
||||
//todo - this should be on httprequestmessage not httpcontext?
|
||||
_preReplacer.Replace(context.HttpContext, preFAndRs);
|
||||
|
||||
_addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.AddHeadersToUpstream, context.HttpContext);
|
||||
|
||||
await _next.Invoke(context);
|
||||
|
||||
var postFAndRs = context.DownstreamReRoute.DownstreamHeadersFindAndReplace;
|
||||
|
||||
_postReplacer.Replace(context.DownstreamResponse, postFAndRs, context.DownstreamRequest);
|
||||
|
||||
_addHeadersToResponse.Add(context.DownstreamReRoute.AddHeadersToDownstream, context.DownstreamResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user