在ASP.Net Core中请求内容解压缩 [英] Request content decompression in ASP.Net Core

查看:353
本文介绍了在ASP.Net Core中请求内容解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我需要将较大的JSON请求有效载荷发布到我的ASP.Net Core Controllers中.有效载荷的大小保证了压缩(至少在我看来).由于ASP.Net Core Controllers似乎不支持开箱即用的压缩请求内容,因此我推出了自己的中间件.

执行此操作非常简单,以至于我不确定是否在这里丢失了某些内容.是因为有一种内置的方法来实现此目的,还是因为我从安全性或性能的角度犯了一些重大错误?

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);
    }
}

解决方案

我知道这是一篇很老的文章,但是如果万一它对某人有所帮助,那么这里有一个nuget包,可以在.net核心中执行请求解压缩

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

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);
    }
}

解决方案

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

这篇关于在ASP.Net Core中请求内容解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