class Racknga::Middleware::Deflater

This is a middleware that deflates response except for IE6. If your Rack application need support IE6, use this middleware instead of Rack::Deflater.

Usage:

require "racknga"

use Racknga::Middleware::Deflater
run YourApplication

You can use this middleware with Racknga::Middleware::Cache. You should use this middleware before the cache middleware:

use Racknga::Middleawre::Deflater
use Racknga::Middleawre::Cache, :database_path => "var/cache/db"
run YourApplication

If you use this middleware after the cache middleware, you get two problems. It’s the first problem pattern that the cache middleware may return deflated response to IE6. It’s the second problem pattern that the cache middleware may return not deflated response to no IE6 user agent. Here are examples:

Problem case:

use Racknga::Middleawre::Cache, :database_path => "var/cache/db"
use Racknga::Middleawre::Deflater
run YourApplication

Problem pattern1:

http://localhost:9292/ by Firefox -> no cache. cache deflated response.
http://localhost:9292/ by IE6     -> use deflated response cache.

Problem pattern2:

http://localhost:9292/ by IE6     -> no cache. cache not deflated response.
http://localhost:9292/ by Firefox -> use not deflated response cache.

Public Class Methods

new(application, options={}) click to toggle source
# File lib/racknga/middleware/deflater.rb, line 57
def initialize(application, options={})
  @application = application
  @deflater = Rack::Deflater.new(@application)
  @options = Utils.normalize_options(options || {})
end

Public Instance Methods

call(environment) click to toggle source

For Rack.

# File lib/racknga/middleware/deflater.rb, line 64
def call(environment)
  if ie6?(environment) or not valid_accept_encoding?(environment)
    @application.call(environment)
  else
    @deflater.call(environment)
  end
end

Private Instance Methods

ie6?(environment) click to toggle source
# File lib/racknga/middleware/deflater.rb, line 73
def ie6?(environment)
  /MSIE 6.0;/ =~ (environment["HTTP_USER_AGENT"] || '')
end
valid_accept_encoding?(environment) click to toggle source
# File lib/racknga/middleware/deflater.rb, line 77
def valid_accept_encoding?(environment)
  request = Rack::Request.new(environment)
  begin
    request.accept_encoding
    true
  rescue
    false
  end
end