aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2019-07-31 09:16:19 +0900
committerAkira Matsuda <ronnie@dio.jp>2019-07-31 11:51:59 +0900
commitb8d29f35f0ef36bfe8c3d3a27ca0b80d02c4a0b0 (patch)
tree2e085bc3bd05f4e74f2214559d4e78a7f2cf4a9c /actionpack
parentf915341628f09c2bd175f17f03bbdf44e6213eb4 (diff)
downloadrails-b8d29f35f0ef36bfe8c3d3a27ca0b80d02c4a0b0.tar.gz
rails-b8d29f35f0ef36bfe8c3d3a27ca0b80d02c4a0b0.tar.bz2
rails-b8d29f35f0ef36bfe8c3d3a27ca0b80d02c4a0b0.zip
Reduce object allocations in Middleware::Static
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/middleware/static.rb23
1 files changed, 12 insertions, 11 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb
index 875c7f9ba1..eddcdbaeac 100644
--- a/actionpack/lib/action_dispatch/middleware/static.rb
+++ b/actionpack/lib/action_dispatch/middleware/static.rb
@@ -32,18 +32,13 @@ module ActionDispatch
return false unless ::Rack::Utils.valid_path? path
path = ::Rack::Utils.clean_path_info path
- paths = [path, "#{path}#{ext}", "#{path}/#{@index}#{ext}"]
+ return ::Rack::Utils.escape_path(path).b if file_readable?(path)
- if match = paths.detect { |p|
- path = File.join(@root, p.b)
- begin
- File.file?(path) && File.readable?(path)
- rescue SystemCallError
- false
- end
- }
- return ::Rack::Utils.escape_path(match).b
- end
+ path_with_ext = path + ext
+ return ::Rack::Utils.escape_path(path_with_ext).b if file_readable?(path_with_ext)
+
+ path << "/" << @index << ext
+ return ::Rack::Utils.escape_path(path).b if file_readable?(path)
end
def call(env)
@@ -95,6 +90,12 @@ module ActionDispatch
false
end
end
+
+ def file_readable?(path)
+ file_path = File.join(@root, path.b)
+ File.file?(file_path) && File.readable?(file_path)
+ rescue SystemCallError
+ end
end
# This middleware will attempt to return the contents of a file's body from