提交 d5ad92ce 编写于 作者: J José Valim

Make static faster as we don't have to serve multiple paths anymore.

上级 afc82882
......@@ -2,25 +2,23 @@
module ActionDispatch
class FileHandler
def initialize(at, root)
@at, @root = at.chomp('/'), root.chomp('/')
@compiled_at = @at.blank? ? nil : /^#{Regexp.escape(at)}/
def initialize(root)
@root = root.chomp('/')
@compiled_root = /^#{Regexp.escape(root)}/
@file_server = ::Rack::File.new(@root)
end
def match?(path)
path = path.dup
if !@compiled_at || path.sub!(@compiled_at, '')
full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
paths = "#{full_path}#{ext}"
matches = Dir[paths]
match = matches.detect { |m| File.file?(m) }
if match
match.sub!(@compiled_root, '')
match
end
full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
paths = "#{full_path}#{ext}"
matches = Dir[paths]
match = matches.detect { |m| File.file?(m) }
if match
match.sub!(@compiled_root, '')
match
end
end
......@@ -39,9 +37,9 @@ def ext
class Static
FILE_METHODS = %w(GET HEAD).freeze
def initialize(app, roots)
def initialize(app, path)
@app = app
@file_handlers = create_file_handlers(roots)
@file_handler = FileHandler.new(path)
end
def call(env)
......@@ -49,24 +47,13 @@ def call(env)
method = env['REQUEST_METHOD']
if FILE_METHODS.include?(method)
@file_handlers.each do |file_handler|
if match = file_handler.match?(path)
env["PATH_INFO"] = match
return file_handler.call(env)
end
if match = @file_handler.match?(path)
env["PATH_INFO"] = match
return @file_handler.call(env)
end
end
@app.call(env)
end
private
def create_file_handlers(roots)
roots = { '' => roots } unless roots.is_a?(Hash)
roots.map do |at, root|
FileHandler.new(at, root) if File.exist?(root)
end.compact
end
end
end
......@@ -47,35 +47,4 @@ def setup
end
include StaticTests
end
class MultipleDirectorisStaticTest < ActiveSupport::TestCase
DummyApp = lambda { |env|
[200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
}
App = ActionDispatch::Static.new(DummyApp,
{ "/" => "#{FIXTURE_LOAD_PATH}/public",
"/blog" => "#{FIXTURE_LOAD_PATH}/blog_public",
"/foo" => "#{FIXTURE_LOAD_PATH}/non_existing_dir"
})
def setup
@app = App
end
include StaticTests
test "serves files from other mounted directories" do
assert_html "/blog/index.html", get("/blog/index.html")
assert_html "/blog/index.html", get("/blog/index")
assert_html "/blog/index.html", get("/blog/")
assert_html "/blog/blog.html", get("/blog/blog/")
assert_html "/blog/blog.html", get("/blog/blog.html")
assert_html "/blog/blog.html", get("/blog/blog")
assert_html "/blog/subdir/index.html", get("/blog/subdir/index.html")
assert_html "/blog/subdir/index.html", get("/blog/subdir/")
assert_html "/blog/subdir/index.html", get("/blog/subdir")
end
end
end
\ No newline at end of file
......@@ -477,15 +477,6 @@ def test_string_asset_id
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
end
def test_env_asset_path
@controller.config.asset_path = "/assets%s"
def @controller.env; @_env ||= {} end
@controller.env["action_dispatch.asset_path"] = "/omg%s"
expected_path = "/assets/omg/images/rails.png"
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
end
def test_proc_asset_id
@controller.config.asset_path = Proc.new do |asset_path|
"/assets.v12345#{asset_path}"
......@@ -495,20 +486,6 @@ def test_proc_asset_id
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
end
def test_env_proc_asset_path
@controller.config.asset_path = Proc.new do |asset_path|
"/assets.v12345#{asset_path}"
end
def @controller.env; @_env ||= {} end
@controller.env["action_dispatch.asset_path"] = Proc.new do |asset_path|
"/omg#{asset_path}"
end
expected_path = "/assets.v12345/omg/images/rails.png"
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
end
def test_image_tag_interpreting_email_cid_correctly
# An inline image has no need for an alt tag to be automatically generated from the cid:
assert_equal '<img src="cid:thi%25%25sis@acontentid" />', image_tag("cid:thi%25%25sis@acontentid")
......
......@@ -161,7 +161,7 @@ def default_middleware_stack
end
if config.serve_static_assets
middleware.use ::ActionDispatch::Static, "/" => paths["public"].first
middleware.use ::ActionDispatch::Static, paths["public"].first
end
middleware.use ::Rack::Lock unless config.allow_concurrency
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册