Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
'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!
Posted
Updated 3-Aug-10 23:22pm
v2

1 solution

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.
 
Share this answer
 
Comments
95ulisse 4-Aug-10 18:13pm    
The bultin compression filter System.IO.Compression.GZipStream works fine and my own stream too. If you try to use each stream singly, it works fine, mine and framework's. The problem comes when I try to use the streams together, so I don't think it's a typo... Also, in the stream chain there is first my minifing stream and after the GZipStream...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900