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:
Dim NewFilter As IO.Stream = App.Response.Filter
Dim strAcceptEncoding As String = App.Request.Headers("Accept-Encoding")
If Not String.IsNullOrEmpty(strAcceptEncoding) Then
If strAcceptEncoding.ToLower.Contains("gzip") OrElse strAcceptEncoding = "*" Then
NewFilter = New IO.Compression.GZipStream(NewFilter, IO.Compression.CompressionMode.Compress)
App.Response.AppendHeader("Content-Encoding", "gzip")
ElseIf strAcceptEncoding.ToLower.Contains("deflate") Then
NewFilter = New IO.Compression.DeflateStream(NewFilter, IO.Compression.CompressionMode.Compress)
App.Response.AppendHeader("Content-Encoding", "deflate")
End If
End If
If App.Response.ContentType.ToLower = "text/html" Then
End If
App.Response.Filter = NewFilter
Thanks for your help!