views:

474

answers:

3

Hi, I used the YSlow Firefox add-on and it return the follow result:

-----------------------------------------------------------------------------------------
Grade D on Compress components with gzip

There are 3 plain text components that should be sent compressed

* http://localhost:63808/WebSite/BemVindo/
* http://localhost:63808/WebSite/css/Global.css?...
* http://localhost:63808/WebSite/js/Global.js?...

-----------------------------------------------------------------------------------------
So I started to search and got this piece of code:

Global.asax

Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)

    Dim Response As HttpResponse = HttpContext.Current.Response
    Dim AcceptEncoding As String = HttpContext.Current.Request.Headers("Accept-Encoding")

    If Not String.IsNullOrEmpty(AcceptEncoding) AndAlso AcceptEncoding.Contains("gzip") OrElse AcceptEncoding.Contains("deflate") Then
        If AcceptEncoding.Contains("deflate") Then
            Response.Filter = New System.IO.Compression.DeflateStream(Response.Filter, System.IO.Compression.CompressionMode.Compress)
            Response.AppendHeader("Content-Encoding", "deflate")
        Else
            Response.Filter = New System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress)
            Response.AppendHeader("Content-Encoding", "gzip")
        End If
    End If

    Response.AppendHeader("Vary", "Content-Encoding")

End Sub

And now I have:

Grade A on Compress components with gzip

Fine, hum? The question is: Is the Application_PreRequestHandlerExecute event the best place to gzip/deflate the requests?

+1  A: 

You can configure this at the web server level: check this link for IIS6 and this for IIS7

Teun D
But I don't have control to IIS... the piece of code actually works, but I'm worried if it will overhead the requests! I'm searching for something like "put this code in another event", or "use a configuration in web.config"
Fernando
Ah, in that case, yes, probably Application_PreRequestHandlerExecute is the best place to do it. Will no do anything for your CSS, javascript requests, though.
Teun D
Microsoft would create a web.config configuration to do this internally... Thanks!
Fernando
+1  A: 

Yes, the Application_PreRequestHandlerExecute event is the best place to gzip/deflate the requests.

Fernando
A: 

Not sure but your code might cause problem in pages using Ajax. The following page shows a similar code but cancel the compression if Ajax is detected: Enabling Gzip and Deflate HTTP Compression in ASP.NET pages. I translated the code in VB but didn't tried it.

Eric

  Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim app As HttpApplication = TryCast(sender, HttpApplication)
Dim acceptEncoding As String = app.Request.Headers("Accept-Encoding")
Dim prevUncompressedStream As Stream = app.Response.Filter

If Not (TypeOf app.Context.CurrentHandler Is Page OrElse app.Context.CurrentHandler.[GetType]().Name = "SyncSessionlessHandler") OrElse app.Request("HTTP_X_MICROSOFTAJAX") IsNot Nothing Then
  Return
End If

If acceptEncoding Is Nothing OrElse acceptEncoding.Length = 0 Then
  Return
End If

acceptEncoding = acceptEncoding.ToLower()

If acceptEncoding.Contains("deflate") OrElse acceptEncoding = "*" Then
  ' defalte
  app.Response.Filter = New DeflateStream(prevUncompressedStream, CompressionMode.Compress)
  app.Response.AppendHeader("Content-Encoding", "deflate")
ElseIf acceptEncoding.Contains("gzip") Then
  ' gzip
  app.Response.Filter = New GZipStream(prevUncompressedStream, CompressionMode.Compress)
  app.Response.AppendHeader("Content-Encoding", "gzip")
End If

End Sub

Eric