You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.7 KiB
C#

using Ocelot.Authentication.Middleware;
using Ocelot.Authorization.Middleware;
using Ocelot.Cache.Middleware;
using Ocelot.Claims.Middleware;
using Ocelot.DownstreamPathManipulation.Middleware;
using Ocelot.DownstreamRouteFinder.Middleware;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.DownstreamUrlCreator.Middleware;
using Ocelot.Errors.Middleware;
using Ocelot.Headers.Middleware;
using Ocelot.LoadBalancer.Middleware;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Multiplexer;
using Ocelot.QueryStrings.Middleware;
using Ocelot.RateLimit.Middleware;
using Ocelot.Request.Middleware;
using Ocelot.Requester.Middleware;
using Ocelot.RequestId.Middleware;
using Ocelot.Responder.Middleware;
using Ocelot.Security.Middleware;
using Ocelot.WebSockets.Middleware;
namespace GatewayAPI
{
public static class OcelotPipelineExtensions
{
public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, OcelotPipelineConfiguration pipelineConfiguration)
{
app.UseDownstreamContextMiddleware();
app.UseExceptionHandlerMiddleware();
app.MapWhen((HttpContext httpContext) => httpContext.WebSockets.IsWebSocketRequest, delegate (IApplicationBuilder wenSocketsApp)
{
wenSocketsApp.UseDownstreamRouteFinderMiddleware();
wenSocketsApp.UseMultiplexingMiddleware();
wenSocketsApp.UseDownstreamRequestInitialiser();
wenSocketsApp.UseLoadBalancingMiddleware();
wenSocketsApp.UseMiddleware<HostReplaceMiddleware>();
wenSocketsApp.UseDownstreamUrlCreatorMiddleware();
wenSocketsApp.UseWebSocketsProxyMiddleware();
});
app.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware);
app.UseResponderMiddleware();
app.UseDownstreamRouteFinderMiddleware();
app.UseMultiplexingMiddleware();
app.UseSecurityMiddleware();
if (pipelineConfiguration.MapWhenOcelotPipeline != null)
{
foreach (KeyValuePair<Func<HttpContext, bool>, Action<IApplicationBuilder>> item in pipelineConfiguration.MapWhenOcelotPipeline)
{
app.MapWhen(item.Key, item.Value);
}
}
app.UseHttpHeadersTransformationMiddleware();
app.UseDownstreamRequestInitialiser();
app.UseRateLimiting();
app.UseRequestIdMiddleware();
app.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware);
if (pipelineConfiguration.AuthenticationMiddleware == null)
{
app.UseAuthenticationMiddleware();
}
else
{
app.Use(pipelineConfiguration.AuthenticationMiddleware);
}
app.UseClaimsToClaimsMiddleware();
app.UseIfNotNull(pipelineConfiguration.PreAuthorizationMiddleware);
if (pipelineConfiguration.AuthorizationMiddleware == null)
{
app.UseAuthorizationMiddleware();
}
else
{
app.Use(pipelineConfiguration.AuthorizationMiddleware);
}
app.UseClaimsToHeadersMiddleware();
app.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
app.UseClaimsToQueryStringMiddleware();
app.UseClaimsToDownstreamPathMiddleware();
app.UseLoadBalancingMiddleware();
app.UseMiddleware<HostReplaceMiddleware>();
app.UseDownstreamUrlCreatorMiddleware();
app.UseOutputCacheMiddleware();
app.UseHttpRequesterMiddleware();
return app.Build();
}
private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
{
if (middleware != null)
{
builder.Use(middleware);
}
}
}
public class HostReplaceMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
public HostReplaceMiddleware(RequestDelegate next, IOcelotLoggerFactory loggerFactory)
: base(loggerFactory.CreateLogger<HostReplaceMiddleware>())
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
DownstreamRequest downstreamRequest = httpContext.Items.DownstreamRequest();
List<PlaceholderNameAndValue> list = httpContext.Items.TemplatePlaceholderNameAndValues();
list.ForEach((v) => {
downstreamRequest.Host = downstreamRequest.Host.Replace(v.Name, v.Value);
});
await _next(httpContext);
}
}
}