Esempio n. 1
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService,
                                 IEnumerable <IApiProxyResponseInterceptorProvider> apiProxyResponseInterceptorProviders,
                                 IEnumerable <IApiProxyErrorResponseInterceptorProvider> apiProxyErrorResponseInterceptorProviders,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            await _next.Invoke(context);

            var apiProxyContext = apiProxyContextService.Get(context);

            if (apiProxyContext.Response.ApiProxyResponse.StatusCode >= 400)
            {
                apiProxyErrorResponseInterceptorProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier)
                ?.Intercept(
                    apiProxyUriResolver.ResolveInboundUri(context),
                    apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                    apiProxyContext.Response.ApiProxyResponse);
            }
            else
            {
                apiProxyResponseInterceptorProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier)
                ?.Intercept(
                    apiProxyUriResolver.ResolveInboundUri(context),
                    apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                    apiProxyContext.Response.ApiProxyResponse);
            }
        }
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService,
                                 IEnumerable <IApiProxyRequestCachingProvider> apiProxyCachingProviders,
                                 IApiProxyResponseCachingService apiProxyResponseCachingService,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var provider = apiProxyCachingProviders.FirstOrDefault(x => x.Identifier == _apiProxy.Identifier);

            if (provider == null)
            {
                // No caching provider defined for Api proxy, call next middleware...
                await _next.Invoke(context);

                return;
            }

            var isCachable = provider.IsCachable(
                apiProxyUriResolver.ResolveInboundUri(context),
                apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy),
                context, out var key, out var options);

            var apiProxyContext = isCachable ? apiProxyContextService.Get(context) : null;

            if (isCachable)
            {
                // Try get Api proxy response from cache...
                var apiProxyResponse = apiProxyResponseCachingService.Get(key);
                if (apiProxyResponse != null)
                {
                    // Api proxy response found in cache...
                    apiProxyContext.Response.HasResponse      = true;
                    apiProxyContext.Response.FetchedFromCache = true;
                    apiProxyContext.Response.ApiProxyResponse = apiProxyResponse;
                    return;
                }
            }

            // Not found in cache or not cachable, call next middleware......
            await _next.Invoke(context);

            if (isCachable)
            {
                if (apiProxyContext.Response.HasResponse &&
                    apiProxyContext.Response.ApiProxyResponse.StatusCode == 200)
                {
                    // Add Api proxy response to cache if status code is 200...
                    apiProxyResponseCachingService.Set(key,
                                                       apiProxyContext.Response.ApiProxyResponse,
                                                       options);
                }
            }
        }
Esempio n. 3
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService, IApiProxyConfiguration apiProxyConfiguration,
                                 IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var apiProxyContext = apiProxyContextService.Get(context);

            apiProxyContext.ExecutionStart = DateTime.Now;

            await _next.Invoke(context);

            apiProxyContext.ExecutionEnd = DateTime.Now;

            if (apiProxyContext.Response.HasResponse)
            {
                await CopyApiProxyResponseToHttpResponse(context, apiProxyContext, apiProxyUriResolver, apiProxyConfiguration.DebugMode);
            }
        }
Esempio n. 4
0
 private void AddDebugInfoToResponse(HttpContext context, HttpResponse response, ApiProxyContext apiProxyContext, IApiProxyUriResolver apiProxyUriResolver)
 {
     response.Headers["api-proxy-outbound-url"]              = apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy).ToString();
     response.Headers["api-proxy-fetched-from-cache"]        = apiProxyContext.Response.FetchedFromCache.ToString().ToLower();
     response.Headers["api-proxy-middleware-execution-time"] = apiProxyContext.ExecutionEnd
                                                               .Subtract(apiProxyContext.ExecutionStart).TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
 }
Esempio n. 5
0
        private async Task CopyApiProxyResponseToHttpResponse(HttpContext context, ApiProxyContext apiProxyContext, IApiProxyUriResolver apiProxyUriResolver, bool debugMode)
        {
            var response = context.Response;

            var apiProxyResponse = apiProxyContext.Response.ApiProxyResponse;

            response.StatusCode = apiProxyResponse.StatusCode;

            foreach (var header in apiProxyResponse.Headers)
            {
                response.Headers[header.Key] = header.Value;
            }

            if (debugMode)
            {
                AddDebugInfoToResponse(context, response, apiProxyContext, apiProxyUriResolver);
            }

            // SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response.
            response.Headers.Remove("transfer-encoding");

            using (var stream = new MemoryStream(apiProxyResponse.Body))
            {
                stream.Position = 0;
                await stream.CopyToAsync(context.Response.Body, 81920, context.RequestAborted);
            }
        }
Esempio n. 6
0
        public async Task Invoke(HttpContext context, IApiProxyContextService apiProxyContextService, IProxyRequestService proxyRequestService, IApiProxyUriResolver apiProxyUriResolver)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var apiProxyContext = apiProxyContextService.Get(context);

            var outboundUri = apiProxyUriResolver.ResolveOutboundUri(context, _apiProxy);

            apiProxyContext.Response.ApiProxyResponse = await proxyRequestService.ProxyRequest(context, outboundUri);

            apiProxyContext.Response.HasResponse = true;
        }