diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2014-08-29 14:54:08 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2014-08-29 14:54:08 -0700 |
commit | 7475b43cdbbbf7456e798210cb97ef20f2225166 (patch) | |
tree | 04ae517943ccc476ca0a8b9b3bdbb21949a558c1 /actionpack/lib/action_dispatch/middleware/static.rb | |
parent | 6a23bf0f4c33151e0cec0648e271dc6f5ab3f686 (diff) | |
parent | 4445478df311a74797d8dc4945c40662f9c1442a (diff) | |
download | rails-7475b43cdbbbf7456e798210cb97ef20f2225166.tar.gz rails-7475b43cdbbbf7456e798210cb97ef20f2225166.tar.bz2 rails-7475b43cdbbbf7456e798210cb97ef20f2225166.zip |
Merge branch 'master' of github.com:rails/rails
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/static.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 25e32cdef8..e66c21ef85 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -16,43 +16,63 @@ module ActionDispatch def initialize(root, cache_control) @root = root.chomp('/') @compiled_root = /^#{Regexp.escape(root)}/ - headers = cache_control && { 'Cache-Control' => cache_control } - @file_server = ::Rack::File.new(@root, headers) + headers = cache_control && { 'Cache-Control' => cache_control } + @file_server = ::Rack::File.new(@root, headers) end def match?(path) - path = unescape_path(path) + path = URI.parser.unescape(path) return false unless path.valid_encoding? - full_path = path.empty? ? @root : File.join(@root, escape_glob_chars(path)) - paths = "#{full_path}#{ext}" + paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"] - matches = Dir[paths] - match = matches.detect { |m| File.file?(m) } - if match - match.sub!(@compiled_root, '') - ::Rack::Utils.escape(match) + if match = paths.detect {|p| File.file?(File.join(@root, p)) } + return ::Rack::Utils.escape(match) end end def call(env) - @file_server.call(env) - end + path = env['PATH_INFO'] + gzip_path = gzip_file_path(path) - def ext - @ext ||= begin - ext = ::ActionController::Base.default_static_extension - "{,#{ext},/index#{ext}}" + if gzip_path && gzip_encoding_accepted?(env) + env['PATH_INFO'] = gzip_path + status, headers, body = @file_server.call(env) + headers['Content-Encoding'] = 'gzip' + headers['Content-Type'] = content_type(path) + else + status, headers, body = @file_server.call(env) end - end - def unescape_path(path) - URI.parser.unescape(path) - end + headers['Vary'] = 'Accept-Encoding' if gzip_path - def escape_glob_chars(path) - path.gsub(/[*?{}\[\]]/, "\\\\\\&") + return [status, headers, body] + ensure + env['PATH_INFO'] = path end + + private + def ext + ::ActionController::Base.default_static_extension + end + + def content_type(path) + ::Rack::Mime.mime_type(::File.extname(path), 'text/plain') + end + + def gzip_encoding_accepted?(env) + env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/i + end + + def gzip_file_path(path) + can_gzip_mime = content_type(path) =~ /\A(?:text\/|application\/javascript)/ + gzip_path = "#{path}.gz" + if can_gzip_mime && File.exist?(File.join(@root, ::Rack::Utils.unescape(gzip_path))) + gzip_path + else + false + end + end end # This middleware will attempt to return the contents of a file's body from |