Class: HerokuDeflater::ServeZippedAssets
- Inherits:
-
Object
- Object
- HerokuDeflater::ServeZippedAssets
- Defined in:
- lib/heroku-deflater/serve_zipped_assets.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, root, assets_path, cache_control = nil) ⇒ ServeZippedAssets
constructor
A new instance of ServeZippedAssets.
Constructor Details
#initialize(app, root, assets_path, cache_control = nil) ⇒ ServeZippedAssets
Returns a new instance of ServeZippedAssets.
12 13 14 15 16 |
# File 'lib/heroku-deflater/serve_zipped_assets.rb', line 12 def initialize(app, root, assets_path, cache_control=nil) @app = app @assets_path = assets_path.chomp('/') + '/' @file_handler = ActionDispatch::FileHandler.new(root, cache_control) end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/heroku-deflater/serve_zipped_assets.rb', line 18 def call(env) if env['REQUEST_METHOD'] == 'GET' request = Rack::Request.new(env) encoding = Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding) if encoding == 'gzip' # See if gzipped version exists in assets directory compressed_path = env['PATH_INFO'] + '.gz' if compressed_path.start_with?(@assets_path) && (match = @file_handler.match?(compressed_path)) # Get the FileHandler to serve up the gzipped file, then strip the .gz suffix env['PATH_INFO'] = match status, headers, body = @file_handler.call(env) env['PATH_INFO'].chomp!('.gz') # Set the Vary HTTP header. vary = headers['Vary'].to_s.split(',').map(&:strip) unless vary.include?('*') || vary.include?('Accept-Encoding') headers['Vary'] = vary.push('Accept-Encoding').join(',') end # Add encoding and type headers['Content-Encoding'] = 'gzip' headers['Content-Type'] = Rack::Mime.mime_type(File.extname(env['PATH_INFO']), 'text/plain') body.close if body.respond_to?(:close) return [status, headers, body] end end end @app.call(env) end |