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, paths, cache_control_manager) ⇒ ServeZippedAssets
constructor
A new instance of ServeZippedAssets.
Constructor Details
#initialize(app, root, paths, cache_control_manager) ⇒ ServeZippedAssets
Returns a new instance of ServeZippedAssets.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/heroku-deflater/serve_zipped_assets.rb', line 15 def initialize(app, root, paths, cache_control_manager) @app = app @paths = paths.map { |p| p.chomp('/') + '/' } if HerokuDeflater.rails_version_5? @file_handler = ActionDispatch::FileHandler.new(root, headers: cache_control_manager.cache_control_headers) else @file_handler = ActionDispatch::FileHandler.new(root, cache_control_manager.cache_control_headers) end end |
Instance Method Details
#call(env) ⇒ Object
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 52 53 54 55 56 57 58 59 |
# File 'lib/heroku-deflater/serve_zipped_assets.rb', line 26 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?(*@paths) && (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 |