diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-06 15:18:35 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-06 15:18:35 -0700 |
commit | 6caeec53d56ccd4085619435930b744bcbaf13fa (patch) | |
tree | 8219ba9c537aca22c8adbc7fd5dde8dcba21749b /actionpack | |
parent | 595bef5df8eae70eb2f87bd8fbb79e81a98ecbae (diff) | |
download | rails-6caeec53d56ccd4085619435930b744bcbaf13fa.tar.gz rails-6caeec53d56ccd4085619435930b744bcbaf13fa.tar.bz2 rails-6caeec53d56ccd4085619435930b744bcbaf13fa.zip |
reuse the request object in the File serving middleware
Implement `serve` on the middleware. Nothing can be placed between the
instance of FileHandler and Static because Static instantiates an
instance of FileHandler. IOW there is no reason to implement the `call`
API in this case.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 5d3c2c3495..9462ae4278 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -48,26 +48,30 @@ module ActionDispatch end def call(env) - path = env['PATH_INFO'] + serve ActionDispatch::Request.new env + end + + def serve(request) + path = request.path_info gzip_path = gzip_file_path(path) - if gzip_path && gzip_encoding_accepted?(env) - env['PATH_INFO'] = gzip_path - status, headers, body = @file_server.call(env) + if gzip_path && gzip_encoding_accepted?(request) + request.path_info = gzip_path + status, headers, body = @file_server.call(request.env) if status == 304 return [status, headers, body] end headers['Content-Encoding'] = 'gzip' headers['Content-Type'] = content_type(path) else - status, headers, body = @file_server.call(env) + status, headers, body = @file_server.call(request.env) end headers['Vary'] = 'Accept-Encoding' if gzip_path return [status, headers, body] ensure - env['PATH_INFO'] = path + request.path_info = path end private @@ -79,8 +83,8 @@ module ActionDispatch ::Rack::Mime.mime_type(::File.extname(path), 'text/plain'.freeze) end - def gzip_encoding_accepted?(env) - env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/i + def gzip_encoding_accepted?(request) + request.accept_encoding =~ /\bgzip\b/i end def gzip_file_path(path) @@ -116,7 +120,7 @@ module ActionDispatch path = req.path_info.chomp('/'.freeze) if match = @file_handler.match?(path) req.path_info = match - return @file_handler.call(req.env) + return @file_handler.serve(req) end end |