带响应过滤器的字节丢失 [英] Byte loss with response filter

查看:94
本文介绍了带响应过滤器的字节丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最后一天,我决定提高网站的性能,因此决定压缩响应.

我创建了一个已在Web.config中注册的模块.该模块为PostRequestHandlerExecute事件注册一个处理程序.在这里,模块将System.IO.Compression.GZipStream设置为响应的筛选器.当响应MIME类型为text/html时,就会出现问题,因为我想缩小html代码.我写了一个新的流来做到这一点,它工作正常,但是当我将其与GZipStream结合使用时,会丢失数据.
示例:在HTML代码的末尾,我找到</htm"而不是</html>"

我的想法是创建一种流链":一个MinifyHTMLStream,它在将数据写入GZipStream中之前先压缩html代码,然后压缩压缩后的html代码,然后再将其发送给客户端.

因此,问题是:为什么只有在流链"中只有1个流时,响应过滤器才能正常工作?

这是代码:

Hi,

the last day, I decided to improve the performance of my website, so I decided to compress the responses.

I''ve created a module that I''ve registered in the Web.config. This module registers an handler for the PostRequestHandlerExecute event. Here the module sets a System.IO.Compression.GZipStream as the filter of the response. The problem comes when the response MIME type is text/html because I want to minify html code. I wrote a new stream to do that and it works fine, but when I combine it with the GZipStream, there is a loss of data.

Example: at the end of the HTML code i find "</htm" instead of "</html>"

My idea was creating a sort of "stream chain": a MinifyHTMLStream that minifies html code before writing data in a GZipStream that compresses minified html code before sending it to the client.

So, the question is: why the response filter works fine only when in the "stream chain" there is only 1 stream?

Here is the code:

'filter to register
            Dim NewFilter As IO.Stream = App.Response.Filter

            'finds the accept-encoding
            Dim strAcceptEncoding As String = App.Request.Headers("Accept-Encoding")

            'checks if the client accepts encoding
            If Not String.IsNullOrEmpty(strAcceptEncoding) Then

                'if the clients supports gzip
                If strAcceptEncoding.ToLower.Contains("gzip") OrElse strAcceptEncoding = "*" Then
                    'sets the filter
                    NewFilter = New IO.Compression.GZipStream(NewFilter, IO.Compression.CompressionMode.Compress)
                    'sets the header for the client
                    App.Response.AppendHeader("Content-Encoding", "gzip")
                ElseIf strAcceptEncoding.ToLower.Contains("deflate") Then
                    'sets the filter
                    NewFilter = New IO.Compression.DeflateStream(NewFilter, IO.Compression.CompressionMode.Compress)
                    'sets the header for the client
                    App.Response.AppendHeader("Content-Encoding", "deflate")
                End If
            End If

            'if the response type is html
            If App.Response.ContentType.ToLower = "text/html" Then
                '--------------------------------------------
                'HERE IS MY STREAM FOR HTML CODE MINIFYING
                'NewFilter = New MinifyHTMLStream(NewFilter)
                '--------------------------------------------

            End If

            'sets the filter
            App.Response.Filter = NewFilter



感谢您的帮助!



Thanks for your help!

推荐答案

此处没有足够的信息来跟踪导致丢失字节的wha.内置压缩过滤器的可能性很小.我的猜测是流浪<其中< =是预期的或类似的错字.

但是,如果您打算在压缩后缩小请求,那么我认为您会遇到困难.您需要将过滤器添加到压缩过滤器之前的链中.
There''s not enough information here to track wha''s causing the missing byte. It''s very unlikely to be the builtin compression filters. My guess will be a stray < where <= was intended or similar typo.

However, if your intention is to Minify the request after the compression then I think you''ll run into difficulty. You''ll need to add your filter to the chain before the compression filter.


这篇关于带响应过滤器的字节丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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