mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 13:30:49 +08:00 
			
		
		
		
	regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment
This commit is contained in:
		@@ -2,25 +2,22 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Repository;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class DownstreamRouteFinderMiddleware
 | 
			
		||||
    public class DownstreamRouteFinderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IDownstreamRouteFinder _downstreamRouteFinder;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
        private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
 | 
			
		||||
 | 
			
		||||
        public DownstreamRouteFinderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IDownstreamRouteFinder downstreamRouteFinder, 
 | 
			
		||||
            IHttpResponder responder,
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository)
 | 
			
		||||
            :base(scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _downstreamRouteFinder = downstreamRouteFinder;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
            _scopedRequestDataRepository = scopedRequestDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -32,12 +29,12 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                await _responder.CreateNotFoundResponse(context);
 | 
			
		||||
                SetPipelineError(downstreamRoute.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("DownstreamRoute", downstreamRoute.Data);
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -7,22 +7,20 @@ using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class DownstreamUrlCreatorMiddleware
 | 
			
		||||
    public class DownstreamUrlCreatorMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
 | 
			
		||||
        private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
 | 
			
		||||
        public DownstreamUrlCreatorMiddleware(RequestDelegate next, 
 | 
			
		||||
            IDownstreamUrlTemplateVariableReplacer urlReplacer,
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository, 
 | 
			
		||||
            IHttpResponder responder)
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository)
 | 
			
		||||
            :base(scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _urlReplacer = urlReplacer;
 | 
			
		||||
            _scopedRequestDataRepository = scopedRequestDataRepository;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
@@ -31,13 +29,19 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                await _responder.CreateNotFoundResponse(context);
 | 
			
		||||
                SetPipelineError(downstreamRoute.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var downstreamUrl = _urlReplacer.ReplaceTemplateVariables(downstreamRoute.Data);
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("DownstreamUrl", downstreamUrl);
 | 
			
		||||
            if (downstreamUrl.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(downstreamUrl.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("DownstreamUrl", downstreamUrl.Data);
 | 
			
		||||
                
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,27 +1,22 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Repository;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Requester;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responder;
 | 
			
		||||
using Ocelot.Library.Infrastructure.RequestBuilder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    using Infrastructure.RequestBuilder;
 | 
			
		||||
 | 
			
		||||
    public class HttpRequestBuilderMiddleware
 | 
			
		||||
    public class HttpRequestBuilderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
        private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
 | 
			
		||||
        private readonly IRequestBuilder _requestBuilder;
 | 
			
		||||
 | 
			
		||||
        public HttpRequestBuilderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IHttpResponder responder,
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository, 
 | 
			
		||||
            IRequestBuilder requestBuilder)
 | 
			
		||||
            :base(scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
            _scopedRequestDataRepository = scopedRequestDataRepository;
 | 
			
		||||
            _requestBuilder = requestBuilder;
 | 
			
		||||
        }
 | 
			
		||||
@@ -32,7 +27,7 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
 | 
			
		||||
            if (downstreamUrl.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                await _responder.CreateNotFoundResponse(context);
 | 
			
		||||
                SetPipelineError(downstreamUrl.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -40,7 +35,13 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
              .Build(context.Request.Method, downstreamUrl.Data, context.Request.Body,
 | 
			
		||||
              context.Request.Headers, context.Request.Cookies, context.Request.QueryString.Value, context.Request.ContentType);
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("Request", request);
 | 
			
		||||
            if (request.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(request.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("Request", request.Data);
 | 
			
		||||
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,29 +1,24 @@
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Repository;
 | 
			
		||||
using Ocelot.Library.Infrastructure.RequestBuilder;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Requester;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    using Infrastructure.RequestBuilder;
 | 
			
		||||
 | 
			
		||||
    public class HttpRequesterMiddleware
 | 
			
		||||
    public class HttpRequesterMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IHttpRequester _requester;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
        private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
 | 
			
		||||
 | 
			
		||||
        public HttpRequesterMiddleware(RequestDelegate next, 
 | 
			
		||||
            IHttpRequester requester, 
 | 
			
		||||
            IHttpResponder responder,
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository)
 | 
			
		||||
            :base(scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _requester = requester;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
            _scopedRequestDataRepository = scopedRequestDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -33,16 +28,19 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
 | 
			
		||||
            if (request.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                await _responder.CreateNotFoundResponse(context);
 | 
			
		||||
                SetPipelineError(request.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var response = await _requester
 | 
			
		||||
                .GetResponse(request.Data);
 | 
			
		||||
            var response = await _requester.GetResponse(request.Data);
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("Response", response.Data);
 | 
			
		||||
            
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
            if (response.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(response.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _scopedRequestDataRepository.Add("Response", response.Data);            
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,10 +6,7 @@ using Ocelot.Library.Infrastructure.Responder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Terminating middleware that is responsible for returning a http response to the client
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class HttpResponderMiddleware
 | 
			
		||||
    public class HttpResponderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
@@ -18,6 +15,7 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
        public HttpResponderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IHttpResponder responder,
 | 
			
		||||
            IScopedRequestDataRepository scopedRequestDataRepository)
 | 
			
		||||
            :base(scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
@@ -26,9 +24,22 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var response = _scopedRequestDataRepository.Get<HttpResponseMessage>("Response");
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
 | 
			
		||||
            await _responder.CreateResponse(context, response.Data);
 | 
			
		||||
            if (PipelineError())
 | 
			
		||||
            {
 | 
			
		||||
                //todo obviously this needs to be better...prob look at response errors
 | 
			
		||||
                // and make a decision i guess
 | 
			
		||||
                var errors = GetPipelineErrors();
 | 
			
		||||
 | 
			
		||||
                await _responder.CreateNotFoundResponse(context);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                var response = _scopedRequestDataRepository.Get<HttpResponseMessage>("Response");
 | 
			
		||||
 | 
			
		||||
                await _responder.CreateResponse(context, response.Data);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								src/Ocelot.Library/Middleware/OcelotMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/Ocelot.Library/Middleware/OcelotMiddleware.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Repository;
 | 
			
		||||
using Ocelot.Library.Infrastructure.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
 | 
			
		||||
 | 
			
		||||
        public OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _scopedRequestDataRepository = scopedRequestDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void SetPipelineError(List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            _scopedRequestDataRepository.Add("OcelotMiddlewareError", true);
 | 
			
		||||
            _scopedRequestDataRepository.Add("OcelotMiddlewareErrors", errors);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool PipelineError()
 | 
			
		||||
        {
 | 
			
		||||
            var response = _scopedRequestDataRepository.Get<bool>("OcelotMiddlewareError");
 | 
			
		||||
            return response.Data;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<Error> GetPipelineErrors()
 | 
			
		||||
        {
 | 
			
		||||
            var response = _scopedRequestDataRepository.Get<List<Error>>("OcelotMiddlewareErrors");
 | 
			
		||||
            return response.Data;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user