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); } }
public ApiProxyContext Get(HttpContext context) { var key = typeof(ApiProxyContext).FullName; var apiProxyRequestContext = context.Items[key] as ApiProxyContext; if (apiProxyRequestContext == null) { apiProxyRequestContext = new ApiProxyContext(); context.Items.Add(key, apiProxyRequestContext); } return(apiProxyRequestContext); }
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); }