Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request content decompression in ASP.Net Core

I sometimes need to post larger JSON request payloads to my ASP.Net Core Controllers. The size of the payload warrants (at least in my opinion) compressing it. Because ASP.Net Core Controllers do not appear to support compressed request content out of the box, I've rolled my own middleware.

Implementing this was so trivial that I'm not sure if I'm missing something here. Either because there's a built-in way to achieve this or because I made some major mistake from a security- or performance standpoint?

public class GzipRequestContentEncodingMiddleware
{
    public GzipRequestContentEncodingMiddleware(RequestDelegate next)
    {
        if (next == null)
            throw new ArgumentNullException(nameof(next));

        this.next = next;
    }

    private readonly RequestDelegate next;
    private const string ContentEncodingHeader = "Content-Encoding";
    private const string ContentEncodingGzip = "gzip";
    private const string ContentEncodingDeflate = "deflate";

    public async Task Invoke(HttpContext context)
    {
        if (context.Request.Headers.Keys.Contains(ContentEncodingHeader) &&
            (context.Request.Headers[ContentEncodingHeader] == ContentEncodingGzip || 
            context.Request.Headers[ContentEncodingHeader] == ContentEncodingDeflate))
        {
            var contentEncoding = context.Request.Headers[ContentEncodingHeader];
            context.Request.Headers.Remove(ContentEncodingHeader);

            var destination = new MemoryStream();

            using (var decompressor = contentEncoding == ContentEncodingGzip
                ? (Stream) new GZipStream(context.Request.Body, CompressionMode.Decompress, true)
                : (Stream) new DeflateStream(context.Request.Body, CompressionMode.Decompress, true))
            {
                await decompressor.CopyToAsync(destination);
            }

            destination.Seek(0, SeekOrigin.Begin);
            context.Request.Body = destination;
            context.Request.Headers["Content-Length"] = destination.Length.ToString(CultureInfo.InvariantCulture);
        }

        await next(context);
    }
}
like image 717
Oliver Weichhold Avatar asked Mar 14 '17 16:03

Oliver Weichhold


People also ask

What is request decompression middleware in ASP NET Core?

See also the full list of breaking changes in ASP.NET Core for .NET 7. The request decompression middleware is a new middleware that uses the Content-Encoding HTTP header to automatically identify and decompress requests with compressed content so that the developer of the server does not need to handle this themselves.

Where can I find response compression in ASP NET Core?

Response Compression middleware and services are provided OOB in ASPNETCORE. You can use the Microsoft.AspNetCore.ResponseCompression nuget package by following instructions on the ASPNETCORE official docs. Thanks for contributing an answer to Stack Overflow!

Do core controllers support compressed requests?

Because ASP.Net Core Controllers do not appear to support compressed request content out of the box, I've rolled my own middleware. Implementing this was so trivial that I'm not sure if I'm missing something here. Either because there's a built-in way to achieve this or because I made some major mistake from a security- or performance standpoint?

Can I compress HTTP Server responses?

HTTP.sys server server and Kestrel server don't currently offer built-in compression support. Usually, any response not natively compressed can benefit from response compression. Responses not natively compressed typically include: CSS, JavaScript, HTML, XML, and JSON. You shouldn't compress natively compressed assets, such as PNG files.


1 Answers

I know that this is a pretty old post, but just in case if it helps someone, here's a nuget package to perform Request Decompression in .net core

https://github.com/alexanderkozlenko/aspnetcore-request-decompression

like image 186
Naren Avatar answered Sep 29 '22 05:09

Naren