PowerShell/test/tools/WebListener/DeflateFilter.cs
Mark Kraus 15a6c5748b Replace httpbin.org/gzip Tests with WebListener and Re-Enable Deflate Tests (#4948)
* Add Gzip and Deflate Support to WebListener

* [Feature] Run Feature tests

* [Feature] Address PR Feedback

* [Feature] Re-Run CI

* [feature] Update WebListener Index page

* [Feature] Run Feature tests

* [Feature] Re-run CI
2017-10-02 13:46:53 -07:00

31 lines
1 KiB
C#

using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
namespace mvc.Controllers
{
internal sealed class DeflateFilter : ResultFilterAttribute
{
public override async Task OnResultExecutionAsync( ResultExecutingContext context, ResultExecutionDelegate next)
{
var httpContext = context.HttpContext;
using (var memoryStream = new MemoryStream())
{
var responseStream = httpContext.Response.Body;
httpContext.Response.Body = memoryStream;
await next();
using (var compressedStream = new DeflateStream(responseStream, CompressionLevel.Fastest))
{
httpContext.Response.Headers.Add("Content-Encoding", new [] { "deflate" });
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(compressedStream);
}
}
}
}
}