public async Task SetResponseOnHttpContext(HttpContext context, HttpResponseMessage response) { _removeOutputHeaders.Remove(response.Headers); foreach (var httpResponseHeader in response.Headers) { AddHeaderIfDoesntExist(context, httpResponseHeader); } foreach (var httpResponseHeader in response.Content.Headers) { AddHeaderIfDoesntExist(context, httpResponseHeader); } var content = await response.Content.ReadAsByteArrayAsync(); AddHeaderIfDoesntExist(context, new KeyValuePair <string, IEnumerable <string> >("Content-Length", new [] { content.Length.ToString() })); context.Response.OnStarting(state => { var httpContext = (HttpContext)state; httpContext.Response.StatusCode = (int)response.StatusCode; return(Task.CompletedTask); }, context); using (Stream stream = new MemoryStream(content)) { await stream.CopyToAsync(context.Response.Body); } }
public async Task SetResponseOnHttpContext(HttpContext context, DownstreamResponse response) { _removeOutputHeaders.Remove(response.Headers); foreach (var httpResponseHeader in response.Headers) { AddHeaderIfDoesntExist(context, httpResponseHeader); } foreach (var httpResponseHeader in response.Content.Headers) { AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value)); } var content = await response.Content.ReadAsStreamAsync(); if (response.Content.Headers.ContentLength != null) { AddHeaderIfDoesntExist(context, new Header("Content-Length", new [] { response.Content.Headers.ContentLength.ToString() })); } context.Response.StatusCode = (int)response.StatusCode; context.Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = response.ReasonPhrase; using (content) { if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0) { await content.CopyToAsync(context.Response.Body); } } }
public async Task SetResponseOnHttpContext(HttpContext context, DownstreamResponse response) { _removeOutputHeaders.Remove(response.Headers); foreach (var httpResponseHeader in response.Headers) { AddHeaderIfDoesntExist(context, httpResponseHeader); } foreach (var httpResponseHeader in response.Content.Headers) { AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value)); } var content = await response.Content.ReadAsByteArrayAsync(); AddHeaderIfDoesntExist(context, new Header("Content-Length", new [] { content.Length.ToString() })); context.Response.OnStarting(state => { var httpContext = (HttpContext)state; httpContext.Response.StatusCode = (int)response.StatusCode; return(Task.CompletedTask); }, context); using (Stream stream = new MemoryStream(content)) { if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0) { await stream.CopyToAsync(context.Response.Body); } } }