mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 10:15:27 +08:00 
			
		
		
		
	use a stream rather than byte array in responder (#519)
This commit is contained in:
		@@ -1,15 +1,14 @@
 | 
				
			|||||||
using System;
 | 
					 | 
				
			||||||
using System.Linq;
 | 
					 | 
				
			||||||
using System.Threading.Tasks;
 | 
					 | 
				
			||||||
using Ocelot.Configuration.Repository;
 | 
					 | 
				
			||||||
using Ocelot.Infrastructure.Extensions;
 | 
					 | 
				
			||||||
using Ocelot.Infrastructure.RequestData;
 | 
					 | 
				
			||||||
using Ocelot.Logging;
 | 
					 | 
				
			||||||
using Ocelot.Middleware;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace Ocelot.Errors.Middleware
 | 
					namespace Ocelot.Errors.Middleware
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    using Configuration;
 | 
					    using Configuration;
 | 
				
			||||||
 | 
					    using System;
 | 
				
			||||||
 | 
					    using System.Linq;
 | 
				
			||||||
 | 
					    using System.Threading.Tasks;
 | 
				
			||||||
 | 
					    using Ocelot.Configuration.Repository;
 | 
				
			||||||
 | 
					    using Ocelot.Infrastructure.Extensions;
 | 
				
			||||||
 | 
					    using Ocelot.Infrastructure.RequestData;
 | 
				
			||||||
 | 
					    using Ocelot.Logging;
 | 
				
			||||||
 | 
					    using Ocelot.Middleware;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    /// <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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user