【问题标题】:Detect GZIP compression in HTTP Module检测 HTTP 模块中的 GZIP 压缩
【发布时间】:2014-10-22 20:24:13
【问题描述】:

如何检测是否为我的 HTTP 模块中的特定请求启用了 GZIP?我对输出应用了一个过滤器,当它作用于 gzip 压缩的内容时,它会以某种方式破坏压缩,并且客户端浏览器会抛出一个无法解码内容的错误。

    public void Init(HttpApplication context)
    {
        // if(HttpContext.Current.IsCompressed) // Check for compressed content here            

        // Set up the filter / replacement.
        context.PostReleaseRequestState += (_, __) =>
        {
            var filterStream = new ResponseFilterStream(HttpContext.Current.Response.Filter);
            filterStream.TransformString += FilterPage;
            HttpContext.Current.Response.Filter = filterStream;
        };
    }

ResponseFilterStream 是一个自定义流,它缓存所有流写入并将内容呈现为事件,以便允许方法修改流的内容。它非常适合修改 HTML 请求(这是我想要的),但我不希望它作用于 gzip 压缩的响应。如何检测 gzipped 响应并防止过滤器流与响应挂钩?

【问题讨论】:

  • 类似if (response.Filter is System.IO.Compression.GZipStream)?

标签: c# gzip httpmodule


【解决方案1】:

对于响应,您可以检查 Encoding http 标头中的 gzipdeflate

对于请求,您需要检查Accept-Encoding http 标头中的gzipdeflate 编码值。

HTTP Compression

【讨论】:

  • 刚试过这个,它有效,但我必须使用Content-Encoding 来响应。
猜你喜欢
  • 2012-12-02
  • 2012-02-12
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 2015-07-14
  • 2011-06-11
  • 2011-02-08
相关资源
最近更新 更多