【问题标题】:How do enable a .Net web-API to accept g-ziped posts如何启用 .Net web-API 来接受 g 压缩的帖子
【发布时间】:2017-03-31 09:47:23
【问题描述】:

我有一个相当标准的 .net MVC 4 Web API 应用程序。

 public class LogsController : ApiController
{

    public HttpResponseMessage PostLog(List<LogDto> logs)
    {
        if (logs != null && logs.Any())
        {
            var goodLogs = new List<Log>();
            var badLogs = new List<LogBad>();

            foreach (var logDto in logs)
            {
                if (logDto.IsValid())
                {
                    goodLogs.Add(logDto.ToLog());
                }
                else
                {
                    badLogs.Add(logDto.ToLogBad());
                }
            }

            if (goodLogs.Any())
            {
                _logsRepo.Save(goodLogs);
            }

            if(badLogs.Any())
            {
                _logsBadRepo.Save(badLogs);
            }


        }
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
}

这一切都很好,我有能够向我发送他们的日志的设备并且运行良好。但是现在我们开始担心传输数据的大小,我们想看看是否接受使用 GZIP 压缩的帖子?

我该怎么做呢?是在 IIS 中设置还是我可以使用操作过滤器?

编辑 1

根据 Filip 的回答,我的想法是我需要在请求到达我的控制器之前拦截它的处理。如果我可以在 Web api 框架尝试将请求的主体解析为我的业务对象之前捕获请求,这会失败,因为请求的主体仍然被压缩。然后我可以解压请求的主体,然后将请求传回处理链,希望 Web Api 框架能够将(解压的)主体解析为我的业务对象。

看起来像使用 DelagatingHandler 是要走的路。它允许我在处理过程中访问请求,但在我的控制器之前。所以我尝试了以下?

 public class gZipHandler : DelegatingHandler
{

    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        string encodingType = request.Headers.AcceptEncoding.First().Value;

        request.Content = new DeCompressedContent(request.Content, encodingType);

        return base.SendAsync(request, cancellationToken);
    }
}

public class DeCompressedContent : HttpContent
{
    private HttpContent originalContent;
    private string encodingType;

    public DeCompressedContent(HttpContent content, string encodType)
    {
        originalContent = content;
        encodingType = encodType;
    }

    protected override bool TryComputeLength(out long length)
    {
        length = -1;

        return false;
    }


    protected override Task<Stream> CreateContentReadStreamAsync()
    {
        return base.CreateContentReadStreamAsync();
    }

    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        Stream compressedStream = null;

        if (encodingType == "gzip")
        {
            compressedStream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true);
        }

        return originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =>
        {
            if (compressedStream != null)
            {
                compressedStream.Dispose();
            }
        });
    }



}

}

这似乎工作正常。 SendAsync 方法在我的控制器之前被调用,并且 DecompressedContent 的构造函数被调用。但是 SerializeToStreamAsync 从未被调用,因此我添加了 CreateContentReadStreamAsync 以查看是否应该在此处进行解压缩,但这也没有被调用。

我觉得我已经接近解决方案了,但只需要一点点额外的东西就可以让它通过。

【问题讨论】:

  • 您是如何在客户端压缩 JSON 数据的?谢谢。

标签: c# asp.net-web-api


【解决方案1】:

我对 POST gzipped 数据到 .NET Web api 控制器有同样的要求。我想出了这个解决方案:

