public static IOcelotPipelineBuilder MapWhen(this IOcelotPipelineBuilder app, Predicate predicate, Action <IOcelotPipelineBuilder> configuration) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var branchBuilder = app.New(); configuration(branchBuilder); var branch = branchBuilder.Build(); var options = new MapWhenOptions { Predicate = predicate, Branch = branch, }; return(app.Use(next => new MapWhenMiddleware(next, options).Invoke)); }
private static void UseIfNotNull(this IOcelotPipelineBuilder builder, Func <DownstreamContext, Func <Task>, Task> middleware) { if (middleware != null) { builder.Use(middleware); } }
public static IOcelotPipelineBuilder BuildCustomOcelotPipeline(this IOcelotPipelineBuilder builder, OcelotPipelineConfiguration pipelineConfiguration) { builder.UseExceptionHandlerMiddleware(); builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest, app => { app.UseDownstreamRouteFinderMiddleware(); app.UseDownstreamRequestInitialiser(); app.UseLoadBalancingMiddleware(); app.UseDownstreamUrlCreatorMiddleware(); app.UseWebSocketsProxyMiddleware(); }); builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware); builder.UseResponderMiddleware(); builder.UseDownstreamRouteFinderMiddleware(); builder.UseSecurityMiddleware(); if (pipelineConfiguration.MapWhenOcelotPipeline != null) { foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline) { builder.MapWhen(pipeline); } } builder.UseHttpHeadersTransformationMiddleware(); builder.UseDownstreamRequestInitialiser(); builder.UseRateLimiting(); builder.UseRequestIdMiddleware(); builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware); if (pipelineConfiguration.AuthenticationMiddleware == null) { builder.UseAuthenticationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthenticationMiddleware); } builder.UseClaimsToClaimsMiddleware(); builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware); if (pipelineConfiguration.AuthorisationMiddleware == null) { builder.UseAuthorisationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthorisationMiddleware); } builder.UseClaimsToHeadersMiddleware(); builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware); builder.UseClaimsToQueryStringMiddleware(); builder.UseLoadBalancingMiddleware(); builder.UseDownstreamUrlCreatorMiddleware(); builder.UseOutputCacheMiddleware(); builder.UseHttpRequesterMiddleware(); return(builder); }
public static IOcelotPipelineBuilder UseGrpcHttpMiddleware(this IOcelotPipelineBuilder builder) { ServiceLocator.Instance = builder.ApplicationServices; var plugin = ServiceLocator.GetService <GrpcPluginFactory>(); var proto = ServiceLocator.GetService <GrpcProtoFactory>(); plugin.InitAsync(); proto.InitAsync(); return(builder.UseMiddleware <OcelotGrpcHttpMiddleware>()); }
public static IOcelotPipelineBuilder UseMiddleware(this IOcelotPipelineBuilder app, Type middleware, params object[] args) { return(app.Use(next => { var methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public); var invokeMethods = methods.Where(m => string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal) || string.Equals(m.Name, InvokeAsyncMethodName, StringComparison.Ordinal) ).ToArray(); if (invokeMethods.Length > 1) { throw new InvalidOperationException(); } if (invokeMethods.Length == 0) { throw new InvalidOperationException(); } var methodinfo = invokeMethods[0]; if (!typeof(Task).IsAssignableFrom(methodinfo.ReturnType)) { throw new InvalidOperationException(); } var parameters = methodinfo.GetParameters(); if (parameters.Length == 0 || parameters[0].ParameterType != typeof(DownstreamContext)) { throw new InvalidOperationException(); } var ctorArgs = new object[args.Length + 1]; ctorArgs[0] = next; Array.Copy(args, 0, ctorArgs, 1, args.Length); var instance = ActivatorUtilities.CreateInstance(app.ApplicationServices, middleware, ctorArgs); if (parameters.Length == 1) { return (OcelotRequestDelegate)methodinfo.CreateDelegate(typeof(OcelotRequestDelegate), instance); } var factory = Compile <object>(methodinfo, parameters); return context => { var serviceProvider = context.HttpContext.RequestServices ?? app.ApplicationServices; if (serviceProvider == null) { throw new InvalidOperationException(); } return factory(instance, context, serviceProvider); }; })); }
public static IOcelotPipelineBuilder Use(this IOcelotPipelineBuilder app, Func <DownstreamContext, Func <Task>, Task> middleware) { return(app.Use(next => { return context => { Func <Task> simpleNext = () => next(context); return middleware(context, simpleNext); }; })); }
public static IOcelotPipelineBuilder MapWhen(this IOcelotPipelineBuilder app, Func <IOcelotPipelineBuilder, Predicate> pipelineBuilderFunc) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (pipelineBuilderFunc == null) { throw new ArgumentNullException(nameof(pipelineBuilderFunc)); } var branchBuilder = app.New(); var predicate = pipelineBuilderFunc.Invoke(app); var branch = branchBuilder.Build(); var options = new MapWhenOptions { Predicate = predicate, Branch = branch }; return(app.Use(next => new MapWhenMiddleware(next, options).Invoke)); }
private static Func <DownstreamContext, bool> AddRpcMiddleware(this IOcelotPipelineBuilder builder) { builder.UseHttpHeadersTransformationMiddleware(); // Initialises downstream request builder.UseDownstreamRequestInitialiser(); // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); // This adds or updates the request id (initally we try and set this based on global config in the error handling middleware) // If anything was set at global level and we have a different setting at re route level the global stuff will be overwritten // This means you can get a scenario where you have a different request id from the first piece of middleware to the request id middleware. builder.UseRequestIdMiddleware(); // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used builder.UseDownstreamUrlCreatorMiddleware(); // We fire off the request and set the response on the scoped data repo builder.UseRpcRequesterMiddleware(); // This check the downstream route scheme is tcp or custom scheme, for example @rpc return(context => context.DownstreamReRoute.DownstreamScheme.Equals("tcp", StringComparison.OrdinalIgnoreCase)); }
public static IOcelotPipelineBuilder UseQueryStringBuilderMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <QueryStringBuilderMiddleware>()); }
public static IOcelotPipelineBuilder UseCustomResponseMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <CustomOcelotMiddleware>()); }
public static IOcelotPipelineBuilder UseResponderMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <ResponderMiddleware>()); }
public static IOcelotPipelineBuilder UseHttpHeadersTransformationMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <HttpHeadersTransformationMiddleware>()); }
public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <DownstreamRequestInitialiserMiddleware>()); }
public static IOcelotPipelineBuilder UseExceptionHandlerMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <ExceptionHandlerMiddleware>()); }
public static IOcelotPipelineBuilder UseCzarClientRateLimitMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <CzarClientRateLimitMiddleware>()); }
public static IOcelotPipelineBuilder UseLoadBalancingMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <LoadBalancingMiddleware>()); }
public static IOcelotPipelineBuilder UseAhphAuthenticationMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <AhphAuthenticationMiddleware>()); }
public OcelotPipelineBuilder(IOcelotPipelineBuilder builder) { ApplicationServices = builder.ApplicationServices; _middlewares = new List <Func <OcelotRequestDelegate, OcelotRequestDelegate> >(); }
public static IOcelotPipelineBuilder UseGrpcHttpMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <GrpcHttpMiddleware>()); }
public static IOcelotPipelineBuilder UseRefreshTokenMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <TokenRefreshMiddleware>()); }
public static IOcelotPipelineBuilder UseWebSocketsProxyMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <WebSocketsProxyMiddleware>()); }
public static IOcelotPipelineBuilder UseMiddleware <TMiddleware>(this IOcelotPipelineBuilder app, params object[] args) { return(app.UseMiddleware(typeof(TMiddleware), args)); }
public static void UseRpcRequesterMiddleware(this IOcelotPipelineBuilder builder) { builder.UseMiddleware <RelayRequesterMiddleware>(); }
public static IOcelotPipelineBuilder UseRequestIdMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <ReRouteRequestIdMiddleware>()); }
public static OcelotRequestDelegate BuildOcelotPipeline(this IOcelotPipelineBuilder builder, OcelotPipelineConfiguration pipelineConfiguration) { // This is registered to catch any global exceptions that are not handled // It also sets the Request Id if anything is set globally builder.UseExceptionHandlerMiddleware(); // If the request is for websockets upgrade we fork into a different pipeline builder.MapWhen(context => context.HttpContext.WebSockets.IsWebSocketRequest, app => { app.UseDownstreamRouteFinderMiddleware(); app.UseDownstreamRequestInitialiser(); app.UseLoadBalancingMiddleware(); app.UseDownstreamUrlCreatorMiddleware(); app.UseWebSocketsProxyMiddleware(); }); // Allow the user to respond with absolutely anything they want. builder.UseIfNotNull(pipelineConfiguration.PreErrorResponderMiddleware); // This is registered first so it can catch any errors and issue an appropriate response builder.UseResponderMiddleware(); // Then we get the downstream route information builder.UseDownstreamRouteFinderMiddleware(); // This security module, IP whitelist blacklist, extended security mechanism builder.UseSecurityMiddleware(); //Expand other branch pipes if (pipelineConfiguration.MapWhenOcelotPipeline != null) { foreach (var pipeline in pipelineConfiguration.MapWhenOcelotPipeline) { builder.MapWhen(pipeline); } } // Now we have the ds route we can transform headers and stuff? builder.UseHttpHeadersTransformationMiddleware(); // Initialises downstream request builder.UseDownstreamRequestInitialiser(); // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); // This adds or updates the request id (initally we try and set this based on global config in the error handling middleware) // If anything was set at global level and we have a different setting at re route level the global stuff will be overwritten // This means you can get a scenario where you have a different request id from the first piece of middleware to the request id middleware. builder.UseRequestIdMiddleware(); // Allow pre authentication logic. The idea being people might want to run something custom before what is built in. builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware); // Now we know where the client is going to go we can authenticate them. // We allow the ocelot middleware to be overriden by whatever the // user wants if (pipelineConfiguration.AuthenticationMiddleware == null) { builder.UseAuthenticationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthenticationMiddleware); } // The next thing we do is look at any claims transforms in case this is important for authorisation builder.UseClaimsToClaimsMiddleware(); // Allow pre authorisation logic. The idea being people might want to run something custom before what is built in. builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware); // Now we have authenticated and done any claims transformation we // can authorise the request // We allow the ocelot middleware to be overriden by whatever the // user wants if (pipelineConfiguration.AuthorisationMiddleware == null) { builder.UseAuthorisationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthorisationMiddleware); } // Now we can run the claims to headers transformation middleware builder.UseClaimsToHeadersMiddleware(); // Allow the user to implement their own query string manipulation logic builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware); // Now we can run any claims to query string transformation middleware builder.UseClaimsToQueryStringMiddleware(); // Get the load balancer for this request builder.UseLoadBalancingMiddleware(); // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used builder.UseDownstreamUrlCreatorMiddleware(); // Not sure if this is the best place for this but we use the downstream url // as the basis for our cache key. builder.UseOutputCacheMiddleware(); //We fire off the request and set the response on the scoped data repo builder.UseHttpRequesterMiddleware(); return(builder.Build()); }
public static IOcelotPipelineBuilder UseDownstreamRouteFinderMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <DownstreamRouteFinderMiddleware>()); }
/// <summary> /// <see cref="OcelotPipelineExtensions"/> /// </summary> private static Func <DownstreamContext, bool> AddGrpcHttpGateway(this IOcelotPipelineBuilder builder, OcelotPipelineConfiguration pipelineConfiguration) { // Now we have the ds route we can transform headers and stuff? builder.UseHttpHeadersTransformationMiddleware(); // Initialises downstream request builder.UseDownstreamRequestInitialiser(); // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); // This adds or updates the request id (initally we try and set this based on global config in the error handling middleware) // If anything was set at global level and we have a different setting at re route level the global stuff will be overwritten // This means you can get a scenario where you have a different request id from the first piece of middleware to the request id middleware. builder.UseRequestIdMiddleware(); // Allow pre authentication logic. The idea being people might want to run something custom before what is built in. builder.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware); // Now we know where the client is going to go we can authenticate them. // We allow the ocelot middleware to be overriden by whatever the // user wants if (pipelineConfiguration.AuthenticationMiddleware == null) { builder.UseAuthenticationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthenticationMiddleware); } // The next thing we do is look at any claims transforms in case this is important for authorisation builder.UseClaimsBuilderMiddleware(); // Allow pre authorisation logic. The idea being people might want to run something custom before what is built in. builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware); // Now we have authenticated and done any claims transformation we // can authorise the request // We allow the ocelot middleware to be overriden by whatever the // user wants if (pipelineConfiguration.AuthorisationMiddleware == null) { builder.UseAuthorisationMiddleware(); } else { builder.Use(pipelineConfiguration.AuthorisationMiddleware); } // Now we can run any header transformation logic builder.UseHttpRequestHeadersBuilderMiddleware(); // Allow the user to implement their own query string manipulation logic builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware); // Now we can run any query string transformation logic builder.UseQueryStringBuilderMiddleware(); // Get the load balancer for this request builder.UseLoadBalancingMiddleware(); // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used builder.UseDownstreamUrlCreatorMiddleware(); // Not sure if this is the best place for this but we use the downstream url // as the basis for our cache key. builder.UseOutputCacheMiddleware(); builder.UseGrpcHttpMiddleware(); //builder.UseHttpRequesterMiddleware(); return((context) => { return context.DownstreamReRoute.DownstreamScheme.Equals("grpc", StringComparison.OrdinalIgnoreCase); }); }
public static IOcelotPipelineBuilder UseClaimsToClaimsMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <ClaimsToClaimsMiddleware>()); }
public static IOcelotPipelineBuilder UseHttpRequestHeadersBuilderMiddleware(this IOcelotPipelineBuilder builder) { return(builder.UseMiddleware <HttpRequestHeadersBuilderMiddleware>()); }
public static IOcelotPipelineBuilder UseMiddleware(this IOcelotPipelineBuilder app, Type middleware, params object[] args) { return(app.Use(next => { var methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public); var invokeMethods = methods.Where(m => string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal) || string.Equals(m.Name, InvokeAsyncMethodName, StringComparison.Ordinal) ).ToArray(); if (invokeMethods.Length > 1) { throw new InvalidOperationException(); } if (invokeMethods.Length == 0) { throw new InvalidOperationException(); } var methodinfo = invokeMethods[0]; if (!typeof(Task).IsAssignableFrom(methodinfo.ReturnType)) { throw new InvalidOperationException(); } var parameters = methodinfo.GetParameters(); if (parameters.Length == 0 || parameters[0].ParameterType != typeof(DownstreamContext)) { throw new InvalidOperationException(); } var ctorArgs = new object[args.Length + 1]; ctorArgs[0] = next; Array.Copy(args, 0, ctorArgs, 1, args.Length); var instance = ActivatorUtilities.CreateInstance(app.ApplicationServices, middleware, ctorArgs); if (parameters.Length == 1) { var ocelotDelegate = (OcelotRequestDelegate)methodinfo.CreateDelegate(typeof(OcelotRequestDelegate), instance); var diagnosticListener = (DiagnosticListener)app.ApplicationServices.GetService(typeof(DiagnosticListener)); var middlewareName = ocelotDelegate.Target.GetType().Name; OcelotRequestDelegate wrapped = async context => { try { Write(diagnosticListener, "Ocelot.MiddlewareStarted", middlewareName, context); await ocelotDelegate(context); } catch (Exception ex) { WriteException(diagnosticListener, ex, "Ocelot.MiddlewareException", middlewareName, context); throw ex; } finally { Write(diagnosticListener, "Ocelot.MiddlewareFinished", middlewareName, context); } }; return wrapped; } var factory = Compile <object>(methodinfo, parameters); return context => { var serviceProvider = context.HttpContext.RequestServices ?? app.ApplicationServices; if (serviceProvider == null) { throw new InvalidOperationException(); } return factory(instance, context, serviceProvider); }; })); }