15

我有一个相当标准的 .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

继菲利普的回答之后,我的想法是我需要在请求到达我的控制器之前拦截它的处理。如果我可以在 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 以查看是否应该在此处进行解压缩,但这也没有被调用。

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

4

4 回答 4

23

我对 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 标头用于指定可接受的响应编码,与请求编码无关。

于 2013-02-20T21:59:33.453 回答
21

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

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

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

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());
于 2013-05-23T15:58:03.303 回答
6

虽然 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 响应将在消息处理程序中为您压缩。

于 2012-09-10T21:38:26.723 回答
0

试试这个

    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);
    }
}
于 2013-07-08T16:09:02.643 回答