public class GZipToJsonHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                           CancellationToken cancellationToken)
    {
        // Handle only if content type is 'application/gzip'
        if (request.Content.Headers.ContentType == null ||
            request.Content.Headers.ContentType.MediaType != "application/gzip")
        {
            return base.SendAsync(request, cancellationToken);
        }

        // Read in the input stream, then decompress in to the outputstream.
        // Doing this asynronously, but not really required at this point
        // since we end up waiting on it right after this.
        Stream outputStream = new MemoryStream();
        Task task = request.Content.ReadAsStreamAsync().ContinueWith(t =>
            {
                Stream inputStream = t.Result;
                var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);

                gzipStream.CopyTo(outputStream);
                gzipStream.Dispose();

                outputStream.Seek(0, SeekOrigin.Begin);
            });

        // Wait for inputstream and decompression to complete. Would be nice
        // to not block here and work async when ready instead, but I couldn't 
        // figure out how to do it in context of a DelegatingHandler.
        task.Wait();

        // This next section is the key...

        // Save the original content
        HttpContent origContent = request.Content;

        // Replace request content with the newly decompressed stream
        request.Content = new StreamContent(outputStream);

        // Copy all headers from original content in to new one
        foreach (var header in origContent.Headers)
        {
            request.Content.Headers.Add(header.Key, header.Value);
        }

        // Replace the original content-type with content type
        // of decompressed data. In our case, we can assume application/json. A
        // more generic and reuseable handler would need some other 
        // way to differentiate the decompressed content type.
        request.Content.Headers.Remove("Content-Type");
        request.Content.Headers.Add("Content-Type", "application/json");

        return base.SendAsync(request, cancellationToken);
    }
}

使用这种方法,通常与 JSON 内容和自动模型绑定一起工作的现有控制器继续工作而无需任何更改。

我不确定为什么其他答案被接受。它提供了一种处理响应的解决方案(这是常见的),但不是请求(这是不常见的)。 Accept-Encoding 头用于指定可接受的响应编码,与请求编码无关。

【讨论】:

  • 是否建议在 outputStream 上调用 Dispose ?如果是,什么时候?谢谢
  • 因为 outputStream 是 MemoryStream,所以不需要显式地释放或关闭流。详情请看这个答案:stackoverflow.com/questions/4274590/…
  • 您应该使用 Content-Encoding 来决定是否需要解压缩,而不是自定义应用程序/gzip。 Content-Type 应该保持不变。
  • @Softlion 很好的建议。我很想修改答案以表明这一点,但我无法验证它是否仍能按预期工作。如果有人可以验证,请随时修改答案。确实,phasselbach 给出的答案比我原来的答案要好,而且它正确地使用了 Content-Encoding。
  • phasselbach 的答案是正确的。 Well DelegatingHandler 现在已被弃用,但它适用于 Owin。
【解决方案2】:

我相信正确的答案是 Kaliatech 的,如果我有足够的声望点,我会留下这个作为评论并投票给他,因为我认为他基本上是正确的。

但是,我的情况需要查看编码类型类型而不是内容类型。使用这种方法,调用系统仍然可以在内容类型中指定内容类型为 json/xml/etc,但指定使用 gzip 或可能的其他编码/压缩机制对数据进行编码。这使我无需在解码输入后更改内容类型,并允许任何内容类型信息以其原始状态流过。

这是代码。同样,这 99% 是 Kaliatech 的回答,包括 cmets,所以如果这有用,请投票给他的帖子。

public class CompressedRequestHandler : DelegatingHandler
{
    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        if (IsRequetCompressed(request))
        {
            request.Content = DecompressRequestContent(request);
        }

        return base.SendAsync(request, cancellationToken);
    }

    private bool IsRequetCompressed(HttpRequestMessage request)
    {
        if (request.Content.Headers.ContentEncoding != null &&
            request.Content.Headers.ContentEncoding.Contains("gzip"))
        {
            return true;
        }

        return false;
    }

    private HttpContent DecompressRequestContent(HttpRequestMessage request)
    {
        // Read in the input stream, then decompress in to the outputstream.
        // Doing this asynronously, but not really required at this point
        // since we end up waiting on it right after this.
        Stream outputStream = new MemoryStream();
        Task task = request.Content.ReadAsStreamAsync().ContinueWith(t =>
            {
                Stream inputStream = t.Result;
                var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);

                gzipStream.CopyTo(outputStream);
                gzipStream.Dispose();

                outputStream.Seek(0, SeekOrigin.Begin);
            });

        // Wait for inputstream and decompression to complete. Would be nice
        // to not block here and work async when ready instead, but I couldn't 
        // figure out how to do it in context of a DelegatingHandler.
        task.Wait();

        // Save the original content
        HttpContent origContent = request.Content;

        // Replace request content with the newly decompressed stream
        HttpContent newContent = new StreamContent(outputStream);

        // Copy all headers from original content in to new one
        foreach (var header in origContent.Headers)
        {
            newContent.Headers.Add(header.Key, header.Value);
        }

        return newContent;
    }

