mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:48:14 +08:00
Feature/#623 (#632)
* #630 only set status code if response hasnt started, otherwise exception * #623 made {RemoteIpAddress} available as placeholder so you can do x-forwarded-for * #623 local address different on mac, windows and linux for integration test
This commit is contained in:
@ -1,22 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
namespace Ocelot.Headers
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Logging;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
public class AddHeadersToRequest : IAddHeadersToRequest
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly IPlaceholders _placeholders;
|
||||
private readonly IOcelotLogger _logger;
|
||||
|
||||
public AddHeadersToRequest(IClaimsParser claimsParser)
|
||||
public AddHeadersToRequest(IClaimsParser claimsParser, IPlaceholders placeholders, IOcelotLoggerFactory factory)
|
||||
{
|
||||
_logger = factory.CreateLogger<AddHeadersToRequest>();
|
||||
_claimsParser = claimsParser;
|
||||
_placeholders = placeholders;
|
||||
}
|
||||
|
||||
public Response SetHeadersOnDownstreamRequest(List<ClaimToThing> claimsToThings, IEnumerable<System.Security.Claims.Claim> claims, DownstreamRequest downstreamRequest)
|
||||
@ -46,6 +53,7 @@ namespace Ocelot.Headers
|
||||
public void SetHeadersOnDownstreamRequest(IEnumerable<AddHeader> headers, HttpContext context)
|
||||
{
|
||||
var requestHeader = context.Request.Headers;
|
||||
|
||||
foreach (var header in headers)
|
||||
{
|
||||
if (requestHeader.ContainsKey(header.Key))
|
||||
@ -53,7 +61,22 @@ namespace Ocelot.Headers
|
||||
requestHeader.Remove(header.Key);
|
||||
}
|
||||
|
||||
requestHeader.Add(header.Key, header.Value);
|
||||
if (header.Value.StartsWith("{") && header.Value.EndsWith("}"))
|
||||
{
|
||||
var value = _placeholders.Get(header.Value);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
_logger.LogWarning($"Unable to add header to response {header.Key}: {header.Value}");
|
||||
continue;
|
||||
}
|
||||
|
||||
requestHeader.Add(header.Key, new StringValues(value.Data));
|
||||
}
|
||||
else
|
||||
{
|
||||
requestHeader.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,13 @@
|
||||
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
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure;
|
||||
using Ocelot.Infrastructure.Extensions;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Responses;
|
||||
|
||||
public class HttpResponseHeaderReplacer : IHttpResponseHeaderReplacer
|
||||
{
|
||||
private readonly IPlaceholders _placeholders;
|
||||
@ -19,8 +17,11 @@ namespace Ocelot.Headers
|
||||
_placeholders = placeholders;
|
||||
}
|
||||
|
||||
public Response Replace(DownstreamResponse response, List<HeaderFindAndReplace> fAndRs, DownstreamRequest request)
|
||||
public Response Replace(DownstreamContext context, List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
var response = context.DownstreamResponse;
|
||||
var request = context.DownstreamRequest;
|
||||
|
||||
foreach (var f in fAndRs)
|
||||
{
|
||||
var dict = response.Headers.ToDictionary(x => x.Key);
|
||||
|
@ -1,14 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Ocelot.Request.Middleware;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Responses;
|
||||
|
||||
public interface IHttpResponseHeaderReplacer
|
||||
{
|
||||
Response Replace(DownstreamResponse response, List<HeaderFindAndReplace> fAndRs, DownstreamRequest httpRequestMessage);
|
||||
Response Replace(DownstreamContext context, List<HeaderFindAndReplace> fAndRs);
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ namespace Ocelot.Headers.Middleware
|
||||
|
||||
var postFAndRs = context.DownstreamReRoute.DownstreamHeadersFindAndReplace;
|
||||
|
||||
_postReplacer.Replace(context.DownstreamResponse, postFAndRs, context.DownstreamRequest);
|
||||
_postReplacer.Replace(context, postFAndRs);
|
||||
|
||||
_addHeadersToResponse.Add(context.DownstreamReRoute.AddHeadersToDownstream, context.DownstreamResponse);
|
||||
}
|
||||
|
@ -1,47 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Infrastructure
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
using Ocelot.Responses;
|
||||
|
||||
public class Placeholders : IPlaceholders
|
||||
{
|
||||
private readonly Dictionary<string, Func<Response<string>>> _placeholders;
|
||||
private readonly Dictionary<string, Func<DownstreamRequest, string>> _requestPlaceholders;
|
||||
private readonly IBaseUrlFinder _finder;
|
||||
private readonly IRequestScopedDataRepository _repo;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public Placeholders(IBaseUrlFinder finder, IRequestScopedDataRepository repo)
|
||||
public Placeholders(IBaseUrlFinder finder, IRequestScopedDataRepository repo, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_repo = repo;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_finder = finder;
|
||||
_placeholders = new Dictionary<string, Func<Response<string>>>();
|
||||
_placeholders.Add("{BaseUrl}", () => new OkResponse<string>(_finder.Find()));
|
||||
_placeholders.Add("{TraceId}", () => {
|
||||
var traceId = _repo.Get<string>("TraceId");
|
||||
if(traceId.IsError)
|
||||
{
|
||||
return new ErrorResponse<string>(traceId.Errors);
|
||||
}
|
||||
_placeholders = new Dictionary<string, Func<Response<string>>>
|
||||
{
|
||||
{ "{BaseUrl}", GetBaseUrl() },
|
||||
{ "{TraceId}", GetTraceId() },
|
||||
{ "{RemoteIpAddress}", GetRemoteIpAddress() }
|
||||
|
||||
return new OkResponse<string>(traceId.Data);
|
||||
});
|
||||
};
|
||||
|
||||
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>();
|
||||
_requestPlaceholders.Add("{DownstreamBaseUrl}", x => {
|
||||
var downstreamUrl = $"{x.Scheme}://{x.Host}";
|
||||
|
||||
if(x.Port != 80 && x.Port != 443)
|
||||
{
|
||||
downstreamUrl = $"{downstreamUrl}:{x.Port}";
|
||||
}
|
||||
|
||||
return $"{downstreamUrl}/";
|
||||
});
|
||||
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
|
||||
{
|
||||
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() }
|
||||
};
|
||||
}
|
||||
|
||||
public Response<string> Get(string key)
|
||||
@ -67,5 +58,56 @@ namespace Ocelot.Infrastructure
|
||||
|
||||
return new ErrorResponse<string>(new CouldNotFindPlaceholderError(key));
|
||||
}
|
||||
|
||||
private Func<Response<string>> GetRemoteIpAddress()
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
// this can blow up so adding try catch and return error
|
||||
try
|
||||
{
|
||||
var remoteIdAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
return new OkResponse<string>(remoteIdAddress);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{RemoteIpAddress}"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Func<DownstreamRequest, string> GetDownstreamBaseUrl()
|
||||
{
|
||||
return x =>
|
||||
{
|
||||
var downstreamUrl = $"{x.Scheme}://{x.Host}";
|
||||
|
||||
if (x.Port != 80 && x.Port != 443)
|
||||
{
|
||||
downstreamUrl = $"{downstreamUrl}:{x.Port}";
|
||||
}
|
||||
|
||||
return $"{downstreamUrl}/";
|
||||
};
|
||||
}
|
||||
|
||||
private Func<Response<string>> GetTraceId()
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
var traceId = _repo.Get<string>("TraceId");
|
||||
if (traceId.IsError)
|
||||
{
|
||||
return new ErrorResponse<string>(traceId.Errors);
|
||||
}
|
||||
|
||||
return new OkResponse<string>(traceId.Data);
|
||||
};
|
||||
}
|
||||
|
||||
private Func<Response<string>> GetBaseUrl()
|
||||
{
|
||||
return () => new OkResponse<string>(_finder.Find());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user