HTTP Module Compression and Whitespace Removal

Discussion in 'ASP.NET 2.0' started by psantiago, Feb 25, 2007.

  1. Hello




    I created an HTTP Module to Compress and Remove all the white space in ASPX Files.


    It actually works pretty well. Certainly has reduced the size of the files and increased download times.


    Process works perfectly until I start dealing with pages that use AJAX.


    I have attached the error message as an image file and the results of fiddler below.


    I also attached the HTTP Module.


    I downloaded and installed Fiddler to assist with the problem.


    Fiddler Results





    = BODY ====
    ScriptManager1=ScriptManager1|Button1
    __EVENTTARGET=
    __EVENTARGUMENT=
    __VIEWSTATE=OyuAJXY2qQCO5FgjWOIufVLIegV3gFQYF0lQQBKJOFoBLX0Hu7iJjxCSUrdvXSiYwROC02ek714jfq7LbVQ3p79BE+zeBjn4FlYEFakl2YQhuLU2oYNHV2cwCO9RNJEU2spk+IpPo3ceU1UWDEWWgw==
    Keywords=
    Loc=17
    Categories=39
    __VIEWSTATEENCRYPTED=
    __EVENTVALIDATION=hIZBJ4SjmsNn113GqXKW2wKnaBOk533SSY0q8vo4hnOyeAXYpe6EeRg2MfU1nKvDMbBPqNNzOqX4Q/l0wc80A2xwjFf8YJq2SQQkBf3qekuDW6n5/w6v2h9InvEmDSY+cknoEuip/aaADJbvSF7fpAbbm/05iQGQ/996gPDcehOwqXyZIM7C/EGXosG9+S9IFlHXL74f57omMHNCjAZKhbX7saiRxmD5ub71p0ZNZRnXdR8+98ZM9X8o6mIEk/N73grEZoIl7fi24WnmSFOAabs3m3WKxMWDcF58GpglMZlsSzRXllGU7hMqSeheQkOY2YZDfpmIPbK2JNU8cxNobg078h/ne9XPhigt9LEMePqB2GINiph0/uiK1Z+Kj5VVfh1aGCkh/xim90Yi0cZhFAZnOL/GEenLGiLQXqsAm895Mit95WQyd64MphZmbe1xg4jUDLN+fnJnDKAGCiblWhBZGSBkLucz8cffpOaGWcQwEVawnpKqHlCCepRemRwe
    Button1=Search






    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/8.0.0.0
    Date: Sun, 25 Feb 2007 04:47:30 GMT
    X-AspNet-Version: 2.0.50727
    Content-Encoding: gzip
    Transfer-Encoding: chunked
    Cache-Control: private
    Content-Type: text/plain; charset=utf-8
    Connection: Close





    POST /BankStaffers2005/Searchlisting.aspx HTTP/1.1
    Accept: */*
    Accept-Language: en-us
    Referer: http://localhost:2191/BankStaffers2005/Searchlisting.aspx
    x-microsoftajax: Delta=true
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    UA-CPU: x86
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
    Host: localhost:2191
    Content-Length: 873
    Proxy-Connection: Keep-Alive
    Pragma: no-cache


    ScriptManager1=ScriptManager1|Button1&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=OyuAJXY2qQCO5FgjWOIufVLIegV3gFQYF0lQQBKJOFoBLX0Hu7iJjxCSUrdvXSiYwROC02ek714jfq7LbVQ3p79BE%2BzeBjn4FlYEFakl2YQhuLU2oYNHV2cwCO9RNJEU2spk%2BIpPo3ceU1UWDEWWgw%3D%3D&Keywords=&Loc=17&Categories=39&__VIEWSTATEENCRYPTED=&__EVENTVALIDATION=hIZBJ4SjmsNn113GqXKW2wKnaBOk533SSY0q8vo4hnOyeAXYpe6EeRg2MfU1nKvDMbBPqNNzOqX4Q%2Fl0wc80A2xwjFf8YJq2SQQkBf3qekuDW6n5%2Fw6v2h9InvEmDSY%2BcknoEuip%2FaaADJbvSF7fpAbbm%2F05iQGQ%2F996gPDcehOwqXyZIM7C%2FEGXosG9%2BS9IFlHXL74f57omMHNCjAZKhbX7saiRxmD5ub71p0ZNZRnXdR8%2B98ZM9X8o6mIEk%2FN73grEZoIl7fi24WnmSFOAabs3m3WKxMWDcF58GpglMZlsSzRXllGU7hMqSeheQkOY2YZDfpmIPbK2JNU8cxNobg078h%2Fne9XPhigt9LEMePqB2GINiph0%2FuiK1Z%2BKj5VVfh1aGCkh%2Fxim90Yi0cZhFAZnOL%2FGEenLGiLQXqsAm895Mit95WQyd64MphZmbe1xg4jUDLN%2BfnJnDKAGCiblWhBZGSBkLucz8cffpOaGWcQwEVawnpKqHlCCepRemRwe&Button1=Search
    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/8.0.0.0
    Date: Sun, 25 Feb 2007 04:47:30 GMT
    X-AspNet-Version: 2.0.50727
    Cache-Control: private
    Content-Type: text/plain; charset=utf-8
    Connection: Close
    Content-Length: 0





    #ResultProtocolHostURLBodyCachingContent-TypeUser-defined
    58200HTTPlocalhost:2191/BankStaffers2005/Searchlisting.aspx3,019private text/plain; charset=utf-8














    MODULE





    Imports Microsoft.VisualBasic


    Imports System.Web


    Imports System


    Imports System.Buffer


    Imports System.Text


    Imports System.IO


    Imports System.IO.Compression


    Imports System.Data


    Imports System.Configuration


    Imports System.Web.Security


    Imports System.Web.UI


    Imports System.Web.UI.WebControls


    Imports System.Web.UI.WebControls.WebParts


    Imports System.Web.UI.HtmlControls


    Imports System.Text.RegularExpressions


    Public Class CompressAndTrim


    Implements IHttpModule


    Private Const GZIP As String = "gzip"


    Private Const DEFLATE As String = "deflate"


    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose


    End Sub


    Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init


    AddHandler app.BeginRequest, AddressOf Me.OnBeginRequest


    End Sub


    Public Sub OnBeginRequest(ByVal s As Object, ByVal e As EventArgs)


    Dim app As HttpApplication = CType(s, HttpApplication)


    If app.Request.RawUrl.Contains(".aspx") Then


    Dim starter As Stream = Nothing


    If IsEncodingAccepted(GZIP) Then


    starter = New GZipStream(app.Response.Filter, CompressionMode.Compress)


    SetEncoding(GZIP)


    ElseIf IsEncodingAccepted(DEFLATE) Then


    starter = New DeflateStream(app.Response.Filter, CompressionMode.Compress)


    SetEncoding(DEFLATE)


    End If


    If starter Is Nothing Then


    app.Response.Filter = New WhitespaceFilter(app.Response.Filter)


    Else


    app.Response.Filter = New WhitespaceFilter(starter)


    End If


    End If


    End Sub


    Private Function IsEncodingAccepted(ByVal encoding As String) As Boolean


    Return HttpContext.Current.Request.Headers("Accept-encoding") <> Nothing And HttpContext.Current.Request.Headers("Accept-encoding").Contains(encoding)


    End Function


    Private Sub SetEncoding(ByVal encoding As String)


    HttpContext.Current.Response.AppendHeader("Content-encoding", encoding)


    End Sub


    End Class


    Class WhitespaceFilter


    Inherits Stream


    Public Sub New(ByVal sink As Stream)


    _sink = sink


    End Sub


    Private _sink As Stream


    Private Shared reg As Regex = New Regex("(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}")


    Public Overrides ReadOnly Property CanRead() As Boolean


    Get


    Return True


    End Get


    End Property


    Public Overrides ReadOnly Property CanSeek() As Boolean


    Get


    Return True


    End Get


    End Property


    Public Overrides ReadOnly Property CanWrite() As Boolean


    Get


    Return True


    End Get


    End Property


    Public Overrides Sub Flush()


    _sink.Flush()


    End Sub


    Public Overrides ReadOnly Property Length() As Long


    Get


    Return 0


    End Get


    End Property


    Private _position As Long


    Public Overrides Property Position() As Long


    Get


    Return _position


    End Get


    Set(ByVal Value As Long)


    _position = Value


    End Set


    End Property


    Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer


    Return _sink.Read(buffer, offset, count)


    End Function


    Public Overrides Function Seek(ByVal offset As Long, ByVal origin As SeekOrigin) As Long


    Return _sink.Seek(offset, origin)


    End Function


    Public Overrides Sub SetLength(ByVal value As Long)


    _sink.SetLength(value)


    End Sub


    Public Overrides Sub Close()


    _sink.Close()


    End Sub


    Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)


    Dim data() As Byte = New Byte(count) {}


    System.Buffer.BlockCopy(buffer, offset, data, 0, count)


    Dim html As String = System.Text.Encoding.Default.GetString(buffer)


    html = reg.Replace(html, String.Empty)


    Dim outdata() As Byte = System.Text.Encoding.Default.GetBytes(html)


    _sink.Write(outdata, 0, outdata.GetLength(0))


    End Sub


    End Class
     

    Attached Files:

Share This Page