然后我在全球范围内注册了这个处理程序,如果您容易受到 DoS 攻击,这可能是一个冒险的提议,但我们的服务被锁定,所以它对我们有用

GlobalConfiguration.Configuration.MessageHandlers.Add(new CompressedRequestHandler());

【讨论】:

  • 如果您解释一下为什么这会暴露 DoS 攻击漏洞,我会非常感兴趣,是因为任何请求都会提高 CPU 使用率吗?
【解决方案3】:

虽然 Web API 不支持开箱即用的 Accept-Encoding 标头,但 Kiran 有一篇很棒的博客文章介绍了如何做到这一点 - http://blogs.msdn.com/b/kiranchalla/archive/2012/09/04/handling-compression-accept-encoding-sample.aspx - 使用自定义 MessageHandler

如果您实施他的解决方案,您只需发出带有Accept-Encoding: gzipAccept-Encoding: deflate 标头的请求,Web API 响应将在消息处理程序中为您压缩。

【讨论】:

  • 酷,谢谢。它很有帮助。我的情况有点不同。我(还)不关心压缩来自我的 web api 的响应,而是接受已经压缩的请求(发布)。我需要解压这些帖子的正文,然后处理其中的数据。请参阅有问题的编辑以了解我的后续步骤。谢谢
  • 与此同时,实际上有一个基于您提到的帖子和其他有关该主题的博文创建的开源库:github.com/azzlack/…
【解决方案4】:

试试这个

    public class DeCompressedContent : HttpContent
{
    private HttpContent originalContent;
    private string encodingType;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="content"></param>
    /// <param name="encodingType"></param>
    public DeCompressedContent(HttpContent content, string encodingType)
    {

        if (content == null) throw new ArgumentNullException("content");
        if (string.IsNullOrWhiteSpace(encodingType)) throw new ArgumentNullException("encodingType");

        this.originalContent = content;
        this.encodingType = encodingType.ToLowerInvariant();

        if (!this.encodingType.Equals("gzip", StringComparison.CurrentCultureIgnoreCase) && !this.encodingType.Equals("deflate", StringComparison.CurrentCultureIgnoreCase))
        {
            throw new InvalidOperationException(string.Format("Encoding {0} is not supported. Only supports gzip or deflate encoding", this.encodingType));
        }

        foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
        {
            this.Headers.TryAddWithoutValidation(header.Key, header.Value);
        }

        this.Headers.ContentEncoding.Add(this.encodingType);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="stream"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        var output = new MemoryStream();

        return this.originalContent
            .CopyToAsync(output).ContinueWith(task =>
            {
                // go to start
                output.Seek(0, SeekOrigin.Begin);

                if (this.encodingType.Equals("gzip", StringComparison.CurrentCultureIgnoreCase))
                {
                    using (var dec = new GZipStream(output, CompressionMode.Decompress))
                    {
                        dec.CopyTo(stream);
                    }
                }
                else
                {
                    using (var def = new DeflateStream(output, CompressionMode.Decompress))
                    {
                        def.CopyTo(stream);
                    }
                }

                if (output != null)
                    output.Dispose();
            });


    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="length"></param>
    /// <returns></returns>
    protected override bool TryComputeLength(out long length)
    {
        length = -1;

        return (false);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2023-02-19
    • 1970-01-01
    相关资源
    最近更新 更多