Class: Racknga::Middleware::Deflater — racknga - Ranguba

Class: Racknga::Middleware::Deflater

Inherits:
Object
  • Object
show all
Defined in:
lib/racknga/middleware/deflater.rb

Overview

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.

Instance Method Summary (collapse)

Constructor Details

- (Deflater) initialize(application, options = {})

A new instance of Deflater



58
59
60
61
62
# File 'lib/racknga/middleware/deflater.rb', line 58

def initialize(application, options={})
  @application = application
  @deflater = Rack::Deflater.new(@application)
  @options = Utils.normalize_options(options || {})
end

Instance Method Details

- (Object) call(environment)

For Rack.



65
66
67
68
69
70
71
# File 'lib/racknga/middleware/deflater.rb', line 65

def call(environment)
  if ie6?(environment) or not valid_accept_encoding?(environment)
    @application.call(environment)
  else
    @deflater.call(environment)
  end
end