支持 IIS 提供的内容的自定义压缩算法
我有一堆互联网设备与 IIS 7.5 上的 MVC 应用程序进行通信。我目前正在使用内置的动态透明压缩(gzip/deflate)。
我希望能够支持不同的压缩算法,对于我发送/接收的内容,该算法比 gzip (7zip) 更好。
换句话说,在客户端上我将添加标头:accepts:gzip、deflate、7zip(或类似),服务器将识别这一点,并在发送内容时应用最佳选择。
将这一切结合在一起的最佳方法是什么? (我知道如何实现实际的 7zip 编码/解码方面)
谢谢。
I have a bunch of internet devices which communicate with my MVC app on IIS 7.5. I'm currently using the built-in dynamic transparent compression (gzip/deflate).
I'd like to be able to support a different compression algorithm, which does a lot better than gzip (7zip) for the content I'm sending/receiving.
In other words, on the client I will add the header: accepts: gzip, deflate, 7zip (or similar), and the server will recognize this, and apply the best choice when sending the content.
What's the best way to go about hooking this all together? (I know how to implement the actual 7zip encode/decode aspect)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在服务器端,您可以使用 Http模块。 HttpModule 与 global.asax 类似,它公开请求和响应生命周期事件 例如 BeginRequest、ReleaseRequestState 和 EndRequest。您可以通过处理适当的事件并更改输出流来更改响应的内容。通常,您可以通过附加输出筛选器。例如,blowery HttpCompress 模块(无已更新)在 ReleaseRequestState 或 PreSendRequestHeaders 事件中附加压缩过滤器:
http://code.google.com/p/httpcompress/source/browse/trunk/Httpcompress/HttpModule.cs
除了压缩内容之外,您还需要设置 Content-Encoding 标头。有关更新的(尽管是测试版)示例,请查看 Rich Crane 的 Wicked Compression 模块。 CodeProject 还有一个 示例 模块。
请记住,如果 IIS 设置为在服务器级别进行压缩,则压缩 HttpModule 可能不会很好地发挥作用。此外,如果您使用 Ajax 或 .axd 处理程序,请为一些特殊情况做好准备。这些请求可能不会像您期望的那样工作,或者可能不会在没有显式连接的情况下通过您的模块。
在客户端,您需要在每个请求中传递自定义的 Accept-Encoding 令牌。如果未设置此标记,请避免在服务器上进行压缩。如果您将来添加客户端,如果所有响应都使用您的自定义压缩进行压缩,则可能不清楚它们失败的原因。
我不确定您的客户端如何发出请求,但大多数 Http 请求/响应管道允许您以与服务器类似的方式访问响应流。在将响应发送到渲染器之前,请检查您的自定义内容编码令牌。如果存在,请运行减压程序。如果不是,则不加更改地传递响应。
On the server side, you can compress your responses using an HttpModule. An HttpModule is similar to global.asax in that it exposes request and response lifecycle events like BeginRequest, ReleaseRequestState, and EndRequest. You can alter the contents of a response by handling the appropriate event and changing the output stream. Typically you alter a response by attaching an output filter. As an example, the blowery HttpCompress module(no longer updated) attaches compression filters in the ReleaseRequestState or PreSendRequestHeaders events:
http://code.google.com/p/httpcompress/source/browse/trunk/HttpCompress/HttpModule.cs
In addition to compressing the content, you'll need to set the Content-Encoding header. For a more recent(though beta) example, check out Rich Crane's Wicked Compression module. CodeProject also has an example module.
Keep in mind, a compressing HttpModule will likely not play nice if IIS is set to compress at the server level. In addition, be prepared for a few corner cases if you're using Ajax or .axd handlers. These requests may not work like you expect or may not pass through your module without explicitly wiring them in.
On the client side, you'll need to pass your custom Accept-Encoding token with each request. Avoid compressing on the server if this token isn't set. If you add clients in the future, it may not be clear why they're failing if all responses are compressed with your custom compression.
I'm not sure how your clients are making requests, but most Http Request/Response pipelines allow you tap into the response stream in a similar way to the server. Before the response is sent to your renderer, check for your custom Content-Encoding token. If it's present, run your decompression routine. If not, pass the response through unaltered.