use a stream rather than byte array in responder (#519)

This commit is contained in:
Tom Pallister 2018-07-31 19:21:12 +01:00 committed by GitHub
parent eb4b996c99
commit b854ca63ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 334 additions and 335 deletions

View File

@ -1,3 +1,6 @@
namespace Ocelot.Errors.Middleware
{
using Configuration;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,10 +10,6 @@ using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging; using Ocelot.Logging;
using Ocelot.Middleware; using Ocelot.Middleware;
namespace Ocelot.Errors.Middleware
{
using Configuration;
/// <summary> /// <summary>
/// Catches all unhandled exceptions thrown by middleware, logs and returns a 500 /// Catches all unhandled exceptions thrown by middleware, logs and returns a 500
/// </summary> /// </summary>

View File

@ -36,7 +36,7 @@ namespace Ocelot.Responder
AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value)); AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value));
} }
var content = await response.Content.ReadAsByteArrayAsync(); var content = await response.Content.ReadAsStreamAsync();
AddHeaderIfDoesntExist(context, new Header("Content-Length", new []{ content.Length.ToString() }) ); AddHeaderIfDoesntExist(context, new Header("Content-Length", new []{ content.Length.ToString() }) );
@ -49,11 +49,11 @@ namespace Ocelot.Responder
return Task.CompletedTask; return Task.CompletedTask;
}, context); }, context);
using (Stream stream = new MemoryStream(content)) using(content)
{ {
if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0) if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0)
{ {
await stream.CopyToAsync(context.Response.Body); await content.CopyToAsync(context.Response.Body);
} }
} }
} }