Class: GzipFilter

Inherits:
Object
  • Object
show all
Includes:
Mongrel::HttpHandlerPlugin
Defined in:
lib/gzip_filter/init.rb

Overview

The GzipFilter compresses an HTTP response using Ruby’s Zlib::GzipWriter. It only does this if the client sends an Accept-Encoding header containing the gzip string.

You can tell mongrel to use this filter by placing the following line in a config file and passing that file to mongrel using the -S option.

Example mongrel.conf:

uri "/", :handler => GzipFilter.new

Another example using the gzip_condition:

uri "/", :handler => GzipFilter.new(:gzip_condition => proc {|request|
  request.params['HTTP_USER_AGENT'] !~ /MSIE\s+6\.0/i
})

Example:

$ echo 'uri "/", :handler => GzipFilter.new' >> config/mongrel.conf
$ mongrel_rails -S config/mongrel.conf

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GzipFilter

You can pass in a conditional function that will be used to determine if the response should be gzipped or not. It’ll get passed the request object for validation. This is called in addition to checks on the Accept-Encoding header.

GzipFilter.new(:gzip_condition => proc {|request|

request.params['HTTP_USER_AGENT'] !~ /MSIE\s+6\.0/i

})



41
42
43
# File 'lib/gzip_filter/init.rb', line 41

def initialize(opts = {})
  @conditions = opts[:gzip_condition]
end

Instance Method Details

#process(request, response) ⇒ Object



45
46
47
48
49
50
# File 'lib/gzip_filter/init.rb', line 45

def process(request, response)
  if should_gzip(request, response)
    response.header["Content-Encoding"] = "gzip"
    response.body = gzip_stream(response.body)
  end
end